You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0 lines
6.9 KiB

  1. !function(a,b,c,d){a.api=a.fn.api=function(c){var e,f,g=a.extend(!0,{},a.api.settings,c),h="function"!=typeof this?this:a("<div/>"),i=g.stateContext?a(g.stateContext):a(h),j="object"==typeof this?a(h):i,k=this,l=(new Date).getTime(),m=[],n=j.selector||"",o=g.namespace+"-module",p=g.className,q=g.metadata,r=g.error,s=j.data(o),t=arguments[0],u=s!==d&&"string"==typeof t,v=[].slice.call(arguments,1);return e={initialize:function(){var c,f,h,k,l,m,n=(new Date).getTime(),o={},s={};return g.serializeForm&&a(this).toJSON()!==d&&(o=e.get.formData(),e.debug("Adding form data to API Request",o),a.extend(!0,g.data,o)),c=a.proxy(g.beforeSend,j)(g),c===d||c?(k=e.get.url(e.get.templateURL()))?(h=a.Deferred().always(function(){g.stateContext&&i.removeClass(p.loading),a.proxy(g.complete,j)()}).done(function(b){e.debug("API request successful"),"json"==g.dataType?b.error!==d?a.proxy(g.failure,i)(b.error,g,j):a.isArray(b.errors)?a.proxy(g.failure,i)(b.errors[0],g,j):a.proxy(g.success,i)(b,g,j):a.proxy(g.success,i)(b,g,j)}).fail(function(b,c,f){var h,j=g.error[c]!==d?g.error[c]:f;if(b!==d)if(b.readyState!==d&&4==b.readyState){if(200!=b.status&&f!==d&&""!==f)e.error(k.statusMessage+f);else if("error"==c&&"json"==g.dataType)try{h=a.parseJSON(b.responseText),h&&h.error!==d&&(j=h.error)}catch(k){e.error(k.JSONParse)}i.removeClass(p.loading).addClass(p.error),g.errorLength>0&&setTimeout(function(){i.removeClass(p.error)},g.errorLength),e.debug("API Request error:",j),a.proxy(g.failure,i)(j,g,this)}else e.debug("Request Aborted (Most likely caused by page change)")}),a.extend(!0,s,g,{success:function(){},failure:function(){},complete:function(){},type:g.method||g.type,data:l,url:k,beforeSend:g.beforeXHR}),g.stateContext&&i.addClass(p.loading),g.progress&&(e.verbose("Adding progress events"),a.extend(!0,s,{xhr:function(){var c=new b.XMLHttpRequest;return c.upload.addEventListener("progress",function(b){var c;b.lengthComputable&&(c=Math.round(b.loaded/b.total*1e4)/100+"%",a.proxy(g.progress,i)(c,b))},!1),c.addEventListener("progress",function(b){var c;b.lengthComputable&&(c=Math.round(b.loaded/b.total*1e4)/100+"%",a.proxy(g.progress,i)(c,b))},!1),c}})),e.verbose("Creating AJAX request with settings: ",s),m=a.ajax(s).always(function(){f=g.loadingLength-((new Date).getTime()-n),g.loadingDelay=0>f?0:f}).done(function(a){var b=this;setTimeout(function(){h.resolveWith(b,[a])},g.loadingDelay)}).fail(function(a,b,c){var d=this;"abort"!=b?setTimeout(function(){h.rejectWith(d,[a,b,c])},g.loadingDelay):i.removeClass(p.error).removeClass(p.loading)}),g.stateContext&&j.data(q.promise,h).data(q.xhr,m),void 0):(e.error(r.missingURL),e.reset(),void 0):(e.error(r.beforeSend),e.reset(),void 0)},get:{formData:function(){return j.closest("form").toJSON()},templateURL:function(){var a,b=j.data(g.metadata.action)||g.action||!1;return b&&(e.debug("Creating url for: ",b),g.api[b]!==d?a=g.api[b]:e.error(r.missingAction)),g.url&&(a=g.url,e.debug("Getting url",a)),a},url:function(b,c){var f;return b&&(f=b.match(g.regExpTemplate),c=c||g.urlData,f&&(e.debug("Looking for URL variables",f),a.each(f,function(g,h){var i=h.substr(2,h.length-3),k=a.isPlainObject(c)&&c[i]!==d?c[i]:j.data(i)!==d?j.data(i):c[i];if(e.verbose("Looking for variable",i,j,j.data(i),c[i]),k===!1)e.debug("Removing variable from URL",f),b=b.replace("/"+h,"");else{if(k===d||!k)return e.error(r.missingParameter+i),b=!1,!1;b=b.replace(h,k)}}))),b}},reset:function(){j.data(q.promise,!1).data(q.xhr,!1),i.removeClass(p.error).removeClass(p.loading)},setting:function(b,c){if(a.isPlainObject(b))a.extend(!0,g,b);else{if(c===d)return g[b];g[b]=c}},internal:function(b,c){if(a.isPlainObject(b))a.extend(!0,e,b);else{if(c===d)return e[b];e[b]=c}},debug:function(){g.debug&&(g.performance?e.performance.log(arguments):(e.debug=Function.prototype.bind.call(console.info,console,g.name+":"),e.debug.apply(console,arguments)))},verbose:function(){g.verbose&&g.debug&&(g.performance?e.performance.log(arguments):(e.verbose=Function.prototype.bind.call(console.info,console,g.name+":"),e.verbose.apply(console,arguments)))},error:func