fa-multi-button.min.js 7.2 KB

1
  1. "use strict";!function(t){t.fn.famultibutton=function(e){function w(){i=!0,o.bg.css("color",C.onBackgroundColor),o.fg.css("color",C.onColor),n.addClass("active"),o.trigger("setOn")}function I(){i=!1,o.bg.css("color",C.offBackgroundColor),o.fg.css("color",C.offColor),n.removeClass("active"),o.trigger("setOff")}function T(t){o.fg.css("color",t)}function k(t){o.bg.css("color",t)}function x(t){o.fg.removeClass(o.fi),o.fi=t,o.fg.addClass(o.fi)}function O(t){o.bg.removeClass(o.bi),o.bi=t,o.bg.addClass(o.bi)}function P(){t("<div />").animate({width:100},{duration:700,easing:"swing",step:function(t,e){var n=(t-e.start)/(e.end-e.start);o.bg.css("color",S(C.onBackgroundColor,C.offBackgroundColor,n)),o.fg.css("color",S(C.onColor,C.offColor,n))},complete:function(){i===!0&&w()}})}function y(t){var e=t.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);return e&&4===e.length?"#"+("0"+parseInt(e[1],10).toString(16)).slice(-2)+("0"+parseInt(e[2],10).toString(16)).slice(-2)+("0"+parseInt(e[3],10).toString(16)).slice(-2):t}function S(t,e,o){t=y(t).replace(/^\s*#|\s*$/g,""),e=y(e).replace(/^\s*#|\s*$/g,""),3==t.length&&(t=t.replace(/(.)/g,"$1$1")),3==e.length&&(e=e.replace(/(.)/g,"$1$1"));var n=parseInt(t.substr(0,2),16),i=parseInt(t.substr(2,2),16),a=parseInt(t.substr(4,2),16),r=parseInt(e.substr(0,2),16),s=parseInt(e.substr(2,2),16),f=parseInt(e.substr(4,2),16),l=r-n,u=s-i,c=f-a;return l=(l*o+n).toString(16).split(".")[0],u=(u*o+i).toString(16).split(".")[0],c=(c*o+a).toString(16).split(".")[0],1==l.length&&(l="0"+l),1==u.length&&(u="0"+u),1==c.length&&(c="0"+c),"#"+l+u+c}function B(e){var i=o.find("canvas#progress");if(e>0){0===i.length&&(i=t("<canvas>").attr({id:"progress"}).appendTo(n));var a=i[0];if(a){a.height=o.h,a.width=o.w;var r=a.width/2,s=a.height/2;if(a.getContext){var f=a.getContext("2d");f.beginPath(),f.strokeStyle=C.onColor,f.arc(r,s,r*(-.4/90*Number(C.progressWidth)+.8),-.5*Math.PI,(-.5+2*e)*Math.PI,!1),f.lineWidth=.8*r*C.progressWidth/100,f.stroke()}}}else o.find("canvas#progress").remove()}function D(){clearTimeout(r),u=c>0?u-=C.step:u+=C.step,u>C.max&&(u=C.max),u<C.min&&(u=C.min),M();var t=f[Math.abs(c)];r=setTimeout(function(){D()},500-t)}function M(){var t=a[0];if(t.height=o.h,t.width=o.w,t.getContext){var e=t.getContext("2d");e.strokeStyle=C.offBackgroundColor;for(var n=C.max,r=C.min,s=t.height-Math.round(t.height*(u-r)/(n-r)),f=0;f<t.height;f+=4)e.lineWidth=1,e.beginPath(),e.moveTo(5,f),e.lineTo(25,f),e.stroke();e.strokeStyle=i?C.onBackgroundColor:S(C.offBackgroundColor,"#ffffff",.4),e.lineWidth=3,e.beginPath(),e.moveTo(5,s),e.lineTo(25,s),e.stroke(),e.fillStyle=S(C.offBackgroundColor,"#ffffff",.4),e.font="10px sans-serif",e.fillText(u,30,t.height)}}function A(){a.css({position:"absolute","z-index":-1}),h?a.animate({left:.6*-o.w+"px"}):a.animate({left:o.w/5+"px",top:"0px"})}function W(t){t=(t||navigator.userAgent).toLowerCase();var e=t.match(/android\s([0-9\.]*)/);return e?e[1]:!1}function $(){var e,f,y,g=W(),v=/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,b=g&&parseFloat(g)<5||v,T=b?"touchstart":"touchstart mousedown",k=b?"touchmove":"touchmove mousemove",x=b?"touchend":"touchend mouseup",O=b?"touchleave":"touchleave mouseout";"push"===C.mode?(n.on(T,function(o){o.stopImmediatePropagation(),f=t(window).scrollTop(),e=t(window).scrollLeft()}),n.on(x,function(n){return n.preventDefault(),n.stopImmediatePropagation(),Math.abs(f-t(window).scrollTop())>3||Math.abs(e-t(window).scrollLeft())>3?void 0:(y=i,w(),"function"==typeof C.toggleOn&&C.toggleOn.call(this),i=!1,setTimeout(function(){P()},200),y===!0&&setTimeout(function(){w()},1e3),o.trigger("clicked"),!1)})):"updown"==C.mode?(n.on(T,function(t){t.preventDefault(),t.stopImmediatePropagation(),y=i,w(),"function"==typeof C.toggleOn&&C.toggleOn.call(this)}),n.on(x,function(t){return t.preventDefault(),t.stopImmediatePropagation(),"function"==typeof C.toggleOff&&C.toggleOff.call(this),i=!1,setTimeout(function(){P()},200),y===!0&&setTimeout(function(){w()},1e3),o.trigger("clicked"),!1})):"toggle"==C.mode?(n.on(T,function(o){o.stopImmediatePropagation(),f=t(window).scrollTop(),e=t(window).scrollLeft()}),n.on(x,function(n){return n.preventDefault(),n.stopImmediatePropagation(),Math.abs(f-t(window).scrollTop())>3||Math.abs(e-t(window).scrollLeft())>3?void 0:(i?(I(),"function"==typeof C.toggleOff&&C.toggleOff.call(this),C.timeout>0&&(m=setTimeout(function(){w()},C.timeout))):(w(),"function"==typeof C.toggleOn&&C.toggleOn.call(this),C.timeout>0&&(m=setTimeout(function(){I()},C.timeout))),o.trigger("clicked"),!1)})):"dimmer"==C.mode&&(n.on(T,function(t){t.preventDefault(),t.stopImmediatePropagation();var e=t.originalEvent;l=e.touches?e.touches[0].clientY:t.pageY,c=0,p=!0}),n.on(O,function(t){t.preventDefault(),t.stopImmediatePropagation(),h&&(h=!1,n.animate({top:0}),clearInterval(r),s=!1,A()),p=!1}),n.on(x,function(t){return t.preventDefault(),t.stopImmediatePropagation(),h?(h=!1,n.animate({top:0}),clearTimeout(r),s=!1,"function"==typeof C.valueChanged&&C.valueChanged.call(this,u)):i?(I(),"function"==typeof C.toggleOff&&C.toggleOff.call(this),C.timeout>0&&(m=setTimeout(function(){w()},C.timeout))):(w(),"function"==typeof C.toggleOn&&C.toggleOn.call(this),C.timeout>0&&(m=setTimeout(function(){I()},C.timeout))),h=!1,p=!1,A(),M(),o.trigger("clicked"),!1}),n.on(k,function(t){t.preventDefault(),t.stopImmediatePropagation(),p&&(h=!0);var e=t.originalEvent;if(d=e.touches?e.touches[0].clientY:t.pageY,c=d-l,c>20&&(c=20),-20>c&&(c=-20),h){this.style.top=c+"px";var o=a[0];o.style.top=-c+"px",s||(A(),D(),s=!0)}}))}if(this.length>1)return this.each(function(){t(this).famultibutton(e)}),this;var n,a,r,m,o=this,i=!1,s=!1,f=[0,10,40,80,120,140,150,160,180,200,240,260,280,300,320,420,430,440,450,460,470],l=0,u=0,c=0,g=0,d=0,h=!1,p=!1,v={backgroundIcon:"fa-circle",classes:["fa-2x"],icon:"fa-power-off",offColor:"#2A2A2A",offBackgroundColor:"#505050",onColor:"#2A2A2A",onBackgroundColor:"#aa6900",mode:"toggle",toggleOn:null,toggleOff:null,valueChanged:null,progressWidth:15,timeout:0,max:100,min:0,step:1},C=t.extend({},v,e),b=function(){C=t.extend({},C,o.data());var e=o.html()!==o.text()?jQuery("<div>",{}).html(o.html()).children().detach():jQuery("<div>",{}).text(o.text());if("none"!==C.onColor&&"none"!==C.offColor&&e.attr("id","fg"),e.addClass("fa-stack-1x"),o.html(""),o.bi=C.backgroundIcon,o.fi=C.icon,n=t("<div/>",{"class":"famultibutton"}),n.addClass("fa-stack"),o.bg=jQuery("<i/>",{id:"bg","class":"fa fa-stack-2x"}).addClass(o.bi),o.fg=jQuery("<i/>",{id:"fg","class":"fa fa-stack-1x"}).addClass(o.fi),C.classes&&C.classes.length>0)for(var i=0,r=C.classes.length;r>i;i++)n.addClass(C.classes[i]);return o.bg.appendTo(n),o.fg.appendTo(n),e.appendTo(n),n.appendTo(o),o.o=C,o.w=n.width(),o.h=n.height(),I(),"dimmer"==C.mode&&(a=t("<canvas>").attr({id:"scale",height:o.h+"px",width:o.w+"px"}).appendTo(n),g=parseInt(a.offset().top)-parseInt(n.offset().top),M(),A()),o.data("famultibutton",o),$(),o};return this.setOn=function(){clearTimeout(m),w()},this.setOff=function(){clearTimeout(m),I()},this.getState=function(){return i},this.getValue=function(){return u},this.setDimValue=function(t){u=t,M()},this.setProgressValue=function(t){B(t)},this.setForegroundColor=function(t){T(t)},this.setBackgroundColor=function(t){k(t)},this.setForegroundIcon=function(t){x(t)},this.setBackgroundIcon=function(t){O(t)},b()}}(jQuery);