diff options
author | Mario <mario@mariovavti.com> | 2023-01-05 18:21:49 +0100 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2023-01-05 18:21:49 +0100 |
commit | 47071c58aa868ee9756a98c967b64c024d22920b (patch) | |
tree | 3a1f0e7cb5931b7287cde54fe84379b75ca11001 /library | |
parent | 79d99688b4febeef6ae552252c254efa6911fd53 (diff) | |
download | volse-hubzilla-47071c58aa868ee9756a98c967b64c024d22920b.tar.gz volse-hubzilla-47071c58aa868ee9756a98c967b64c024d22920b.tar.bz2 volse-hubzilla-47071c58aa868ee9756a98c967b64c024d22920b.zip |
fix affinity slider updates - issue #1714
Diffstat (limited to 'library')
-rw-r--r-- | library/jRange/.gitignore | 2 | ||||
-rw-r--r-- | library/jRange/demo/index.html | 87 | ||||
-rw-r--r-- | library/jRange/jquery.range-min.js | 2 | ||||
-rw-r--r-- | library/jRange/jquery.range.js | 146 |
4 files changed, 184 insertions, 53 deletions
diff --git a/library/jRange/.gitignore b/library/jRange/.gitignore index 089ae868a..bdedd7dd3 100644 --- a/library/jRange/.gitignore +++ b/library/jRange/.gitignore @@ -1,2 +1,2 @@ - +.DS_Store *.codekit diff --git a/library/jRange/demo/index.html b/library/jRange/demo/index.html index 19a5f33f0..61966ba22 100644 --- a/library/jRange/demo/index.html +++ b/library/jRange/demo/index.html @@ -38,17 +38,18 @@ <div class="demo-section"> <div class="demo-code"> <pre><code class="language-javascript">$('.single-slider').jRange({ - from: 0, - to: 100, - step: 1, - scale: [0,25,50,75,100], + from: -2.0, + to: 2.0, + step: 0.5, + scale: [-2.0,-1.0,0.0,1.0,2.0], format: '%s', width: 300, - showLabels: true + showLabels: true, + snap: true });</code></pre> </div> <div class="demo-output"> - <input class="single-slider" type="hidden" value="25"/> + <input class="single-slider" type="hidden" value="0.0"/> </div> </div> <div class="demo-section"> @@ -180,6 +181,15 @@ <p>True if this is a range selector. If its a range the value of hidden input will be set comma-seperated, e.g., "25,75"</p> </td> </tr> + <tr> + <td>snap</td> + <td>Optional</td> + <td>Boolean</td> + <td> + <code>Default : false</code> + <p>True to snap slider to step values</p> + </td> + </tr> <tr> <td>disable</td> <td>Optional</td> @@ -203,8 +213,62 @@ <p>For single slider value is without comma, however for a range selector value is comma-seperated.</p> </td> </tr> + <tr> + <td>ondragend</td> + <td>Optional</td> + <td>Function</td> + <td> + <p>ondragend callback. Useful if you want to fire event just once per slider drag.</p> + </td> + </tr> + <tr> + <td>onbarclicked</td> + <td>Optional</td> + <td>Function</td> + <td> + <p>called when user clicks on the bar</p> + </td> + </tr> </table> + <h2>Modification</h2> + <h3>Change values on runtime</h3> + <p>Methods which you can call to dynamically modify current values and range. + <table class="plugin-options" width='900'> + <tr> + <th width="150">Method</th> + <th></th> + <th></th> + <th>Description</th> + </tr> + <tr> + <td>setValue</td> + <td></td> + <td></td> + <td> + <p>sets the current value of the slider without changing its range, if you want to update the range as well use <code>updateRange</code> instead.</p> + <code> + $('.slider').jRange('setValue', '10,20');<br> + $('.slider').jRange('setValue', '10'); + </code> + </td> + </tr> + <tr> + <td>updateRange</td> + <td></td> + <td></td> + <td> + <p>'updateRange' to change (min, max) value and interval after initialized.</p> + <code> + $('.slider').jRange('updateRange', '0,100');<br> + $('.slider').jRange('updateRange', '0,100', '25,50');<br> + $('.slider').jRange('updateRange', '0,100', 25); + </code> + <p>passing second parameter also sets its current value</p> + </td> + </tr> + </table> + <div class="footer"> <a href="https://github.com/nitinhayaran/jRange" class="large-github">Get it from Github</a> </div> @@ -235,13 +299,14 @@ <script type="text/javascript"> $(document).ready(function(){ $('.single-slider').jRange({ - from: 0, - to: 100, - step: 1, - scale: [0,25,50,75,100], + from: -2.0, + to: 2.0, + step: 0.5, + scale: [-2.0,-1.0,0.0,1.0,2.0], format: '%s', width: 300, - showLabels: true + showLabels: true, + snap: true }); $('.range-slider').jRange({ from: 0, diff --git a/library/jRange/jquery.range-min.js b/library/jRange/jquery.range-min.js index 589913e57..3299649c9 100644 --- a/library/jRange/jquery.range-min.js +++ b/library/jRange/jquery.range-min.js @@ -1 +1 @@ -!function($,t,i,s){"use strict";var o=function(){return this.init.apply(this,arguments)};o.prototype={defaults:{onstatechange:function(){},isRange:!1,showLabels:!0,showScale:!0,step:1,format:"%s",theme:"theme-green",width:300,disable:!1},template:'<div class="slider-container"> <div class="back-bar"> <div class="selected-bar"></div> <div class="pointer low"></div><div class="pointer-label">123456</div> <div class="pointer high"></div><div class="pointer-label">456789</div> <div class="clickable-dummy"></div> </div> <div class="scale"></div> </div>',init:function(t,i){this.options=$.extend({},this.defaults,i),this.inputNode=$(t),this.options.value=this.inputNode.val()||(this.options.isRange?this.options.from+","+this.options.from:this.options.from),this.domNode=$(this.template),this.domNode.addClass(this.options.theme),this.inputNode.after(this.domNode),this.domNode.on("change",this.onChange),this.pointers=$(".pointer",this.domNode),this.lowPointer=this.pointers.first(),this.highPointer=this.pointers.last(),this.labels=$(".pointer-label",this.domNode),this.lowLabel=this.labels.first(),this.highLabel=this.labels.last(),this.scale=$(".scale",this.domNode),this.bar=$(".selected-bar",this.domNode),this.clickableBar=this.domNode.find(".clickable-dummy"),this.interval=this.options.to-this.options.from,this.render()},render:function(){return 0!==this.inputNode.width()||this.options.width?(this.domNode.width(this.options.width||this.inputNode.width()),this.inputNode.hide(),this.isSingle()&&(this.lowPointer.hide(),this.lowLabel.hide()),this.options.showLabels||this.labels.hide(),this.attachEvents(),this.options.showScale&&this.renderScale(),void this.setValue(this.options.value)):void console.log("jRange : no width found, returning")},isSingle:function(){return"number"==typeof this.options.value?!0:-1!==this.options.value.indexOf(",")||this.options.isRange?!1:!0},attachEvents:function(){this.clickableBar.click($.proxy(this.barClicked,this)),this.pointers.on("mousedown touchstart",$.proxy(this.onDragStart,this)),this.pointers.bind("dragstart",function(t){t.preventDefault()})},onDragStart:function(t){if(!(this.options.disable||"mousedown"===t.type&&1!==t.which)){t.stopPropagation(),t.preventDefault();var s=$(t.target);this.pointers.removeClass("last-active"),s.addClass("focused last-active"),this[(s.hasClass("low")?"low":"high")+"Label"].addClass("focused"),$(i).on("mousemove.slider touchmove.slider",$.proxy(this.onDrag,this,s)),$(i).on("mouseup.slider touchend.slider touchcancel.slider",$.proxy(this.onDragEnd,this))}},onDrag:function(t,i){i.stopPropagation(),i.preventDefault(),i.originalEvent.touches&&i.originalEvent.touches.length?i=i.originalEvent.touches[0]:i.originalEvent.changedTouches&&i.originalEvent.changedTouches.length&&(i=i.originalEvent.changedTouches[0]);var s=i.clientX-this.domNode.offset().left;this.domNode.trigger("change",[this,t,s])},onDragEnd:function(t){this.pointers.removeClass("focused"),this.labels.removeClass("focused"),$(i).off(".slider")},barClicked:function(t){if(!this.options.disable){var i=t.pageX-this.clickableBar.offset().left;if(this.isSingle())this.setPosition(this.pointers.last(),i,!0,!0);else{var s=Math.abs(parseInt(this.pointers.first().css("left"),10)-i+this.pointers.first().width()/2)<Math.abs(parseInt(this.pointers.last().css("left"),10)-i+this.pointers.first().width()/2)?this.pointers.first():this.pointers.last();this.setPosition(s,i,!0,!0)}}},onChange:function(t,i,s,o){var e,n;i.isSingle()?(e=0,n=i.domNode.width()):(e=s.hasClass("high")?i.lowPointer.position().left+i.lowPointer.width()/2:0,n=s.hasClass("low")?i.highPointer.position().left+i.highPointer.width()/2:i.domNode.width());var h=Math.min(Math.max(o,e),n);i.setPosition(s,h,!0)},setPosition:function(t,i,s,o){var e,n=this.lowPointer.position().left,h=this.highPointer.position().left,a=this.highPointer.width()/2;s||(i=this.prcToPx(i)),t[0]===this.highPointer[0]?h=Math.round(i-a):n=Math.round(i-a),t[o?"animate":"css"]({left:Math.round(i-a)}),e=this.isSingle()?0:n+a,this.bar[o?"animate":"css"]({width:Math.round(h+a-e),left:e}),this.showPointerValue(t,i,o),this.isReadonly()},setValue:function(t){var i=t.toString().split(",");this.options.value=t;var s=this.valuesToPrc(2===i.length?i:[0,i[0]]);this.isSingle()?this.setPosition(this.highPointer,s[1]):(this.setPosition(this.lowPointer,s[0]),this.setPosition(this.highPointer,s[1]))},renderScale:function(){for(var t=this.options.scale||[this.options.from,this.options.to],i=Math.round(100/(t.length-1)*10)/10,s="",o=0;o<t.length;o++)s+='<span style="left: '+o*i+'%">'+("|"!=t[o]?"<ins>"+t[o]+"</ins>":"")+"</span>";this.scale.html(s),$("ins",this.scale).each(function(){$(this).css({marginLeft:-$(this).outerWidth()/2})})},getBarWidth:function(){var t=this.options.value.split(",");return t.length>1?parseInt(t[1],10)-parseInt(t[0],10):parseInt(t[0],10)},showPointerValue:function(t,i,o){var e=$(".pointer-label",this.domNode)[t.hasClass("low")?"first":"last"](),n,h=this.positionToValue(i);if($.isFunction(this.options.format)){var a=this.isSingle()?s:t.hasClass("low")?"low":"high";n=this.options.format(h,a)}else n=this.options.format.replace("%s",h);var l=e.html(n).width(),r=i-l/2;r=Math.min(Math.max(r,0),this.options.width-l),e[o?"animate":"css"]({left:r}),this.setInputValue(t,h)},valuesToPrc:function(t){var i=100*(t[0]-this.options.from)/this.interval,s=100*(t[1]-this.options.from)/this.interval;return[i,s]},prcToPx:function(t){return this.domNode.width()*t/100},positionToValue:function(t){var i=t/this.domNode.width()*this.interval;return i+=this.options.from,Math.round(i/this.options.step)*this.options.step},setInputValue:function(t,i){if(this.isSingle())this.options.value=i.toString();else{var s=this.options.value.split(",");this.options.value=t.hasClass("low")?i+","+s[1]:s[0]+","+i}this.inputNode.val()!==this.options.value&&(this.inputNode.val(this.options.value),this.options.onstatechange.call(this,this.options.value))},getValue:function(){return this.options.value},isReadonly:function(){this.domNode.toggleClass("slider-readonly",this.options.disable)},disable:function(){this.options.disable=!0,this.isReadonly()},enable:function(){this.options.disable=!1,this.isReadonly()},toggleDisable:function(){this.options.disable=!this.options.disable,this.isReadonly()}};var e="jRange";$.fn[e]=function(i){var s=arguments,n;return this.each(function(){var h=$(this),a=$.data(this,"plugin_"+e),l="object"==typeof i&&i;a||(h.data("plugin_"+e,a=new o(this,l)),$(t).resize(function(){a.setValue(a.getValue())})),"string"==typeof i&&(n=a[i].apply(a,Array.prototype.slice.call(s,1)))}),n||this}}(jQuery,window,document);
\ No newline at end of file +!function($,t,i,s){"use strict";var o=function(){return this.init.apply(this,arguments)};o.prototype={defaults:{onstatechange:function(){},ondragend:function(){},onbarclicked:function(){},isRange:!1,showLabels:!0,showScale:!0,step:1,format:"%s",theme:"theme-green",width:300,disable:!1,snap:!1},template:'<div class="slider-container"> <div class="back-bar"> <div class="selected-bar"></div> <div class="pointer low"></div><div class="pointer-label low">123456</div> <div class="pointer high"></div><div class="pointer-label high">456789</div> <div class="clickable-dummy"></div> </div> <div class="scale"></div> </div>',init:function(t,i){this.options=$.extend({},this.defaults,i),this.inputNode=$(t),this.options.value=this.inputNode.val()||(this.options.isRange?this.options.from+","+this.options.from:""+this.options.from),this.domNode=$(this.template),this.domNode.addClass(this.options.theme),this.inputNode.after(this.domNode),this.domNode.on("change",this.onChange),this.pointers=$(".pointer",this.domNode),this.lowPointer=this.pointers.first(),this.highPointer=this.pointers.last(),this.labels=$(".pointer-label",this.domNode),this.lowLabel=this.labels.first(),this.highLabel=this.labels.last(),this.scale=$(".scale",this.domNode),this.bar=$(".selected-bar",this.domNode),this.clickableBar=this.domNode.find(".clickable-dummy"),this.interval=this.options.to-this.options.from,this.render()},render:function(){return 0!==this.inputNode.width()||this.options.width?(this.options.width=this.options.width||this.inputNode.width(),this.domNode.width(this.options.width),this.inputNode.hide(),this.isSingle()&&(this.lowPointer.hide(),this.lowLabel.hide()),this.options.showLabels||this.labels.hide(),this.attachEvents(),this.options.showScale&&this.renderScale(),void this.setValue(this.options.value)):void console.log("jRange : no width found, returning")},isSingle:function(){return"number"==typeof this.options.value?!0:-1===this.options.value.indexOf(",")&&!this.options.isRange},attachEvents:function(){this.clickableBar.click($.proxy(this.barClicked,this)),this.pointers.on("mousedown touchstart",$.proxy(this.onDragStart,this)),this.pointers.bind("dragstart",function(t){t.preventDefault()})},onDragStart:function(t){if(!(this.options.disable||"mousedown"===t.type&&1!==t.which)){t.stopPropagation(),t.preventDefault();var s=$(t.target);this.pointers.removeClass("last-active"),s.addClass("focused last-active"),this[(s.hasClass("low")?"low":"high")+"Label"].addClass("focused"),$(i).on("mousemove.slider touchmove.slider",$.proxy(this.onDrag,this,s)),$(i).on("mouseup.slider touchend.slider touchcancel.slider",$.proxy(this.onDragEnd,this))}},onDrag:function(t,i){i.stopPropagation(),i.preventDefault(),i.originalEvent.touches&&i.originalEvent.touches.length?i=i.originalEvent.touches[0]:i.originalEvent.changedTouches&&i.originalEvent.changedTouches.length&&(i=i.originalEvent.changedTouches[0]);var s=i.clientX-this.domNode.offset().left;this.domNode.trigger("change",[this,t,s])},onDragEnd:function(t){this.pointers.removeClass("focused").trigger("rangeslideend"),this.labels.removeClass("focused"),$(i).off(".slider"),this.options.ondragend.call(this,this.options.value)},barClicked:function(t){if(!this.options.disable){var i=t.pageX-this.clickableBar.offset().left;if(this.isSingle())this.setPosition(this.pointers.last(),i,!0,!0);else{var s=Math.abs(parseFloat(this.pointers.first().css("left"),10)),o=this.pointers.first().width()/2,e=Math.abs(parseFloat(this.pointers.last().css("left"),10)),n=this.pointers.first().width()/2,a=Math.abs(s-i+o),h=Math.abs(e-i+n),l;l=a==h?s>i?this.pointers.first():this.pointers.last():h>a?this.pointers.first():this.pointers.last(),this.setPosition(l,i,!0,!0)}this.options.onbarclicked.call(this,this.options.value)}},onChange:function(t,i,s,o){var e,n;e=0,n=i.domNode.width(),i.isSingle()||(e=s.hasClass("high")?parseFloat(i.lowPointer.css("left"))+i.lowPointer.width()/2:0,n=s.hasClass("low")?parseFloat(i.highPointer.css("left"))+i.highPointer.width()/2:i.domNode.width());var a=Math.min(Math.max(o,e),n);i.setPosition(s,a,!0)},setPosition:function(t,i,s,o){var e,n,a=parseFloat(this.lowPointer.css("left")),h=parseFloat(this.highPointer.css("left"))||0,l=this.highPointer.width()/2;if(s||(i=this.prcToPx(i)),this.options.snap){var r=this.correctPositionForSnap(i);if(-1===r)return;i=r}t[0]===this.highPointer[0]?h=Math.round(i-l):a=Math.round(i-l),t[o?"animate":"css"]({left:Math.round(i-l)}),this.isSingle()?e=0:(e=a+l,n=h+l);var d=Math.round(h+l-e);this.bar[o?"animate":"css"]({width:Math.abs(d),left:d>0?e:e+d}),this.showPointerValue(t,i,o),this.isReadonly()},correctPositionForSnap:function(t){var i=this.positionToValue(t)-this.options.from,s=this.options.width/(this.interval/this.options.step),o=i/this.options.step*s;return o+s/2>=t&&t>=o-s/2?o:-1},setValue:function(t){var i=t.toString().split(",");i[0]=Math.min(Math.max(i[0],this.options.from),this.options.to)+"",i.length>1&&(i[1]=Math.min(Math.max(i[1],this.options.from),this.options.to)+""),this.options.value=t;var s=this.valuesToPrc(2===i.length?i:[0,i[0]]);this.isSingle()?this.setPosition(this.highPointer,s[1]):(this.setPosition(this.lowPointer,s[0]),this.setPosition(this.highPointer,s[1]))},renderScale:function(){for(var t=this.options.scale||[this.options.from,this.options.to],i=Math.round(100/(t.length-1)*10)/10,s="",o=0;o<t.length;o++)s+='<span style="left: '+o*i+'%">'+("|"!=t[o]?"<ins>"+t[o]+"</ins>":"")+"</span>";this.scale.html(s),$("ins",this.scale).each(function(){$(this).css({marginLeft:-$(this).outerWidth()/2})})},getBarWidth:function(){var t=this.options.value.split(",");return t.length>1?parseFloat(t[1])-parseFloat(t[0]):parseFloat(t[0])},showPointerValue:function(t,i,o){var e=$(".pointer-label",this.domNode)[t.hasClass("low")?"first":"last"](),n,a=this.positionToValue(i);if($.isFunction(this.options.format)){var h=this.isSingle()?s:t.hasClass("low")?"low":"high";n=this.options.format(a,h)}else n=this.options.format.replace("%s",a);var l=e.html(n).width(),r=i-l/2;r=Math.min(Math.max(r,0),this.options.width-l),e[o?"animate":"css"]({left:r}),this.setInputValue(t,a)},valuesToPrc:function(t){var i=100*(parseFloat(t[0])-parseFloat(this.options.from))/this.interval,s=100*(parseFloat(t[1])-parseFloat(this.options.from))/this.interval;return[i,s]},prcToPx:function(t){return this.domNode.width()*t/100},isDecimal:function(){return-1!==(this.options.value+this.options.from+this.options.to).indexOf(".")},positionToValue:function(t){var i=t/this.domNode.width()*this.interval;if(i=parseFloat(i,10)+parseFloat(this.options.from,10),this.isDecimal()){var s=Math.round(Math.round(i/this.options.step)*this.options.step*100)/100;if(0!==s)for(s=""+s,-1===s.indexOf(".")&&(s+=".");s.length-s.indexOf(".")<3;)s+="0";else s="0.00";return s}return Math.round(i/this.options.step)*this.options.step},setInputValue:function(t,i){if(this.isSingle())this.options.value=i.toString();else{var s=this.options.value.split(",");t.hasClass("low")?this.options.value=i+","+s[1]:this.options.value=s[0]+","+i}this.inputNode.val()!==this.options.value&&(this.inputNode.val(this.options.value).trigger("change"),this.options.onstatechange.call(this,this.options.value))},getValue:function(){return this.options.value},getOptions:function(){return this.options},getRange:function(){return this.options.from+","+this.options.to},isReadonly:function(){this.domNode.toggleClass("slider-readonly",this.options.disable)},disable:function(){this.options.disable=!0,this.isReadonly()},enable:function(){this.options.disable=!1,this.isReadonly()},toggleDisable:function(){this.options.disable=!this.options.disable,this.isReadonly()},updateRange:function(t,i){var s=t.toString().split(",");this.interval=parseInt(s[1])-parseInt(s[0]),i?this.setValue(i):this.setValue(this.getValue())}};var e="jRange";$.fn[e]=function(i){var s=arguments,n;return this.each(function(){var a=$(this),h=$.data(this,"plugin_"+e),l="object"==typeof i&&i;h||(a.data("plugin_"+e,h=new o(this,l)),$(t).resize(function(){h.setValue(h.getValue())})),"string"==typeof i&&(n=h[i].apply(h,Array.prototype.slice.call(s,1)))}),n||this}}(jQuery,window,document);
\ No newline at end of file diff --git a/library/jRange/jquery.range.js b/library/jRange/jquery.range.js index 860b74511..924dc8d41 100644 --- a/library/jRange/jquery.range.js +++ b/library/jRange/jquery.range.js @@ -27,6 +27,8 @@ jRange.prototype = { defaults: { onstatechange: function() {}, + ondragend: function() {}, + onbarclicked: function() {}, isRange: false, showLabels: true, showScale: true, @@ -34,13 +36,14 @@ format: '%s', theme: 'theme-green', width: 300, - disable: false + disable: false, + snap: false }, template: '<div class="slider-container">\ <div class="back-bar">\ <div class="selected-bar"></div>\ - <div class="pointer low"></div><div class="pointer-label">123456</div>\ - <div class="pointer high"></div><div class="pointer-label">456789</div>\ + <div class="pointer low"></div><div class="pointer-label low">123456</div>\ + <div class="pointer high"></div><div class="pointer-label high">456789</div>\ <div class="clickable-dummy"></div>\ </div>\ <div class="scale"></div>\ @@ -48,7 +51,7 @@ init: function(node, options) { this.options = $.extend({}, this.defaults, options); this.inputNode = $(node); - this.options.value = this.inputNode.val() || (this.options.isRange ? this.options.from + ',' + this.options.from : this.options.from); + this.options.value = this.inputNode.val() || (this.options.isRange ? this.options.from + ',' + this.options.from : '' + this.options.from); this.domNode = $(this.template); this.domNode.addClass(this.options.theme); this.inputNode.after(this.domNode); @@ -71,7 +74,8 @@ console.log('jRange : no width found, returning'); return; } else { - this.domNode.width(this.options.width || this.inputNode.width()); + this.options.width = this.options.width || this.inputNode.width(); + this.domNode.width(this.options.width); this.inputNode.hide(); } @@ -129,9 +133,11 @@ this.domNode.trigger('change', [this, pointer, position]); }, onDragEnd: function(e) { - this.pointers.removeClass('focused'); + this.pointers.removeClass('focused') + .trigger('rangeslideend'); this.labels.removeClass('focused'); $(document).off('.slider'); + this.options.ondragend.call(this, this.options.value); }, barClicked: function(e) { if(this.options.disable) return; @@ -139,31 +145,52 @@ if (this.isSingle()) this.setPosition(this.pointers.last(), x, true, true); else { - var pointer = Math.abs(parseInt(this.pointers.first().css('left'), 10) - x + this.pointers.first().width() / 2) < Math.abs(parseInt(this.pointers.last().css('left'), 10) - x + this.pointers.first().width() / 2) ? - this.pointers.first() : this.pointers.last(); + var firstLeft = Math.abs(parseFloat(this.pointers.first().css('left'), 10)), + firstHalfWidth = this.pointers.first().width() / 2, + lastLeft = Math.abs(parseFloat(this.pointers.last().css('left'), 10)), + lastHalfWidth = this.pointers.first().width() / 2, + leftSide = Math.abs(firstLeft - x + firstHalfWidth), + rightSide = Math.abs(lastLeft - x + lastHalfWidth), + pointer; + + if(leftSide == rightSide) { + pointer = x < firstLeft ? this.pointers.first() : this.pointers.last(); + } else { + pointer = leftSide < rightSide ? this.pointers.first() : this.pointers.last(); + } this.setPosition(pointer, x, true, true); } + this.options.onbarclicked.call(this, this.options.value); }, onChange: function(e, self, pointer, position) { var min, max; - if (self.isSingle()) { - min = 0; - max = self.domNode.width(); - } else { - min = pointer.hasClass('high') ? self.lowPointer.position().left + self.lowPointer.width() / 2 : 0; - max = pointer.hasClass('low') ? self.highPointer.position().left + self.highPointer.width() / 2 : self.domNode.width(); + min = 0; + max = self.domNode.width(); + + if (!self.isSingle()) { + min = pointer.hasClass('high') ? parseFloat(self.lowPointer.css("left")) + (self.lowPointer.width() / 2) : 0; + max = pointer.hasClass('low') ? parseFloat(self.highPointer.css("left")) + (self.highPointer.width() / 2) : self.domNode.width(); } + var value = Math.min(Math.max(position, min), max); self.setPosition(pointer, value, true); }, setPosition: function(pointer, position, isPx, animate) { - var leftPos, - lowPos = this.lowPointer.position().left, - highPos = this.highPointer.position().left, + var leftPos, rightPos, + lowPos = parseFloat(this.lowPointer.css("left")), + highPos = parseFloat(this.highPointer.css("left")) || 0, circleWidth = this.highPointer.width() / 2; if (!isPx) { position = this.prcToPx(position); } + if(this.options.snap){ + var expPos = this.correctPositionForSnap(position); + if(expPos === -1){ + return; + }else{ + position = expPos; + } + } if (pointer[0] === this.highPointer[0]) { highPos = Math.round(position - circleWidth); } else { @@ -176,17 +203,33 @@ leftPos = 0; } else { leftPos = lowPos + circleWidth; + rightPos = highPos + circleWidth; } + var w = Math.round(highPos + circleWidth - leftPos); this.bar[animate ? 'animate' : 'css']({ - 'width': Math.round(highPos + circleWidth - leftPos), - 'left': leftPos + 'width': Math.abs(w), + 'left': (w>0) ? leftPos : leftPos + w }); this.showPointerValue(pointer, position, animate); this.isReadonly(); }, + correctPositionForSnap: function(position){ + var currentValue = this.positionToValue(position) - this.options.from; + var diff = this.options.width / (this.interval / this.options.step), + expectedPosition = (currentValue / this.options.step) * diff; + if( position <= expectedPosition + diff / 2 && position >= expectedPosition - diff / 2){ + return expectedPosition; + }else{ + return -1; + } + }, // will be called from outside setValue: function(value) { var values = value.toString().split(','); + values[0] = Math.min(Math.max(values[0], this.options.from), this.options.to) + ''; + if (values.length > 1){ + values[1] = Math.min(Math.max(values[1], this.options.from), this.options.to) + ''; + } this.options.value = value; var prc = this.valuesToPrc(values.length === 2 ? values : [0, values[0]]); if (this.isSingle()) { @@ -214,15 +257,17 @@ getBarWidth: function() { var values = this.options.value.split(','); if (values.length > 1) { - return parseInt(values[1], 10) - parseInt(values[0], 10); + return parseFloat(values[1]) - parseFloat(values[0]); } else { - return parseInt(values[0], 10); + return parseFloat(values[0]); } }, showPointerValue: function(pointer, position, animate) { var label = $('.pointer-label', this.domNode)[pointer.hasClass('low') ? 'first' : 'last'](); var text; var value = this.positionToValue(position); + // Is it higer or lower than it should be? + if ($.isFunction(this.options.format)) { var type = this.isSingle() ? undefined : (pointer.hasClass('low') ? 'low' : 'high'); text = this.options.format(value, type); @@ -239,17 +284,36 @@ this.setInputValue(pointer, value); }, valuesToPrc: function(values) { - var lowPrc = ((values[0] - this.options.from) * 100 / this.interval), - highPrc = ((values[1] - this.options.from) * 100 / this.interval); + var lowPrc = ((parseFloat(values[0]) - parseFloat(this.options.from)) * 100 / this.interval), + highPrc = ((parseFloat(values[1]) - parseFloat(this.options.from)) * 100 / this.interval); return [lowPrc, highPrc]; }, prcToPx: function(prc) { return (this.domNode.width() * prc) / 100; }, + isDecimal: function() { + return ((this.options.value + this.options.from + this.options.to).indexOf(".")===-1) ? false : true; + }, positionToValue: function(pos) { var value = (pos / this.domNode.width()) * this.interval; - value = value + this.options.from; - return Math.round(value / this.options.step) * this.options.step; + value = parseFloat(value, 10) + parseFloat(this.options.from, 10); + if (this.isDecimal()) { + var final = Math.round(Math.round(value / this.options.step) * this.options.step *100)/100; + if (final!==0.0) { + final = '' + final; + if (final.indexOf(".")===-1) { + final = final + "."; + } + while (final.length - final.indexOf('.')<3) { + final = final + "0"; + } + } else { + final = "0.00"; + } + return final; + } else { + return Math.round(value / this.options.step) * this.options.step; + } }, setInputValue: function(pointer, v) { // if(!isChanged) return; @@ -264,13 +328,20 @@ } } if (this.inputNode.val() !== this.options.value) { - this.inputNode.val(this.options.value); + this.inputNode.val(this.options.value) + .trigger('change'); this.options.onstatechange.call(this, this.options.value); } }, getValue: function() { return this.options.value; }, + getOptions: function() { + return this.options; + }, + getRange: function() { + return this.options.from + "," + this.options.to; + }, isReadonly: function(){ this.domNode.toggleClass('slider-readonly', this.options.disable); }, @@ -285,23 +356,18 @@ toggleDisable: function(){ this.options.disable = !this.options.disable; this.isReadonly(); + }, + updateRange: function(range, value) { + var values = range.toString().split(','); + this.interval = parseInt(values[1]) - parseInt(values[0]); + if(value){ + this.setValue(value); + }else{ + this.setValue(this.getValue()); + } } }; - /*$.jRange = function (node, options) { - var jNode = $(node); - if(!jNode.data('jrange')){ - jNode.data('jrange', new jRange(node, options)); - } - return jNode.data('jrange'); - }; - - $.fn.jRange = function (options) { - return this.each(function(){ - $.jRange(this, options); - }); - };*/ - var pluginName = 'jRange'; // A really lightweight plugin wrapper around the constructor, // preventing against multiple instantiations |