|
|
/*! * # Semantic UI 2.0.0 - API * http://github.com/semantic-org/semantic-ui/
* * * Copyright 2015 Contributors * Released under the MIT license * http://opensource.org/licenses/MIT
* */ !function(e,r,t,n){"use strict";e.api=e.fn.api=function(t){var o,a=e(e.isFunction(this)?r:this),s=a.selector||"",i=(new Date).getTime(),c=[],u=arguments[0],l="string"==typeof u,d=[].slice.call(arguments,1);return a.each(function(){var r,a,f,g,m,p=e.isPlainObject(t)?e.extend(!0,{},e.fn.api.settings,t):e.extend({},e.fn.api.settings),b=p.namespace,v=p.metadata,h=p.selector,y=p.error,q=p.className,R="."+b,x="module-"+b,k=e(this),A=k.closest(h.form),D=p.stateContext?e(p.stateContext):k,P=this,T=D.get(),S=k.data(x);m={initialize:function(){l||(m.create.cache(),m.bind.events()),m.instantiate()},instantiate:function(){m.verbose("Storing instance of module",m),S=m,k.data(x,S)},destroy:function(){m.verbose("Destroying previous module for",P),k.removeData(x).off(R)},bind:{events:function(){var e=m.get.event();e?(m.debug("Attaching API events to element",e),k.on(e+R,m.event.trigger)):"now"==p.on&&(m.debug("Querying API now",e),m.query())}},read:{cachedResponse:function(e){var r;return m.cache||m.create.cache(),r=m.cache.response[e]!==n?m.cache.response[e]:!1,m.debug("Using cached response",e,r),r}},write:{cachedResponse:function(e,r){return m.cache||m.create.cache(),r&&""===r?void m.debug("Response empty, not caching",r):(m.verbose("Storing cached response for url",e,r),void(m.cache.response[e]=r))}},query:function(){if(m.is.disabled())return void m.debug("Element is disabled API request aborted");if(m.is.loading()&&0===p.throttle)return void m.debug("Cancelling request, previous request is still pending");if(p.defaultData&&e.extend(!0,p.urlData,m.get.defaultData()),(p.serializeForm!==!1||D.is("form"))&&("json"==p.serializeForm?e.extend(!0,p.data,m.get.formData()):p.data=m.get.formData()),a=m.get.settings(),a===!1)return m.cancelled=!0,void m.error(y.beforeSend);if(m.cancelled=!1,p.url?(m.debug("Using specified url",f),f=m.add.urlData(p.url)):(f=m.add.urlData(m.get.templateURL()),m.debug("Added URL Data to url",f)),!f&&!m.is.mocked()){if(!m.is.form())return void m.error(y.missingURL,p.action);f=k.attr("action")||"",m.debug("No url or action specified, defaulting to form action",f)}return m.set.loading(),r=e.extend(!0,{},p,{type:p.method||p.type,data:g,url:p.base+f,beforeSend:p.beforeXHR,success:function(){},failure:function(){},complete:function(){}}),m.debug("Querying URL",r.url),m.debug("Sending data",g,r.method),m.verbose("Using AJAX settings",r),"local"===p.cache&&m.read.cachedResponse(f)?(m.request=m.create.request(),void m.request.resolveWith(T,[m.read.cachedResponse(f)])):void(m.is.loading()?(m.debug("Repeated request throttled",p.throttle),m.timer=setTimeout(function(){m.request=m.create.request(),m.xhr=m.create.xhr(),p.onRequest.call(T,m.request,m.xhr)},p.throttle)):(m.request=m.create.request(),m.xhr=m.create.xhr(),p.onRequest.call(T,m.request,m.xhr)))},is:{disabled:function(){return k.filter(p.filter).length>0},form:function(){return k.is("form")},mocked:function(){return p.mockResponse||p.mockResponseAsync},input:function(){return k.is("input")},loading:function(){return m.request&&"pending"==m.request.state()}},was:{cancelled:function(){return m.cancelled||!1},succesful:function(){return m.request&&"resolved"==m.request.state()},failure:function(){return m.request&&"rejected"==m.request.state()},complete:function(){return m.request&&("resolved"==m.request.state()||"rejected"==m.request.state())}},add:{urlData:function(r,t){var o,a;return r&&(o=r.match(p.regExp.required),a=r.match(p.regExp.optional),t=t||p.urlData,o&&(m.debug("Looking for required URL variables",o),e.each(o,function(o,a){var s=-1!==a.indexOf("$")?a.substr(2,a.length-3):a.substr(1,a.length-2),i=e.isPlainObject(t)&&t[s]!==n?t[s]:k.data(s)!==n?k.data(s):D.data(s)!==n?D.data(s):t[s];return i===n?(m.error(y.requiredParameter,s,r),r=!1,!1):(m.verbose("Found required variable",s,i),void(r=r.replace(a,i)))})),a&&(m.debug("Looking for optional URL variables",o),e.each(a,function(o,a){var s=-1!==a.indexOf("$")?a.substr(3,a.length-4):a.substr(2,a.length-3),i=e.isPlainObject(t)&&t[s]!==n?t[s]:k.data(s)!==n?k.data(s):D.data(s)!==n?D.data(s):t[s];i!==n?(m.verbose("Op
|