forked from leongersen/noUiSlider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.nouislider.min.js
2 lines (2 loc) · 5.57 KB
/
jquery.nouislider.min.js
1
2
/* noUiSlider 3.2.1 fork by tellibus.com to support Zepto */
(function(e){e.fn.noUiSlider=function(t,n){function i(e,t,n){if(typeof e==="function"){e.call(t,n)}}function o(e,t,n){var r=t.data("setup"),i=r.handles,o=r.settings,u=r.pos;e=e<0?0:e>100?100:e;if(o.handles==2){if(n.is(":first-child")){var a=parseFloat(i[1][0].style[u])-o.margin;e=e>a?a:e}else{var a=parseFloat(i[0][0].style[u])+o.margin;e=e<a?a:e}}if(o.step){var f=s.from(o.range,o.step);e=Math.round(e/f)*f}return e}function u(e){try{if(typeof e.originalEvent=="undefined"){return[e.clientX||e.clientX||e.touches[0].clientX,e.clientY||e.clientY||e.touches[0].clientY]}else{return[e.clientX||e.originalEvent.clientX||e.originalEvent.touches[0].clientX,e.clientY||e.originalEvent.clientY||e.originalEvent.touches[0].clientY]}}catch(t){return["x","y"]}}function a(e,t){return parseFloat(e[0].style[t])}var r=window.navigator.msPointerEnabled?2:"ontouchend"in document?3:1;if(window.debug&&console){console.log(r)}var s={to:function(e,t){t=e[0]<0?t+Math.abs(e[0]):t-e[0];return t*100/this._length(e)},from:function(e,t){return t*100/this._length(e)},is:function(e,t){return t*this._length(e)/100+e[0]},_length:function(e){return e[0]>e[1]?e[0]-e[1]:e[1]-e[0]}};var f={handles:2,serialization:{to:["",""],resolution:.01}};methods={create:function(){return this.each(function(){function n(e,t,n){e.css(d,t+"%").data("input").val(s.is(l.range,t).toFixed(b))}var l=e.extend(f,t),c="<a><div></div></a>",h=e(this).data("_isnS_",true),p=[],d,v,m="",g=function(e){return!isNaN(parseFloat(e))&&isFinite(e)},y=(l.serialization.resolution=l.serialization.resolution||.01).toString().split("."),b=y[0]==1?0:y[1].length;l.start=g(l.start)?[l.start,0]:l.start;e.each(l,function(e,t){if(g(t)){l[e]=parseFloat(t)}else if(typeof t=="object"&&g(t[0])){t[0]=parseFloat(t[0]);if(g(t[1])){t[1]=parseFloat(t[1])}}var n=false;t=typeof t=="undefined"?"x":t;switch(e){case"range":case"start":n=t.length!=2||!g(t[0])||!g(t[1]);break;case"handles":n=t<1||t>2||!g(t);break;case"connect":n=t!="lower"&&t!="upper"&&typeof t!="boolean";break;case"orientation":n=t!="vertical"&&t!="horizontal";break;case"margin":case"step":n=typeof t!="undefined"&&!g(t);break;case"serialization":n=typeof t!="object"||!g(t.resolution)||typeof t.to=="object"&&t.to.length<l.handles;break;case"slide":n=typeof t!="function";break}if(n&&console){console.error("Bad input for "+e+" on slider:",h)}});l.margin=l.margin?s.from(l.range,l.margin):0;if(l.serialization.to instanceof e||typeof l.serialization.to=="string"||l.serialization.to===false){l.serialization.to=[l.serialization.to]}if(l.orientation=="vertical"){m+="vertical";d="top";v=1}else{m+="horizontal";d="left";v=0}m+=l.connect?l.connect=="lower"?" connect lower":" connect":"";h.addClass(m);for(var w=0;w<l.handles;w++){p[w]=h.append(c).children().last();var E=s.to(l.range,l.start[w]);p[w].css(d,E+"%");if(E==100&&p[w].is(":first-child")){p[w].css("z-index",2)}var S=".noUiSlider",x=(r===1?"mousedown":r===2?"MSPointerDown":"touchstart")+S+"X",T=(r===1?"mousemove":r===2?"MSPointerMove":"touchmove")+S,N=(r===1?"mouseup":r===2?"MSPointerUp":"touchend")+S;p[w].find("div").on(x,function(t){e("body").bind("selectstart"+S,function(){return false});if(!h.hasClass("disabled")){e("body").addClass("TOUCH");var n=e(this).addClass("active").parent(),r=n.add(e(document)).add("body"),a=parseFloat(n[0].style[d]),f=u(t),c=f,m=false;e(document).on(T,function(e){e.preventDefault();var t=u(e);if(t[0]=="x"){return}t[0]-=f[0];t[1]-=f[1];var r=[c[0]!=t[0],c[1]!=t[1]],g=a+t[v]*100/(v?h.height():h.width());g=o(g,h,n);if(r[v]&&g!=m){n.css(d,g+"%").data("input").val(s.is(l.range,g).toFixed(b));i(l.slide,h.data("_n",true));m=g;n.css("z-index",p.length==2&&g==100&&n.is(":first-child")?2:1)}c=t}).on(N,function(){r.off(S);e("body").removeClass("TOUCH");if(h.find(".active").removeClass("active").data("_n")){h.data("_n",false).change()}})}}).on("click",function(e){e.stopPropagation()})}if(r==1){h.on("click",function(e){if(!h.hasClass("disabled")){var t=u(e),r=(t[v]-h.offset()[d])*100/(v?h.height():h.width()),s=p.length>1?t[v]<(p[0].offset()[d]+p[1].offset()[d])/2?p[0]:p[1]:p[0];n(s,o(r,h,s),h);i(l.slide,h);h.change()}})}for(var w=0;w<p.length;w++){var C=s.is(l.range,a(p[w],d)).toFixed(b);if(typeof l.serialization.to[w]=="string"){p[w].data("input",h.append('<input type="hidden" name="'+l.serialization.to[w]+'">').find("input").last().val(C).change(function(e){e.stopPropagation()}))}else if(l.serialization.to[w]==false){p[w].data("input",{val:function(e){if(typeof e!="undefined"){this.handle.data("noUiVal",e)}else{return this.handle.data("noUiVal")}},handle:p[w]})}else{p[w].data("input",l.serialization.to[w].data("handleNR",w).val(C).change(function(){var t=[null,null];t[e(this).data("handleNR")]=e(this).val();h.val(t)}))}}e(this).data("setup",{settings:l,handles:p,pos:d,res:b})})},val:function(){if(typeof arguments[0]!=="undefined"){var t=typeof arguments[0]=="number"?[arguments[0]]:arguments[0];return this.each(function(){var n=e(this).data("setup");for(var r=0;r<n.handles.length;r++){if(t[r]!=null){var i=o(s.to(n.settings.range,t[r]),e(this),n.handles[r]);n.handles[r].css(n.pos,i+"%").data("input").val(s.is(n.settings.range,i).toFixed(n.res))}}})}else{var n=e(this).data("setup").handles,r=[];for(var i=0;i<n.length;i++){r.push(parseFloat(n[i].data("input").val()))}return r.length==1?r[0]:r}},disabled:function(){return n?e(this).addClass("disabled"):e(this).removeClass("disabled")}};var l=e.fn.val;e.fn.val=function(){return this.data("_isnS_")?methods.val.apply(this,arguments):l.apply(this,arguments)};return t=="disabled"?methods.disabled.apply(this):methods.create.apply(this)}})(window.jQuery||window.Zepto)