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
12 KiB

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