Completed LuCI Livestats
[project/luci.git] / applications / luci-livestats / htdocs / luci-static / resources / livestats / JsonRpc.js
1 /* MochiKit.JsonRpc */
2
3 if (typeof(dojo) != 'undefined') {
4     dojo.provide("MochiKit.JsonRpc");
5     dojo.require("MochiKit.Base");
6     dojo.require("MochiKit.DOM");
7     dojo.require("MochiKit.Async");
8 }
9
10 if (typeof(JSAN) != 'undefined') {
11     JSAN.use("MochiKit.Base", []);
12     JSAN.use("MochiKit.DOM", []);
13     JSAN.use("MochiKit.Async", []);
14 }
15
16 try {
17     if (typeof(MochiKit.Base) == 'undefined' ||
18         typeof(MochiKit.DOM) == 'undefined' ||
19         typeof(MochiKit.Async) == 'undefined') {
20         throw "";
21     }
22 } catch (e) {
23     throw "MochiKit.JsonRpc depends on MochiKit.Base, MochiKit.DOM and MochiKit.Async";
24 }
25
26 if (typeof(MochiKit.JsonRpc) == 'undefined') {
27     MochiKit.JsonRpc = {};
28 }
29
30 MochiKit.JsonRpc.NAME = "MochiKit.JsonRpc";
31 MochiKit.JsonRpc.VERSION = "0.90";
32
33 MochiKit.JsonRpc.__repr__ = function () {
34     return "[" + this.NAME + " " + this.VERSION + "]";
35 }
36
37 MochiKit.JsonRpc.toString = function () {
38     return this.__repr__();
39 }
40
41 MochiKit.JsonRpc.JsonRpcError = function (message) {
42     this.message = message;
43     this.name = 'JsonRpcError';
44 }
45
46 MochiKit.JsonRpc.JsonRpcError.prototype = new Error();
47 MochiKit.JsonRpc.JsonRpcError.prototype.repr = function () {
48     return 'JsonRpcError(' + this.message + ')';
49 }
50
51 MochiKit.JsonRpc.JsonRpcError.prototype.toString = function () {
52     return this.repr();
53 }
54
55 MochiKit.JsonRpc.jsonObject = function (o) {
56     var attrs=[];
57     for(attr in o){
58         if(typeof o[attr] != "function"){
59             attrs.push('"' + attr + '": ' + json(o[attr]));
60         }
61     }
62     return "{" + attrs.join(", ") + "}";
63 }
64
65 MochiKit.JsonRpc.isObject = function (o) {
66     return true;
67 }
68
69 MochiKit.JsonRpc.jsonArray = function (o) {
70     return "[" + MochiKit.Base.map(json, o).join(", ") + "]";
71 }
72
73 var MB = MochiKit.Base
74
75 MochiKit.JsonRpc.jsonRegistry = new MochiKit.Base.AdapterRegistry();
76 MochiKit.JsonRpc.jsonRegistry.register('arrayLike',MB.isArrayLike,MochiKit.JsonRpc.jsonArray);
77 MochiKit.JsonRpc.jsonRegistry.register("string", MB.typeMatcher("string"), MB.reprString);
78 MochiKit.JsonRpc.jsonRegistry.register("numbers", MB.typeMatcher("number", "boolean"), MB.reprNumber);
79 MochiKit.JsonRpc.jsonRegistry.register("undefined", MB.isUndefined, MB.reprUndefined);
80 MochiKit.JsonRpc.jsonRegistry.register("null", MB.isNull, MB.reprNull);
81 MochiKit.JsonRpc.jsonRegistry.register("objectLike", MochiKit.JsonRpc.isObject, MochiKit.JsonRpc.jsonObject);
82
83 MochiKit.JsonRpc.json = function (o) {
84     try {
85         if (typeof(o.__json__) == 'function') {
86             return o.__json__();
87         } else if (typeof(o.json) == 'function' && o.json != arguments.callee) {
88             return o.json();
89         }
90         return jsonRegistry.match(o);
91     } catch (e) {
92         if (typeof(o.NAME) == 'string' && (
93                 o.toString == Function.prototype.toString ||
94                 o.toString == Object.prototype.toString
95             )) {
96             return o.NAME;
97         }
98         return o;
99     }
100
101 }
102
103
104 MochiKit.JsonRpc.JsonRpcCall = function (method,params) {
105     this.method = method;
106     this.params = params;
107     this.id = '0';
108 }
109
110 MochiKit.JsonRpc.JsonRpcProxy = function (url,methNames) {
111     MochiKit.Base.bindMethods(this);
112     this.url = url;
113     if (methNames) {
114         MochiKit.Base.map(this._proxyMethod,methNames);
115     }
116 }
117
118 update(MochiKit.JsonRpc.JsonRpcProxy.prototype, {
119     'call': function () {
120         var arglist = MochiKit.Base.map(null,arguments)
121         var methname = arglist.shift()
122         log(arglist);
123         var callobj = new MochiKit.JsonRpc.JsonRpcCall(methname,arglist);
124         var callstr = json(callobj);
125         var req = MochiKit.Async.getXMLHttpRequest();
126         req.open("POST",this.url,true);
127         req.setRequestHeader("Content-Type","text/plain");
128         req.setRequestHeader("Content-Length",callstr.length);
129         var d = MochiKit.Async.sendXMLHttpRequest(req,callstr);
130         d.addCallback(MochiKit.Async.evalJSONRequest);
131         d.addCallback(this._extractResult);
132
133         return d
134     },
135     'addSingleMethod': function (methName) {
136         if (methName) {
137             this._proxyMethod(methName);
138         }
139     },
140     'addMethods': function (methNames) {
141         if (methNames) {
142             MochiKit.Base.map(this._proxyMethod,methNames);
143         }
144     },
145     '_extractResult': function (resp) {
146         if (!resp.error){
147             return resp.result;
148         } else {
149             throw new MochiKit.JsonRpc.JsonRpcError(resp.error);
150         }
151     },
152     '_proxyMethod': function (methname) {
153         this[methname] = MochiKit.Base.partial(this.call,methname);
154     }
155 });
156
157 MochiKit.JsonRpc.DomObjectFromJson = function (){
158     var retval = false;
159     if (arguments.length == 1) {
160         var arg = arguments[0];
161         if (typeof(arg) == 'string'){
162             retval = MochiKit.DOM.SPAN(null,arg);
163         } else {
164             var objrepr = arguments[0];
165             var elem = document.createElement(objrepr[0]);
166             var attrs = objrepr[1];
167             if (attrs) {
168                 MochiKit.DOM.updateNodeAttributes(elem, attrs);
169             }
170             if (objrepr.length >= 3){
171                 var extraobj = objrepr[2]
172                 for (var i=0;i<extraobj.length;i++) {
173                     var value = MochiKit.JsonRpc.DomObjectFromJson(extraobj[i]);
174                     if (value) {
175                         elem.appendChild(value);
176                     }
177                 }
178             }
179             retval =  elem;
180         }
181     }
182     return retval;
183 };
184
185 MochiKit.JsonRpc.EXPORT = [
186     "JsonRpcError",
187     "JsonRpcProxy",
188 ];
189
190 MochiKit.JsonRpc.EXPORT_OK = [
191     "jsonObject",
192     "jsonArray",
193     "jsonRegistry",
194     "json",
195     "JsonRpcCall",
196     "DomObjectFromJson",
197 ];
198
199 MochiKit.JsonRpc.__new__ = function () {
200
201     this.EXPORT_TAGS = {
202         ":common": this.EXPORT,
203         ":all": MochiKit.Base.concat(this.EXPORT, this.EXPORT_OK)
204     };
205
206     MochiKit.Base.nameFunctions(this);
207
208 };
209
210 MochiKit.JsonRpc.__new__();
211
212 if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined')
213     || (typeof(MochiKit.__compat__) == 'boolean' && MochiKit.__compat__)) {
214     (function (self) {
215             var all = self.EXPORT_TAGS[":all"];
216             for (var i = 0; i < all.length; i++) {
217                 this[all[i]] = self[all[i]];
218             }
219         })(MochiKit.JsonRpc);
220 }