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

(function(e,t,n,a){e.fn.state=function(t){var n,o=e(this),i=o.selector||"",r=arguments[0],s=[].slice.call(arguments,1),l=(new Date).getTime(),c=[];return o.each(function(){var u,d=e(this),f=e.extend(!0,{},e.fn.state.settings,t),g=this,m=d.data("module-"+f.namespace),p="string"==typeof r,v=f.namespace,h=f.metadata,b=f.className,x=f.states,y=f.text;u={initialize:function(){u.verbose("Initializing module",g),f.automatic&&u.add.defaults(),f.context&&""!==i?(u.allows("hover")&&e(g,f.context).on(i,"mouseenter."+v,u.hover.enable).on(i,"mouseleave."+v,u.hover.disable),u.allows("pressed")&&e(g,f.context).on(i,"mousedown."+v,u.pressed.enable).on(i,"mouseup."+v,u.pressed.disable),u.allows("focus")&&e(g,f.context).on(i,"focus."+v,u.focus.enable).on(i,"blur."+v,u.focus.disable),e(f.context).on(i,"mouseenter."+v,u.text.change).on(i,"mouseleave."+v,u.text.reset).on(i,"click."+v,u.toggle)):(u.allows("hover")&&d.on("mouseenter."+v,u.hover.enable).on("mouseleave."+v,u.hover.disable),u.allows("pressed")&&d.on("mousedown."+v,u.pressed.enable).on("mouseup."+v,u.pressed.disable),u.allows("focus")&&d.on("focus."+v,u.focus.enable).on("blur."+v,u.focus.disable),d.on("mouseenter."+v,u.text.change).on("mouseleave."+v,u.text.reset).on("click."+v,u.toggle)),d.data("module-"+v,u)},destroy:function(){u.verbose("Destroying previous module",g),d.off("."+v)},refresh:function(){u.verbose("Refreshing selector cache",g),d=e(g)},add:{defaults:function(){var n=t&&e.isPlainObject(t.states)?t.states:{};e.each(f.defaults,function(t,o){u.is[t]!==a&&u.is[t]()&&(u.verbose("Adding default states",t,g),e.extend(f.states,o,n))})}},is:{active:function(){return d.hasClass(b.active)},loading:function(){return d.hasClass(b.loading)},inactive:function(){return!d.hasClass(b.active)},enabled:function(){return!d.is(f.filter.active)},disabled:function(){return d.is(f.filter.active)},textEnabled:function(){return!d.is(f.filter.text)},button:function(){return d.is(".button:not(a, .submit)")},input:function(){return d.is("input")}},allows:function(e){return x[e]||!1},enable:function(e){u.allows(e)&&d.addClass(b[e])},disable:function(e){u.allows(e)&&d.removeClass(b[e])},textFor:function(e){return y[e]||!1},focus:{enable:function(){d.addClass(b.focus)},disable:function(){d.removeClass(b.focus)}},hover:{enable:function(){d.addClass(b.hover)},disable:function(){d.removeClass(b.hover)}},pressed:{enable:function(){d.addClass(b.pressed).one("mouseleave",u.pressed.disable)},disable:function(){d.removeClass(b.pressed)}},toggle:function(){var e=d.data(h.promise);u.allows("active")&&u.is.enabled()&&(u.refresh(),e!==a?u.listenTo(e):u.change())},listenTo:function(t){u.debug("API request detected, waiting for state signal",t),t?(y.loading&&u.text.update(y.loading),e.when(t).then(function(){"resolved"==t.state()?(u.debug("API request succeeded"),f.activateTest=function(){return!0},f.deactivateTest=function(){return!0}):(u.debug("API request failed"),f.activateTest=function(){return!1},f.deactivateTest=function(){return!1}),u.change()})):(f.activateTest=function(){return!1},f.deactivateTest=function(){return!1})},change:function(){u.debug("Determining state change direction"),u.is.inactive()?u.activate():u.deactivate(),f.sync&&u.sync(),f.onChange()},activate:function(){e.proxy(f.activateTest,g)()&&(u.debug("Setting state to active"),d.addClass(b.active),u.text.update(y.active))},deactivate:function(){e.proxy(f.deactivateTest,g)()&&(u.debug("Setting state to inactive"),d.removeClass(b.active),u.text.update(y.inactive))},sync:function(){u.verbose("Syncing other buttons to current state"),u.is.active()?o.not(d).state("activate"):o.not(d).state("deactivate")},text:{get:function(){return f.selector.text?d.find(f.selector.text).text():d.html()},flash:function(e,t){var n=u.text.get();e=e||f.text.flash,t=t||f.flashDuration,u.text.update(e),setTimeout(function(){u.text.update(n)},t)},change:function(){u.verbose("Checking if text should be changed"),u.is.textEnabled()&&(u.is.active()?y.hover?(u.verbose("Changing text to hover text",y.hover),u.text.update(y.hover)):y.disable&&(u.verbose("Changing text to disable text",y.disable),u.text.update(y.disable)):y.hover?(u.verbose("Changing text to hover text",y.disable),u.text.update(y.hover)):y.enable&&(u.verbose("Changing text to enable text",y.disable),u.text.update(y.enable)))},reset:function(){var e=y.active||d.data(h.storedText),t=y.inactive||d.data(h.storedText);u.is.textEnabled()&&(u.is.active()&&e?(u.verbose("Resetting active text",e),u.text.update(e)):t&&(u.verbose("Resetting inactive text",e),u.text.update(t)))},update:function(e){var t=u.text.get();e&&e!==t&&(u.debug("Updating text",e),f.selector.text?d.data(h.storedText,e).find(f.selector.text).text(e):d.data(h.storedText,e).html(e))}},setting:function(e,t){return t===a?f[e]:(f[e]=t,a)},performance:{log:function(e){var t,n;f.performance&&(t=(new Date).getTime(),n=t-l,l=t,c.push({Name:e,"Execution Time":n}),clearTimeout(u.performance.timer),u.performance.timer=setTimeout(u.performance.display,100))},display:function(){var t=f.moduleName+" Performance ("+i+")";f.moduleName+": "+i+"("+o.size()+" elements)",console.group!==a&&c.length>0&&(console.groupCollapsed(t),console.table?console.table(c):e.each(c,function(e,t){console.log(t.Name+":"+t["Execution Time"])}),console.groupEnd(),c=[])}},verbose:function(){f.verbose&&f.debug&&(u.performance.log(arguments[0]),u.verbose=Function.prototype.bind.call(console.info,console,f.moduleName+":"))},debug:function(){f.debug&&(u.performance.log(arguments[0]),u.verbose=Function.prototype.bind.call(console.info,console,f.moduleName+":"))},error:function(){console.log!==a&&(u.error=Function.prototype.bind.call(console.log,console,f.moduleName+":"))},invoke:function(t,n,o){var i,r;return o=o||[].slice.call(arguments,2),"string"==typeof t&&m!==a&&(t=t.split("."),i=t.length-1,e.each(t,function(t,n){return e.isPlainObject(m[n])&&t!=i?(m=m[n],!0):m[n]!==a?(r=m[n],!0):(u.error(f.errors.method),!1)})),e.isFunction(r)?r.apply(n,o):r}},p?n=u.invoke(r,this,s):(m!==a&&u.destroy(),u.initialize())}),n!==a?n:this},e.fn.state.settings={moduleName:"State Module",debug:!0,verbose:!1,namespace:"state",performance:!0,onChange:function(){},activateTest:function(){return!0},deactivateTest:function(){return!0},automatic:!0,sync:!1,flashDuration:3e3,filter:{text:".loading, .disabled",active:".disabled"},context:!1,errors:{method:"The method you called is not defined."},metadata:{promise:"promise",storedText:"stored-text"},className:{focus:"focus",hover:"hover",pressed:"down",active:"active",loading:"loading"},selector:{text:!1},defaults:{input:{hover:!0,focus:!0,pressed:!0,loading:!1,active:!1},button:{hover:!0,focus:!1,pressed:!0,active:!1,loading:!0}},states:{hover:!0,focus:!0,pressed:!0,loading:!1,active:!1},text:{flash:!1,hover:!1,active:!1,inactive:!1,enable:!1,disable:!1}}})(jQuery,window,document);