|
|
!function(j,O,w){"use strict";O=void 0!==O&&O.Math==Math?O:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")();j.api=j.fn.api=function(q){var R,e=j.isFunction(this)?j(O):j(this),x=e.selector||"",S=(new Date).getTime(),A=[],k=q,T="string"==typeof k,P=[].slice.call(arguments,1);return e.each(function(){var s,i,r,e,a,u=j.isPlainObject(q)?j.extend(!0,{},j.fn.api.settings,q):j.extend({},j.fn.api.settings),t=u.namespace,n=u.metadata,o=u.selector,c=u.error,d=u.className,l="."+t,g="module-"+t,f=j(this),p=f.closest(o.form),m=u.stateContext?j(u.stateContext):f,b=this,v=m[0],h=f.data(g),y={initialize:function(){T||y.bind.events(),y.instantiate()},instantiate:function(){y.verbose("Storing instance of module",y),h=y,f.data(g,h)},destroy:function(){y.verbose("Destroying previous module for",b),f.removeData(g).off(l)},bind:{events:function(){var e=y.get.event();e?(y.verbose("Attaching API events to element",e),f.on(e+l,y.event.trigger)):"now"==u.on&&(y.debug("Querying API endpoint immediately"),y.query())}},decode:{json:function(e){if(e!==w&&"string"==typeof e)try{e=JSON.parse(e)}catch(e){}return e}},read:{cachedResponse:function(e){var t;if(O.Storage!==w)return t=sessionStorage.getItem(e),y.debug("Using cached response",e,t),t=y.decode.json(t);y.error(c.noStorage)}},write:{cachedResponse:function(e,t){t&&""===t?y.debug("Response empty, not caching",t):O.Storage!==w?(j.isPlainObject(t)&&(t=JSON.stringify(t)),sessionStorage.setItem(e,t),y.verbose("Storing cached response for url",e,t)):y.error(c.noStorage)}},query:function(){if(y.is.disabled())y.debug("Element is disabled API request aborted");else{if(y.is.loading()){if(!u.interruptRequests)return void y.debug("Cancelling request, previous request is still pending");y.debug("Interrupting previous request"),y.abort()}if(u.defaultData&&j.extend(!0,u.urlData,y.get.defaultData()),u.serializeForm&&(u.data=y.add.formData(u.data)),!1===(i=y.get.settings()))return y.cancelled=!0,void y.error(c.beforeSend);if(y.cancelled=!1,(r=y.get.templatedURL())||y.is.mocked()){if((r=y.add.urlData(r))||y.is.mocked()){if(i.url=u.base+r,s=j.extend(!0,{},u,{type:u.method||u.type,data:e,url:u.base+r,beforeSend:u.beforeXHR,success:function(){},failure:function(){},complete:function(){}}),y.debug("Querying URL",s.url),y.verbose("Using AJAX settings",s),"local"===u.cache&&y.read.cachedResponse(r))return y.debug("Response returned from local cache"),y.request=y.create.request(),void y.request.resolveWith(v,[y.read.cachedResponse(r)]);u.throttle?u.throttleFirstRequest||y.timer?(y.debug("Throttling request",u.throttle),clearTimeout(y.timer),y.timer=setTimeout(function(){y.timer&&delete y.timer,y.debug("Sending throttled request",e,s.method),y.send.request()},u.throttle)):(y.debug("Sending request",e,s.method),y.send.request(),y.timer=setTimeout(function(){},u.throttle)):(y.debug("Sending request",e,s.method),y.send.request())}}else y.error(c.missingURL)}},should:{removeError:function(){return!0===u.hideError||"auto"===u.hideError&&!y.is.form()}},is:{disabled:function(){return 0<f.filter(o.disabled).length},expectingJSON:function(){return"json"===u.dataType||"jsonp"===u.dataType},form:function(){return f.is("form")||m.is("form")},mocked:function(){return u.mockResponse||u.mockResponseAsync||u.response||u.responseAsync},input:function(){return f.is("input")},loading:function(){return!!y.request&&"pending"==y.request.state()},abortedRequest:function(e){return e&&e.readyState!==w&&0===e.readyState?(y.verbose("XHR request determined to be aborted"),!0):(y.verbose("XHR request was not aborted"),!1)},validResponse:function(e){return y.is.expectingJSON()&&j.isFunction(u.successTest)?(y.debug("Checking JSON returned success",u.successTest,e),u.successTest(e)?(y.debug("Response passed success test",e),!0):(y.debug("Response failed success test",e),!1)):(y.verbose("Response is not JSON, skipping validation",u.successTest,e),!0)}},was:{cancelled:function(){return y.cancelled||!1},succesful:function(){return y.request&&"resolved"==y.request.state()},failure:function(){return y.request&&"rejected"==y.request.st
|