forked from leongersen/noUiSlider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.nouislider.min.js
6 lines (6 loc) · 5.36 KB
/
jquery.nouislider.min.js
1
2
3
4
5
6
/** noUislider 2.5.1
** No copyrights or licenses. Do what you like. Feel free to share this code, or build upon it.
** @author: @leongersen
** @repository: https://github.com/leongersen/noUiSlider
**/
(function(a){a.fn.noUiSlider=function(b,c){function d(a){return a<0}function e(a){return Math.abs(a)}function f(a,b){return Math.round(a/b)*b}function g(a){return jQuery.extend(true,{},a)}var h,i,j,c=c||[],k,l="ontouchstart"in document.documentElement;h={knobs:2,connect:true,scale:[0,100],start:[25,75],to:0,knob:0,change:"",end:"",step:false,save:false,click:true};j={scale:function(a,b,c){var f=b[0],g=b[1];if(d(f)){a=a+e(f);g=g+e(f)}else{a=a-f;g=g-f}return a*c/g},deScale:function(a,b,c){b[1]=d(b[0])?b[1]+e(b[0]):b[1]-b[0];return a*b[1]/c+b[0]},connect:function(a){if(a.connect){if(a.knobs.length>1){a.connect.css({left:a.low.left(),right:a.slider.innerWidth()-a.up.left()})}else{a.low?a.connect.css({left:a.low.left(),right:0}):a.connect.css({left:0,right:a.slider.innerWidth()-a.up.left()})}}},left:function(){return parseFloat(a(this).css("left"))},call:function(a,b,c){if(typeof a=="function"){a.call(b,c)}},bounce:function(a,b,c,d){var e=false;if(d.is(a.up)){if(a.low&&b<a.low.left()){b=a.low.left();e=true}}else{if(a.up&&b>a.up.left()){b=a.up.left();e=true}}if(b>a.slider.innerWidth()){b=a.slider.innerWidth();e=true}else if(b<0){b=0;e=true}return[b,e]}};i={init:function(){return this.each(function(){var b,d,e;d=a(this).css("position","relative");e=new Object;e.options=a.extend(h,c);b=e.options;typeof b.start=="object"?1:b.start=[b.start];e.slider=d;e.low=a('<div class="noUi-handle noUi-lowerHandle"><div></div></div>');e.up=a('<div class="noUi-handle noUi-upperHandle"><div></div></div>');e.connect=a('<div class="noUi-midBar"></div>');b.connect?e.connect.appendTo(e.slider):e.connect=false;if(b.knobs===1){if(b.connect===true||b.connect==="lower"){e.low=false;e.up=e.up.appendTo(e.slider);e.knobs=[e.up]}else if(b.connect==="upper"||!b.connect){e.low=e.low.prependTo(e.slider);e.up=false;e.knobs=[e.low]}}else{e.low=e.low.prependTo(e.slider);e.up=e.up.appendTo(e.slider);e.knobs=[e.low,e.up]}if(e.low){e.low.left=j.left}if(e.up){e.up.left=j.left}e.slider.children().css("position","absolute");a.each(e.knobs,function(c){a(this).css({left:j.scale(b.start[c],e.options.scale,e.slider.innerWidth()),zIndex:c+1}).children().bind(l?"touchstart.noUi":"mousedown.noUi",k.start)});if(b.click){e.slider.click(k.click).find("*:not(.noUi-midBar)").click(k.flse)}j.connect(e);e.options=b;e.slider.data("api",e)})},move:function(){var b,d,e,f,h;b=g(a(this).data("api"));b.options=a.extend(b.options,c);f=b.options.knob;f=b.knobs[f=="lower"||f==0?0:1];d=j.bounce(b,j.scale(b.options.to,b.options.scale,b.slider.innerWidth()),f.left(),f);f.css("left",d[0]);if(f.is(b.up)&&f.left()==0||f.is(b.low)&&f.left()==b.slider.innerWidth()){f.css("zIndex",parseInt(f.css("zIndex"))+2)}if(c.save===true){b.options.scale=c.scale;a(this).data("api",b)}j.connect(b);j.call(b.options.change,b.slider,"move");j.call(b.options.end,b.slider,"move")},value:function(){var b,d,e;e=g(a(this).data("api"));e.options=a.extend(e.options,c);b=e.low?Math.round(j.deScale(e.low.left(),e.options.scale,e.slider.innerWidth())):false;d=e.up?Math.round(j.deScale(e.up.left(),e.options.scale,e.slider.innerWidth())):false;if(c.save){e.options.scale=c.scale;a(this).data("api",e)}return[b,d]},api:function(){return a(this).data("api")},disable:function(){return this.each(function(){a(this).addClass("disabled")})},enable:function(){return this.each(function(){a(this).removeClass("disabled")})}},k={start:function(b){if(!a(this).parent().parent().hasClass("disabled")){b.preventDefault();a("body").bind("selectstart.noUi",k.flse);a(this).addClass("noUi-activeHandle");a(document).bind(l?"touchmove.noUi":"mousemove.noUi",k.move);l?a(this).bind("touchend.noUi",k.end):a(document).bind("mouseup.noUi",k.end)}},move:function(b){var c=new Object,d,e,f=false,g,h;d=a(".noUi-activeHandle");e=d.parent().parent().data("api");g=d.parent().is(e.low)?e.low:e.up;c.nw=b.pageX-Math.round(e.slider.offset().left);if(isNaN(c.nw)){c.nw=b.originalEvent.touches[0].pageX-Math.round(e.slider.offset().left)}c.cur=g.left();h=j.bounce(e,c.nw,c.cur,g);c.nw=h[0];f=h[1];if(e.options.step&&!f){m=e.options.step==e.options.scale[0]?2:1;var i=j.scale(e.options.step*m,e.options.scale,e.slider.innerWidth());if(Math.abs(c.cur-c.nw)>=i){c.nw=c.nw<c.cur?c.cur-i:c.cur+i;f=true}}else{f=true}if(c.nw===c.cur){f=false}if(f){g.css("left",c.nw);if(g.is(e.up)&&g.left()==0||g.is(e.low)&&g.left()==e.slider.innerWidth()){g.css("zIndex",parseInt(g.css("zIndex"))+2)}j.connect(e);j.call(e.options.change,e.slider,"slide")}},end:function(){var b,c;b=a(".noUi-activeHandle");c=b.parent().parent().data("api");a(document).add("body").add(b.removeClass("noUi-activeHandle").parent()).unbind(".noUi");j.call(c.options.end,c.slider,"slide")},click:function(b){if(!a(this).hasClass("disabled")){var c=a(this).data("api");var d=c.options;var e=b.pageX-c.slider.offset().left;e=d.step?f(e,j.scale(d.step,d.scale,c.slider.innerWidth())):e;if(c.low&&c.up){e<(c.low.left()+c.up.left())/2?c.low.css("left",e):c.up.css("left",e)}else{c.knobs[0].css("left",e)}j.connect(c);j.call(d.change,c.slider,"click");j.call(d.end,c.slider,"click")}},flse:function(){return false}};if(i[b]){return i[b].apply(this,Array.prototype.slice.call(arguments,1))}else if(typeof b==="object"||!b){return i.init.apply(this,arguments)}else{a.error("No such method: "+b)}}})(jQuery)