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

!function(a,b,c,d){"use strict";a.fn.checkbox=function(b){var c,e=a(this),f=e.selector||"",g=(new Date).getTime(),h=[],i=arguments[0],j="string"==typeof i,k=[].slice.call(arguments,1);return e.each(function(){var e,l,m=a.extend(!0,{},a.fn.checkbox.settings,b),n=m.className,o=m.namespace,p=m.selector,q=m.error,r="."+o,s="module-"+o,t=a(this),u=a(this).next(p.label).first(),v=a(this).find(p.input),w=t.data(s),x=this;l={initialize:function(){l.verbose("Initializing checkbox",m),t.on("click"+r,l.toggle).on("keydown"+r,p.input,l.event.keydown),l.is.checked()?(l.set.checked(),m.fireOnInit&&a.proxy(m.onChecked,v.get())()):(l.remove.checked(),m.fireOnInit&&a.proxy(m.onUnchecked,v.get())()),l.observeChanges(),l.instantiate()},instantiate:function(){l.verbose("Storing instance of module",l),w=l,t.data(s,l)},destroy:function(){l.verbose("Destroying previous module"),t.off(r).removeData(s),v.off(r,l.event.keydown),u.off(r)},refresh:function(){t=a(this),u=a(this).next(p.label).first(),v=a(this).find(p.input)},observeChanges:function(){MutationObserver!==d&&(e=new MutationObserver(function(){l.debug("DOM tree modified, updating selector cache"),l.refresh()}),e.observe(x,{childList:!0,subtree:!0}),l.debug("Setting up mutation observer",e))},attachEvents:function(b,c){var d=a(b);c=a.isFunction(l[c])?l[c]:l.toggle,d.size()>0?(l.debug("Attaching checkbox events to element",b,c),d.on("click"+r,c)):l.error(q.notFound)},event:{keydown:function(b){var c=b.which,d={enter:13,escape:27};c==d.escape&&(l.verbose("Escape key pressed blurring field"),t.blur()),b.ctrlKey||c!=d.enter||(l.verbose("Enter key pressed, toggling checkbox"),a.proxy(l.toggle,this)(),b.preventDefault())}},is:{radio:function(){return t.hasClass(n.radio)},checked:function(){return v.prop("checked")!==d&&v.prop("checked")},unchecked:function(){return!l.is.checked()}},can:{change:function(){return!(t.hasClass(n.disabled)||t.hasClass(n.readOnly)||v.prop("disabled"))},uncheck:function(){return"boolean"==typeof m.uncheckable?m.uncheckable:!l.is.radio()}},set:{checked:function(){t.addClass(n.checked)}},remove:{checked:function(){t.removeClass(n.checked)}},disable:function(){l.debug("Enabling checkbox functionality"),t.addClass(n.disabled),v.removeProp("disabled"),a.proxy(m.onDisabled,v.get())()},enable:function(){l.debug("Disabling checkbox functionality"),t.removeClass(n.disabled),v.prop("disabled","disabled"),a.proxy(m.onEnabled,v.get())()},check:function(){l.debug("Enabling checkbox",v),v.prop("checked",!0).trigger("change"),l.set.checked(),a.proxy(m.onChange,v.get())(),a.proxy(m.onChecked,v.get())()},uncheck:function(){l.debug("Disabling checkbox"),v.prop("checked",!1).trigger("change"),l.remove.checked(),a.proxy(m.onChange,v.get())(),a.proxy(m.onUnchecked,v.get())()},toggle:function(){return l.can.change()?(l.verbose("Determining new checkbox state"),void(l.is.unchecked()?l.check():l.is.checked()&&l.can.uncheck()&&l.uncheck())):(console.log(l.can.change()),void l.debug("Checkbox is read-only or disabled, ignoring toggle"))},setting:function(b,c){if(l.debug("Changing setting",b,c),a.isPlainObject(b))a.extend(!0,m,b);else{if(c===d)return m[b];m[b]=c}},internal:function(b,c){if(a.isPlainObject(b))a.extend(!0,l,b);else{if(c===d)return l[b];l[b]=c}},debug:function(){m.debug&&(m.performance?l.performance.log(arguments):(l.debug=Function.prototype.bind.call(console.info,console,m.name+":"),l.debug.apply(console,arguments)))},verbose:function(){m.verbose&&m.debug&&(m.performance?l.performance.log(arguments):(l.verbose=Function.prototype.bind.call(console.info,console,m.name+":"),l.verbose.apply(console,arguments)))},error:function(){l.error=Function.prototype.bind.call(console.error,console,m.name+":"),l.error.apply(console,arguments)},performance:{log:function(a){var b,c,d;m.performance&&(b=(new Date).getTime(),d=g||b,c=b-d,g=b,h.push({Name:a[0],Arguments:[].slice.call(a,1)||"",Element:x,"Execution Time":c})),clearTimeout(l.performance.timer),l.performance.timer=setTimeout(l.performance.display,100)},display:function(){var b=m.name+":",c=0;g=!1,clearTimeout(l.performance.timer),a.each(h,function(a,b){c+=b["Execution Time"]}),b+=" "+c+"ms",f&&(b+=" '"+f+"'"),(console.group!==d||console.table!==d)&&h.length>0&&(console.groupCollapsed(b),console.table?console.table(h):a.each(h,function(a,b){console.log(b.Name+": "+b["Execution Time"]+"ms")}),console.groupEnd()),h=[]}},invoke:function(b,e,f){var g,h,i,j=w;return e=e||k,f=x||f,"string"==typeof b&&j!==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(j[f])&&c!=g)j=j[f];else{if(j[f]!==d)return h=j[f],!1;if(!a.isPlainObject(j[e])||c==g)return j[e]!==d?(h=j[e],!1):!1;j=j[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}},j?(w===d&&l.initialize(),l.invoke(i)):(w!==d&&l.destroy(),l.initialize())}),c!==d?c:this},a.fn.checkbox.settings={name:"Checkbox",namespace:"checkbox",debug:!1,verbose:!0,performance:!0,uncheckable:"auto",fireOnInit:!0,onChange:function(){},onChecked:function(){},onUnchecked:function(){},onEnabled:function(){},onDisabled:function(){},className:{checked:"checked",disabled:"disabled",radio:"radio",readOnly:"read-only"},error:{method:"The method you called is not defined."},selector:{input:"input[type=checkbox], input[type=radio]",label:"label"}}}(jQuery,window,document);