|
|
!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?(o.allows("hover")&&a(u,f.context).on(g,"mouseenter"+r,o.enable.hover).on(g,"mouseleave"+r,o.disable.hover),o.allows("down")&&a(u,f.context).on(g,"mousedown"+r,o.enable.down).on(g,"mouseup"+r,o.disable.down),o.allows("focus")&&a(u,f.context).on(g,"focus"+r,o.enable.focus).on(g,"blur"+r,o.disable.focus),a(f.context).on(g,"mouseenter"+r,o.change.text).on(g,"mouseleave"+r,o.reset.text).on(g,"click"+r,o.toggle.state)):(o.allows("hover")&&t.on("mouseenter"+r,o.enable.hover).on("mouseleave"+r,o.disable.hover),o.allows("down")&&t.on("mousedown"+r,o.enable.down).on("mouseup"+r,o.disable.down),o.allows("focus")&&t.on("focus"+r,o.enable.focus).on("blur"+r,o.disable.focus),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:{state:function(a){o.allows(a)&&t.addClass(n[a])},focus:function(){t.addClass(n.focus)},hover:function(){t.addClass(n.hover)},down:function(){t.addClass(n.down)}},disable:{state:function(a){o.allows(a)&&t.removeClass(n[a])},focus:function(){t.removeClass(n.focus)},hover:function(){t.removeClass(n.hover)},down:function(){t.removeClass(n.down)}},toggle:{state:function(){var a=t.data(m.promise);o.allows("active")&&o.is.enabled()&&(o.refresh(),a!==d?o.listenTo(a):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.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:
|