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.7 KiB

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