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