slider_pips.min.js 6.5 KB

1234
  1. /*! jQuery-ui-Slider-Pips - v1.11.3 - 2016-03-15
  2. * Copyright (c) 2016 Simon Goellner <simey.me@gmail.com>; Licensed MIT */
  3. !function(e){"use strict";var i={pips:function(i){function l(i){var l,s,t,a,n,r=[],o=0;if(u.values()&&u.values().length){for(t=u.values(),a=e.map(t,function(e){return Math.abs(e-i)}),n=Math.min.apply(Math,a),l=0;l<a.length;l++)a[l]===n&&r.push(l);for(o=r[0],s=0;s<r.length;s++)u._lastChangedValue===r[s]&&(o=r[s]);u.options.range&&2===r.length&&(i>t[1]?o=r[1]:i<t[0]&&(o=r[0]))}return o}function s(){u.element.off(".selectPip").on("mousedown.slider",u.element.data("mousedown-original")).removeClass("ui-slider-pips").find(".ui-slider-pip").remove()}function t(i,s){if(!u.option("disabled")){var t=e(i).data("value"),a=l(t);u.values()&&u.values().length?u.options.values[a]=u._trimAlignValue(t):u.options.value=u._trimAlignValue(t),u._refreshValue(),u._change(s,a)}}function a(i){var l,s,t,a,r,o=i,p="ui-slider-pip",d="",f=u.value(),v=u.values();if("first"===i?o=0:"last"===i&&(o=h),t=c+u.options.step*o,a=t.toString().replace(".","-"),r=Math.round((o-c)/g.step),l="array"===e.type(g.labels)?g.labels[r]||"":"object"===e.type(g.labels)?"first"===i?g.labels.first||"":"last"===i?g.labels.last||"":"array"===e.type(g.labels.rest)?g.labels.rest[r-1]||"":t:t,"first"===i?(s="0%",p+=" ui-slider-pip-first",p+="label"===g.first?" ui-slider-pip-label":"",p+=g.first===!1?" ui-slider-pip-hide":""):"last"===i?(s="100%",p+=" ui-slider-pip-last",p+="label"===g.last?" ui-slider-pip-label":"",p+=g.last===!1?" ui-slider-pip-hide":""):(s=(100/h*i).toFixed(4)+"%",p+="label"===g.rest?" ui-slider-pip-label":"",p+=g.rest===!1?" ui-slider-pip-hide":""),p+=" ui-slider-pip-"+a,v&&v.length){for(n=0;n<v.length;n++)t===v[n]&&(p+=" ui-slider-pip-initial-"+(n+1),p+=" ui-slider-pip-selected-"+(n+1));u.options.range&&t>v[0]&&t<v[1]&&(p+=" ui-slider-pip-inrange")}else t===f&&(p+=" ui-slider-pip-initial",p+=" ui-slider-pip-selected"),u.options.range&&("min"===u.options.range&&f>t||"max"===u.options.range&&t>f)&&(p+=" ui-slider-pip-inrange");return d="horizontal"===u.options.orientation?"left: "+s:"bottom: "+s,'<span class="'+p+'" style="'+d+'"><span class="ui-slider-line"></span><span class="ui-slider-label" data-value="'+t+'">'+g.formatLabel(l)+"</span></span>"}var n,r,o,p,d,u=this,f="",c=u._valueMin(),v=u._valueMax(),h=(v-c)/u.options.step,m=u.element.find(".ui-slider-handle"),g={first:"label",last:"label",rest:"pip",labels:!1,prefix:"",suffix:"",step:h>100?Math.floor(.05*h):1,formatLabel:function(e){return this.prefix+e+this.suffix}};if("object"!==e.type(i)&&"undefined"!==e.type(i))return void("destroy"===i?s():"refresh"===i&&u.element.slider("pips",u.element.data("pips-options")));e.extend(g,i),u.element.data("pips-options",g),u.options.pipStep=Math.abs(Math.round(g.step))||1,u.element.off(".selectPip").addClass("ui-slider-pips").find(".ui-slider-pip").remove();var b={single:function(i){this.resetClasses(),d.filter(".ui-slider-pip-"+this.classLabel(i)).addClass("ui-slider-pip-selected"),u.options.range&&d.each(function(l,s){var t=e(s).children(".ui-slider-label").data("value");("min"===u.options.range&&i>t||"max"===u.options.range&&t>i)&&e(s).addClass("ui-slider-pip-inrange")})},range:function(i){for(this.resetClasses(),n=0;n<i.length;n++)d.filter(".ui-slider-pip-"+this.classLabel(i[n])).addClass("ui-slider-pip-selected-"+(n+1));u.options.range&&d.each(function(l,s){var t=e(s).children(".ui-slider-label").data("value");t>i[0]&&t<i[1]&&e(s).addClass("ui-slider-pip-inrange")})},classLabel:function(e){return e.toString().replace(".","-")},resetClasses:function(){var e=/(^|\s*)(ui-slider-pip-selected|ui-slider-pip-inrange)(-{1,2}\d+|\s|$)/gi;d.removeClass(function(i,l){return(l.match(e)||[]).join(" ")})}};for(f+=a("first"),o=u.options.pipStep;h>o;o+=u.options.pipStep)f+=a(o);for(f+=a("last"),u.element.append(f),d=u.element.find(".ui-slider-pip"),p=e._data(u.element.get(0),"events").mousedown&&e._data(u.element.get(0),"events").mousedown.length?e._data(u.element.get(0),"events").mousedown:u.element.data("mousedown-handlers"),u.element.data("mousedown-handlers",p.slice()),r=0;r<p.length;r++)"slider"===p[r].namespace&&u.element.data("mousedown-original",p[r].handler);u.element.off("mousedown.slider").on("mousedown.selectPip",function(i){var s=e(i.target),a=l(s.data("value")),n=m.eq(a);if(n.addClass("ui-state-active"),s.is(".ui-slider-label"))t(s,i),u.element.one("mouseup.selectPip",function(){n.removeClass("ui-state-active").focus()});else{var r=u.element.data("mousedown-original");r(i)}}),u.element.on("slide.selectPip slidechange.selectPip",function(i,l){var s=e(this),t=s.slider("value"),a=s.slider("values");l&&(t=l.value,a=l.values),u.values()&&u.values().length?b.range(a):b.single(t)})},"float":function(i){function l(){a.element.off(".sliderFloat").removeClass("ui-slider-float").find(".ui-slider-tip, .ui-slider-tip-label").remove()}function s(i){var l=[],s=e.map(i,function(e){return Math.ceil((e-n)/a.options.step)});if("array"===e.type(f.labels))for(t=0;t<i.length;t++)l[t]=f.labels[s[t]]||i[t];else if("object"===e.type(f.labels))for(t=0;t<i.length;t++)i[t]===n?l[t]=f.labels.first||n:i[t]===r?l[t]=f.labels.last||r:"array"===e.type(f.labels.rest)?l[t]=f.labels.rest[s[t]-1]||i[t]:l[t]=i[t];else for(t=0;t<i.length;t++)l[t]=i[t];return l}var t,a=this,n=a._valueMin(),r=a._valueMax(),o=a._value(),p=a._values(),d=[],u=a.element.find(".ui-slider-handle"),f={handle:!0,pips:!1,labels:!1,prefix:"",suffix:"",event:"slidechange slide",formatLabel:function(e){return this.prefix+e+this.suffix}};if("object"!==e.type(i)&&"undefined"!==e.type(i))return void("destroy"===i?l():"refresh"===i&&a.element.slider("float",a.element.data("float-options")));if(e.extend(f,i),a.element.data("float-options",f),n>o&&(o=n),o>r&&(o=r),p&&p.length)for(t=0;t<p.length;t++)p[t]<n&&(p[t]=n),p[t]>r&&(p[t]=r);if(a.element.addClass("ui-slider-float").find(".ui-slider-tip, .ui-slider-tip-label").remove(),f.handle)for(d=s(a.values()&&a.values().length?p:[o]),t=0;t<d.length;t++)u.eq(t).append(e('<span class="ui-slider-tip">'+f.formatLabel(d[t])+"</span>"));f.pips&&a.element.find(".ui-slider-label").each(function(i,l){var t,a,n=e(l),r=[n.data("value")];t=f.formatLabel(s(r)[0]),a=e('<span class="ui-slider-tip-label">'+t+"</span>").insertAfter(n)}),"slide"!==f.event&&"slidechange"!==f.event&&"slide slidechange"!==f.event&&"slidechange slide"!==f.event&&(f.event="slidechange slide"),a.element.off(".sliderFloat").on(f.event+".sliderFloat",function(i,l){var t="array"===e.type(l.value)?l.value:[l.value],a=f.formatLabel(s(t)[0]);e(l.handle).find(".ui-slider-tip").html(a)})}};e.extend(!0,e.ui.slider.prototype,i)}(jQuery);