blob: effcb6cb90108a6cd1047967cdf88cdb79cb0d38 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001/**
2 * bootstrap-switch - Turn checkboxes and radio buttons into toggle switches.
3 *
4 * @version v3.3.3
5 * @homepage http://www.bootstrap-switch.org
6 * @author Mattia Larentis <mattia@larentis.eu> (http://larentis.eu)
7 * @license Apache-2.0
8 */
9
10'use strict';var _createClass=function(){function a(b,c){for(var e,d=0;d<c.length;d++)e=c[d],e.enumerable=e.enumerable||!1,e.configurable=!0,'value'in e&&(e.writable=!0),Object.defineProperty(b,e.key,e)}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError('Cannot call a class as a function')}(function(a,b){var c=function(){function d(e){var g=this,f=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};_classCallCheck(this,d),this.$element=a(e),this.options=a.extend({},a.fn.bootstrapSwitch.defaults,this._getElementOptions(),f),this.prevOptions={},this.$wrapper=a('<div>',{class:function _class(){var h=[];return h.push(g.options.state?'on':'off'),g.options.size&&h.push(g.options.size),g.options.disabled&&h.push('disabled'),g.options.readonly&&h.push('readonly'),g.options.indeterminate&&h.push('indeterminate'),g.options.inverse&&h.push('inverse'),g.$element.attr('id')&&h.push('id-'+g.$element.attr('id')),h.map(g._getClass.bind(g)).concat([g.options.baseClass],g._getClasses(g.options.wrapperClass)).join(' ')}}),this.$container=a('<div>',{class:this._getClass('container')}),this.$on=a('<span>',{html:this.options.onText,class:this._getClass('handle-on')+' '+this._getClass(this.options.onColor)}),this.$off=a('<span>',{html:this.options.offText,class:this._getClass('handle-off')+' '+this._getClass(this.options.offColor)}),this.$label=a('<span>',{html:this.options.labelText,class:this._getClass('label')}),this.$element.on('init.bootstrapSwitch',this.options.onInit.bind(this,e)),this.$element.on('switchChange.bootstrapSwitch',function(){for(var j=arguments.length,h=Array(j),k=0;k<j;k++)h[k]=arguments[k];!1===g.options.onSwitchChange.apply(e,h)&&(g.$element.is(':radio')?a('[name="'+g.$element.attr('name')+'"]').trigger('previousState.bootstrapSwitch',!0):g.$element.trigger('previousState.bootstrapSwitch',!0))}),this.$container=this.$element.wrap(this.$container).parent(),this.$wrapper=this.$container.wrap(this.$wrapper).parent(),this.$element.before(this.options.inverse?this.$off:this.$on).before(this.$label).before(this.options.inverse?this.$on:this.$off),this.options.indeterminate&&this.$element.prop('indeterminate',!0),this._init(),this._elementHandlers(),this._handleHandlers(),this._labelHandlers(),this._formHandler(),this._externalLabelHandler(),this.$element.trigger('init.bootstrapSwitch',this.options.state)}return _createClass(d,[{key:'setPrevOptions',value:function setPrevOptions(){this.prevOptions=Object.assign({},this.options)}},{key:'state',value:function state(e,f){return'undefined'==typeof e?this.options.state:this.options.disabled||this.options.readonly||this.options.state&&!this.options.radioAllOff&&this.$element.is(':radio')?this.$element:(this.$element.is(':radio')?a('[name="'+this.$element.attr('name')+'"]').trigger('setPreviousOptions.bootstrapSwitch'):this.$element.trigger('setPreviousOptions.bootstrapSwitch'),this.options.indeterminate&&this.indeterminate(!1),this.$element.prop('checked',!!e).trigger('change.bootstrapSwitch',f),this.$element)}},{key:'toggleState',value:function toggleState(e){return this.options.disabled||this.options.readonly?this.$element:this.options.indeterminate?(this.indeterminate(!1),this.state(!0)):this.$element.prop('checked',!this.options.state).trigger('change.bootstrapSwitch',e)}},{key:'size',value:function size(e){return'undefined'==typeof e?this.options.size:(null!=this.options.size&&this.$wrapper.removeClass(this._getClass(this.options.size)),e&&this.$wrapper.addClass(this._getClass(e)),this._width(),this._containerPosition(),this.options.size=e,this.$element)}},{key:'animate',value:function animate(e){return'undefined'==typeof e?this.options.animate:this.options.animate===!!e?this.$element:this.toggleAnimate()}},{key:'toggleAnimate',value:function toggleAnimate(){return this.options.animate=!this.options.animate,this.$wrapper.toggleClass(this._getClass('animate')),this.$element}},{key:'disabled',value:function disabled(e){return'undefined'==typeof e?this.options.disabled:this.options.disabled===!!e?this.$element:this.toggleDisabled()}},{key:'toggleDisabled',value:function toggleDisabled(){return this.options.disabled=!this.options.disabled,this.$element.prop('disabled',this.options.disabled),this.$wrapper.toggleClass(this._getClass('disabled')),this.$element}},{key:'readonly',value:function readonly(e){return'undefined'==typeof e?this.options.readonly:this.options.readonly===!!e?this.$element:this.toggleReadonly()}},{key:'toggleReadonly',value:function toggleReadonly(){return this.options.readonly=!this.options.readonly,this.$element.prop('readonly',this.options.readonly),this.$wrapper.toggleClass(this._getClass('readonly')),this.$element}},{key:'indeterminate',value:function indeterminate(e){return'undefined'==typeof e?this.options.indeterminate:this.options.indeterminate===!!e?this.$element:this.toggleIndeterminate()}},{key:'toggleIndeterminate',value:function toggleIndeterminate(){return this.options.indeterminate=!this.options.indeterminate,this.$element.prop('indeterminate',this.options.indeterminate),this.$wrapper.toggleClass(this._getClass('indeterminate')),this._containerPosition(),this.$element}},{key:'inverse',value:function inverse(e){return'undefined'==typeof e?this.options.inverse:this.options.inverse===!!e?this.$element:this.toggleInverse()}},{key:'toggleInverse',value:function toggleInverse(){this.$wrapper.toggleClass(this._getClass('inverse'));var e=this.$on.clone(!0),f=this.$off.clone(!0);return this.$on.replaceWith(f),this.$off.replaceWith(e),this.$on=f,this.$off=e,this.options.inverse=!this.options.inverse,this.$element}},{key:'onColor',value:function onColor(e){return'undefined'==typeof e?this.options.onColor:(this.options.onColor&&this.$on.removeClass(this._getClass(this.options.onColor)),this.$on.addClass(this._getClass(e)),this.options.onColor=e,this.$element)}},{key:'offColor',value:function offColor(e){return'undefined'==typeof e?this.options.offColor:(this.options.offColor&&this.$off.removeClass(this._getClass(this.options.offColor)),this.$off.addClass(this._getClass(e)),this.options.offColor=e,this.$element)}},{key:'onText',value:function onText(e){return'undefined'==typeof e?this.options.onText:(this.$on.html(e),this._width(),this._containerPosition(),this.options.onText=e,this.$element)}},{key:'offText',value:function offText(e){return'undefined'==typeof e?this.options.offText:(this.$off.html(e),this._width(),this._containerPosition(),this.options.offText=e,this.$element)}},{key:'labelText',value:function labelText(e){return'undefined'==typeof e?this.options.labelText:(this.$label.html(e),this._width(),this.options.labelText=e,this.$element)}},{key:'handleWidth',value:function handleWidth(e){return'undefined'==typeof e?this.options.handleWidth:(this.options.handleWidth=e,this._width(),this._containerPosition(),this.$element)}},{key:'labelWidth',value:function labelWidth(e){return'undefined'==typeof e?this.options.labelWidth:(this.options.labelWidth=e,this._width(),this._containerPosition(),this.$element)}},{key:'baseClass',value:function baseClass(){return this.options.baseClass}},{key:'wrapperClass',value:function wrapperClass(e){return'undefined'==typeof e?this.options.wrapperClass:(e||(e=a.fn.bootstrapSwitch.defaults.wrapperClass),this.$wrapper.removeClass(this._getClasses(this.options.wrapperClass).join(' ')),this.$wrapper.addClass(this._getClasses(e).join(' ')),this.options.wrapperClass=e,this.$element)}},{key:'radioAllOff',value:function radioAllOff(e){if('undefined'==typeof e)return this.options.radioAllOff;var f=!!e;return this.options.radioAllOff===f?this.$element:(this.options.radioAllOff=f,this.$element)}},{key:'onInit',value:function onInit(e){return'undefined'==typeof e?this.options.onInit:(e||(e=a.fn.bootstrapSwitch.defaults.onInit),this.options.onInit=e,this.$element)}},{key:'onSwitchChange',value:function onSwitchChange(e){return'undefined'==typeof e?this.options.onSwitchChange:(e||(e=a.fn.bootstrapSwitch.defaults.onSwitchChange),this.options.onSwitchChange=e,this.$element)}},{key:'destroy',value:function destroy(){var e=this.$element.closest('form');return e.length&&e.off('reset.bootstrapSwitch').removeData('bootstrap-switch'),this.$container.children().not(this.$element).remove(),this.$element.unwrap().unwrap().off('.bootstrapSwitch').removeData('bootstrap-switch'),this.$element}},{key:'_getElementOptions',value:function _getElementOptions(){return{state:this.$element.is(':checked'),size:this.$element.data('size'),animate:this.$element.data('animate'),disabled:this.$element.is(':disabled'),readonly:this.$element.is('[readonly]'),indeterminate:this.$element.data('indeterminate'),inverse:this.$element.data('inverse'),radioAllOff:this.$element.data('radio-all-off'),onColor:this.$element.data('on-color'),offColor:this.$element.data('off-color'),onText:this.$element.data('on-text'),offText:this.$element.data('off-text'),labelText:this.$element.data('label-text'),handleWidth:this.$element.data('handle-width'),labelWidth:this.$element.data('label-width'),baseClass:this.$element.data('base-class'),wrapperClass:this.$element.data('wrapper-class')}}},{key:'_width',value:function _width(){var g=this,e=this.$on.add(this.$off).add(this.$label).css('width',''),f=void 0;return f='auto'===this.options.handleWidth?Math.round(Math.max(this.$on.width(),this.$off.width())):this.options.handleWidth,e.width(f),this.$label.width(function(h,j){return'auto'===g.options.labelWidth?j<f?f:j:g.options.labelWidth}),this._handleWidth=this.$on.outerWidth(),this._labelWidth=this.$label.outerWidth(),this.$container.width(2*this._handleWidth+this._labelWidth),this.$wrapper.width(this._handleWidth+this._labelWidth)}},{key:'_containerPosition',value:function _containerPosition(){var g=this,e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:this.options.state,f=arguments[1];this.$container.css('margin-left',function(){var h=[0,'-'+g._handleWidth+'px'];return g.options.indeterminate?'-'+g._handleWidth/2+'px':e?g.options.inverse?h[1]:h[0]:g.options.inverse?h[0]:h[1]})}},{key:'_init',value:function _init(){var f=this,e=function(){f.setPrevOptions(),f._width(),f._containerPosition(),setTimeout(function(){if(f.options.animate)return f.$wrapper.addClass(f._getClass('animate'))},50)};if(this.$wrapper.is(':visible'))return void e();var g=b.setInterval(function(){if(f.$wrapper.is(':visible'))return e(),b.clearInterval(g)},50)}},{key:'_elementHandlers',value:function _elementHandlers(){var e=this;return this.$element.on({'setPreviousOptions.bootstrapSwitch':this.setPrevOptions.bind(this),'previousState.bootstrapSwitch':function previousStateBootstrapSwitch(){e.options=e.prevOptions,e.options.indeterminate&&e.$wrapper.addClass(e._getClass('indeterminate')),e.$element.prop('checked',e.options.state).trigger('change.bootstrapSwitch',!0)},'change.bootstrapSwitch':function changeBootstrapSwitch(f,g){f.preventDefault(),f.stopImmediatePropagation();var h=e.$element.is(':checked');e._containerPosition(h),h===e.options.state||(e.options.state=h,e.$wrapper.toggleClass(e._getClass('off')).toggleClass(e._getClass('on')),!g&&(e.$element.is(':radio')&&a('[name="'+e.$element.attr('name')+'"]').not(e.$element).prop('checked',!1).trigger('change.bootstrapSwitch',!0),e.$element.trigger('switchChange.bootstrapSwitch',[h])))},'focus.bootstrapSwitch':function focusBootstrapSwitch(f){f.preventDefault(),e.$wrapper.addClass(e._getClass('focused'))},'blur.bootstrapSwitch':function blurBootstrapSwitch(f){f.preventDefault(),e.$wrapper.removeClass(e._getClass('focused'))},'keydown.bootstrapSwitch':function keydownBootstrapSwitch(f){!f.which||e.options.disabled||e.options.readonly||(37===f.which||39===f.which)&&(f.preventDefault(),f.stopImmediatePropagation(),e.state(39===f.which))}})}},{key:'_handleHandlers',value:function _handleHandlers(){var e=this;return this.$on.on('click.bootstrapSwitch',function(f){return f.preventDefault(),f.stopPropagation(),e.state(!1),e.$element.trigger('focus.bootstrapSwitch')}),this.$off.on('click.bootstrapSwitch',function(f){return f.preventDefault(),f.stopPropagation(),e.state(!0),e.$element.trigger('focus.bootstrapSwitch')})}},{key:'_labelHandlers',value:function _labelHandlers(){var f=this;this.$label.on({click:function click(g){g.stopPropagation()},'mousedown.bootstrapSwitch touchstart.bootstrapSwitch':function mousedownBootstrapSwitchTouchstartBootstrapSwitch(g){f._dragStart||f.options.disabled||f.options.readonly||(g.preventDefault(),g.stopPropagation(),f._dragStart=(g.pageX||g.originalEvent.touches[0].pageX)-parseInt(f.$container.css('margin-left'),10),f.options.animate&&f.$wrapper.removeClass(f._getClass('animate')),f.$element.trigger('focus.bootstrapSwitch'))},'mousemove.bootstrapSwitch touchmove.bootstrapSwitch':function mousemoveBootstrapSwitchTouchmoveBootstrapSwitch(g){if(null!=f._dragStart){var h=(g.pageX||g.originalEvent.touches[0].pageX)-f._dragStart;g.preventDefault(),h<-f._handleWidth||0<h||(f._dragEnd=h,f.$container.css('margin-left',f._dragEnd+'px'))}},'mouseup.bootstrapSwitch touchend.bootstrapSwitch':function mouseupBootstrapSwitchTouchendBootstrapSwitch(g){if(f._dragStart){if(g.preventDefault(),f.options.animate&&f.$wrapper.addClass(f._getClass('animate')),f._dragEnd){var h=f._dragEnd>-(f._handleWidth/2);f._dragEnd=!1,f.state(f.options.inverse?!h:h)}else f.state(!f.options.state);f._dragStart=!1}},'mouseleave.bootstrapSwitch':function mouseleaveBootstrapSwitch(){f.$label.trigger('mouseup.bootstrapSwitch')}})}},{key:'_externalLabelHandler',value:function _externalLabelHandler(){var f=this,e=this.$element.closest('label');e.on('click',function(g){g.preventDefault(),g.stopImmediatePropagation(),g.target===e[0]&&f.toggleState()})}},{key:'_formHandler',value:function _formHandler(){var e=this.$element.closest('form');e.data('bootstrap-switch')||e.on('reset.bootstrapSwitch',function(){b.setTimeout(function(){e.find('input').filter(function(){return a(this).data('bootstrap-switch')}).each(function(){return a(this).bootstrapSwitch('state',this.checked)})},1)}).data('bootstrap-switch',!0)}},{key:'_getClass',value:function _getClass(e){return this.options.baseClass+'-'+e}},{key:'_getClasses',value:function _getClasses(e){return a.isArray(e)?e.map(this._getClass.bind(this)):[this._getClass(e)]}}]),d}();a.fn.bootstrapSwitch=function(d){for(var f=arguments.length,e=Array(1<f?f-1:0),g=1;g<f;g++)e[g-1]=arguments[g];var h=this;return this.each(function(){var j=a(this),k=j.data('bootstrap-switch');k||(k=new c(this,d),j.data('bootstrap-switch',k)),'string'==typeof d&&(h=k[d].apply(k,e))}),h},a.fn.bootstrapSwitch.Constructor=c,a.fn.bootstrapSwitch.defaults={state:!0,size:null,animate:!0,disabled:!1,readonly:!1,indeterminate:!1,inverse:!1,radioAllOff:!1,onColor:'primary',offColor:'default',onText:'ON',offText:'OFF',labelText:'&nbsp',handleWidth:'auto',labelWidth:'auto',baseClass:'bootstrap-switch',wrapperClass:'wrapper',onInit:function onInit(){},onSwitchChange:function onSwitchChange(){}}})(window.jQuery,window);