aboutsummaryrefslogtreecommitdiffstats
path: root/jquery.rpc_5823.js
blob: 3b7bd56071839f6d95d776f44a6cddbaa42b0630 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237


window.jQuery = window.jQuery || {};

jQuery.rpc = function(url, dataType, onLoadCallback, version, methods) {
	return new (function(url, dataType, onLoadCallback, version) {
		version = version || "1.0";
		dataType = dataType || "json";
		if(dataType != "json" && dataType != "xml") {
			new Error("IllegalArgument: Unsupported data type");
		}

	        var _self = this;

	        function pad2(f) {
			if(f<=9) {
				return "0"+f;
			} else {
				return ""+f;
			}
		}

		var serializeToXml = function(data) {
			switch (typeof data) {
			case 'boolean':
				return '<boolean>'+ ((data) ? '1' : '0') +'</boolean>';
			case 'number':
				var parsed = parseInt(data);
				if(parsed == data) {
					return '<int>'+ data +'</int>';
				}
				return '<double>'+ data +'</double>';
			case 'string':
				return '<string>'+ data +'</string>';
			case 'object':
				if(data instanceof Date) {
					return '<dateTime.iso8601>'+ data.getFullYear() + pad2(data.getMonth()) + pad2(data.getDate()) +'T'+ pad2(data.getHours()) +':'+ pad2(data.getMinutes()) +':'+ pad2(data.getSeconds()) +'</dateTime.iso8601>';
				} else if(data instanceof Array) {
					var ret = '<array><data>'+"\n";
					for (var i=0; i < data.length; i++) {
						ret += '  <value>'+ serializeToXml(data[i]) +"</value>\n";
					}
					ret += '</data></array>';
					return ret;
				} else {
					var ret = '<struct>'+"\n";
					jQuery.each(data, function(key, value) {
						ret += "  <member><name>"+ key +"</name><value>";
						ret += serializeToXml(value) +"</value></member>\n";
					});
					ret += '</struct>';
					return ret;
				}
			}
		}
		var xmlRpc = function(method, params) {
			var ret = '<?xml version="'+version+'"?><methodCall><methodName>'+method+'</methodName><params>';
			for(var i=0; i<params.length; i++) {
				ret += "<param><value>"+serializeToXml(params[i])+"</value></param>";
			}
			ret += "</params></methodCall>";
			return ret;
		}
		var parseXmlValue = function(node) {
			var jnode = jQuery(node);
			childs = jnode.children();

			// String not enclosed in a <string> tag
			if(childs.length==0) {
				var s="";
				for(var j=0; j<jnode[0].childNodes.length;j++) {
					s+=new String(jnode[0].childNodes[j].nodeValue);
				}
				return s;
			}

			for(var i=0; i < childs.length; i++) {
				switch(childs[i].tagName) {
				case 'boolean':
					return (jQuery(childs[i]).text() == 1);
				case 'int':
					return parseInt(jQuery(childs[i]).text());
				case 'double':
					return parseFloat(jQuery(childs[i]).text());
				case "string":
					return jQuery(childs[i]).text();
				case "array":
					var ret = [];
					jQuery("> data > value", childs[i]).each(
						function() {
							ret.push(parseXmlValue(this));
						}
					);
					return ret;
				case "struct":
					var ret = {};
					jQuery("> member", childs[i]).each(
						function() {
							ret[jQuery( "> name", this).text()] = parseXmlValue(jQuery("value", this));
						}
					);
					return ret;
				case "dateTime.iso8601":
					/* TODO: fill me :( */
					return 0;
				}
			}
		}
		var parseXmlResponse = function(data) {
			var ret = {};
			ret.version = version;
			jQuery("methodResponse params param > value", data).each(
				function(index) {
					ret.result = parseXmlValue(this);
				}
			);
			jQuery("methodResponse fault > value", data).each(
				function(index) {
					ret.error = parseXmlValue(this);
				}
			);
			return ret;
		}
		var rpc_contents = {
			'xml':'text/xml'
			,'json':'application/json'
		};
		var _rpc = function(method) {
			var params = [];
			var async; // whether we're async or not
			var callback;
		        var nargs=arguments.length;

			if(nargs > 1 && typeof(arguments[nargs-1])=="function") {
				callback = arguments[nargs-1];
				nargs=nargs-1;
				async=true;
			} else {
				async=false;
			}

			for (var i=1; i<nargs; i++) {
				params.push(arguments[i]);
			}

			var data;
			if(dataType == 'json') {
				data = {"version":version, "method":method, "params":params};
			} else {
				data = xmlRpc(method, params);
			}

			if(async) {
				jQuery.ajax({
					url: url,
					dataType: dataType,
					type: "POST",
					data: data,
					success: function(inp) {
						var json = inp;
						if(dataType == "xml") {
							json = parseXmlResponse(inp);
						}
						// console.log("json response:", json);
						callback(json);
					},
					processData: false,
					contentType: rpc_contents[dataType]});
			} else {
				var result = jQuery.ajax({
					url: url,
					dataType: dataType,
					type: "POST",
					data: data,
					async: false,
					contentType: rpc_contents[dataType]});
				if(dataType=="json") {
					return eval("("+result.responseText+")");
				} else {
					var myresult=parseXmlResponse(result.responseXML);
					return myresult;
				}
			}

		};
		function populate(methods) {
			// console.log(json);
			/* get the functions */
			var proc = null;
			for(var i = 0; i<methods.length; i++) {
				proc = methods[i];
				var obj = _self;
				var objStack = proc.split(/\./);
				for(var j = 0; j < (objStack.length - 1); j++){
					obj[objStack[j]] = obj[objStack[j]] || {};
					obj = obj[objStack[j]];
				}
				/* add the new procedure */
				obj[objStack[j]] = (
					function(method, obj) {
						var _outer = {"method":method,"rpc":_rpc};
						return function() {
							var params = [];
							params.push(_outer.method);
							for (var i=0; i<arguments.length; i++) {
								params.push(arguments[i]);
							}
							return _rpc.apply(_self, params);
						}
					}
				)(proc, _rpc);
			}
			// console.log('Load was performed.');
		}

		function asyncCallback(json) {
			populate(json.result);
			if(onLoadCallback) {
				onLoadCallback(_self);
			}
		}

		if(!methods) {
			_rpc("system.listMethods", asyncCallback);
		} else {
			populate(methods);
			if(onLoadCallback) {
			    // not sure this works - the intention is to call the callback as soon as this
			    // javascript function returns control back to the browser, to emulate the behaviour
			    // that the system.listMethods approach
				setTimeout(function(){onLoadCallback(_self);},0);
			}
		}

	})(url, dataType, onLoadCallback, version);
};