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