Commit 5d130f09 authored by Valentin Hervieu's avatar Valentin Hervieu

fix(roundStep): Fix the roundStep internal function that was too strict.

I was not accepting sliders with floor=7, ceil=7 and step=10 that are actually valid.
parent 56a1763c
/*! angularjs-slider - v2.5.0 - /*! angularjs-slider - v2.5.0 -
(c) Rafal Zajac <rzajac@gmail.com>, Valentin Hervieu <valentin@hervieu.me>, Jussi Saarivirta <jusasi@gmail.com>, Angelin Sirbu <angelin.sirbu@gmail.com> - (c) Rafal Zajac <rzajac@gmail.com>, Valentin Hervieu <valentin@hervieu.me>, Jussi Saarivirta <jusasi@gmail.com>, Angelin Sirbu <angelin.sirbu@gmail.com> -
https://github.com/angular-slider/angularjs-slider - https://github.com/angular-slider/angularjs-slider -
2016-01-24 */ 2016-01-31 */
/*jslint unparam: true */ /*jslint unparam: true */
/*global angular: false, console: false, define, module */ /*global angular: false, console: false, define, module */
(function(root, factory) { (function(root, factory) {
...@@ -631,14 +631,14 @@ ...@@ -631,14 +631,14 @@
this.step = +this.options.step; this.step = +this.options.step;
this.precision = +this.options.precision; this.precision = +this.options.precision;
this.minValue = this.options.floor;
this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel); this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel);
if (this.range) if (this.range)
this.scope.rzSliderHigh = this.roundStep(this.scope.rzSliderHigh); this.scope.rzSliderHigh = this.roundStep(this.scope.rzSliderHigh);
this.minValue = this.roundStep(+this.options.floor);
if (this.options.ceil != null) if (this.options.ceil != null)
this.maxValue = this.roundStep(+this.options.ceil); this.maxValue = this.options.ceil;
else else
this.maxValue = this.options.ceil = this.range ? this.scope.rzSliderHigh : this.scope.rzSliderModel; this.maxValue = this.options.ceil = this.range ? this.scope.rzSliderHigh : this.scope.rzSliderModel;
...@@ -798,7 +798,7 @@ ...@@ -798,7 +798,7 @@
callOnStart: function() { callOnStart: function() {
if (this.options.onStart) { if (this.options.onStart) {
var self = this; var self = this;
this.scope.$evalAsync(function () { this.scope.$evalAsync(function() {
self.options.onStart(self.options.id, self.scope.rzSliderModel, self.scope.rzSliderHigh); self.options.onStart(self.options.id, self.scope.rzSliderModel, self.scope.rzSliderHigh);
}); });
} }
...@@ -813,7 +813,7 @@ ...@@ -813,7 +813,7 @@
callOnChange: function() { callOnChange: function() {
if (this.options.onChange) { if (this.options.onChange) {
var self = this; var self = this;
this.scope.$evalAsync(function () { this.scope.$evalAsync(function() {
self.options.onChange(self.options.id, self.scope.rzSliderModel, self.scope.rzSliderHigh); self.options.onChange(self.options.id, self.scope.rzSliderModel, self.scope.rzSliderHigh);
}); });
} }
...@@ -828,7 +828,7 @@ ...@@ -828,7 +828,7 @@
callOnEnd: function() { callOnEnd: function() {
if (this.options.onEnd) { if (this.options.onEnd) {
var self = this; var self = this;
this.scope.$evalAsync(function () { this.scope.$evalAsync(function() {
self.options.onEnd(self.options.id, self.scope.rzSliderModel, self.scope.rzSliderHigh); self.options.onEnd(self.options.id, self.scope.rzSliderModel, self.scope.rzSliderHigh);
}); });
} }
...@@ -993,16 +993,16 @@ ...@@ -993,16 +993,16 @@
}, },
/** /**
* Round value to step and precision * Round value to step and precision based on minValue
* *
* @param {number} value * @param {number} value
* @returns {number} * @returns {number}
*/ */
roundStep: function(value) { roundStep: function(value) {
var steppedValue = parseFloat(value / this.step).toPrecision(12) var steppedDifference = parseFloat((value - this.minValue) / this.step).toPrecision(12);
steppedValue = Math.round(steppedValue) * this.step; steppedDifference = Math.round(+steppedDifference) * this.step;
steppedValue = steppedValue.toFixed(this.precision); var newValue = (this.minValue + (+steppedDifference)).toFixed(this.precision);
return +steppedValue; return +newValue;
}, },
/** /**
......
This diff is collapsed.
...@@ -635,14 +635,14 @@ ...@@ -635,14 +635,14 @@
this.step = +this.options.step; this.step = +this.options.step;
this.precision = +this.options.precision; this.precision = +this.options.precision;
this.minValue = this.options.floor;
this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel); this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel);
if (this.range) if (this.range)
this.scope.rzSliderHigh = this.roundStep(this.scope.rzSliderHigh); this.scope.rzSliderHigh = this.roundStep(this.scope.rzSliderHigh);
this.minValue = this.roundStep(+this.options.floor);
if (this.options.ceil != null) if (this.options.ceil != null)
this.maxValue = this.roundStep(+this.options.ceil); this.maxValue = this.options.ceil;
else else
this.maxValue = this.options.ceil = this.range ? this.scope.rzSliderHigh : this.scope.rzSliderModel; this.maxValue = this.options.ceil = this.range ? this.scope.rzSliderHigh : this.scope.rzSliderModel;
...@@ -802,7 +802,7 @@ ...@@ -802,7 +802,7 @@
callOnStart: function() { callOnStart: function() {
if (this.options.onStart) { if (this.options.onStart) {
var self = this; var self = this;
this.scope.$evalAsync(function () { this.scope.$evalAsync(function() {
self.options.onStart(self.options.id, self.scope.rzSliderModel, self.scope.rzSliderHigh); self.options.onStart(self.options.id, self.scope.rzSliderModel, self.scope.rzSliderHigh);
}); });
} }
...@@ -817,7 +817,7 @@ ...@@ -817,7 +817,7 @@
callOnChange: function() { callOnChange: function() {
if (this.options.onChange) { if (this.options.onChange) {
var self = this; var self = this;
this.scope.$evalAsync(function () { this.scope.$evalAsync(function() {
self.options.onChange(self.options.id, self.scope.rzSliderModel, self.scope.rzSliderHigh); self.options.onChange(self.options.id, self.scope.rzSliderModel, self.scope.rzSliderHigh);
}); });
} }
...@@ -832,7 +832,7 @@ ...@@ -832,7 +832,7 @@
callOnEnd: function() { callOnEnd: function() {
if (this.options.onEnd) { if (this.options.onEnd) {
var self = this; var self = this;
this.scope.$evalAsync(function () { this.scope.$evalAsync(function() {
self.options.onEnd(self.options.id, self.scope.rzSliderModel, self.scope.rzSliderHigh); self.options.onEnd(self.options.id, self.scope.rzSliderModel, self.scope.rzSliderHigh);
}); });
} }
...@@ -997,16 +997,16 @@ ...@@ -997,16 +997,16 @@
}, },
/** /**
* Round value to step and precision * Round value to step and precision based on minValue
* *
* @param {number} value * @param {number} value
* @returns {number} * @returns {number}
*/ */
roundStep: function(value) { roundStep: function(value) {
var steppedValue = parseFloat(value / this.step).toPrecision(12) var steppedDifference = parseFloat((value - this.minValue) / this.step).toPrecision(12);
steppedValue = Math.round(steppedValue) * this.step; steppedDifference = Math.round(+steppedDifference) * this.step;
steppedValue = steppedValue.toFixed(this.precision); var newValue = (this.minValue + (+steppedDifference)).toFixed(this.precision);
return +steppedValue; return +newValue;
}, },
/** /**
......
...@@ -968,7 +968,7 @@ describe('rzslider - ', function() { ...@@ -968,7 +968,7 @@ describe('rzslider - ', function() {
createSlider(sliderConf); createSlider(sliderConf);
}); });
it('should have a valid roundStep for integer values', function() { it('should have a valid roundStep for integer values when floor is 0', function() {
expect(slider.roundStep(10)).to.equal(10); expect(slider.roundStep(10)).to.equal(10);
expect(slider.roundStep(9)).to.equal(10); expect(slider.roundStep(9)).to.equal(10);
expect(slider.roundStep(11)).to.equal(10); expect(slider.roundStep(11)).to.equal(10);
...@@ -982,7 +982,33 @@ describe('rzslider - ', function() { ...@@ -982,7 +982,33 @@ describe('rzslider - ', function() {
expect(slider.roundStep(-14)).to.equal(-10); expect(slider.roundStep(-14)).to.equal(-10);
}); });
it('should have a valid roundStep for floating values', function() { it('should have a valid roundStep for integer values when floor is above 0', function() {
scope.slider.options.floor = 3;
scope.slider.options.ceil = 103;
scope.$digest();
expect(slider.roundStep(3)).to.equal(3);
expect(slider.roundStep(13)).to.equal(13);
expect(slider.roundStep(12)).to.equal(13);
expect(slider.roundStep(14)).to.equal(13);
expect(slider.roundStep(18)).to.equal(23);
expect(slider.roundStep(17)).to.equal(13);
});
it('should have a valid roundStep for integer values when floor is below 0', function() {
scope.slider.options.floor = -25;
scope.$digest();
expect(slider.roundStep(-5)).to.equal(-5);
expect(slider.roundStep(-15)).to.equal(-15);
expect(slider.roundStep(-16)).to.equal(-15);
expect(slider.roundStep(-14)).to.equal(-15);
expect(slider.roundStep(-21)).to.equal(-25);
expect(slider.roundStep(-20)).to.equal(-15);
expect(slider.roundStep(-19)).to.equal(-15);
});
it('should have a valid roundStep for floating values when floor is 0', function() {
scope.slider.options.precision = 1; scope.slider.options.precision = 1;
scope.slider.options.step = 0.1; scope.slider.options.step = 0.1;
scope.$digest(); scope.$digest();
...@@ -1003,6 +1029,39 @@ describe('rzslider - ', function() { ...@@ -1003,6 +1029,39 @@ describe('rzslider - ', function() {
expect(slider.roundStep(-1.14)).to.equal(-1.1); expect(slider.roundStep(-1.14)).to.equal(-1.1);
}); });
it('should have a valid roundStep for floating values when floor is above 0', function() {
scope.slider.options.floor = 3;
scope.slider.options.ceil = 103;
scope.slider.options.precision = 1;
scope.slider.options.step = 0.1;
scope.$digest();
expect(slider.roundStep(3)).to.equal(3);
expect(slider.roundStep(13)).to.equal(13);
expect(slider.roundStep(1.1)).to.equal(1.1);
expect(slider.roundStep(1.09)).to.equal(1.1);
expect(slider.roundStep(1.11)).to.equal(1.1);
expect(slider.roundStep(1.15)).to.equal(1.2);
expect(slider.roundStep(1.14)).to.equal(1.1);
});
it('should have a valid roundStep for floating values when floor is below 0', function() {
scope.slider.options.floor = -25;
scope.slider.options.ceil = 75;
scope.slider.options.precision = 1;
scope.slider.options.step = 0.1;
scope.$digest();
expect(slider.roundStep(-25)).to.equal(-25);
expect(slider.roundStep(-5)).to.equal(-5);
expect(slider.roundStep(-1.1)).to.equal(-1.1);
expect(slider.roundStep(-1.09)).to.equal(-1.1);
expect(slider.roundStep(-1.11)).to.equal(-1.1);
expect(slider.roundStep(-1.16)).to.equal(-1.2);
expect(slider.roundStep(-1.15)).to.equal(-1.1);
expect(slider.roundStep(-1.14)).to.equal(-1.1);
});
it('should have a valid hideEl', function() { it('should have a valid hideEl', function() {
var el = angular.element('<div></div>'); var el = angular.element('<div></div>');
slider.hideEl(el); slider.hideEl(el);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment