-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery.fs.ranger.min.js
9 lines (8 loc) · 4.77 KB
/
jquery.fs.ranger.min.js
1
2
3
4
5
6
7
8
9
/*
* Ranger v3.1.1 - 2015-04-04
* A jQuery plugin for cross browser range inputs. Part of the formstone library.
* http://classic.formstone.it/ranger/
*
* Copyright 2015 Ben Plum; MIT Licensed
*/
!function(a){"use strict";function b(b){b=a.extend({},m,b);for(var d=a(this),e=0,f=d.length;f>e;e++)c(d.eq(e),b);return d}function c(b,c){if(!b.data("ranger")){a.extend(c,b.data("ranger-options")),c.formatter===a.noop&&(c.formatter=l);var f=parseFloat(b.attr("min"))||0,g=parseFloat(b.attr("max"))||100,j=parseFloat(b.attr("step"))||1,m=parseFloat(b.val())||f+(g-f)/2;c.vertical="vertical"===b.attr("orient")||c.vertical;var o='<div class="ranger';c.vertical&&(o+=" ranger-vertical"),c.label&&(o+=" ranger-labels"),o+='">',o+='<div class="ranger-track">',o+='<span class="ranger-handle">',o+='<span class="ranger-marker"></span>',o+="</span>",o+="</div>",o+="</div>",b.addClass("ranger-element").after(o);var p=b.next(".ranger"),q=p.find(".ranger-track"),r=p.find(".ranger-handle"),s=p.find(".ranger-output");c.label&&(c.vertical?p.prepend('<span class="ranger-label max">'+c.formatter.call(this,c.labels.max?c.labels.max:g)+"</span>").append('<span class="ranger-label min">'+c.formatter.call(this,c.labels.min?c.labels.min:f)+"</span>"):p.prepend('<span class="ranger-label min">'+c.formatter.call(this,c.labels.min?c.labels.min:f)+"</span>").append('<span class="ranger-label max">'+c.formatter.call(this,c.labels.max?c.labels.max:g)+"</span>")),p.is(":disabled")&&p.addClass("disabled");var t=a.extend({},c,{$input:b,$ranger:p,$track:q,$handle:r,$output:s,min:f,max:g,step:j,stepDigits:j.toString().length-j.toString().indexOf("."),value:m});b.on("focus.ranger",t,h).on("blur.ranger",t,i).on("change.ranger input.ranger",t,k).data("ranger",t),p.on("touchstart.ranger mousedown.ranger",".ranger-track",t,d).on("touchstart.ranger mousedown.ranger",".ranger-handle",t,e),n.reset.apply(b)}}function d(b){b.preventDefault(),b.stopPropagation();var c=b.data;c.$input.is(":disabled")||(f(b),c.$ranger.addClass("focus"),a("body").on("touchmove.ranger mousemove.ranger",c,f).one("touchend.ranger touchcancel.ranger mouseup.ranger",c,g))}function e(b){b.preventDefault(),b.stopPropagation();var c=b.data;c.$input.is(":disabled")||(c.$ranger.addClass("focus"),a("body").on("touchmove.ranger mousemove.ranger",c,f).one("touchend.ranger touchcancel.ranger mouseup.ranger",c,g))}function f(a){a.preventDefault(),a.stopPropagation();var b=a.originalEvent,c=a.data,d=c.$track.offset(),e=0;if(c.vertical){var f="undefined"!=typeof b.targetTouches?b.targetTouches[0].pageY:a.pageY;e=1-(f-d.top)/c.trackHeight}else{var g="undefined"!=typeof b.targetTouches?b.targetTouches[0].pageX:a.pageX;e=(g-d.left)/c.trackWidth}j(c,e)}function g(b){b.preventDefault(),b.stopPropagation();var c=b.data;c.$ranger.removeClass("focus"),a("body").off(".ranger")}function h(a){a.data.$ranger.addClass("focus")}function i(a){a.data.$ranger.removeClass("focus")}function j(a,b,c){a.increment>1&&(b=a.vertical?Math.round(b*a.stepCount)*a.increment/a.trackHeight:Math.round(b*a.stepCount)*a.increment/a.trackWidth),0>b&&(b=0),b>1&&(b=1);var d=(a.min-a.max)*b;d=-parseFloat(d.toFixed(a.stepDigits)),a.$handle.css(a.vertical?"bottom":"left",100*b+"%"),d+=a.min,d!==a.value&&d&&c!==!0&&(a.$input.val(d).trigger("change",[!0]),a.value=d)}function k(a,b){var c=a.data;if(!b&&!c.$input.is(":disabled")){var d=(c.$input.val()-c.min)/(c.max-c.min);j(c,d)}}function l(a){var b=a.toString().split(".");return b[0]=b[0].replace(/\B(?=(\d{3})+(?!\d))/g,","),b.join(".")}var m={customClass:"",formatter:a.noop,label:!0,labels:{max:!1,min:!1},vertical:!1},n={defaults:function(b){return m=a.extend(m,b||{}),"object"==typeof this?a(this):!0},destroy:function(){return a(this).each(function(b,c){var d=a(c),e=d.data("ranger");e&&(e.$ranger.off(".ranger").remove(),e.$input.off(".ranger").removeClass("ranger-element").removeData("ranger"))})},disable:function(){return a(this).each(function(b,c){var d=a(c),e=d.data("ranger");e&&(e.$input.prop("disabled",!0),e.$ranger.addClass("disabled"))})},enable:function(){return a(this).each(function(b,c){var d=a(c),e=d.data("ranger");e&&(e.$input.prop("disabled",!1),e.$ranger.removeClass("disabled"))})},reset:function(){return a(this).each(function(b,c){var d=a(c),e=d.data("ranger");if(e){e.stepCount=(e.max-e.min)/e.step,e.vertical?(e.trackHeight=e.$track.outerHeight(),e.handleHeight=e.$handle.outerHeight(),e.increment=e.trackHeight/e.stepCount):(e.trackWidth=e.$track.outerWidth(),e.handleWidth=e.$handle.outerWidth(),e.increment=e.trackWidth/e.stepCount);var f=(e.$input.val()-e.min)/(e.max-e.min);j(e,f,!0)}})}};a.fn.ranger=function(a){return n[a]?n[a].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof a&&a?this:b.apply(this,arguments)},a.ranger=function(a){"defaults"===a&&n.defaults.apply(this,Array.prototype.slice.call(arguments,1))}}(jQuery,window);