String createCallbackFn()

in browser/src/main/java/org/netbeans/html/presenters/browser/Browser.java [517:556]


    String createCallbackFn(String prefix, String id) {
        StringBuilder sb = new StringBuilder();
        sb.append("this.toBrwsrSrvr = function(name, a1, a2, a3, a4) {\n"
            + "var url = '").append(prefix).append("command.js?id=").append(id).append("&name=' + name;\n"
            + "var body = 'p0=' + encodeURIComponent(a1);\n"
            + "body += '&p1=' + encodeURIComponent(a2);\n"
            + "body += '&p2=' + encodeURIComponent(a3);\n"
            + "body += '&p3=' + encodeURIComponent(a4);\n"
            + "var request = new XMLHttpRequest();\n"
        );
        if (Browser.this.config.debug) {
            sb.append(""
            + "console.log('PUT ... ' + body.substring(0, 80));\n"
            + "var now = new Date().getTime();\n"
            );
        }
        sb.append(""
            + "var async = name === 'p';\n"
            + "request.open('PUT', url, async);\n"
            + "request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');\n"
            + "request.send(body);\n"
            + "if (async) return '';\n"
            + "var txt = request.responseText;\n"
        );
        if (Browser.this.config.debug) {
            sb.append(""
            + "var then = new Date().getTime();\n"
            + "if (txt && txt !== 'null') {\n"
            + "  var cmd = document.getElementById('cmd');\n"
            + "  if (cmd) cmd.innerHTML = txt.substring(0,80);\n"
            + "}\n"
            + "console.log('... PUT [' + (then - now) + 'ms]: ' + txt.substring(0, 80));\n"
            );
        }
        sb.append(""
            + "return txt;\n"
            + "};\n"
        );
        return sb.toString();
    }