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