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.

0 lines
7.0 KiB

10 years ago
  1. !function(a,b,c,d){a.fn.state=function(b){var e,f=a(this),g=a.extend(!0,{},a.fn.state.settings,b),h=f.selector||"",i=("ontouchstart"in c.documentElement,(new Date).getTime()),j=[],k=arguments[0],l="string"==typeof k,m=[].slice.call(arguments,1),n=(g.error,g.metadata),o=g.className,p=g.namespace,q=g.states,r=g.text,s="."+p,t=p+"-module";return f.each(function(){var c,p=a(this),u=this,v=p.data(t);c={initialize:function(){c.verbose("Initializing module"),g.automatic&&c.add.defaults(),g.context&&""!==h?a(g.context).on(h,"mouseenter"+s,c.change.text).on(h,"mouseleave"+s,c.reset.text).on(h,"click"+s,c.toggle.state):p.on("mouseenter"+s,c.change.text).on("mouseleave"+s,c.reset.text).on("click"+s,c.toggle.state),c.instantiate()},instantiate:function(){c.verbose("Storing instance of module",c),v=c,p.data(t,c)},destroy:function(){c.verbose("Destroying previous module",v),p.off(s).removeData(t)},refresh:function(){c.verbose("Refreshing selector cache"),p=a(u)},add:{defaults:function(){var e=b&&a.isPlainObject(b.states)?b.states:{};a.each(g.defaults,function(b,f){c.is[b]!==d&&c.is[b]()&&(c.verbose("Adding default states",b,u),a.extend(g.states,f,e))})}},is:{active:function(){return p.hasClass(o.active)},loading:function(){return p.hasClass(o.loading)},inactive:function(){return!p.hasClass(o.active)},enabled:function(){return!p.is(g.filter.active)},disabled:function(){return p.is(g.filter.active)},textEnabled:function(){return!p.is(g.filter.text)},button:function(){return p.is(".button:not(a, .submit)")},input:function(){return p.is("input")}},allow:function(a){c.debug("Now allowing state",a),q[a]=!0},disallow:function(a){c.debug("No longer allowing",a),q[a]=!1},allows:function(a){return q[a]||!1},enable:function(){p.removeClass(o.disabled)},disable:function(){p.addClass(o.disabled)},setState:function(a){c.allows(a)&&p.addClass(o[a])},removeState:function(a){c.allows(a)&&p.removeClass(o[a])},toggle:{state:function(){var b;if(c.allows("active")&&c.is.enabled()){if(c.refresh(),a.fn.api!==d&&(b=p.api("get request")))return void c.listenTo(b);c.change.state()}}},listenTo:function(b){c.debug("API request detected, waiting for state signal",b),b?(r.loading&&c.update.text(r.loading),a.when(b).then(function(){"resolved"==b.state()?(c.debug("API request succeeded"),g.activateTest=function(){return!0},g.deactivateTest=function(){return!0}):(c.debug("API request failed"),g.activateTest=function(){return!1},g.deactivateTest=function(){return!1}),c.change.state()})):(g.activateTest=function(){return!1},g.deactivateTest=function(){return!1})},change:{state:function(){c.debug("Determining state change direction"),c.is.inactive()?c.activate():c.deactivate(),g.sync&&c.sync(),a.proxy(g.onChange,u)()},text:function(){c.is.textEnabled()&&(c.is.disabled()?(c.verbose("Changing text to disabled text",r.hover),c.update.text(r.disabled)):c.is.active()?r.hover?(c.verbose("Changing text to hover text",r.hover),c.update.text(r.hover)):r.disable&&(c.verbose("Changing text to disable text",r.disable),c.update.text(r.disable)):r.hover?(c.verbose("Changing text to hover text",r.disable),c.update.text(r.hover)):r.enable&&(c.verbose("Changing text to enable text",r.enable),c.update.text(r.enable)))}},activate:function(){a.proxy(g.activateTest,u)()&&(c.debug("Setting state to active"),p.addClass(o.active),c.update.text(r.active)),a.proxy(g.onActivate,u)()},deactivate:function(){a.proxy(g.deactivateTest,u)()&&(c.debug("Setting state to inactive"),p.removeClass(o.active),c.update.text(r.inactive)),a.proxy(g.onDeactivate,u)()},sync:function(){c.verbose("Syncing other buttons to current state"),f.not(p).state(c.is.active()?"activate":"deactivate")},get:{text:function(){return g.selector.text?p.find(g.selector.text).text():p.html()},textFor:function(a){return r[a]||!1}},flash:{text:function(b,d,e){var f=c.get.text();c.debug("Flashing text message",b,d),b=b||g.text.flash,d=d||g.flashDuration,e=e||function(){},c.update.text(b),setTimeout(function(){c.update.text(f),a.proxy(e,u)()},d)}},reset:{text:function(){var a=r.active||p.data(n.storedText),b=r.inactive||p.data(n.storedT