diff options
author | Matěj Cepl <mcepl@redhat.com> | 2009-11-15 23:35:25 +0100 |
---|---|---|
committer | Matěj Cepl <mcepl@redhat.com> | 2009-11-15 23:35:25 +0100 |
commit | 34bd7b9b8c29d5d7d72a18ed376d93fa7ba980fe (patch) | |
tree | a224966e71847bdc2d61dd7f2f13ffd067451cba | |
parent | 65db58735021ec41ea90316165de47f2d6f55ffe (diff) | |
download | bugzilla-triage-34bd7b9b8c29d5d7d72a18ed376d93fa7ba980fe.tar.gz |
jquery.rpc.js added (with various patches)
-rw-r--r-- | bugzillaBugTriage.user.js | 4 | ||||
-rw-r--r-- | jquery-rpc-drupal-fix.js | 174 | ||||
-rw-r--r-- | jquery.rpc.js | 176 | ||||
-rw-r--r-- | jquery.rpc_2968.patch | 13 | ||||
-rw-r--r-- | jquery.rpc_5823.js | 237 |
5 files changed, 604 insertions, 0 deletions
diff --git a/bugzillaBugTriage.user.js b/bugzillaBugTriage.user.js index eed156c..1a798a4 100644 --- a/bugzillaBugTriage.user.js +++ b/bugzillaBugTriage.user.js @@ -9,6 +9,9 @@ // @include https://bugzilla.redhat.com/show_bug.cgi* // @include https://bugzilla.redhat.com/process_bug.cgi // @require http://mcepl.fedorapeople.org/scripts/xmlrpc.js +// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js +// @require http://mcepl.fedorapeople.org/scripts/jquery.rpc.js + // ==/UserScript== // https://bugzilla.redhat.com/show_bug.cgi?id=451951 @@ -407,6 +410,7 @@ function queryForSelection() { * @param callback function catching callback */ function sendRequest(url,data,method,callback) { + //$.rpc(url, dataType, onLoadCallback, version); GM_xmlhttpRequest({ method: method, url: url, diff --git a/jquery-rpc-drupal-fix.js b/jquery-rpc-drupal-fix.js new file mode 100644 index 0000000..1d29d95 --- /dev/null +++ b/jquery-rpc-drupal-fix.js @@ -0,0 +1,174 @@ +window.jQuery = window.jQuery || {}; +window.jQuery.fn = window.jQuery.fn || {}; + +jQuery.fn.rpc = 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; + 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() + data.getMonth() + data.getDate() +'T'+ data.getHours() +':'+ data.getMinutes() +':'+ 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) { + childs = jQuery(node).children(); + 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 NULL; + } + } + } + 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, callback) { + var params = []; + for (var i=2; i<arguments.length; i++) { + params.push(arguments[i]); + } + console.log(params); + var data; + if(dataType == 'json') { + data = {"version":version, "method":method, "params":params}; + } else { + data = xmlRpc(method, params); + } + console.log(data); + 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] + }); + }; + _rpc("system.listMethods", + function(json) { + console.log(json); + /* get the functions */ + if(!json.result) { + return; + } + var proc = null; + for(var i = 0; i<json.result.length; i++) { + proc = json.result[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(callback) { + var params = []; + params.push(_outer.method); + params.push(callback); + for (var i=1; i<arguments.length; i++) { + params.push(arguments[i]); + } + _rpc.apply(_self, params); + } + } + )(proc, _rpc); + } + console.log('Load was performed.'); + if(onLoadCallback) { + onLoadCallback(_self); + } + } + ); +}; + diff --git a/jquery.rpc.js b/jquery.rpc.js new file mode 100644 index 0000000..9d975ef --- /dev/null +++ b/jquery.rpc.js @@ -0,0 +1,176 @@ + + +window.jQuery = window.jQuery || {}; + +jQuery.rpc = function(url, dataType, onLoadCallback, version) { + 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; + 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() + data.getMonth() + data.getDate() +'T'+ data.getHours() +':'+ data.getMinutes() +':'+ 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) { + childs = jQuery(node).children(); + 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 NULL; + } + } + } + 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, callback) { + var params = []; + for (var i=2; i<arguments.length; i++) { + params.push(arguments[i]); + } + // console.log(params); + var data; + if(dataType == 'json') { + data = {"version":version, "method":method, "params":params}; + } else { + data = xmlRpc(method, params); + } + 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] + }); + }; + _rpc("system.listMethods", + function(json) { + // console.log(json); + /* get the functions */ + if(!json.result) { + return; + } + var proc = null; + for(var i = 0; i<json.result.length; i++) { + proc = json.result[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(callback) { + var params = []; + params.push(_outer.method); + params.push(callback); + for (var i=1; i<arguments.length; i++) { + params.push(arguments[i]); + } + _rpc.apply(_self, params); + } + } + )(proc, _rpc); + } + // console.log('Load was performed.'); + if(onLoadCallback) { + onLoadCallback(_self); + } + } + ); + })(url, dataType, onLoadCallback, version); +}; + diff --git a/jquery.rpc_2968.patch b/jquery.rpc_2968.patch new file mode 100644 index 0000000..94344c3 --- /dev/null +++ b/jquery.rpc_2968.patch @@ -0,0 +1,13 @@ +Also, the parseXMLValue function should probably trow an error, +instead of failing silently. + +--- jquery.rpc.js_1.txt 2008-05-23 13:39:21.000000000 +1200 ++++ jquery.rpc.js 2008-06-19 13:13:05.000000000 +1200 +@@ -58,6 +56,7 @@ +case 'boolean': +return (jQuery(childs[i]).text() == 1); +case 'int': ++ case 'i4': +return parseInt(jQuery(childs[i]).text()); +case 'double': +return parseFloat(jQuery(childs[i]).text()); diff --git a/jquery.rpc_5823.js b/jquery.rpc_5823.js new file mode 100644 index 0000000..3b7bd56 --- /dev/null +++ b/jquery.rpc_5823.js @@ -0,0 +1,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); +}; + |