UNPKG

jq-button-range-slider

Version:
9 lines 5.52 kB
/* * jq-button-range-slider - v1.0.2 * jQuery range slider plugin with buttons as a values. * https://mohandere.github.io/jq-button-range-slider/ * * Made by Mohan Dere * Under MIT License */ !function(a){"use strict";"function"==typeof define&&define.amd?define(["jquery"],a):"undefined"!=typeof exports?module.exports=a(require("jquery")):a(jQuery)}(function(a){"use strict";var b=window.JqButtonRangeSlider||{};b=function(){function b(b,d){var e=this;e.isTouchDevice=navigator.userAgent.match(/(iPhone|iPod|iPad|Android|playbook|silk|BlackBerry|BB10|Windows Phone|Tizen|Bada|webOS|IEMobile|Opera Mini)/),e.defaults={className:"yo-button-range-slider",sliderOptions:[],template:'<% for (var i = 0; i < sliderOptions.length; i++) { %> <button type="button" class="yo-btn yo-range-btn" value="<%=sliderOptions[i].value%>"><%=sliderOptions[i].name%></button><% } %>'},e.options=a.extend({},e.defaults,d),e.$el=a(b),e.$sliderButtons=null,e.lastClickedButtonIndex=-9999,e.sliderVars={lowerBound:{index:-1},upperBound:{index:-1}},e.sliderLength=e.options.sliderOptions.length,e.sliderMaxIndex=e.sliderLength-1,e.slideHandler=a.proxy(e.slideHandler,e),e.resizeHandler=a.proxy(e.resizeHandler,e),e.instanceUid=c++,e.init(!0)}var c=0;return b}(),b.prototype.tmpl=function(a,b){var c=/\W/.test(a)?new Function("obj","var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('"+a.replace(/[\r\t\n]/g," ").split("<%").join("\t").replace(/((^|%>)[^\t]*)'/g,"$1\r").replace(/\t=(.*?)%>/g,"',$1,'").split("\t").join("');").split("%>").join("p.push('").split("\r").join("\\'")+"');}return p.join('');"):cache[a]=cache[a]||tmpl(document.getElementById(a).innerHTML);return b?c(b):c},b.prototype.init=function(a){var b=this;b.$el.addClass(b.options.className),b.$el.html(b.tmpl(b.options.template,{sliderOptions:b.options.sliderOptions})),b.$sliderButtons=b.$el.find(".yo-range-btn"),b.$sliderButtons.click(b.slideHandler),b.$el.resize(b.resizeHandler),a&&b.$el.trigger("init",[b])},b.prototype.slideHandler=function(b){var c=this,d=a(b.currentTarget),e=c.$sliderButtons.index(d),f=c.getDirectionToMove(e);switch(f){case"L":c.lastClickedButtonIndex===e||c.deActivateBtnAtIndex(e,f)?(e+=1,c.setSliderBounds(e)):c.isLowerBoundSet()?e>c.getSliderBounds(1)?c.setSliderBounds(e,1):c.setSliderBounds(e):(c.setSliderBounds(e),c.setSliderBounds(e,1));break;case"R":c.lastClickedButtonIndex===e||c.deActivateBtnAtIndex(e,f)?(e-=1,c.setSliderBounds(e,1)):c.isUpperBoundSet()?e<c.getSliderBounds()?c.setSliderBounds(e):c.setSliderBounds(e,1):(c.setSliderBounds(e),c.setSliderBounds(e,1))}c.highlightsUI(),c.lastClickedButtonIndex=e;var g=c.$el.find(".yo-range-btn.active").length;return g||c.reset(),c.$el.trigger("afterChange",[c.getSliderValue(),c.getSliderRangeValue(),c]),!1},b.prototype.getDirectionToMove=function(a){var b=this,c=[0,b.sliderMaxIndex];b.isLowerBoundSet()&&b.isUpperBoundSet()&&(c=[b.getSliderBounds(),b.getSliderBounds(1)]);var d=c.reduce(function(b,c){return Math.abs(c-a)<Math.abs(b-a)?c:b});return d===c[1]?"R":"L"},b.prototype.deActivateBtnAtIndex=function(a,b){var c,d,e=this,f=e.$el.find(".yo-range-btn.active").length;switch(f||(c=!1),b){case"L":var g=e.$el.find(".yo-range-btn.active").eq(0);d=e.$sliderButtons.index(g),c=d===a;break;case"R":var h=e.$el.find(".yo-range-btn.active").eq(f-1);d=e.$sliderButtons.index(h),c=d===a}return c||(c=!1),c},b.prototype.highlightsUI=function(){var a=this;a.$sliderButtons.removeClass("active");for(var b=a.sliderVars.lowerBound.index;b<=a.sliderVars.upperBound.index;b++)a.$sliderButtons.eq(b).addClass("active")},b.prototype.isLowerBoundSet=function(){var a=this;return a.sliderVars.lowerBound.index>-1},b.prototype.isUpperBoundSet=function(){var a=this;return a.sliderVars.upperBound.index>-1},b.prototype.getSliderRangeValue=function(){var a=this,b=a.$sliderButtons.eq(a.sliderVars.lowerBound.index).attr("value"),c=a.$sliderButtons.eq(a.sliderVars.upperBound.index).attr("value"),d={lb:{index:a.sliderVars.lowerBound.index,value:b},ub:{index:a.sliderVars.upperBound.index,value:c}};return d},b.prototype.getSliderValue=function(){var b=this,c=[];return b.$el.find(".yo-range-btn.active").each(function(b,d){c.push(a(d).val())}),c},b.prototype.getSliderBounds=function(a){var b=this;return a?b.sliderVars.upperBound.index:b.sliderVars.lowerBound.index},b.prototype.setSliderBounds=function(a,b){var c=this;b?c.sliderVars.upperBound.index=a:c.sliderVars.lowerBound.index=a},b.prototype.reset=function(){var a=this;a.setSliderBounds(-9999),a.setSliderBounds(-9999,1),a.highlightsUI(),a.lastClickedButtonIndex=-9999,a.$el.trigger("reset",[a])},b.prototype.destroy=function(){var a=this;a.$sliderButtons.unbind(),a.$sliderButtons.remove(),a.$el.removeClass(a.options.className),a.$el.trigger("destroy",[a])},b.prototype.setRange=function(b){var c=this;a.each(c.options.sliderOptions,function(a,d){d.value===b.lb&&(c.sliderVars.lowerBound.index=a,c.sliderVars.lowerBound.value=b.lb),d.value===b.ub&&(c.sliderVars.upperBound.index=a,c.sliderVars.upperBound.value=b.ub)}),c.lastClickedButtonIndex=c.sliderVars.upperBound.index,c.highlightsUI()},b.prototype.resizeHandler=function(){},b.prototype.getSlider=function(){return this},a.fn.jqButtonRangeSlider=function(){var a,c,d=this,e=arguments[0],f=Array.prototype.slice.call(arguments,1),g=d.length;for(a=0;a<g;a++)if("object"==typeof e||"undefined"==typeof e?d[a].jqButtonRangeSlider=new b(d[a],e):c=d[a].jqButtonRangeSlider[e].apply(d[a].jqButtonRangeSlider,f),"undefined"!=typeof c)return c;return d}});