12 lines
7.1 KiB
JavaScript
12 lines
7.1 KiB
JavaScript
/* Copyright Notice
|
|
* bootstrap5-toggle v3.7.4
|
|
* https://palcarazm.github.io/bootstrap5-toggle/
|
|
* @author 2011-2014 Min Hur (https://github.com/minhur)
|
|
* @author 2018-2019 Brent Ely (https://github.com/gitbrent)
|
|
* @author 2022 Pablo Alcaraz Martínez (https://github.com/palcarazm)
|
|
* @funding GitHub Sponsors
|
|
* @see https://github.com/sponsors/palcarazm
|
|
* @license MIT
|
|
* @see https://github.com/palcarazm/bootstrap5-toggle/blob/master/LICENSE
|
|
*/
|
|
!function(a){"use strict";function o(t,e){this.$element=a(t),this.options=a.extend({},this.defaults(),e),this.render()}function h(t,e){e.options.tristate?e.$toggle.hasClass("indeterminate")?(e.determinate(!0),e.toggle()):e.indeterminate():e.toggle(),t.preventDefault()}function r(t){var e,i;return 0!==a(t).length&&((t=t.clone()).css({visibility:"hidden",width:"",height:"",maxWidth:"",maxHeight:""}),a("body").append(t),e=t.outerWidth(),i=t.outerHeight(),t.remove(),{width:e,height:i})}o.DEFAULTS={on:"On",off:"Off",onstyle:"primary",offstyle:"default",onvalue:null,offvalue:null,size:"normal",style:"",width:null,height:null,tabindex:0,tristate:!1,name:null},o.prototype.defaults=function(){return{on:this.$element.attr("data-on")||o.DEFAULTS.on,off:this.$element.attr("data-off")||o.DEFAULTS.off,onstyle:this.$element.attr("data-onstyle")||o.DEFAULTS.onstyle,offstyle:this.$element.attr("data-offstyle")||o.DEFAULTS.offstyle,onvalue:this.$element.attr("value")||this.$element.attr("data-onvalue")||o.DEFAULTS.onvalue,offvalue:this.$element.attr("data-offvalue")||o.DEFAULTS.offvalue,size:this.$element.attr("data-size")||o.DEFAULTS.size,style:this.$element.attr("data-style")||o.DEFAULTS.style,width:this.$element.attr("data-width")||o.DEFAULTS.width,height:this.$element.attr("data-height")||o.DEFAULTS.height,tabindex:this.$element.attr("tabindex")||o.DEFAULTS.tabindex,tristate:this.$element.is("[tristate]")||o.DEFAULTS.tristate,name:this.$element.attr("name")||o.DEFAULTS.name}},o.prototype.render=function(){let t;switch(this.options.size){case"large":case"lg":t="btn-lg";break;case"small":case"sm":t="btn-sm";break;case"mini":case"xs":t="btn-xs";break;default:t=""}var e=a('<label class="btn">').html(this.options.on).addClass("btn-"+this.options.onstyle+" "+t),i=(this.$element.prop("id")&&e.prop("for",this.$element.prop("id")),a('<label class="btn">').html(this.options.off).addClass("btn-"+this.options.offstyle+" "+t)),n=(this.$element.prop("id")&&i.prop("for",this.$element.prop("id")),a('<span class="toggle-handle btn">').addClass(t)),s=a('<div class="toggle-group">').append(e,i,n),o=a('<div class="toggle btn" data-toggle="toggle" role="button">').addClass(this.$element.prop("checked")?"btn-"+this.options.onstyle:"btn-"+this.options.offstyle+" off").addClass(t).addClass(this.options.style).attr("title",this.$element.prop("title")).attr("tabindex",this.options.tabindex);(this.$element.prop("disabled")||this.$element.prop("readonly"))&&(o.addClass("disabled"),o.attr("disabled","disabled")),this.options.onvalue&&this.$element.val(this.options.onvalue);let l=null;this.options.offvalue&&((l=this.$element.clone()).val(this.options.offvalue),l.attr("data-toggle","invert-toggle"),l.removeAttr("id"),l.prop("checked",!this.$element.prop("checked"))),this.$element.wrap(o),a.extend(this,{$toggle:this.$element.parent(),$toggleOn:e,$toggleOff:i,$toggleGroup:s,$invElement:l}),this.$toggle.append(l,s);o=r(e),s=r(i),n=r(n),n=this.options.width||Math.max(o.width,s.width)+n.width/2,o=this.options.height||Math.max(o.height,s.height);this.$toggle.css({width:n,height:o}),e.addClass("toggle-on"),i.addClass("toggle-off"),this.options.height&&(e.css("line-height",e.height()+"px"),i.css("line-height",i.height()+"px")),this.$toggle.on("touchstart",t=>{h(t,this)}),this.$toggle.on("click",t=>{h(t,this)}),this.$toggle.on("keypress",t=>{" "==t.key&&h(t,this)})},o.prototype.toggle=function(t=!1){this.$element.prop("checked")?this.off(t):this.on(t)},o.prototype.on=function(t=!1){if(this.$element.prop("disabled")||this.$element.prop("readonly"))return!1;this.$toggle.removeClass("btn-"+this.options.offstyle+" off").addClass("btn-"+this.options.onstyle),this.$element.prop("checked",!0),this.$invElement&&this.$invElement.prop("checked",!1),t||this.trigger()},o.prototype.off=function(t=!1){if(this.$element.prop("disabled")||this.$element.prop("readonly"))return!1;this.$toggle.removeClass("btn-"+this.options.onstyle).addClass("btn-"+this.options.offstyle+" off"),this.$element.prop("checked",!1),this.$invElement&&this.$invElement.prop("checked",!0),t||this.trigger()},o.prototype.indeterminate=function(t=!1){if(!this.options.tristate||this.$element.prop("disabled")||this.$element.prop("readonly"))return!1;this.$toggle.addClass("indeterminate"),this.$element.prop("indeterminate",!0),this.$element.removeAttr("name"),this.$invElement&&this.$invElement.prop("indeterminate",!0),this.$invElement&&this.$invElement.removeAttr("name"),t||this.trigger()},o.prototype.determinate=function(t=!1){if(!this.options.tristate||this.$element.prop("disabled")||this.$element.prop("readonly"))return!1;this.$toggle.removeClass("indeterminate"),this.$element.prop("indeterminate",!1),this.options.name&&this.$element.attr("name",this.options.name),this.$invElement&&this.$invElement.prop("indeterminate",!1),this.$invElement&&this.options.name&&this.$invElement.attr("name",this.options.name),t||this.trigger()},o.prototype.enable=function(){this.$toggle.removeClass("disabled"),this.$toggle.removeAttr("disabled"),this.$element.prop("disabled",!1),this.$element.prop("readonly",!1),this.$invElement&&(this.$invElement.prop("disabled",!1),this.$invElement.prop("readonly",!1))},o.prototype.disable=function(){this.$toggle.addClass("disabled"),this.$toggle.attr("disabled","disabled"),this.$element.prop("disabled",!0),this.$element.prop("readonly",!1),this.$invElement&&(this.$invElement.prop("disabled",!0),this.$invElement.prop("readonly",!1))},o.prototype.readonly=function(){this.$toggle.addClass("disabled"),this.$toggle.attr("disabled","disabled"),this.$element.prop("disabled",!1),this.$element.prop("readonly",!0),this.$invElement&&(this.$invElement.prop("disabled",!1),this.$invElement.prop("readonly",!0))},o.prototype.update=function(t){this.$element.prop("disabled")?this.disable():this.$element.prop("readonly")?this.readonly():this.enable(),this.$element.prop("checked")?this.on(t):this.off(t)},o.prototype.trigger=function(t){this.$element.off("change.bs.toggle"),t||this.$element.change(),this.$element.on("change.bs.toggle",a.proxy(function(){this.update()},this))},o.prototype.destroy=function(){this.$element.off("change.bs.toggle"),this.$toggleGroup.remove(),this.$invElement&&this.$invElement.remove(),this.$element.removeData("bs.toggle"),this.$element.unwrap()};let t=a.fn.bootstrapToggle;a.fn.bootstrapToggle=function(n){let s=Array.prototype.slice.call(arguments,1)[0];return this.each(function(){var t=a(this);let e=t.data("bs.toggle");var i="object"==typeof n&&n;e||(e=new o(this,i),t.data("bs.toggle",e)),"string"==typeof n&&e[n]&&"boolean"==typeof s?e[n](s):"string"==typeof n&&e[n]&&e[n]()})},a.fn.bootstrapToggle.Constructor=o,a.fn.toggle.noConflict=function(){return a.fn.bootstrapToggle=t,this},a(function(){a("input[type=checkbox][data-toggle^=toggle]:not('.tiny-toggle')").bootstrapToggle()})}(jQuery); |