r24985: Start to revert us back to the old-style SWAT, while trying not to
[kai/samba.git] / swat / scripting / client / call.js
1 /*
2         client side js functions for remote calls into the server
3
4         Copyright Andrew Tridgell 2005
5         released under the GNU GPL Version 2 or later
6 */
7
8 var __call = new Object();
9
10 /*
11   we can't use the qooxdoo portability layer for this, as it assumes
12   you are using an XML transport, so instead replicate the portability
13   code for remote calls here. Don't look too closely or you will go
14   blind.
15 */
16 __call._activex = window.ActiveXObject && !(new QxClient).isOpera() ? true : false;
17 __call._activexobj = null;
18 __call._ok = QxXmlHttpLoader._http || QxXmlHttpLoader._activex;
19
20 if (__call._activex) {
21         var servers = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
22         for (var i=0; i<servers.length; i++) {
23                 try {
24                         var o = new ActiveXObject(servers[i] + ".XMLHTTP");
25                         __call._activexobj = servers[i];
26                         o = null;
27                 } catch(ex) {};
28         };
29 };
30
31 /*
32   return a http object ready for a remote call
33 */
34 function __http_object() {
35         return __call._activex ? 
36                 new ActiveXObject(__call._activexobj + ".XMLHTTP") : 
37                 new XMLHttpRequest();
38 }
39
40 /*
41         usage:
42
43           vserver_call(url, func, callback, args);
44
45         'func' is a function name to call on the server
46         any additional arguments are passed to func() on the server
47
48         The callback() function is called with the returned
49         object. 'callback' may be null.
50 */
51 function vserver_call_url(url, func, callback, args) {
52         var args2 = new Object();
53         args2.length = args.length;
54         var i;
55         for (i=0;i<args.length;i++) {
56                 args2[i] = args[i];
57         }
58         var req = __http_object();
59         req.open("POST", url, true);
60         req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
61         req.send("ajaj_func=" + func + "&ajaj_args=" + encodeObject(args2));
62         req.onreadystatechange = function() { 
63                 if (4 == req.readyState && callback != null) {
64                         var o = decodeObject(req.responseText);
65                         callback(o.res);
66                 }
67         }
68 }
69
70
71 /*
72         usage:
73
74           server_call_url(url, func, callback, ...);
75
76         'func' is a function name to call on the server
77         any additional arguments are passed to func() on the server
78
79         The callback() function is called with the returned
80         object. 'callback' may be null.
81 */
82 function server_call_url(url, func, callback) {
83         var args = new Object();
84         var i;
85         for (i=3;i<arguments.length;i++) {
86                 args[i-3] = arguments[i];
87         }
88         args.length = i-3;
89         vserver_call_url(url, func, callback, args);
90 }
91
92
93 /*
94         call printf on the server
95 */
96 function srv_printf() {
97         vserver_call_url('/scripting/general_calls.esp', 'srv_printf', null, arguments);
98 }
99
100 /*
101         usage:
102
103           server_call(func, callback, ...);
104
105         'func' is a function name to call on the server
106         any additional arguments are passed to func() on the server
107
108         The callback() function is called with the returned
109         object. 'callback' may be null.
110 */
111 function server_call(func, callback) {
112         var args = new Array(arguments.length-2);
113         var i;
114         for (i=0;i<args.length;i++) {
115                 args[i] = arguments[i+1];
116         }
117         vserver_call_url("@request.REQUEST_URI", func, callback, args);
118 }