From 71026d4d8766bcdfce35dc488f5bf9ac7642eac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Alcaraz=20Mart=C3=ADnez?= Date: Sat, 16 Jul 2022 11:52:00 +0200 Subject: [PATCH 1/3] feat: Add readonly attribute - If original checkbox have readonly attribue, the toggle will be disabled but the checkbox will still be readonly. - If readonly methods is called, the toggle will be disabled but the checkbox will be readonly. This feature allows to get the checkbox on value in the payload of a submited form. refs: Closes palcarazm/bootstrap5-toggle#35 --- js/bootstrap5-toggle.ecmas.js | 21 ++++++++++++++++----- js/bootstrap5-toggle.ecmas.min.js | 2 +- js/bootstrap5-toggle.ecmas.min.js.map | 2 +- js/bootstrap5-toggle.js | 16 +++++++++++++--- js/bootstrap5-toggle.min.js | 2 +- js/bootstrap5-toggle.min.js.map | 2 +- 6 files changed, 33 insertions(+), 12 deletions(-) diff --git a/js/bootstrap5-toggle.ecmas.js b/js/bootstrap5-toggle.ecmas.js index 5745cc1..b86502a 100644 --- a/js/bootstrap5-toggle.ecmas.js +++ b/js/bootstrap5-toggle.ecmas.js @@ -112,7 +112,7 @@ ecmasToggle.classList.add(style); }); } - if (this.element.disabled){ + if (this.element.disabled || this.element.readOnly){ ecmasToggle.classList.add('disabled'); ecmasToggle.setAttribute('disabled', 'disabled'); } @@ -165,7 +165,7 @@ } on(silent = false) { - if (this.element.disabled) return false; + if (this.element.disabled || this.element.readOnly) return false; this.ecmasToggle.classList.remove('btn-' + this.options.offstyle); this.ecmasToggle.classList.add('btn-' + this.options.onstyle); this.ecmasToggle.classList.remove('off'); @@ -174,7 +174,7 @@ } off(silent = false) { - if (this.element.disabled) return false; + if (this.element.disabled || this.element.readOnly) return false; this.ecmasToggle.classList.remove('btn-' + this.options.onstyle); this.ecmasToggle.classList.add('btn-' + this.options.offstyle); this.ecmasToggle.classList.add('off'); @@ -186,16 +186,26 @@ this.ecmasToggle.classList.remove('disabled'); this.ecmasToggle.removeAttribute('disabled'); this.element.removeAttribute('disabled'); + this.element.removeAttribute('readonly'); } disable() { this.ecmasToggle.classList.add('disabled'); - this.ecmasToggle.setAttribute('disabled', 'disabled'); - this.element.setAttribute('disabled', 'disabled'); + this.ecmasToggle.setAttribute('disabled', ''); + this.element.setAttribute('disabled', ''); + this.element.removeAttribute('readonly'); + } + + readonly() { + this.ecmasToggle.classList.add('disabled'); + this.ecmasToggle.setAttribute('disabled', ''); + this.element.removeAttribute('disabled'); + this.element.setAttribute('readonly', ''); } update(silent) { if (this.element.disabled) this.disable(); + else if (this.element.readOnly) this.readonly(); else this.enable(); if (this.element.checked) this.on(silent); else this.off(silent); @@ -230,6 +240,7 @@ else if (options.toLowerCase() == 'off') _bsToggle.off(silent); else if (options.toLowerCase() == 'enable') _bsToggle.enable(); else if (options.toLowerCase() == 'disable') _bsToggle.disable(); + else if (options.toLowerCase() == 'readonly') _bsToggle.readonly(); else if (options.toLowerCase() == 'destroy') _bsToggle.destroy(); } }; diff --git a/js/bootstrap5-toggle.ecmas.min.js b/js/bootstrap5-toggle.ecmas.min.js index 2ea74ea..b7e2299 100644 --- a/js/bootstrap5-toggle.ecmas.min.js +++ b/js/bootstrap5-toggle.ecmas.min.js @@ -8,5 +8,5 @@ * @see https://github.com/palcarazm/bootstrap5-toggle/blob/master/LICENSE */ -"use strict";!function(){class i{constructor(e,t){const s="On",i="primary",o="Off",l="secondary",n="",a="",d=null,h=null;t=t||{},this.element=e,this.options={on:this.element.getAttribute("data-on")||t.on||s,onstyle:this.element.getAttribute("data-onstyle")||t.onstyle||i,off:this.element.getAttribute("data-off")||t.off||o,offstyle:this.element.getAttribute("data-offstyle")||t.offstyle||l,size:this.element.getAttribute("data-size")||t.size||n,style:this.element.getAttribute("data-style")||t.style||a,width:this.element.getAttribute("data-width")||t.width||d,height:this.element.getAttribute("data-height")||t.height||h},this.render()}render(){function e(e){var t=window.getComputedStyle(e),e=e.offsetHeight,s=parseFloat(t.borderTopWidth);return e-parseFloat(t.borderBottomWidth)-s-parseFloat(t.paddingTop)-parseFloat(t.paddingBottom)}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=""}let s=document.createElement("label"),i=(s.setAttribute("class","btn btn-"+this.options.onstyle+" "+t),s.setAttribute("for",this.element.id),s.innerHTML=this.options.on,document.createElement("label")),o=(i.setAttribute("class","btn btn-"+this.options.offstyle+" "+t),i.setAttribute("for",this.element.id),i.innerHTML=this.options.off,document.createElement("span")),l=(o.setAttribute("class","toggle-handle btn "+t),document.createElement("div")),n=(l.setAttribute("class","toggle-group"),l.appendChild(s),l.appendChild(i),l.appendChild(o),document.createElement("div"));n.setAttribute("class","toggle btn"),n.classList.add(this.element.checked?"btn-"+this.options.onstyle:"btn-"+this.options.offstyle),this.element.checked||n.classList.add("off"),this.options.size&&n.classList.add(t),this.options.style&&this.options.style.split(" ").forEach(e=>{n.classList.add(e)}),this.element.disabled&&(n.classList.add("disabled"),n.setAttribute("disabled","disabled")),this.element.parentElement.insertBefore(n,this.element),n.appendChild(this.element),n.appendChild(l),n.style.width=(this.options.width||Math.max(s.getBoundingClientRect().width,i.getBoundingClientRect().width)+o.getBoundingClientRect().width/2)+"px",n.style.height=(this.options.height||Math.max(s.getBoundingClientRect().height,i.getBoundingClientRect().height))+"px",s.classList.add("toggle-on"),i.classList.add("toggle-off"),this.options.height&&(s.style.lineHeight=e(s)+"px",i.style.lineHeight=e(i)+"px"),n.addEventListener("touchstart",e=>{this.toggle(),e.preventDefault()}),n.addEventListener("click",e=>{this.toggle(),e.preventDefault()}),this.ecmasToggle=n,this.element.bsToggle=this}toggle(e=!1){this.element.checked?this.off(e):this.on(e)}on(e=!1){if(this.element.disabled)return!1;this.ecmasToggle.classList.remove("btn-"+this.options.offstyle),this.ecmasToggle.classList.add("btn-"+this.options.onstyle),this.ecmasToggle.classList.remove("off"),this.element.checked=!0,e||this.trigger()}off(e=!1){if(this.element.disabled)return!1;this.ecmasToggle.classList.remove("btn-"+this.options.onstyle),this.ecmasToggle.classList.add("btn-"+this.options.offstyle),this.ecmasToggle.classList.add("off"),this.element.checked=!1,e||this.trigger()}enable(){this.ecmasToggle.classList.remove("disabled"),this.ecmasToggle.removeAttribute("disabled"),this.element.removeAttribute("disabled")}disable(){this.ecmasToggle.classList.add("disabled"),this.ecmasToggle.setAttribute("disabled","disabled"),this.element.setAttribute("disabled","disabled")}update(e){this.element.disabled?this.disable():this.enable(),this.element.checked?this.on(e):this.off(e)}trigger(e){e||this.element.dispatchEvent(new Event("change",{bubbles:!0}))}destroy(){this.ecmasToggle.parentNode.insertBefore(this.element,this.ecmasToggle),this.ecmasToggle.parentNode.removeChild(this.ecmasToggle),delete this.element.bsToggle,delete this.ecmasToggle}}Element.prototype.bootstrapToggle=function(e,t){let s=this.bsToggle||new i(this,e);e&&"string"==typeof e&&("toggle"==e.toLowerCase()?s.toggle(t):"on"==e.toLowerCase()?s.on(t):"off"==e.toLowerCase()?s.off(t):"enable"==e.toLowerCase()?s.enable():"disable"==e.toLowerCase()?s.disable():"destroy"==e.toLowerCase()&&s.destroy())},"undefined"!=typeof window&&(window.onload=function(){document.querySelectorAll('input[type=checkbox][data-toggle="toggle"]').forEach(function(e){e.bootstrapToggle()})}),"undefined"!=typeof module&&module.exports&&(module.exports=i)}(); +"use strict";!function(){class i{constructor(e,t){const s="On",i="primary",l="Off",o="secondary",n="",a="",d=null,h=null;t=t||{},this.element=e,this.options={on:this.element.getAttribute("data-on")||t.on||s,onstyle:this.element.getAttribute("data-onstyle")||t.onstyle||i,off:this.element.getAttribute("data-off")||t.off||l,offstyle:this.element.getAttribute("data-offstyle")||t.offstyle||o,size:this.element.getAttribute("data-size")||t.size||n,style:this.element.getAttribute("data-style")||t.style||a,width:this.element.getAttribute("data-width")||t.width||d,height:this.element.getAttribute("data-height")||t.height||h},this.render()}render(){function e(e){var t=window.getComputedStyle(e),e=e.offsetHeight,s=parseFloat(t.borderTopWidth);return e-parseFloat(t.borderBottomWidth)-s-parseFloat(t.paddingTop)-parseFloat(t.paddingBottom)}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=""}let s=document.createElement("label"),i=(s.setAttribute("class","btn btn-"+this.options.onstyle+" "+t),s.setAttribute("for",this.element.id),s.innerHTML=this.options.on,document.createElement("label")),l=(i.setAttribute("class","btn btn-"+this.options.offstyle+" "+t),i.setAttribute("for",this.element.id),i.innerHTML=this.options.off,document.createElement("span")),o=(l.setAttribute("class","toggle-handle btn "+t),document.createElement("div")),n=(o.setAttribute("class","toggle-group"),o.appendChild(s),o.appendChild(i),o.appendChild(l),document.createElement("div"));n.setAttribute("class","toggle btn"),n.classList.add(this.element.checked?"btn-"+this.options.onstyle:"btn-"+this.options.offstyle),this.element.checked||n.classList.add("off"),this.options.size&&n.classList.add(t),this.options.style&&this.options.style.split(" ").forEach(e=>{n.classList.add(e)}),(this.element.disabled||this.element.readOnly)&&(n.classList.add("disabled"),n.setAttribute("disabled","disabled")),this.element.parentElement.insertBefore(n,this.element),n.appendChild(this.element),n.appendChild(o),n.style.width=(this.options.width||Math.max(s.getBoundingClientRect().width,i.getBoundingClientRect().width)+l.getBoundingClientRect().width/2)+"px",n.style.height=(this.options.height||Math.max(s.getBoundingClientRect().height,i.getBoundingClientRect().height))+"px",s.classList.add("toggle-on"),i.classList.add("toggle-off"),this.options.height&&(s.style.lineHeight=e(s)+"px",i.style.lineHeight=e(i)+"px"),n.addEventListener("touchstart",e=>{this.toggle(),e.preventDefault()}),n.addEventListener("click",e=>{this.toggle(),e.preventDefault()}),this.ecmasToggle=n,this.element.bsToggle=this}toggle(e=!1){this.element.checked?this.off(e):this.on(e)}on(e=!1){if(this.element.disabled||this.element.readOnly)return!1;this.ecmasToggle.classList.remove("btn-"+this.options.offstyle),this.ecmasToggle.classList.add("btn-"+this.options.onstyle),this.ecmasToggle.classList.remove("off"),this.element.checked=!0,e||this.trigger()}off(e=!1){if(this.element.disabled||this.element.readOnly)return!1;this.ecmasToggle.classList.remove("btn-"+this.options.onstyle),this.ecmasToggle.classList.add("btn-"+this.options.offstyle),this.ecmasToggle.classList.add("off"),this.element.checked=!1,e||this.trigger()}enable(){this.ecmasToggle.classList.remove("disabled"),this.ecmasToggle.removeAttribute("disabled"),this.element.removeAttribute("disabled"),this.element.removeAttribute("readonly")}disable(){this.ecmasToggle.classList.add("disabled"),this.ecmasToggle.setAttribute("disabled",""),this.element.setAttribute("disabled",""),this.element.removeAttribute("readonly")}readonly(){this.ecmasToggle.classList.add("disabled"),this.ecmasToggle.setAttribute("disabled",""),this.element.removeAttribute("disabled"),this.element.setAttribute("readonly","")}update(e){this.element.disabled?this.disable():this.element.readOnly?this.readonly():this.enable(),this.element.checked?this.on(e):this.off(e)}trigger(e){e||this.element.dispatchEvent(new Event("change",{bubbles:!0}))}destroy(){this.ecmasToggle.parentNode.insertBefore(this.element,this.ecmasToggle),this.ecmasToggle.parentNode.removeChild(this.ecmasToggle),delete this.element.bsToggle,delete this.ecmasToggle}}Element.prototype.bootstrapToggle=function(e,t){let s=this.bsToggle||new i(this,e);e&&"string"==typeof e&&("toggle"==e.toLowerCase()?s.toggle(t):"on"==e.toLowerCase()?s.on(t):"off"==e.toLowerCase()?s.off(t):"enable"==e.toLowerCase()?s.enable():"disable"==e.toLowerCase()?s.disable():"readonly"==e.toLowerCase()?s.readonly():"destroy"==e.toLowerCase()&&s.destroy())},"undefined"!=typeof window&&(window.onload=function(){document.querySelectorAll('input[type=checkbox][data-toggle="toggle"]').forEach(function(e){e.bootstrapToggle()})}),"undefined"!=typeof module&&module.exports&&(module.exports=i)}(); //# sourceMappingURL=bootstrap5-toggle.ecmas.min.js.map \ No newline at end of file diff --git a/js/bootstrap5-toggle.ecmas.min.js.map b/js/bootstrap5-toggle.ecmas.min.js.map index 3c2493b..d2116dc 100644 --- a/js/bootstrap5-toggle.ecmas.min.js.map +++ b/js/bootstrap5-toggle.ecmas.min.js.map @@ -1 +1 @@ -{"version":3,"file":"bootstrap5-toggle.ecmas.min.js","sources":["bootstrap5-toggle.ecmas.js"],"names":["Toggle","constructor","element","options","DEFAULTS","this","on","getAttribute","onstyle","off","offstyle","size","style","width","height","render","calcH","el","styles","window","getComputedStyle","offsetHeight","borderTopWidth","parseFloat","borderBottomWidth","paddingTop","paddingBottom","let","ecmasToggleOn","document","createElement","ecmasToggleOff","setAttribute","id","innerHTML","ecmasToggleHandle","ecmasToggleGroup","ecmasToggle","appendChild","classList","add","checked","split","forEach","disabled","parentElement","insertBefore","Math","max","getBoundingClientRect","lineHeight","addEventListener","toggle","e","preventDefault","bsToggle","silent","remove","trigger","enable","removeAttribute","disable","update","dispatchEvent","Event","bubbles","destroy","parentNode","removeChild","Element","prototype","bootstrapToggle","_bsToggle","toLowerCase","onload","querySelectorAll","ele","module","exports"],"mappings":"cAaA,iBAIOA,EACLC,YAAYC,EAASC,GACpB,MAAMC,EACD,KADCA,EAEI,UAFJA,EAGA,MAHAA,EAIK,YAJLA,EAKC,GALDA,EAME,GANFA,EAOE,KAPFA,EAQG,KAETD,EAAUA,GAAW,GAGrBE,KAAKH,QAAUA,EAGfG,KAAKF,QAAU,CACdG,GAAID,KAAKH,QAAQK,aAAa,YAAcJ,EAAQG,IAAMF,EAC1DI,QAASH,KAAKH,QAAQK,aAAa,iBAAmBJ,EAAQK,SAAWJ,EACzEK,IAAKJ,KAAKH,QAAQK,aAAa,aAAeJ,EAAQM,KAAOL,EAC7DM,SAAUL,KAAKH,QAAQK,aAAa,kBAAoBJ,EAAQO,UAAYN,EAC5EO,KAAMN,KAAKH,QAAQK,aAAa,cAAgBJ,EAAQQ,MAAQP,EAChEQ,MAAOP,KAAKH,QAAQK,aAAa,eAAiBJ,EAAQS,OAASR,EACnES,MAAOR,KAAKH,QAAQK,aAAa,eAAiBJ,EAAQU,OAAST,EACnEU,OAAQT,KAAKH,QAAQK,aAAa,gBAAkBJ,EAAQW,QAAUV,GAIvEC,KAAKU,SAENA,SACC,SAASC,EAAMC,G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,EAAQsE,cAA4BD,EAAUX,UACrB,WAAzB1D,EAAQsE,eAA4BD,EAAUN,YAQnC,oBAAX/C,SACVA,OAAOuD,OAAS,WACf7C,SAAS8C,iBAAiB,8CAA8ChC,QAAQ,SAASiC,GACxFA,EAAIL,sBAKe,oBAAXM,QAA0BA,OAAOC,UAC3CD,OAAOC,QAAU9E,GA5OnB"} \ No newline at end of file +{"version":3,"file":"bootstrap5-toggle.ecmas.min.js","sources":["bootstrap5-toggle.ecmas.js"],"names":["Toggle","constructor","element","options","DEFAULTS","this","on","getAttribute","onstyle","off","offstyle","size","style","width","height","render","calcH","el","styles","window","getComputedStyle","offsetHeight","borderTopWidth","parseFloat","borderBottomWidth","paddingTop","paddingBottom","let","ecmasToggleOn","document","createElement","ecmasToggleOff","setAttribute","id","innerHTML","ecmasToggleHandle","ecmasToggleGroup","ecmasToggle","appendChild","classList","add","checked","split","forEach","disabled","readOnly","parentElement","insertBefore","Math","max","getBoundingClientRect","lineHeight","addEventListener","toggle","e","preventDefault","bsToggle","silent","remove","trigger","enable","removeAttribute","disable","readonly","update","dispatchEvent","Event","bubbles","destroy","parentNode","removeChild","Element","prototype","bootstrapToggle","_bsToggle","toLowerCase","onload","querySelectorAll","ele","module","exports"],"mappings":"cAaA,iBAIOA,EACLC,YAAYC,EAASC,GACpB,MAAMC,EACD,KADCA,EAEI,UAFJA,EAGA,MAHAA,EAIK,YAJLA,EAKC,GALDA,EAME,GANFA,EAOE,KAPFA,EAQG,KAETD,EAAUA,GAAW,GAGrBE,KAAKH,QAAUA,EAGfG,KAAKF,QAAU,CACdG,GAAID,KAAKH,QAAQK,aAAa,YAAcJ,EAAQG,IAAMF,EAC1DI,QAASH,KAAKH,QAAQK,aAAa,iBAAmBJ,EAAQK,SAAWJ,EACzEK,IAAKJ,KAAKH,QAAQK,aAAa,aAAeJ,EAAQM,KAAOL,EAC7DM,SAAUL,KAAKH,QAAQK,aAAa,kBAAoBJ,EAAQO,UAAYN,EAC5EO,KAAMN,K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AGmD,GAC7BpD,KAAKI,IAAIgD,GAGfE,QAAQF,GACFA,GAAQpD,KAAKH,QAAQ+D,cAAc,IAAIC,MAAM,SAAU,CAAEC,SAAS,KAGxEC,UAEC/D,KAAKgC,YAAYgC,WAAWtB,aAAa1C,KAAKH,QAASG,KAAKgC,aAC5DhC,KAAKgC,YAAYgC,WAAWC,YAAYjE,KAAKgC,oBAGtChC,KAAKH,QAAQsD,gBACbnD,KAAKgC,aAQdkC,QAAQC,UAAUC,gBAAkB,SAAStE,EAASsD,GACrD9B,IAAI+C,EAAYrE,KAAKmD,UAAY,IAAIxD,EAAOK,KAAMF,GAG9CA,GAA8B,iBAAZA,IACQ,UAAzBA,EAAQwE,cAA2BD,EAAUrB,OAAOI,GACtB,MAAzBtD,EAAQwE,cAAuBD,EAAUpE,GAAGmD,GACnB,OAAzBtD,EAAQwE,cAAwBD,EAAUjE,IAAIgD,GACrB,UAAzBtD,EAAQwE,cAA2BD,EAAUd,SACpB,WAAzBzD,EAAQwE,cAA4BD,EAAUZ,UACrB,YAAzB3D,EAAQwE,cAA6BD,EAAUX,WACtB,WAAzB5D,EAAQwE,eAA4BD,EAAUN,YAQnC,oBAAXjD,SACVA,OAAOyD,OAAS,WACf/C,SAASgD,iBAAiB,8CAA8ClC,QAAQ,SAASmC,GACxFA,EAAIL,sBAKe,oBAAXM,QAA0BA,OAAOC,UAC3CD,OAAOC,QAAUhF,GAvPnB"} \ No newline at end of file diff --git a/js/bootstrap5-toggle.js b/js/bootstrap5-toggle.js index 2e4cc71..1ad0f38 100644 --- a/js/bootstrap5-toggle.js +++ b/js/bootstrap5-toggle.js @@ -89,7 +89,7 @@ let $toggle = $('
') .addClass( this.$element.prop('checked') ? 'btn-' +this.options.onstyle : 'btn-' +this.options.offstyle+' off' ) .addClass(size).addClass(this.options.style) - if (this.$element.prop('disabled')){ + if (this.$element.prop('disabled') || this.$element.prop('readonly')){ $toggle.addClass('disabled') $toggle.attr('disabled', 'disabled') } @@ -141,14 +141,14 @@ } Toggle.prototype.on = function (silent = false) { - if (this.$element.prop('disabled')) return false + if (this.$element.prop('disabled') || this.$element.prop('readonly')) return false this.$toggle.removeClass('btn-' +this.options.offstyle + ' off').addClass('btn-' +this.options.onstyle) this.$element.prop('checked', true) if (!silent) this.trigger() } Toggle.prototype.off = function (silent = false) { - if (this.$element.prop('disabled')) return false + if (this.$element.prop('disabled') || this.$element.prop('readonly')) return false this.$toggle.removeClass('btn-' +this.options.onstyle).addClass('btn-' +this.options.offstyle + ' off') this.$element.prop('checked', false) if (!silent) this.trigger() @@ -158,16 +158,26 @@ this.$toggle.removeClass('disabled') this.$toggle.removeAttr('disabled') this.$element.prop('disabled', false) + this.$element.prop('readonly',false) } Toggle.prototype.disable = function () { this.$toggle.addClass('disabled') this.$toggle.attr('disabled', 'disabled') this.$element.prop('disabled', true) + this.$element.prop('readonly', false) + } + + Toggle.prototype.readonly = function () { + this.$toggle.addClass('disabled') + this.$toggle.attr('disabled', 'disabled') + this.$element.prop('disabled', false) + this.$element.prop('readonly', true) } Toggle.prototype.update = function (silent) { if (this.$element.prop('disabled')) this.disable() + else if (this.$element.prop('readonly')) this.readonly() else this.enable() if (this.$element.prop('checked')) this.on(silent) else this.off(silent) diff --git a/js/bootstrap5-toggle.min.js b/js/bootstrap5-toggle.min.js index 84c0086..96a3a16 100644 --- a/js/bootstrap5-toggle.min.js +++ b/js/bootstrap5-toggle.min.js @@ -8,5 +8,5 @@ * @see https://github.com/palcarazm/bootstrap5-toggle/blob/master/LICENSE */ -!function(a){"use strict";function l(t,e){this.$element=a(t),this.options=a.extend({},this.defaults(),e),this.render()}l.DEFAULTS={on:"On",off:"Off",onstyle:"primary",offstyle:"secondary",size:"normal",style:"",width:null,height:null},l.prototype.defaults=function(){return{on:this.$element.attr("data-on")||l.DEFAULTS.on,off:this.$element.attr("data-off")||l.DEFAULTS.off,onstyle:this.$element.attr("data-onstyle")||l.DEFAULTS.onstyle,offstyle:this.$element.attr("data-offstyle")||l.DEFAULTS.offstyle,size:this.$element.attr("data-size")||l.DEFAULTS.size,style:this.$element.attr("data-style")||l.DEFAULTS.style,width:this.$element.attr("data-width")||l.DEFAULTS.width,height:this.$element.attr("data-height")||l.DEFAULTS.height}},l.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=""}let e=a('