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.
 
 
 

1 lines
7.0 KiB

!function(a,b,c,d){a.fn.state=function(b){var c,e=a(this),f=a.extend(!0,{},a.fn.state.settings,b),g=e.selector||"",h=(new Date).getTime(),i=[],j=arguments[0],k="string"==typeof j,l=[].slice.call(arguments,1),m=(f.error,f.metadata),n=f.className,o=f.namespace,p=f.states,q=f.text,r="."+o,s=o+"-module";return e.each(function(){var o,t=a(this),u=this,v=t.data(s);o={initialize:function(){o.verbose("Initializing module"),f.automatic&&o.add.defaults(),f.context&&""!==g?a(f.context).on(g,"mouseenter"+r,o.change.text).on(g,"mouseleave"+r,o.reset.text).on(g,"click"+r,o.toggle.state):t.on("mouseenter"+r,o.change.text).on("mouseleave"+r,o.reset.text).on("click"+r,o.toggle.state),o.instantiate()},instantiate:function(){o.verbose("Storing instance of module",o),v=o,t.data(s,o)},destroy:function(){o.verbose("Destroying previous module",v),t.off(r).removeData(s)},refresh:function(){o.verbose("Refreshing selector cache"),t=a(u)},add:{defaults:function(){var c=b&&a.isPlainObject(b.states)?b.states:{};a.each(f.defaults,function(b,e){o.is[b]!==d&&o.is[b]()&&(o.verbose("Adding default states",b,u),a.extend(f.states,e,c))})}},is:{active:function(){return t.hasClass(n.active)},loading:function(){return t.hasClass(n.loading)},inactive:function(){return!t.hasClass(n.active)},enabled:function(){return!t.is(f.filter.active)},disabled:function(){return t.is(f.filter.active)},textEnabled:function(){return!t.is(f.filter.text)},button:function(){return t.is(".button:not(a, .submit)")},input:function(){return t.is("input")}},allow:function(a){o.debug("Now allowing state",a),p[a]=!0},disallow:function(a){o.debug("No longer allowing",a),p[a]=!1},allows:function(a){return p[a]||!1},enable:function(){t.removeClass(n.disabled)},disable:function(){t.addClass(n.disabled)},setState:function(a){o.allows(a)&&t.addClass(n[a])},removeState:function(a){o.allows(a)&&t.removeClass(n[a])},toggle:{state:function(){var b;if(o.allows("active")&&o.is.enabled()){if(o.refresh(),a.fn.api!==d&&(b=t.api("get request")))return void o.listenTo(b);o.change.state()}}},listenTo:function(b){o.debug("API request detected, waiting for state signal",b),b?(q.loading&&o.update.text(q.loading),a.when(b).then(function(){"resolved"==b.state()?(o.debug("API request succeeded"),f.activateTest=function(){return!0},f.deactivateTest=function(){return!0}):(o.debug("API request failed"),f.activateTest=function(){return!1},f.deactivateTest=function(){return!1}),o.change.state()})):(f.activateTest=function(){return!1},f.deactivateTest=function(){return!1})},change:{state:function(){o.debug("Determining state change direction"),o.is.inactive()?o.activate():o.deactivate(),f.sync&&o.sync(),a.proxy(f.onChange,u)()},text:function(){o.is.textEnabled()&&(o.is.disabled()?(o.verbose("Changing text to disabled text",q.hover),o.update.text(q.disabled)):o.is.active()?q.hover?(o.verbose("Changing text to hover text",q.hover),o.update.text(q.hover)):q.disable&&(o.verbose("Changing text to disable text",q.disable),o.update.text(q.disable)):q.hover?(o.verbose("Changing text to hover text",q.disable),o.update.text(q.hover)):q.enable&&(o.verbose("Changing text to enable text",q.enable),o.update.text(q.enable)))}},activate:function(){a.proxy(f.activateTest,u)()&&(o.debug("Setting state to active"),t.addClass(n.active),o.update.text(q.active)),a.proxy(f.onActivate,u)()},deactivate:function(){a.proxy(f.deactivateTest,u)()&&(o.debug("Setting state to inactive"),t.removeClass(n.active),o.update.text(q.inactive)),a.proxy(f.onDeactivate,u)()},sync:function(){o.verbose("Syncing other buttons to current state"),e.not(t).state(o.is.active()?"activate":"deactivate")},get:{text:function(){return f.selector.text?t.find(f.selector.text).text():t.html()},textFor:function(a){return q[a]||!1}},flash:{text:function(b,c,d){var e=o.get.text();o.debug("Flashing text message",b,c),b=b||f.text.flash,c=c||f.flashDuration,d=d||function(){},o.update.text(b),setTimeout(function(){o.update.text(e),a.proxy(d,u)()},c)}},reset:{text:function(){var a=q.active||t.data(m.storedText),b=q.inactive||t.data(m.storedText);o.is.textEnabled()&&(o.is.active()&&a?(o.verbose("Resetting active text",a),o.update.text(a)):b&&(o.verbose("Resetting inactive text",a),o.update.text(b)))}},update:{text:function(a){var b=o.get.text();a&&a!==b?(o.debug("Updating text",a),f.selector.text?t.data(m.storedText,a).find(f.selector.text).text(a):t.data(m.storedText,a).html(a)):o.debug("Text is already sane, ignoring update",a)}},setting:function(b,c){return o.debug("Changing setting",b,c),c===d?f[b]:void(a.isPlainObject(b)?a.extend(!0,f,b):f[b]=c)},internal:function(b,c){return o.debug("Changing internal",b,c),c===d?o[b]:void(a.isPlainObject(b)?a.extend(!0,o,b):o[b]=c)},debug:function(){f.debug&&(f.performance?o.performance.log(arguments):(o.debug=Function.prototype.bind.call(console.info,console,f.name+":"),o.debug.apply(console,arguments)))},verbose:function(){f.verbose&&f.debug&&(f.performance?o.performance.log(arguments):(o.verbose=Function.prototype.bind.call(console.info,console,f.name+":"),o.verbose.apply(console,arguments)))},error:function(){o.error=Function.prototype.bind.call(console.error,console,f.name+":"),o.error.apply(console,arguments)},performance:{log:function(a){var b,c,d;f.performance&&(b=(new Date).getTime(),d=h||b,c=b-d,h=b,i.push({Element:u,Name:a[0],Arguments:[].slice.call(a,1)||"","Execution Time":c})),clearTimeout(o.performance.timer),o.performance.timer=setTimeout(o.performance.display,100)},display:function(){var b=f.name+":",c=0;h=!1,clearTimeout(o.performance.timer),a.each(i,function(a,b){c+=b["Execution Time"]}),b+=" "+c+"ms",g&&(b+=" '"+g+"'"),e.size()>1&&(b+=" ("+e.size()+")"),(console.group!==d||console.table!==d)&&i.length>0&&(console.groupCollapsed(b),console.table?console.table(i):a.each(i,function(a,b){console.log(b.Name+": "+b["Execution Time"]+"ms")}),console.groupEnd()),i=[]}},invoke:function(b,e,f){var g,h,i,j=v;return e=e||l,f=u||f,"string"==typeof b&&j!==d&&(b=b.split(/[\. ]/),g=b.length-1,a.each(b,function(c,e){var f=c!=g?e+b[c+1].charAt(0).toUpperCase()+b[c+1].slice(1):b;if(a.isPlainObject(j[f])&&c!=g)j=j[f];else{if(j[f]!==d)return h=j[f],!1;if(!a.isPlainObject(j[e])||c==g)return j[e]!==d?(h=j[e],!1):!1;j=j[e]}})),a.isFunction(h)?i=h.apply(f,e):h!==d&&(i=h),a.isArray(c)?c.push(i):c!==d?c=[c,i]:i!==d&&(c=i),h}},k?(v===d&&o.initialize(),o.invoke(j)):(v!==d&&o.destroy(),o.initialize())}),c!==d?c:this},a.fn.state.settings={name:"State",debug:!1,verbose:!0,namespace:"state",performance:!0,onActivate:function(){},onDeactivate:function(){},onChange:function(){},activateTest:function(){return!0},deactivateTest:function(){return!0},automatic:!0,sync:!1,flashDuration:3e3,filter:{text:".loading, .disabled",active:".disabled"},context:!1,error:{method:"The method you called is not defined."},metadata:{promise:"promise",storedText:"stored-text"},className:{active:"active",disabled:"disabled",loading:"loading"},selector:{text:!1},defaults:{input:{disabled:!0,loading:!0,active:!0},button:{disabled:!0,loading:!0,active:!0}},states:{disabled:!0,loading:!0,active:!0},text:{disabled:!1,flash:!1,hover:!1,active:!1,inactive:!1,enable:!1,disable:!1}}}(jQuery,window,document);