wishthis/semantic/dist/components/checkbox.min.js
2022-02-23 22:46:34 +01:00

11 lines
No EOL
12 KiB
JavaScript

/*
* # Fomantic UI - 2.8.8
* https://github.com/fomantic/Fomantic-UI
* http://fomantic-ui.com/
*
* Copyright 2022 Contributors
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
*/
!function(I,E,S,O){"use strict";I.isFunction=I.isFunction||function(e){return"function"==typeof e&&"number"!=typeof e.nodeType},E=void 0!==E&&E.Math==Math?E:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")(),I.fn.checkbox=function(s){var u,e=I(this),b=e.selector||"",y=(new Date).getTime(),C=[],x=s,w="string"==typeof x,D=[].slice.call(arguments,1);return e.each(function(){var t,h=I.extend(!0,{},I.fn.checkbox.settings,s),n=h.className,o=h.namespace,g=h.selector,l=h.error,i="."+o,a="module-"+o,p=I(this),e=I(this).children(g.label),f=I(this).children(g.input),k=f[0],c=!1,v=!1,r=p.data(a),d=this,m={initialize:function(){m.verbose("Initializing checkbox",h),m.create.label(),m.bind.events(),m.set.tabbable(),m.hide.input(),m.observeChanges(),m.instantiate(),m.setup()},instantiate:function(){m.verbose("Storing instance of module",m),r=m,p.data(a,m)},destroy:function(){m.verbose("Destroying module"),m.unbind.events(),m.show.input(),p.removeData(a)},fix:{reference:function(){p.is(g.input)&&(m.debug("Behavior called on <input> adjusting invoked element"),p=p.closest(g.checkbox),m.refresh())}},setup:function(){m.set.initialLoad(),m.is.indeterminate()?(m.debug("Initial value is indeterminate"),m.indeterminate()):m.is.checked()?(m.debug("Initial value is checked"),m.check()):(m.debug("Initial value is unchecked"),m.uncheck()),m.remove.initialLoad()},refresh:function(){e=p.children(g.label),f=p.children(g.input),k=f[0]},hide:{input:function(){m.verbose("Modifying <input> z-index to be unselectable"),f.addClass(n.hidden)}},show:{input:function(){m.verbose("Modifying <input> z-index to be selectable"),f.removeClass(n.hidden)}},observeChanges:function(){"MutationObserver"in E&&((t=new MutationObserver(function(e){m.debug("DOM tree modified, updating selector cache"),m.refresh()})).observe(d,{childList:!0,subtree:!0}),m.debug("Setting up mutation observer",t))},attachEvents:function(e,n){var t=I(e);n=I.isFunction(m[n])?m[n]:m.toggle,0<t.length?(m.debug("Attaching checkbox events to element",e,n),t.on("click"+i,n)):m.error(l.notFound)},preventDefaultOnInputTarget:function(){"undefined"!=typeof event&&null!==event&&I(event.target).is(g.input)&&(m.verbose("Preventing default check action after manual check action"),event.preventDefault())},event:{change:function(e){m.should.ignoreCallbacks()||h.onChange.call(k)},click:function(e){var n=I(e.target);n.is(g.input)?m.verbose("Using default check action on initialized checkbox"):n.is(g.link)?m.debug("Clicking link inside checkbox, skipping toggle"):(m.toggle(),f.focus(),e.preventDefault())},keydown:function(e){var n=e.which,t=13,i=32,o=27,a=37,s=38,u=39,b=40,c=m.get.radios(),r=c.index(p),l=c.length,d=!1;if(n==a||n==s?d=(0===r?l:r)-1:n!=u&&n!=b||(d=r===l-1?0:r+1),!m.should.ignoreCallbacks()&&!1!==d){if(!1===h.beforeUnchecked.apply(k))return m.verbose("Option not allowed to be unchecked, cancelling key navigation"),!1;if(!1===h.beforeChecked.apply(I(c[d]).children(g.input)[0]))return m.verbose("Next option should not allow check, cancelling key navigation"),!1}v=n==o?(m.verbose("Escape key pressed blurring field"),f.blur(),!0):!(e.ctrlKey||!(n==i||n==t&&h.enableEnterKey))&&(m.verbose("Enter/space key pressed, toggling checkbox"),m.toggle(),!0)},keyup:function(e){v&&e.preventDefault()}},check:function(){m.should.allowCheck()&&(m.debug("Checking checkbox",f),m.set.checked(),m.should.ignoreCallbacks()||(h.onChecked.call(k),m.trigger.change()),m.preventDefaultOnInputTarget())},uncheck:function(){m.should.allowUncheck()&&(m.debug("Unchecking checkbox"),m.set.unchecked(),m.should.ignoreCallbacks()||(h.onUnchecked.call(k),m.trigger.change()),m.preventDefaultOnInputTarget())},indeterminate:function(){m.should.allowIndeterminate()?m.debug("Checkbox is already indeterminate"):(m.debug("Making checkbox indeterminate"),m.set.indeterminate(),m.should.ignoreCallbacks()||(h.onIndeterminate.call(k),m.trigger.change()))},determinate:function(){m.should.allowDeterminate()?m.debug("Checkbox is already determinate"):(m.debug("Making checkbox determinate"),m.set.determinate(),m.should.ignoreCallbacks()||(h.onDeterminate.call(k),m.trigger.change()))},enable:function(){m.is.enabled()?m.debug("Checkbox is already enabled"):(m.debug("Enabling checkbox"),m.set.enabled(),m.should.ignoreCallbacks()||(h.onEnable.call(k),h.onEnabled.call(k),m.trigger.change()))},disable:function(){m.is.disabled()?m.debug("Checkbox is already disabled"):(m.debug("Disabling checkbox"),m.set.disabled(),m.should.ignoreCallbacks()||(h.onDisable.call(k),h.onDisabled.call(k),m.trigger.change()))},get:{radios:function(){var e=m.get.name();return I('input[name="'+e+'"]').closest(g.checkbox)},otherRadios:function(){return m.get.radios().not(p)},name:function(){return f.attr("name")}},is:{initialLoad:function(){return c},radio:function(){return f.hasClass(n.radio)||"radio"==f.attr("type")},indeterminate:function(){return f.prop("indeterminate")!==O&&f.prop("indeterminate")},checked:function(){return f.prop("checked")!==O&&f.prop("checked")},disabled:function(){return f.prop("disabled")!==O&&f.prop("disabled")},enabled:function(){return!m.is.disabled()},determinate:function(){return!m.is.indeterminate()},unchecked:function(){return!m.is.checked()}},should:{allowCheck:function(){return m.is.determinate()&&m.is.checked()&&!m.is.initialLoad()?(m.debug("Should not allow check, checkbox is already checked"),!1):!(!m.should.ignoreCallbacks()&&!1===h.beforeChecked.apply(k))||(m.debug("Should not allow check, beforeChecked cancelled"),!1)},allowUncheck:function(){return m.is.determinate()&&m.is.unchecked()&&!m.is.initialLoad()?(m.debug("Should not allow uncheck, checkbox is already unchecked"),!1):!(!m.should.ignoreCallbacks()&&!1===h.beforeUnchecked.apply(k))||(m.debug("Should not allow uncheck, beforeUnchecked cancelled"),!1)},allowIndeterminate:function(){return m.is.indeterminate()&&!m.is.initialLoad()?(m.debug("Should not allow indeterminate, checkbox is already indeterminate"),!1):!(!m.should.ignoreCallbacks()&&!1===h.beforeIndeterminate.apply(k))||(m.debug("Should not allow indeterminate, beforeIndeterminate cancelled"),!1)},allowDeterminate:function(){return m.is.determinate()&&!m.is.initialLoad()?(m.debug("Should not allow determinate, checkbox is already determinate"),!1):!(!m.should.ignoreCallbacks()&&!1===h.beforeDeterminate.apply(k))||(m.debug("Should not allow determinate, beforeDeterminate cancelled"),!1)},ignoreCallbacks:function(){return c&&!h.fireOnInit}},can:{change:function(){return!(p.hasClass(n.disabled)||p.hasClass(n.readOnly)||f.prop("disabled")||f.prop("readonly"))},uncheck:function(){return"boolean"==typeof h.uncheckable?h.uncheckable:!m.is.radio()}},set:{initialLoad:function(){c=!0},checked:function(){m.verbose("Setting class to checked"),p.removeClass(n.indeterminate).addClass(n.checked),m.is.radio()&&m.uncheckOthers(),m.is.indeterminate()||!m.is.checked()?(m.verbose("Setting state to checked",k),f.prop("indeterminate",!1).prop("checked",!0)):m.debug("Input is already checked, skipping input property change")},unchecked:function(){m.verbose("Removing checked class"),p.removeClass(n.indeterminate).removeClass(n.checked),m.is.indeterminate()||!m.is.unchecked()?(m.debug("Setting state to unchecked"),f.prop("indeterminate",!1).prop("checked",!1)):m.debug("Input is already unchecked")},indeterminate:function(){m.verbose("Setting class to indeterminate"),p.addClass(n.indeterminate),m.is.indeterminate()?m.debug("Input is already indeterminate, skipping input property change"):(m.debug("Setting state to indeterminate"),f.prop("indeterminate",!0))},determinate:function(){m.verbose("Removing indeterminate class"),p.removeClass(n.indeterminate),m.is.determinate()?m.debug("Input is already determinate, skipping input property change"):(m.debug("Setting state to determinate"),f.prop("indeterminate",!1))},disabled:function(){m.verbose("Setting class to disabled"),p.addClass(n.disabled),m.is.disabled()?m.debug("Input is already disabled, skipping input property change"):(m.debug("Setting state to disabled"),f.prop("disabled","disabled"))},enabled:function(){m.verbose("Removing disabled class"),p.removeClass(n.disabled),m.is.enabled()?m.debug("Input is already enabled, skipping input property change"):(m.debug("Setting state to enabled"),f.prop("disabled",!1))},tabbable:function(){m.verbose("Adding tabindex to checkbox"),f.attr("tabindex")===O&&f.attr("tabindex",0)}},remove:{initialLoad:function(){c=!1}},trigger:{change:function(){var e,n=f[0];n&&(e=S.createEvent("HTMLEvents"),m.verbose("Triggering native change event"),e.initEvent("change",!0,!1),n.dispatchEvent(e))}},create:{label:function(){0<f.prevAll(g.label).length?(f.prev(g.label).detach().insertAfter(f),m.debug("Moving existing label",e)):m.has.label()||(e=I("<label>").insertAfter(f),m.debug("Creating label",e))}},has:{label:function(){return 0<e.length}},bind:{events:function(){m.verbose("Attaching checkbox events"),p.on("click"+i,m.event.click).on("change"+i,m.event.change).on("keydown"+i,g.input,m.event.keydown).on("keyup"+i,g.input,m.event.keyup)}},unbind:{events:function(){m.debug("Removing events"),p.off(i)}},uncheckOthers:function(){var e=m.get.otherRadios();m.debug("Unchecking other radios",e),e.removeClass(n.checked)},toggle:function(){m.can.change()?m.is.indeterminate()||m.is.unchecked()?(m.debug("Currently unchecked"),m.check()):m.is.checked()&&m.can.uncheck()&&(m.debug("Currently checked"),m.uncheck()):m.is.radio()||m.debug("Checkbox is read-only or disabled, ignoring toggle")},setting:function(e,n){if(m.debug("Changing setting",e,n),I.isPlainObject(e))I.extend(!0,h,e);else{if(n===O)return h[e];I.isPlainObject(h[e])?I.extend(!0,h[e],n):h[e]=n}},internal:function(e,n){if(I.isPlainObject(e))I.extend(!0,m,e);else{if(n===O)return m[e];m[e]=n}},debug:function(){!h.silent&&h.debug&&(h.performance?m.performance.log(arguments):(m.debug=Function.prototype.bind.call(console.info,console,h.name+":"),m.debug.apply(console,arguments)))},verbose:function(){!h.silent&&h.verbose&&h.debug&&(h.performance?m.performance.log(arguments):(m.verbose=Function.prototype.bind.call(console.info,console,h.name+":"),m.verbose.apply(console,arguments)))},error:function(){h.silent||(m.error=Function.prototype.bind.call(console.error,console,h.name+":"),m.error.apply(console,arguments))},performance:{log:function(e){var n,t;h.performance&&(t=(n=(new Date).getTime())-(y||n),y=n,C.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:d,"Execution Time":t})),clearTimeout(m.performance.timer),m.performance.timer=setTimeout(m.performance.display,500)},display:function(){var e=h.name+":",t=0;y=!1,clearTimeout(m.performance.timer),I.each(C,function(e,n){t+=n["Execution Time"]}),e+=" "+t+"ms",b&&(e+=" '"+b+"'"),(console.group!==O||console.table!==O)&&0<C.length&&(console.groupCollapsed(e),console.table?console.table(C):I.each(C,function(e,n){console.log(n.Name+": "+n["Execution Time"]+"ms")}),console.groupEnd()),C=[]}},invoke:function(i,e,n){var o,a,t,c=r;return e=e||D,n=d||n,"string"==typeof i&&c!==O&&(i=i.split(/[\. ]/),o=i.length-1,I.each(i,function(e,n){var t=e!=o?n+i[e+1].charAt(0).toUpperCase()+i[e+1].slice(1):i;if(I.isPlainObject(c[t])&&e!=o)c=c[t];else{if(c[t]!==O)return a=c[t],!1;{if(!I.isPlainObject(c[n])||e==o)return c[n]!==O?a=c[n]:m.error(l.method,i),!1;c=c[n]}}})),I.isFunction(a)?t=a.apply(n,e):a!==O&&(t=a),Array.isArray(u)?u.push(t):u!==O?u=[u,t]:t!==O&&(u=t),a}};w?(r===O&&m.initialize(),m.invoke(x)):(r!==O&&r.invoke("destroy"),m.initialize())}),u!==O?u:this},I.fn.checkbox.settings={name:"Checkbox",namespace:"checkbox",silent:!1,debug:!1,verbose:!0,performance:!0,uncheckable:"auto",fireOnInit:!1,enableEnterKey:!0,onChange:function(){},beforeChecked:function(){},beforeUnchecked:function(){},beforeDeterminate:function(){},beforeIndeterminate:function(){},onChecked:function(){},onUnchecked:function(){},onDeterminate:function(){},onIndeterminate:function(){},onEnable:function(){},onDisable:function(){},onEnabled:function(){},onDisabled:function(){},className:{checked:"checked",indeterminate:"indeterminate",disabled:"disabled",hidden:"hidden",radio:"radio",readOnly:"read-only"},error:{method:"The method you called is not defined"},selector:{checkbox:".ui.checkbox",label:"label, .box",input:'input[type="checkbox"], input[type="radio"]',link:"a[href]"}}}(jQuery,window,document);