Skip to content

Commit 1e5a483

Browse files
author
Valentin Hervieu
committed
feat(range): Add a minRange option
The minimum range authorized on the slider. Closes #231
1 parent 171aacc commit 1e5a483

File tree

9 files changed

+87
-22
lines changed

9 files changed

+87
-22
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.5.0 (not-released)
2+
## Features
3+
- Add a `minRange` option to set a minimal range (#231).
4+
15
# 2.4.1 (2016-01-15)
26
## Performance improvements
37
- Remove the $timeout call in the init method (#223).

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ The default options are:
157157
ceil: null, //defaults to rz-slider-model
158158
step: 1,
159159
precision: 0,
160+
minRange: 0,
160161
id: null,
161162
translate: null,
162163
stepsArray: null,
@@ -192,6 +193,8 @@ The default options are:
192193

193194
**precision** - _Number (defaults to 0)_: The precision to display values with. The `toFixed()` is used internally for this.
194195

196+
**minRange** - _Number (defaults to 0)_: The minimum range authorized on the slider.
197+
195198
**translate** - _Function(value, sliderId)_: Custom translate function. Use this if you want to translate values displayed on the slider. For example if you want to display dollar amounts instead of just numbers:
196199
```html
197200
<div>

demo/demo.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,37 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) {
66
value: 10
77
};
88

9+
//Range slider config
10+
$scope.rangeSlider = {
11+
minValue: 10,
12+
maxValue: 90,
13+
options: {
14+
floor: 0,
15+
ceil: 100,
16+
step: 1
17+
}
18+
};
19+
20+
//Range slider with minRange config
21+
$scope.minRangeSlider = {
22+
minValue: 10,
23+
maxValue: 90,
24+
options: {
25+
floor: 0,
26+
ceil: 100,
27+
step: 1,
28+
minRange: 10
29+
}
30+
};
31+
932
//Slider with selection bar
1033
$scope.slider_visible_bar = {
1134
value: 10,
1235
options: {
1336
showSelectionBar: true
1437
}
1538
};
39+
1640
//Slider with selection bar end
1741
$scope.slider_visible_bar_end = {
1842
value: 10,
@@ -22,17 +46,6 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) {
2246
}
2347
};
2448

25-
//Range slider config
26-
$scope.minRangeSlider = {
27-
minValue: 10,
28-
maxValue: 90,
29-
options: {
30-
floor: 0,
31-
ceil: 100,
32-
step: 1
33-
}
34-
};
35-
3649
//Slider with selection bar
3750
$scope.color_slider_bar = {
3851
value: 12,

demo/index.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@ <h2>Simple slider</h2>
2727

2828
<article>
2929
<h2>Range slider</h2>
30-
Min Value: <input type="number" ng-model="minRangeSlider.minValue"/><br/>
31-
Max Value: <input type="number" ng-model="minRangeSlider.maxValue"/><br/>
30+
Min Value: <input type="number" ng-model="rangeSlider.minValue"/><br/>
31+
Max Value: <input type="number" ng-model="rangeSlider.maxValue"/><br/>
32+
<rzslider
33+
rz-slider-model="rangeSlider.minValue"
34+
rz-slider-high="rangeSlider.maxValue"
35+
rz-slider-options="rangeSlider.options"
36+
></rzslider>
37+
</article>
38+
39+
<article>
40+
<h2>Range slider with minimum range of 10</h2>
3241
<rzslider
3342
rz-slider-model="minRangeSlider.minValue"
3443
rz-slider-high="minRangeSlider.maxValue"

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.4.1 -
22
(c) Rafal Zajac <[email protected]>, Valentin Hervieu <[email protected]>, Jussi Saarivirta <[email protected]>, Angelin Sirbu <[email protected]> -
33
https://github.com/angular-slider/angularjs-slider -
4-
2016-01-15 */
4+
2016-01-22 */
55
rzslider {
66
position: relative;
77
display: inline-block;

dist/rzslider.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*! angularjs-slider - v2.4.1 -
22
(c) Rafal Zajac <[email protected]>, Valentin Hervieu <[email protected]>, Jussi Saarivirta <[email protected]>, Angelin Sirbu <[email protected]> -
33
https://github.com/angular-slider/angularjs-slider -
4-
2016-01-15 */
4+
2016-01-22 */
55
/*jslint unparam: true */
66
/*global angular: false, console: false, define, module */
77
(function(root, factory) {
@@ -31,6 +31,7 @@
3131
ceil: null, //defaults to rz-slider-model
3232
step: 1,
3333
precision: 0,
34+
minRange: 0,
3435
id: null,
3536
translate: null,
3637
stepsArray: null,
@@ -736,7 +737,7 @@
736737
this.scope.ticks = [];
737738
for (var i = 0; i < ticksCount; i++) {
738739
var value = this.roundStep(this.minValue + i * this.step);
739-
var tick =   {
740+
var tick = {
740741
selected: this.isTickSelected(value)
741742
};
742743
if (tick.selected && this.options.getSelectionBarColor) {
@@ -1514,7 +1515,9 @@
15141515
var switched = false;
15151516

15161517
if (this.range) {
1517-
/* This is to check if we need to switch the min and max handles*/
1518+
newValue = this.applyMinRange(newValue);
1519+
newOffset = this.valueToOffset(newValue);
1520+
/* This is to check if we need to switch the min and max handles */
15181521
if (this.tracking === 'rzSliderModel' && newValue >= this.scope.rzSliderHigh) {
15191522
switched = true;
15201523
this.scope[this.tracking] = this.scope.rzSliderHigh;
@@ -1553,6 +1556,21 @@
15531556
return switched;
15541557
},
15551558

1559+
applyMinRange: function(newValue) {
1560+
if (this.options.minRange !== 0) {
1561+
var oppositeValue = this.tracking === 'rzSliderModel' ? this.scope.rzSliderHigh : this.scope.rzSliderModel,
1562+
difference = Math.abs(newValue - oppositeValue);
1563+
1564+
if (difference < this.options.minRange) {
1565+
if (this.tracking === 'rzSliderModel')
1566+
return this.scope.rzSliderHigh - this.options.minRange;
1567+
else
1568+
return this.scope.rzSliderModel + this.options.minRange;
1569+
}
1570+
}
1571+
return newValue;
1572+
},
1573+
15561574
/**
15571575
* Apply the model values using scope.$apply.
15581576
* We wrap it with the internalChange flag to avoid the watchers to be called

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: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
ceil: null, //defaults to rz-slider-model
3636
step: 1,
3737
precision: 0,
38+
minRange: 0,
3839
id: null,
3940
translate: null,
4041
stepsArray: null,
@@ -740,7 +741,7 @@
740741
this.scope.ticks = [];
741742
for (var i = 0; i < ticksCount; i++) {
742743
var value = this.roundStep(this.minValue + i * this.step);
743-
var tick =   {
744+
var tick = {
744745
selected: this.isTickSelected(value)
745746
};
746747
if (tick.selected && this.options.getSelectionBarColor) {
@@ -1518,7 +1519,9 @@
15181519
var switched = false;
15191520

15201521
if (this.range) {
1521-
/* This is to check if we need to switch the min and max handles*/
1522+
newValue = this.applyMinRange(newValue);
1523+
newOffset = this.valueToOffset(newValue);
1524+
/* This is to check if we need to switch the min and max handles */
15221525
if (this.tracking === 'rzSliderModel' && newValue >= this.scope.rzSliderHigh) {
15231526
switched = true;
15241527
this.scope[this.tracking] = this.scope.rzSliderHigh;
@@ -1557,6 +1560,21 @@
15571560
return switched;
15581561
},
15591562

1563+
applyMinRange: function(newValue) {
1564+
if (this.options.minRange !== 0) {
1565+
var oppositeValue = this.tracking === 'rzSliderModel' ? this.scope.rzSliderHigh : this.scope.rzSliderModel,
1566+
difference = Math.abs(newValue - oppositeValue);
1567+
1568+
if (difference < this.options.minRange) {
1569+
if (this.tracking === 'rzSliderModel')
1570+
return this.scope.rzSliderHigh - this.options.minRange;
1571+
else
1572+
return this.scope.rzSliderModel + this.options.minRange;
1573+
}
1574+
}
1575+
return newValue;
1576+
},
1577+
15601578
/**
15611579
* Apply the model values using scope.$apply.
15621580
* We wrap it with the internalChange flag to avoid the watchers to be called

0 commit comments

Comments
 (0)