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
5.8 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=j.data("module-"+g.namespace),l=j.get(),m=arguments[0],n="string"==typeof m,o=[].slice.call(arguments,1),p=g.className,q=g.metadata,r=g.errors;return f={initialize:function(){var c,e,h,k,l,m,n=(new Date).getTime(),o={},r={},s=g.errors;return g.serializeForm&&a(this).toJSON()!==d&&(o=f.get.formData(),f.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=f.get.url(f.get.templateURL()))?(h=a.Deferred().always(function(){g.stateContext&&i.removeClass(p.loading),a.proxy(g.complete,j)()}).done(function(b){f.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,e){var h,j=g.errors[c]!==d?g.errors[c]:e;if(b!==d)if(b.readyState!==d&&4==b.readyState){if(200!=b.status&&e!==d&&""!==e)f.error(s.statusMessage+e);else if("error"==c&&"json"==g.dataType)try{h=a.parseJSON(b.responseText),h&&h.error!==d&&(j=h.error)}catch(k){f.error(s.JSONParse)}i.removeClass(p.loading).addClass(p.error),g.errorLength>0&&setTimeout(function(){i.removeClass(p.error)},g.errorLength),f.debug("API Request error:",j),a.proxy(g.failure,i)(j,g,this)}else f.debug("Request Aborted (Most likely caused by page change)")}),a.extend(!0,r,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&&(f.verbose("Adding progress events"),a.extend(!0,r,{xhr:function(){var c=new b.XMLHttpRequest;return c.upload.addEventListener("progress",function(b){var c;b.lengthComputable&&(c=Math.round(1e4*(b.loaded/b.total))/100+"%",a.proxy(g.progress,i)(c,b))},!1),c.addEventListener("progress",function(b){var c;b.lengthComputable&&(c=Math.round(1e4*(b.loaded/b.total))/100+"%",a.proxy(g.progress,i)(c,b))},!1),c}})),f.verbose("Creating AJAX request with settings: ",r),m=a.ajax(r).always(function(){e=g.loadingLength-((new Date).getTime()-n),g.loadingDelay=0>e?0:e}).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):(f.error(s.missingURL),f.reset(),void 0):(f.error(s.beforeSend),f.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&&(f.debug("Creating url for: ",b),g.api[b]!==d?a=g.api[b]:f.error(r.missingAction)),g.url&&(a=g.url,f.debug("Getting url",a)),a},url:function(b,c){var e;return b&&(e=b.match(g.regExpTemplate),c=c||g.urlData,e&&(f.debug("Looking for URL variables",e),a.each(e,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(f.verbose("Looking for variable",i,j,j.data(i),c[i]),k===!1)f.debug("Removing variable from URL",e),b=b.replace("/"+h,"");else{if(k===d||!k)return f.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(a,b){return b===d?g[a]:(g[a]=b,void 0)},verbose:function(){g.verbose&&g.debug&&(f.verbose=Function.prototype.bind.call(console.info,console,g.moduleName+":"))},debug:function(){g.debug&&(f.debug=Function.prototype.bind.call(console.info,console,g.moduleName+":"))},error:function(){console.log!==d&&(f.error=Function.prototype.bind.call(console.error,console,g.moduleName+":"))},invoke:function(b,c,e){var g,h;return c=c||o,e=l||e,"string"==typeof b&&k!==d&&(b=b.split("."),g=b.length-1,a.each(b,function(b,c){return a.isPlainObject(k[c])&&b!=g?(k=k[c],!0):k[c]!==d?(h=k[c],!0):(f.error(r.met