1 line
5.7 KiB
JavaScript
Raw Normal View History

2019-06-06 15:21:15 +03:00
(function(a,b,c){"use strict";var d,e,f,g,h,i,j,k,l,m,n,o;j=function(a){throw new Error("ERROR: jquery.rest: "+a)},l=function(a){var b;for(b="";a-->0;)b+=" ";return b},i=function(b){return a.btoa||j("You need a polyfill for 'btoa' to use basic auth."),a.btoa(b)},m=function(b){return a.JSON||j("You need a polyfill for 'JSON' to use stringify."),a.JSON.stringify(b)},k=function(a,c){var d;return d=function(){},d.prototype=a,b.extend(!0,new d,c)},n=function(a){return a&&b.isPlainObject(a)?(b.each(a,function(a){return g[a]===c?j("Unknown option: '"+a+"'"):void 0}),null):!1},o=function(a,c){return"string"!==b.type(c)?j("'"+a+"' must be a string"):void 0},h=function(){return alert('"delete()" has been deprecated. Please use "destroy()" or "del()" instead.')},g={url:"",cache:0,request:function(a,c){return b.ajax(c)},isSingle:!1,autoClearCache:!0,cachableMethods:["GET"],methodOverride:!1,stringifyData:!1,stripTrailingSlash:!1,password:null,username:null,verbs:{create:"POST",read:"GET",update:"PUT",destroy:"DELETE"},ajax:{dataType:"json"}},d=function(){function a(a){this.parent=a,this.c={}}return a.prototype.valid=function(a){var b;return b=(new Date).getTime()-a.getTime(),b<=1e3*this.parent.opts.cache},a.prototype.key=function(a){var c,d=this;return c="",b.each(a,function(a,e){return c+=a+"="+(b.isPlainObject(e)?"{"+d.key(e)+"}":e)+"|"}),c},a.prototype.get=function(a){var b;return(b=this.c[a])?this.valid(b.created)?b.data:void 0:void 0},a.prototype.put=function(a,b){return this.c[a]={created:new Date,data:b}},a.prototype.clear=function(a){var c=this;return a?b.each(this.c,function(b){return b.match(a)?delete c.c[b]:void 0}):this.c={}},a}(),f=function(){function a(a,c,d,e){this.name=a,this.method=c,null==d&&(d={}),this.parent=e,o("name",this.name),o("method",this.method),n(d),this.parent[this.name]&&j("Cannot add Verb: '"+a+"' already exists"),this.method=c.toUpperCase(),d.url||(d.url=""),this.opts=k(this.parent.opts,d),this.root=this.parent.root,this.custom=!g.verbs[this.name],this.call=b.proxy(this.call,this),this.call.instance=this}return a.prototype.call=function(){var a,b,c;return c=this.parent.extractUrlData(this.method,arguments),b=c.url,a=c.data,this.custom&&(b+=this.opts.url||this.name),this.parent.ajax.call(this,this.method,b,a)},a.prototype.show=function(a){return console.log(l(a)+this.name+": "+this.method)},a}(),e=function(){function a(b,c,d){null==c&&(c={}),n(c),d&&d instanceof a?(this.name=b,o("name",this.name),this.constructChild(d,c)):(this.url=b||"",o("url",this.url),this.constructRoot(c))}return a.prototype.constructRoot=function(a){return this.opts=k(g,a),this.root=this,this.expectedIds=0,this.urlNoId=this.url,this.cache=new d(this),this.parent=null,this.name=this.opts.name||"ROOT"},a.prototype.constructChild=function(c,d){return this.parent=c,o("name",this.name),this.parent instanceof a||this.error("Invalid parent"),this.parent[this.name]&&this.error("'"+name+"' already exists"),d.url||(d.url=""),this.opts=k(this.parent.opts,d),this.opts.isSingle="isSingle"in d&&d.isSingle,this.root=this.parent.root,this.urlNoId=this.parent.url+(""+(this.opts.url||this.name)+"/"),this.url=this.urlNoId,this.expectedIds=this.parent.expectedIds,this.opts.isSingle||(this.expectedIds+=1,this.url+=":ID_"+this.expectedIds+"/"),b.each(this.opts.verbs,b.proxy(this.addVerb,this)),this.destroy?(this.del=this.destroy,this["delete"]=h):void 0},a.prototype.error=function(a){return j("Cannot add Resource: "+a)},a.prototype.add=function(b,c){return this[b]=new a(b,c,this)},a.prototype.addVerb=function(a,b,c){return this[a]=new f(a,b,c,this).call},a.prototype.show=function(c){return null==c&&(c=0),c>25&&j("Plugin Bug! Recursion Fail"),this.name&&console.log(l(c)+this.name+": "+this.url),b.each(this,function(a,d){return"function"===b.type(d)&&d.instance instanceof f&&"del"!==a?d.instance.show(c+1):void 0}),b.each(this,function(b,d){return"parent"!==b&&"root"!==b&&d instanceof a?d.show(c+1):void 0}),null},a.prototype.toString=function(){return this.name},a.prototype.extractUrlData=function(a,c){var d,e,f,g,h,i,k,l,m,n,o,p,q,r,s,t;for(k=[],g=nu