Skip to content

feat(enforceStep): Add an enforceStep option (defaults to true) #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ The default options are:
selectionBarColor: null,
keyboardSupport: true,
scale: 1,
enforceStep: true,
enforceRange: false,
noSwitching: false,
onlyBindHandles: false,
Expand Down Expand Up @@ -258,6 +259,8 @@ $scope.slider = {

**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.

**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.

**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.

**noSwitching** - _Boolean (defaults to false)_: Set to true to prevent to user from switching the min and max handles. *Applies to range slider only.*
Expand Down
2 changes: 1 addition & 1 deletion dist/rzslider.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*! angularjs-slider - v2.6.0 -
(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 -
2016-01-31 */
2016-02-06 */
rzslider {
position: relative;
display: inline-block;
Expand Down
11 changes: 7 additions & 4 deletions dist/rzslider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*! angularjs-slider - v2.6.0 -
(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 -
2016-02-03 */
2016-02-06 */
/*jslint unparam: true */
/*global angular: false, console: false, define, module */
(function(root, factory) {
Expand Down Expand Up @@ -52,6 +52,7 @@
selectionBarColor: null,
keyboardSupport: true,
scale: 1,
enforceStep: true,
enforceRange: false,
noSwitching: false,
onlyBindHandles: false,
Expand Down Expand Up @@ -636,9 +637,11 @@

this.minValue = this.options.floor;

this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel);
if (this.range)
this.scope.rzSliderHigh = this.roundStep(this.scope.rzSliderHigh);
if (this.options.enforceStep) {
this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel);
if (this.range)
this.scope.rzSliderHigh = this.roundStep(this.scope.rzSliderHigh);
}

if (this.options.ceil != null)
this.maxValue = this.options.ceil;
Expand Down
2 changes: 1 addition & 1 deletion dist/rzslider.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/rzslider.min.js

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions src/rzslider.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
selectionBarColor: null,
keyboardSupport: true,
scale: 1,
enforceStep: true,
enforceRange: false,
noSwitching: false,
onlyBindHandles: false,
Expand Down Expand Up @@ -640,9 +641,11 @@

this.minValue = this.options.floor;

this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel);
if (this.range)
this.scope.rzSliderHigh = this.roundStep(this.scope.rzSliderHigh);
if (this.options.enforceStep) {
this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel);
if (this.range)
this.scope.rzSliderHigh = this.roundStep(this.scope.rzSliderHigh);
}

if (this.options.ceil != null)
this.maxValue = this.options.ceil;
Expand Down
22 changes: 20 additions & 2 deletions tests/spec/rz-slider-service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ describe('rzslider - ', function() {
slider.resetSlider.called.should.be.true;
});

it('should round the model value to the step', function() {
it('should round the model value to the step by default', function() {
scope.slider.value = 54;
scope.$digest();
expect(scope.slider.value).to.equal(50);

scope.slider.value = 55;
scope.$digest();
$timeout.flush(); //to flush the throttle function
$timeout.flush(); //to flush the throttle function since we modify twice in a row
expect(scope.slider.value).to.equal(60);
});

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

it('should not round value to step if enforceStep is false', function() {
scope.slider.options.enforceStep = false;
scope.$digest();

scope.slider.value = 14;
scope.$digest();
expect(scope.slider.value).to.equal(14);
});

it('should round value to step if enforceStep is true', function() {
scope.slider.options.enforceStep = true;
scope.$digest();

scope.slider.value = 14;
scope.$digest();
expect(scope.slider.value).to.equal(10);
});

it('should set the showTicks scope flag to true when showTicks is true', function() {
scope.slider.options.showTicks = true;
scope.$digest();
Expand Down