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.
 
 
 

1 lines
5.5 KiB

(function(e,t,n,o){e.api=e.fn.api=function(n){var a,i=e.extend(!0,{},e.api.settings,n),r="function"!=typeof this?this:e("<div/>"),s=i.stateContext?e(i.stateContext):e(r),c="object"==typeof this?e(r):s,l=c.data(i.metadata.action)||i.action||!1,u=i.className,d=i.metadata,p=i.errors;return a={initialize:function(){var r,p,f,g,m,h,y,v=!1,x=(new Date).getTime(),b={},C={},S=i.errors;return i.serializeForm&&e(this).toJSON()!==o&&(b=c.closest("form").toJSON(),e.extend(!0,i.data,b),a.debug("Adding form data to API Request",b)),r=e.proxy(i.beforeSend,c)(i),r===o||r?(l&&(a.debug("Initializing API Request for: ",l),i.api[l]!==o?g=i.api[l]:a.error(S.missingAction)),i.url&&(g=i.url,a.debug("Using specified url: ",g)),g||(a.error(S.missingURL),a.reset()),m=g.match(i.regExpTemplate),m&&(a.debug("Looking for URL variables",m),e.each(m,function(t,r){var s=r.substr(2,r.length-3),l=e.isPlainObject(n.urlData)&&n.urlData[s]!==o?n.urlData[s]:c.data(s)!==o?c.data(s):i.urlData[s];a.verbose("Looking for variable",s,c,c.data(s),i.urlData[s]),l===!1?(a.debug("Removing variable from URL",m),g=g.replace("/"+r,"")):l!==o&&l?g=g.replace(r,l):(a.error(S.missingParameter+s),v=!0)})),v?(a.reset(),o):(f=e.Deferred().always(function(){i.stateContext&&s.removeClass(u.loading),e.proxy(i.complete,c)()}).done(function(t){a.debug("API request successful"),"json"==i.dataType?t.success===!0?e.proxy(i.success,s)(t,i,c):(a.debug("JSON success flag is not set."),t.error!==o?e.proxy(i.failure,s)(t.error,i,c):e.isArray(t.errors)?e.proxy(i.failure,s)(t.errors[0],i,c):t.message!==o?e.proxy(i.failure,s)(t.message,i,c):e.proxy(i.failure,s)(S.error,i,c)):e.proxy(i.success,s)(t,i,c)}).fail(function(t,n,r){var c,l=i.errors[n]!==o?i.errors[n]:r;if(t!==o)if(t.readyState!==o&&4==t.readyState){if(200!=t.status&&r!==o&&""!==r)a.error(S.statusMessage+r);else if("error"==n&&"json"==i.dataType)try{c=e.parseJSON(t.responseText),c&&c.error!==o&&(l=c.error)}catch(d){a.error(S.JSONParse)}s.removeClass(u.loading).addClass(u.error),i.errorLength>0&&setTimeout(function(){s.removeClass(u.error)},i.errorLength),a.debug("API Request error:",l),e.proxy(i.failure,s)(l,i,this)}else a.debug("Request Aborted (Most likely caused by page change)")}),e.extend(!0,C,i,{type:i.method||i.type,data:h,url:g,beforeSend:i.beforeXHR}),i.stateContext&&s.addClass(u.loading),i.progress&&(a.verbose("Adding progress events"),e.extend(!0,C,{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(i.progress,s)(n,t))},!1),n.addEventListener("progress",function(t){var n;t.lengthComputable&&(n=Math.round(1e4*(t.loaded/t.total))/100+"%",e.proxy(i.progress,s)(n,t))},!1),n}})),a.verbose("Creating AJAX request with settings: ",C),y=e.ajax(C).always(function(){p=i.loadingLength-((new Date).getTime()-x),i.loadingDelay=0>p?0:p}).done(function(e){var t=this;setTimeout(function(){f.resolveWith(t,[e])},i.loadingDelay)}).fail(function(e,t,n){var o=this;"abort"!=t?setTimeout(function(){f.rejectWith(o,[e,t,n])},i.loadingDelay):s.removeClass(u.error).removeClass(u.loading)}),i.stateContext&&c.data(d.promise,f).data(d.xhr,y),o)):(a.error(S.beforeSend),a.reset(),o)},reset:function(){c.data(d.promise,!1).data(d.xhr,!1),s.removeClass(u.error).removeClass(u.loading),a.error(p.exitConditions)},setting:function(e,t){return t===o?i[e]:(i[e]=t,o)},verbose:function(){i.verbose&&a.debug.apply(this,arguments)},debug:function(){var e=[],t=i.moduleName+": "+arguments[0],n=[].slice.call(arguments,1),o=console.info||console.log||function(){};o=Function.prototype.bind.call(o,console),i.debug&&(e.push(t),o.apply(console,e.concat(n)))},error:function(){var e=[],t=i.moduleName+": "+arguments[0],n=[].slice.call(arguments,1),o=console.warn||console.log||function(){};o=Function.prototype.bind.call(o,console),i.debug&&(e.push(t),e.concat(n),o.apply(console,e.concat(n)))}},a.initialize(),this},e.fn.apiButton=function(t){return e(this).each(function(){var n,o=e(this),a=e(this).selector||"",i=e.isFunction(t)?e.extend(!0,{},e.api.settings,e.fn.apiButton.settings,{stateContext:this,success:t}):e.extend(!0,{},e.api.settings,e.fn.apiButton.settings,{stateContext:this},t);n={initialize:function(){i.context&&""!==a?e(i.context).on(a,"click."+i.namespace,n.click):o.on("click."+i.namespace,n.click)},click:function(){i.filter&&0!==e(this).filter(i.filter).size()||e.proxy(e.api,this)(i)}},n.initialize()}),this},e.api.settings={moduleName:"API Module",namespace:"api",verbose:!0,debug:!0,api:{},beforeSend:function(e){return e},beforeXHR:function(){},success:function(){},complete:function(){},failure:function(){},progress:!1,errors:{missingAction:"API action used but no url was defined",missingURL:"URL not specified for the API action",missingParameter:"Missing an essential URL parameter: ",timeout:"Your request timed out",error:"There was an error with your request",parseError:"There was an error parsing your request",JSONParse:"JSON could not be parsed during error handling",statusMessage:"Server gave an error: ",beforeSend:"The before send function has aborted the request",exitConditions:"API Request Aborted. Exit conditions met"},className:{loading:"loading",error:"error"},metadata:{action:"action",promise:"promise",xhr:"xhr"},regExpTemplate:/\{\$([A-z]+)\}/g,action:!1,url:!1,urlData:!1,serializeForm:!1,stateContext:!1,method:"get",data:{},dataType:"json",cache:!0,loadingLength:200,errorLength:2e3},e.fn.apiButton.settings={filter:".disabled, .loading",context:!1,stateContext:!1}})(jQuery,window,document);