You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

10 lines
9.9 KiB

10 years ago
  1. /*
  2. * # Semantic UI
  3. * git://github.com/Semantic-Org/Semantic-UI.git
  4. *
  5. *
  6. * Copyright 2014 Contributors
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. !function(e,r,t,n){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,p.selector),h=p.error,y=p.className,q="."+b,x="module-"+b,D=e(this),A=D.closest(v.form),T=p.stateContext?e(p.stateContext):D,P=this,S=T.get(),R=D.data(x);m={initialize:function(){var e=m.get.event();l||(e?(m.debug("Attaching API events to element",e),D.on(e+q,m.event.trigger)):m.query()),m.instantiate()},instantiate:function(){m.verbose("Storing instance of module",m),R=m,D.data(x,R)},destroy:function(){m.verbose("Destroying previous module for",P),D.removeData(x).off(q)},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||T.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 void m.error(h.beforeSend);if(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(h.missingURL,p.action);m.debug("No url or action specified, defaulting to form action"),f=D.attr("action")}m.set.loading(),r=e.extend(!0,{},p,{type:p.method||p.type,data:g,url:f,beforeSend:p.beforeXHR,success:function(){},failure:function(){},complete:function(){}}),m.verbose("Creating AJAX request with settings",r),m.is.loading()?m.timer=setTimeout(function(){m.request=m.create.request(),m.xhr=m.create.xhr()},p.throttle):(m.request=m.create.request(),m.xhr=m.create.xhr())},is:{disabled:function(){return D.filter(p.filter).size()>0},loading:function(){return m.request&&"pending"==m.request.state()}},was:{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]:D.data(i)!==n?D.data(i):T.data(i)!==n?T.data(i):t[i];return s===n?(m.error(h.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]:D.data(i)!==n?D.data(i):T.data(i)!==n?T.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(r){m.remove.loading(),e.proxy(p.onComplete,S)(r,D)},done:function(r){m.debug("API Response Received",r),"json"==p.dataType&&e.isFunction(p.successTest)?(m.debug("Checking JSON returned success",p.successTest,r),p.successTest(r)?e.proxy(p.onSuccess,S)(r,D):(m.debug("JSON test specified by user and response failed",r),e.proxy(p.onFailure,S)(r,D))):e.proxy(p.onSuccess,S)(r,D)},error:function(r,t,o){var a,i=p.error[t]!==n?p.er