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