Skip to content

Commit f24acb8

Browse files
author
Valentin Hervieu
committed
feat(enforceStep): Add an enforceStep option (defaults to true)
1 parent dc0217e commit f24acb8

File tree

7 files changed

+40
-13
lines changed

7 files changed

+40
-13
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ The default options are:
184184
selectionBarColor: null,
185185
keyboardSupport: true,
186186
scale: 1,
187+
enforceStep: true,
187188
enforceRange: false,
188189
noSwitching: false,
189190
onlyBindHandles: false,
@@ -258,6 +259,8 @@ $scope.slider = {
258259

259260
**scale** - _Number (defaults to 1)_: If you display the slider in an element that uses `transform: scale(0.5)`, set the `scale` value to 2 so that the slider is rendered properly and the events are handled correctly.
260261

262+
**enforceStep** - _Boolean (defaults to true)_: Set to true to force the value to be rounded to the step, even when modified from the outside.. When set to false, if the model values are modified from outside the slider, they are not rounded and can be between two steps.
263+
261264
**enforceRange** - _Boolean (defaults to false)_: Set to true to round the `rzSliderModel` and `rzSliderHigh` to the slider range even when modified from outside the slider. When set to false, if the model values are modified from outside the slider, they are not rounded but they are still rendered properly on the slider.
262265

263266
**noSwitching** - _Boolean (defaults to false)_: Set to true to prevent to user from switching the min and max handles. *Applies to range slider only.*

dist/rzslider.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*! angularjs-slider - v2.6.0 -
22
(c) Rafal Zajac <rzajac@gmail.com>, Valentin Hervieu <valentin@hervieu.me>, Jussi Saarivirta <jusasi@gmail.com>, Angelin Sirbu <angelin.sirbu@gmail.com> -
33
https://github.com/angular-slider/angularjs-slider -
4-
2016-01-31 */
4+
2016-02-06 */
55
rzslider {
66
position: relative;
77
display: inline-block;

dist/rzslider.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*! angularjs-slider - v2.6.0 -
22
(c) Rafal Zajac <rzajac@gmail.com>, Valentin Hervieu <valentin@hervieu.me>, Jussi Saarivirta <jusasi@gmail.com>, Angelin Sirbu <angelin.sirbu@gmail.com> -
33
https://github.com/angular-slider/angularjs-slider -
4-
2016-02-03 */
4+
2016-02-06 */
55
/*jslint unparam: true */
66
/*global angular: false, console: false, define, module */
77
(function(root, factory) {
@@ -52,6 +52,7 @@
5252
selectionBarColor: null,
5353
keyboardSupport: true,
5454
scale: 1,
55+
enforceStep: true,
5556
enforceRange: false,
5657
noSwitching: false,
5758
onlyBindHandles: false,
@@ -636,9 +637,11 @@
636637

637638
this.minValue = this.options.floor;
638639

639-
this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel);
640-
if (this.range)
641-
this.scope.rzSliderHigh = this.roundStep(this.scope.rzSliderHigh);
640+
if (this.options.enforceStep) {
641+
this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel);
642+
if (this.range)
643+
this.scope.rzSliderHigh = this.roundStep(this.scope.rzSliderHigh);
644+
}
642645

643646
if (this.options.ceil != null)
644647
this.maxValue = this.options.ceil;

dist/rzslider.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rzslider.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rzslider.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
selectionBarColor: null,
5757
keyboardSupport: true,
5858
scale: 1,
59+
enforceStep: true,
5960
enforceRange: false,
6061
noSwitching: false,
6162
onlyBindHandles: false,
@@ -640,9 +641,11 @@
640641

641642
this.minValue = this.options.floor;
642643

643-
this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel);
644-
if (this.range)
645-
this.scope.rzSliderHigh = this.roundStep(this.scope.rzSliderHigh);
644+
if (this.options.enforceStep) {
645+
this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel);
646+
if (this.range)
647+
this.scope.rzSliderHigh = this.roundStep(this.scope.rzSliderHigh);
648+
}
646649

647650
if (this.options.ceil != null)
648651
this.maxValue = this.options.ceil;

tests/spec/rz-slider-service-test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ describe('rzslider - ', function() {
104104
slider.resetSlider.called.should.be.true;
105105
});
106106

107-
it('should round the model value to the step', function() {
107+
it('should round the model value to the step by default', function() {
108108
scope.slider.value = 54;
109109
scope.$digest();
110110
expect(scope.slider.value).to.equal(50);
111111

112112
scope.slider.value = 55;
113113
scope.$digest();
114-
$timeout.flush(); //to flush the throttle function
114+
$timeout.flush(); //to flush the throttle function since we modify twice in a row
115115
expect(scope.slider.value).to.equal(60);
116116
});
117117

@@ -317,6 +317,24 @@ describe('rzslider - ', function() {
317317
expect(slider.options.step).to.equal(1);
318318
});
319319

320+
it('should not round value to step if enforceStep is false', function() {
321+
scope.slider.options.enforceStep = false;
322+
scope.$digest();
323+
324+
scope.slider.value = 14;
325+
scope.$digest();
326+
expect(scope.slider.value).to.equal(14);
327+
});
328+
329+
it('should round value to step if enforceStep is true', function() {
330+
scope.slider.options.enforceStep = true;
331+
scope.$digest();
332+
333+
scope.slider.value = 14;
334+
scope.$digest();
335+
expect(scope.slider.value).to.equal(10);
336+
});
337+
320338
it('should set the showTicks scope flag to true when showTicks is true', function() {
321339
scope.slider.options.showTicks = true;
322340
scope.$digest();

0 commit comments

Comments
 (0)