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.
11 lines
10 KiB
11 lines
10 KiB
/*
|
|
* # Semantic UI - 1.8.0
|
|
* 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),T=p.stateContext?e(p.stateContext):D,S=this,R=T.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",S),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||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(y.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(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.verbose("Creating AJAX request with settings",t),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).length>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(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):T.data(i)!==n?T.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):T.data(i)!==n?T.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(R,e,D)},done:function(t){m.debug("API Response Received",t),"json"==p.dataType&&e.isFunction(p.successTest)?(m.debug("Checking JSON returned success",p.successTest,t),p.successTest(t)?p.onSuccess.call(R,t,D):(m.debug("JSON test specified by user and response failed",t),p.onFailure.call(R,t,D))):p.onSuccess.call(R,t,D)},error:function(t,r,o){var a,i=p.error[r]!==n?p.error[r]:o;if(t!==n)if(t.readyState!==n&&4==t.readyState){if(200!=t.status&&o!==n&&""!==o)m.error(y.statusMessage+o);else if("error"==r&&"json"==p.dataType)try{a=e.parseJSON(t.responseText),a&&a.error!==n&&(i=a.error)}catch(s){m.error(y.JSONParse)}m.remove.loading(),m.set.error(),p.errorDuration&&setTimeout(m.remove.error,p.errorDuration),m.debug("API Request error:",i),p.onError.call(R,i,D)}else p.onAbort.call(R,i,D),m.debug("Request Aborted (Most likely caused by page change or CORS Policy)",r,o)}}},create:{request:function(){return e.Deferred().always(m.event.request.complete).done(m.event.request.done).fail(m.event.request.error)},xhr:function(){return e.ajax(t).always(m.event.xhr.always).done(m.event.xhr.done).fail(m.event.xhr.fail)}},set:{error:function(){m.verbose("Adding error state to element",T),T.addClass(q.error)},loading:function(){m.verbose("Adding loading state to element",T),T.addClass(q.loading)}},remove:{error:function(){m.verbose("Removing error state from element",T),T.removeClass(q.error)},loading:function(){m.verbose("Removing loading state from element",T),T.removeClass(q.loading)}},get:{request:function(){return m.request||!1},xhr:function(){return m.xhr||!1},settings:function(){var e;return e=p.beforeSend.call(D,p),e&&(e.success!==n&&(m.debug("Legacy success callback detected",e),m.error(y.legacyParameters,e.success),e.onSuccess=e.success),e.failure!==n&&(m.debug("Legacy failure callback detected",e),m.error(y.legacyParameters,e.failure),e.onFailure=e.failure),e.complete!==n&&(m.debug("Legacy complete callback detected",e),m.error(y.legacyParameters,e.complete),e.onComplete=e.complete)),e===n&&m.error(y.noReturnedValue),e!==n?e:p},defaultData:function(){var t={};return e.isWindow(S)||(D.is("input")?t.value=D.val():D.is("form")||(t.text=D.text())),t},event:function(){return e.isWindow(S)||"now"==p.on?(m.debug("API called without element, no events attached"),!1):"auto"==p.on?D.is("input")?S.oninput!==n?"input":S.onpropertychange!==n?"propertychange":"keyup":D.is("form")?"submit":"click":p.on},formData:function(){var t;return e(this).serializeObject()!==n?t=P.serializeObject():(m.error(y.missingSerialize),t=P.serialize()),m.debug("Retrieved form data",t),t},templateURL:function(e){var t;return e=e||D.data(v.action)||p.action||!1,e&&(m.debug("Looking up url for action",e,p.api),p.api[e]!==n?(t=p.api[e],m.debug("Found template url",t)):m.error(y.missingAction,p.action,p.api)),t}},abort:function(){var e=m.get.xhr();e&&"resolved"!==e.state()&&(m.debug("Cancelling API request"),e.abort(),m.request.rejectWith(p.apiSettings))},reset:function(){m.remove.error(),m.remove.loading()},setting:function(t,r){if(m.debug("Changing setting",t,r),e.isPlainObject(t))e.extend(!0,p,t);else{if(r===n)return p[t];p[t]=r}},internal:function(t,r){if(e.isPlainObject(t))e.extend(!0,m,t);else{if(r===n)return m[t];m[t]=r}},debug:function(){p.debug&&(p.performance?m.performance.log(arguments):(m.debug=Function.prototype.bind.call(console.info,console,p.name+":"),m.debug.apply(console,arguments)))},verbose:function(){p.verbose&&p.debug&&(p.performance?m.performance.log(arguments):(m.verbose=Function.prototype.bind.call(console.info,console,p.name+":"),m.verbose.apply(console,arguments)))},error:function(){m.error=Function.prototype.bind.call(console.error,console,p.name+":"),m.error.apply(console,arguments)},performance:{log:function(e){var t,r,n;p.performance&&(t=(new Date).getTime(),n=s||t,r=t-n,s=t,u.push({Name:e[0],Arguments:[].slice.call(e,1)||"","Execution Time":r})),clearTimeout(m.performance.timer),m.performance.timer=setTimeout(m.performance.display,100)},display:function(){var t=p.name+":",r=0;s=!1,clearTimeout(m.performance.timer),e.each(u,function(e,t){r+=t["Execution Time"]}),t+=" "+r+"ms",i&&(t+=" '"+i+"'"),(console.group!==n||console.table!==n)&&u.length>0&&(console.groupCollapsed(t),console.table?console.table(u):e.each(u,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),u=[]}},invoke:function(t,r,a){var i,s,u,c=j;return r=r||d,a=S||a,"string"==typeof t&&c!==n&&(t=t.split(/[\. ]/),i=t.length-1,e.each(t,function(r,o){var a=r!=i?o+t[r+1].charAt(0).toUpperCase()+t[r+1].slice(1):t;if(e.isPlainObject(c[a])&&r!=i)c=c[a];else{if(c[a]!==n)return s=c[a],!1;if(!e.isPlainObject(c[o])||r==i)return c[o]!==n?(s=c[o],!1):(m.error(y.method,t),!1);c=c[o]}})),e.isFunction(s)?u=s.apply(a,r):s!==n&&(u=s),e.isArray(o)?o.push(u):o!==n?o=[o,u]:u!==n&&(o=u),s}},l?(j===n&&m.initialize(),m.invoke(c)):(j!==n&&m.destroy(),m.initialize())}),o!==n?o:this},e.api.settings={name:"API",namespace:"api",debug:!0,verbose:!0,performance:!0,on:"auto",filter:".disabled",stateContext:!1,loadingDuration:0,errorDuration:2e3,action:!1,url:!1,base:"",urlData:{},defaultData:!0,serializeForm:!1,throttle:0,method:"get",data:{},dataType:"json",beforeSend:function(e){return e},beforeXHR:function(){},onSuccess:function(){},onComplete:function(){},onFailure:function(){},onError:function(){},onAbort:function(){},successTest:!1,error:{beforeSend:"The before send function has aborted the request",error:"There was an error with your request",exitConditions:"API Request Aborted. Exit conditions met",JSONParse:"JSON could not be parsed during error handling",legacyParameters:"You are using legacy API success callback names",method:"The method you called is not defined",missingAction:"API action used but no url was defined",missingSerialize:"Required dependency jquery-serialize-object missing, using basic serialize",missingURL:"No URL specified for api event",noReturnedValue:"The beforeSend callback must return a settings object, beforeSend ignored.",parseError:"There was an error parsing your request",requiredParameter:"Missing a required URL parameter: ",statusMessage:"Server gave an error: ",timeout:"Your request timed out"},regExp:{required:/\{\$*[A-z0-9]+\}/g,optional:/\{\/\$*[A-z0-9]+\}/g},className:{loading:"loading",error:"error"},selector:{form:"form"},metadata:{action:"action"}},e.api.settings.api={}}(jQuery,window,document);
|