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