|
|
/* * # Semantic UI - 1.10.1 * https://github.com/Semantic-Org/Semantic-UI
* http://www.semantic-ui.com/
* * Copyright 2014 Contributors * Released under the MIT license * http://opensource.org/licenses/MIT
* */ !function(e,t,r,n){e.api=e.fn.api=function(r){var o,a=e(e.isFunction(this)?t: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 t,a,f,g,m,p=e.isPlainObject(r)?e.extend(!0,{},e.fn.api.settings,r):e.extend({},e.fn.api.settings),b=p.namespace,v=p.metadata,h=p.selector,y=p.error,q=p.className,x="."+b,A="module-"+b,D=e(this),P=D.closest(h.form),R=p.stateContext?e(p.stateContext):D,T=this,S=R.get(),j=D.data(A);m={initialize:function(){var e=m.get.event();l||(e?(m.debug("Attaching API events to element",e),D.on(e+x,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),j=m,D.data(A,j)},destroy:function(){m.verbose("Destroying previous module for",T),D.removeData(A).off(x)},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||R.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){if(!D.is("form"))return void m.error(y.missingURL,p.action);m.debug("No url or action specified, defaulting to form action"),f=D.attr("action")}m.set.loading(),t=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",t.url),m.debug("Sending data",g,t.method),m.verbose("Using AJAX settings",t),m.is.loading()?m.timer=setTimeout(function(){m.request=m.create.request(),m.xhr=m.create.xhr(),p.onRequest.call(S,m.request,m.xhr)},p.throttle):(m.request=m.create.request(),m.xhr=m.create.xhr(),p.onRequest.call(S,m.request,m.xhr))},is:{disabled:function(){return D.filter(p.filter).length>0},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(t,r){var o,a;return t&&(o=t.match(p.regExp.required),a=t.match(p.regExp.optional),r=r||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(r)&&r[i]!==n?r[i]:D.data(i)!==n?D.data(i):R.data(i)!==n?R.data(i):r[i];return s===n?(m.error(y.requiredParameter,i,t),t=!1,!1):(m.verbose("Found required variable",i,s),void(t=t.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(r)&&r[i]!==n?r[i]:D.data(i)!==n?D.data(i):R.data(i)!==n?R.data(i):r[i];s!==n?(m.verbose("Optional variable Found",i,s),t=t.replace(a,s)):(m.verbose("Optional variable not found",i),t=-1!==t.indexOf("/"+a)?t.replace("/"+a,""):t.replace(a,""))}))),t}},event:{trigger:function(e){m.query(),("submit"==e.type||"click"==e.type)&&e.preventDefault()},xhr:{always:function(){},done:function(e){var t=this,r=(new Date).getTime()-s,n=p.loadingDuration-r;n=n>0?n:0,setTimeout(function(){m.request.resolveWith(t,[e])},n)},fail:function(e,t,r){var n=this,o=(new Date).getTime()-s,a=p.loadingDuration-o;a=a>0?a:0,setTimeout(function(){"abort"!==t?m.request.rejectWith(n,[e,t,r]):m.reset()},a)}},request:{complete:function(e){m.remove.loading(),p.onComplete.call(S,e,D)},done:function(t){m.debug("API Response Received",t),"json"==p.dataType&&e.isFunction(p.successTest)?(m.debug("Checking JSON return
|