-
Notifications
You must be signed in to change notification settings - Fork 8
/
ToggleSwitch.min.js
7 lines (7 loc) · 2.86 KB
/
ToggleSwitch.min.js
1
2
3
4
5
6
7
var c=!0,f=!1;
function g(a,b,d){this.c=a;this.a=document.createElement("div");this.a.className="ts-track "+this.c.className;this.a.innerHTML='<div class="ts-switch-container"><span class="ts-on-text">'+b+'</span><span class="ts-switch"></span><span class="ts-off-text">'+d+"</span></div>";this.h=this.a.firstChild;this.g=this.h.firstChild;this.b=this.g.nextSibling;this.K=this.b.previousSibling;this.a.addEventListener("click",this.v.bind(this),f);this.b.addEventListener("touchend",this.F.bind(this),f);this.b.addEventListener("touchstart",
this.H.bind(this),f);this.b.addEventListener("touchmove",this.G.bind(this),f);document.addEventListener("mousemove",this.C.bind(this),f);this.b.addEventListener("mousedown",this.B.bind(this),f);document.addEventListener("mouseup",this.D.bind(this),f);this.c.parentNode.replaceChild(this.a,this.c);this.a.appendChild(this.c);this.c.checked&&(this.i(),this.n(c,c))}
g.prototype={m:f,l:f,f:f,J:function(){return this.m},p:function(){this.n(c)},o:function(){this.n(f)},toggle:function(){this.m?this.o():this.p()},v:function(){this.f||this.toggle();this.f=this.l=f},B:function(){this.i();this.l=c},C:function(a){this.l&&(this.f=c,this.r(a,a.pageX))},D:function(){this.f&&this.t()},H:function(a){this.i();a.preventDefault()},G:function(a){1==a.touches.length&&this.r(a,a.touches[0].pageX,c)},F:function(){this.t()},r:function(a,b,d){var e=this.z(b);b=this.w(b);var h=this.j(),
j=this.d();e<=j?(e=j,b=this.k()):e>=h&&(e=h,b=0);this.h.style.marginLeft=e+"px";this.a.style.backgroundPosition=b+"px";d&&a.preventDefault()},i:function(){this.u(this.a,"no-transition")},A:function(){this.s(this.a,"no-transition")},z:function(a){var b=this.e(this.a).left;return a-b- -this.d()-this.b.offsetWidth/2},w:function(a){var b=this.e(this.a).left;return a-b- -this.k()-this.b.offsetWidth/2},q:function(){return this.g.offsetWidth+(this.e(this.b).left-this.e(this.g).left-this.g.offsetWidth)},
j:function(){return this.a.offsetWidth-this.b.offsetWidth-this.q()-1},d:function(){return-this.q()-1},k:function(){return-this.a.offsetWidth+this.b.offsetWidth-1},t:function(){var a=parseInt(this.h.style.marginLeft,0),b=this.j(),d=this.d();a>(b+d)/2?this.p():this.o()},n:function(a,b){this.m=a;b||this.A();var d=a?this.j():this.d(),e=a?0:this.k();this.h.style.marginLeft=d+"px";this.a.style.backgroundPosition=e+"px 0px";a?this.c.setAttribute("checked","checked"):this.c.removeAttribute("checked");this.I&&
this.I(a)},e:function(a){var b=curtop=0;if(a.offsetParent){do b+=a.offsetLeft,curtop+=a.offsetTop;while(a=a.offsetParent)}return{left:b,top:curtop}},s:function(a,b){a.className=(a.className||"").replace(RegExp("(\\b"+b+"\\b)"),"").trim()},u:function(a,b){this.s(a,b);a.className+=" "+b}};window.ToggleSwitch=g;window.ToggleSwitch.prototype.on=g.prototype.p;window.ToggleSwitch.prototype.isOn=g.prototype.J;window.ToggleSwitch.prototype.off=g.prototype.o;window.ToggleSwitch.prototype.toggle=g.prototype.toggle;