|
|
/*! * # Semantic UI x.x - 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),i=a.selector||"",s=(new Date).getTime(),u=[],c=arguments[0],l="string"==typeof c,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(){var e=m.get.event();l||(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())),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)},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)}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),m.is.loading()?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 i=-1!==a.indexOf("$")?a.substr(2,a.length-3):a.substr(1,a.length-2),s=e.isPlainObject(t)&&t[i]!==n?t[i]:k.data(i)!==n?k.data(i):D.data(i)!==n?D.data(i):t[i];return s===n?(m.error(y.requiredParameter,i,r),r=!1,!1):(m.verbose("Found required variable",i,s),void(r=r.replace(a,s)))})),a&&(m.debug("Looking for optional URL variables",o),e.each(a,function(o,a){var i=-1!==a.indexOf("$")?a.substr(3,a.length-4):a.substr(2,a.length-3),s=e.isPlainObject(t)&&t[i]!==n?t[i]:k.data(i)!==n?k.data(i):D.data(i)!==n?D.data(i):t[i];s!==n?(m.verbose("Optional variable Found",i,s),r=r.replace(a,s)):(m.verbose("Optional variable not found",i),r=-1!==r.indexOf("/"+a)?r.replace("/"+a,""):r.replace(a,""))}))),r}},event:{trigger:function(e){m.query(),("submit"==e.type||"click"==e.type)&&e.preventDefault()},xhr:{always:function(){},done:function(e){var r=this,t=(new Date).getTime()-s,n=p.loadingDuration-t;n=n>0?n:0,setTimeout(function(){m.request.resolveWith(r,[e])},n)},fail:function(e,r,t){var n=this,o=(new Date).getTime()-s,a=p.loadingDuration-o;a=a>0?a:0,setTimeout(function(){"abort"!==r?m.request.rejectWith(n,[e,r,t]):m.reset()},a)}},request:{complete:function(e){m.r
|