src/pyams_skin/resources/js/ext/jquery-jsonrpc.min.js
changeset 475 79ae32c82e3e
parent 43 d366c88a0f88
equal deleted inserted replaced
473:ca7ba7882abe 475:79ae32c82e3e
     1 (function($,undefined){$.extend({jsonRPC:{version:"2.0",endPoint:null,namespace:null,setup:function(params){this._validateConfigParams(params);this.endPoint=params.endPoint;this.namespace=params.namespace;this.cache=params.cache!==undefined?params.cache:true;return this},withOptions:function(params,callback){this._validateConfigParams(params);if(callback===undefined){throw ("No callback specified")}origParams={endPoint:this.endPoint,namespace:this.namespace};this.setup(params);callback.call(this);this.setup(origParams)},request:function(method,options){if(options===undefined){options={id:1}}if(options.id===undefined){options.id=1}if(options.cache===undefined){options.cache=this.cache}this._validateRequestMethod(method);this._validateRequestParams(options.params);this._validateRequestCallbacks(options.success,options.error);this._doRequest(JSON.stringify(this._requestDataObj(method,options.params,options.id)),options);return true},batchRequest:function(requests,options){if(options===undefined){options={}}if(!$.isArray(requests)||requests.length===0){throw ("Invalid requests supplied for jsonRPC batchRequest. Must be an array object that contain at least a method attribute")}var _that=this;$.each(requests,function(i,req){_that._validateRequestMethod(req.method);_that._validateRequestParams(req.params);if(req.id===undefined){req.id=i+1}});this._validateRequestCallbacks(options.success,options.error);var data=[],request;for(var i=0;i<requests.length;i++){request=requests[i];data.push(this._requestDataObj(request.method,request.params,request.id))}this._doRequest(JSON.stringify(data),options)},_validateConfigParams:function(params){if(params===undefined){throw ("No params specified")}else{if(params.endPoint&&typeof(params.endPoint)!=="string"){throw ("endPoint must be a string")}if(params.namespace&&typeof(params.namespace)!=="string"){throw ("namespace must be a string")}}},_validateRequestMethod:function(method){if(typeof(method)!=="string"){throw ("Invalid method supplied for jsonRPC request")}return true},_validateRequestParams:function(params){if(!(params===null||params===undefined||typeof(params)==="object"||$.isArray(params))){throw ("Invalid params supplied for jsonRPC request. It must be empty, an object or an array.")}return true},_validateRequestCallbacks:function(success,error){if(success!==undefined&&typeof(success)!=="function"){throw ("Invalid success callback supplied for jsonRPC request")}if(error!==undefined&&typeof(error)!=="function"){throw ("Invalid error callback supplied for jsonRPC request")}return true},_doRequest:function(data,options){var _that=this;$.ajax({type:"POST",async:false!==options.async,dataType:"json",contentType:"application/json",url:this._requestUrl((options.endPoint||options.url),options.cache),data:data,cache:options.cache,processData:false,error:function(json){_that._requestError.call(_that,json,options.error)},success:function(json){_that._requestSuccess.call(_that,json,options.success,options.error)}})},_requestUrl:function(url,cache){url=url||this.endPoint;if(!cache){if(url.indexOf("?")<0){url+="?tm="+new Date().getTime()}else{url+="&tm="+new Date().getTime()}}return url},_requestDataObj:function(method,params,id){var dataObj={jsonrpc:this.version,method:this.namespace?this.namespace+"."+method:method,id:id};if(params!==undefined){dataObj.params=params}return dataObj},_requestError:function(json,error){if(error!==undefined&&typeof(error)==="function"){if(typeof(json.responseText)==="string"){try{error(eval("("+json.responseText+")"))}catch(e){error(this._response())}}else{error(this._response())}}},_requestSuccess:function(json,success,error){var response=this._response(json);if(response.error&&typeof(error)==="function"){error(response);return}if(typeof(success)==="function"){success(response)}},_response:function(json){if(json===undefined){return{error:"Internal server error",version:"2.0"}}else{try{if(typeof(json)==="string"){json=eval("("+json+")")}if(($.isArray(json)&&json.length>0&&json[0].jsonrpc!=="2.0")||(!$.isArray(json)&&json.jsonrpc!=="2.0")){throw"Version error"}return json}catch(e){return{error:"Internal server error: "+e,version:"2.0"}}}}}})})(jQuery);
     1 !function($,undefined){$.extend({jsonRPC:{version:"2.0",endPoint:null,namespace:null,setup:function(e){return this._validateConfigParams(e),this.endPoint=e.endPoint,this.namespace=e.namespace,this.cache=e.cache===undefined||e.cache,this},withOptions:function(e,t){if(this._validateConfigParams(e),t===undefined)throw"No callback specified";origParams={endPoint:this.endPoint,namespace:this.namespace},this.setup(e),t.call(this),this.setup(origParams)},request:function(e,t){return t===undefined&&(t={id:1}),t.id===undefined&&(t.id=1),t.cache===undefined&&(t.cache=this.cache),this._validateRequestMethod(e),this._validateRequestParams(t.params),this._validateRequestCallbacks(t.success,t.error),this._doRequest(JSON.stringify(this._requestDataObj(e,t.params,t.id)),t),!0},batchRequest:function(e,t){if(t===undefined&&(t={}),!$.isArray(e)||0===e.length)throw"Invalid requests supplied for jsonRPC batchRequest. Must be an array object that contain at least a method attribute";var n=this;$.each(e,function(e,t){n._validateRequestMethod(t.method),n._validateRequestParams(t.params),t.id===undefined&&(t.id=e+1)}),this._validateRequestCallbacks(t.success,t.error);for(var s,r=[],i=0;i<e.length;i++)s=e[i],r.push(this._requestDataObj(s.method,s.params,s.id));this._doRequest(JSON.stringify(r),t)},_validateConfigParams:function(e){if(e===undefined)throw"No params specified";if(e.endPoint&&"string"!=typeof e.endPoint)throw"endPoint must be a string";if(e.namespace&&"string"!=typeof e.namespace)throw"namespace must be a string"},_validateRequestMethod:function(e){if("string"!=typeof e)throw"Invalid method supplied for jsonRPC request";return!0},_validateRequestParams:function(e){if(null!==e&&e!==undefined&&"object"!=typeof e&&!$.isArray(e))throw"Invalid params supplied for jsonRPC request. It must be empty, an object or an array.";return!0},_validateRequestCallbacks:function(e,t){if(e!==undefined&&"function"!=typeof e)throw"Invalid success callback supplied for jsonRPC request";if(t!==undefined&&"function"!=typeof t)throw"Invalid error callback supplied for jsonRPC request";return!0},_doRequest:function(e,t){var n=this;$.ajax({type:"POST",async:!1!==t.async,dataType:"json",contentType:"application/json",url:this._requestUrl(t.endPoint||t.url,t.cache),data:e,cache:t.cache,processData:!1,error:function(e){n._requestError.call(n,e,t.error)},success:function(e){n._requestSuccess.call(n,e,t.success,t.error)}})},_requestUrl:function(e,t){return e=e||this.endPoint,t||(e.indexOf("?")<0?e+="?tm="+(new Date).getTime():e+="&tm="+(new Date).getTime()),e},_requestDataObj:function(e,t,n){var s={jsonrpc:this.version,method:this.namespace?this.namespace+"."+e:e,id:n};return t!==undefined&&(s.params=t),s},_requestError:function(json,error){if(error!==undefined&&"function"==typeof error)if("string"==typeof json.responseText)try{error(eval("("+json.responseText+")"))}catch(e){error(this._response())}else error(this._response())},_requestSuccess:function(e,t,n){var s=this._response(e);s.error&&"function"==typeof n?n(s):"function"==typeof t&&t(s)},_response:function(json){if(json===undefined)return{error:"Internal server error",version:"2.0"};try{if("string"==typeof json&&(json=eval("("+json+")")),$.isArray(json)&&json.length>0&&"2.0"!==json[0].jsonrpc||!$.isArray(json)&&"2.0"!==json.jsonrpc)throw"Version error";return json}catch(e){return{error:"Internal server error: "+e,version:"2.0"}}}}})}(jQuery);