summaryrefslogtreecommitdiffstats
path: root/swat/scripting/client
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2005-08-05 19:16:13 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:31:21 -0500
commit3a02c8b116e80d2fd70b1957a5ed85acb0977b7f (patch)
tree9cf5d92f577886ab3143a90aaab58c086e8f20a5 /swat/scripting/client
parent8808465e5b70fd6388ed02a2cfda6015ffc914e5 (diff)
downloadsamba-3a02c8b116e80d2fd70b1957a5ed85acb0977b7f.tar.gz
samba-3a02c8b116e80d2fd70b1957a5ed85acb0977b7f.tar.xz
samba-3a02c8b116e80d2fd70b1957a5ed85acb0977b7f.zip
r9134: added the client side js library code for handling remote 'AJAJ' calls
into our web server (This used to be commit 530717122a3c7e34258db8ed0bfd0cc00624b903)
Diffstat (limited to 'swat/scripting/client')
-rw-r--r--swat/scripting/client/call.js38
-rw-r--r--swat/scripting/client/encoder.js84
2 files changed, 122 insertions, 0 deletions
diff --git a/swat/scripting/client/call.js b/swat/scripting/client/call.js
new file mode 100644
index 00000000000..bb89390e454
--- /dev/null
+++ b/swat/scripting/client/call.js
@@ -0,0 +1,38 @@
+/*
+ client side js functions for remote calls into the server
+
+ Copyright Andrew Tridgell 2005
+ released under the GNU GPL Version 2 or later
+*/
+
+
+/*
+ usage:
+
+ server_call(url, func, callback, ...);
+
+ 'func' is a function name to call on the server
+ any additional arguments are passed to func() on the server
+
+ The callback() function is called with the returned
+ object. 'callback' may be null.
+*/
+function server_call(url, func, callback) {
+ var req = new XMLHttpRequest();
+ req.open("POST", url, true);
+ req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
+ var args = new Object();
+ var i;
+ for (i=3;i<arguments.length;i++) {
+ args[i-3] = arguments[i];
+ }
+ args.length = i-3;
+ req.send("func=" + func + "&args=" + encodeObject(args));
+ req.onreadystatechange = function() {
+ if (4 == req.readyState && callback != null) {
+ var o = decodeObject(req.responseText);
+ callback(o.res);
+ }
+ }
+}
+
diff --git a/swat/scripting/client/encoder.js b/swat/scripting/client/encoder.js
new file mode 100644
index 00000000000..20eec9e2c35
--- /dev/null
+++ b/swat/scripting/client/encoder.js
@@ -0,0 +1,84 @@
+/*
+ client side js functions for encoding/decoding objects into linear strings
+
+ Copyright Andrew Tridgell 2005
+ released under the GNU GPL Version 2 or later
+*/
+/*
+ usage:
+
+ enc = encodeObject(obj);
+ obj = decodeObject(enc);
+
+ The encoded format of the object is a string that is safe to
+ use in URLs
+
+ Note that only data elements are encoded, not functions
+*/
+
+function count_members(o) {
+ var i, count = 0;
+ for (i in o) {
+ count++;
+ }
+ return count;
+}
+
+function encodeObject(o) {
+ var i, r = count_members(o) + ":";
+ for (i in o) {
+ var t = typeof(o[i]);
+ if (t == 'object') {
+ r = r + "" + i + ":" + t + ":" + encodeObject(o[i]);
+ } else if (t == 'string') {
+ var s = encodeURIComponent(o[i]).replace(/%/g,'#');
+ r = r + "" + i + ":" + t + ":" + s + ":";
+ } else if (t == 'boolean' || t == 'number') {
+ r = r + "" + i + ":" + t + ":" + o[i] + ":";
+ } else if (t == 'undefined' || t == 'null') {
+ r = r + "" + i + ":" + t + ":";
+ } else {
+ alert("Unable to encode type " + t);
+ }
+ }
+ return r;
+}
+
+function decodeObjectArray(a) {
+ var o = new Object();
+ var i, count = a[a.i]; a.i++;
+ for (i=0;i<count;i++) {
+ var name = a[a.i]; a.i++;
+ var type = a[a.i]; a.i++;
+ var value;
+ if (type == 'object') {
+ o[name] = decodeObjectArray(a);
+ } else if (type == "string") {
+ value = decodeURIComponent(a[a.i].replace(/#/g,'%')); a.i++;
+ o[name] = value;
+ } else if (type == "boolean") {
+ value = a[a.i]; a.i++;
+ if (value == 'true') {
+ o[name] = true;
+ } else {
+ o[name] = false;
+ }
+ } else if (type == "undefined") {
+ o[name] = undefined;
+ } else if (type == "null") {
+ o[name] = null;
+ } else if (type == "number") {
+ value = a[a.i]; a.i++;
+ o[name] = value * 1;
+ } else {
+ alert("Unable to delinearise type " + t);
+ }
+ }
+ return o;
+}
+
+function decodeObject(str) {
+ var a = str.split(':');
+ a.i = 0;
+ return decodeObjectArray(a);
+}