Skip to content

feat(range): Add a pushRange option #341

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
Jul 2, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 5.1.0 (2016-07-02)
## Features
- Add a `pushRange` option (#341).

# 5.0.1 (2016-07-01)
## Fix
- Switch from using opacity to visibility to show/hide elements (#362).
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ The default options are:
maxLimit: null,
minRange: null,
maxRange: null,
pushRange: false,
id: null,
translate: null,
getLegend: null,
Expand Down Expand Up @@ -243,6 +244,8 @@ The default options are:

**maxRange** - _Number (defaults to null)_: The maximum range authorized on the slider. *Applies to range slider only.*

**pushRange** - _Boolean (defaults to false)_: Set to true to have a push behavior. When the min handle goes above the max, the max is moved as well (and vice-versa). The range between min and max is defined by the `step` option (defaults to 1) and can also be override by the `minRange` option. *Applies to range slider only.*

**translate** - _Function(value, sliderId, label)_: Custom translate function. Use this if you want to translate values displayed on the slider.
`sliderId` can be used to determine the slider for which we are translating the value. `label` is a string that can take the following values:
- *'model'*: the model label
Expand Down
12 changes: 12 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) {
}
};

//Range slider with minRange and pushRange config
$scope.minPushRangeSlider = {
minValue: 40,
maxValue: 60,
options: {
floor: 0,
ceil: 100,
minRange: 10,
pushRange: true
}
};

//Slider with selection bar
$scope.slider_visible_bar = {
value: 10,
Expand Down
9 changes: 9 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ <h2>Range slider with noSwitching=true</h2>
></rzslider>
</article>

<article>
<h2>Range slider with minimum range of 10 and pushRange option</h2>
<rzslider
rz-slider-model="minPushRangeSlider.minValue"
rz-slider-high="minPushRangeSlider.maxValue"
rz-slider-options="minPushRangeSlider.options"
></rzslider>
</article>

<article>
<h2>Slider with visible selection bar</h2>
<rzslider
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 - v5.0.1 -
(c) Rafal Zajac <[email protected]>, Valentin Hervieu <[email protected]>, Jussi Saarivirta <[email protected]>, Angelin Sirbu <[email protected]> -
https://github.com/angular-slider/angularjs-slider -
2016-07-01 */
2016-07-02 */
.rzslider {
position: relative;
display: inline-block;
Expand Down
93 changes: 61 additions & 32 deletions dist/rzslider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*! angularjs-slider - v5.0.1 -
(c) Rafal Zajac <[email protected]>, Valentin Hervieu <[email protected]>, Jussi Saarivirta <[email protected]>, Angelin Sirbu <[email protected]> -
https://github.com/angular-slider/angularjs-slider -
2016-07-01 */
2016-07-02 */
/*jslint unparam: true */
/*global angular: false, console: false, define, module */
(function(root, factory) {
Expand Down Expand Up @@ -33,6 +33,7 @@
precision: 0,
minRange: null,
maxRange: null,
pushRange: false,
minLimit: null,
maxLimit: null,
id: null,
Expand Down Expand Up @@ -1906,40 +1907,47 @@

newValue = this.applyMinMaxLimit(newValue);
if (this.range) {
newValue = this.applyMinMaxRange(newValue);
/* This is to check if we need to switch the min and max handles */
if (this.tracking === 'lowValue' && newValue > this.highValue) {
if (this.options.noSwitching && this.highValue !== this.minValue) {
newValue = this.applyMinMaxRange(this.highValue);
}
else {
this.lowValue = this.highValue;
this.applyLowValue();
this.updateHandles(this.tracking, this.maxH.rzsp);
this.updateAriaAttributes();
this.tracking = 'highValue';
this.minH.removeClass('rz-active');
this.maxH.addClass('rz-active');
if (this.options.keyboardSupport)
this.focusElement(this.maxH);
}
if (this.options.pushRange) {
newValue = this.applyPushRange(newValue);
valueChanged = true;
} else if (this.tracking === 'highValue' && newValue < this.lowValue) {
if (this.options.noSwitching && this.lowValue !== this.maxValue) {
newValue = this.applyMinMaxRange(this.lowValue);
}
else {
newValue = this.applyMinMaxRange(newValue);
/* This is to check if we need to switch the min and max handles */
if (this.tracking === 'lowValue' && newValue > this.highValue) {
if (this.options.noSwitching && this.highValue !== this.minValue) {
newValue = this.applyMinMaxRange(this.highValue);
}
else {
this.lowValue = this.highValue;
this.applyLowValue();
this.updateHandles(this.tracking, this.maxH.rzsp);
this.updateAriaAttributes();
this.tracking = 'highValue';
this.minH.removeClass('rz-active');
this.maxH.addClass('rz-active');
if (this.options.keyboardSupport)
this.focusElement(this.maxH);
}
valueChanged = true;
}
else {
this.highValue = this.lowValue;
this.applyHighValue();
this.updateHandles(this.tracking, this.minH.rzsp);
this.updateAriaAttributes();
this.tracking = 'lowValue';
this.maxH.removeClass('rz-active');
this.minH.addClass('rz-active');
if (this.options.keyboardSupport)
this.focusElement(this.minH);
else if (this.tracking === 'highValue' && newValue < this.lowValue) {
if (this.options.noSwitching && this.lowValue !== this.maxValue) {
newValue = this.applyMinMaxRange(this.lowValue);
}
else {
this.highValue = this.lowValue;
this.applyHighValue();
this.updateHandles(this.tracking, this.minH.rzsp);
this.updateAriaAttributes();
this.tracking = 'lowValue';
this.maxH.removeClass('rz-active');
this.minH.addClass('rz-active');
if (this.options.keyboardSupport)
this.focusElement(this.minH);
}
valueChanged = true;
}
valueChanged = true;
}
}

Expand Down Expand Up @@ -1988,6 +1996,27 @@
return newValue;
},

applyPushRange: function(newValue) {
var difference = this.tracking === 'lowValue' ? this.highValue - newValue : newValue - this.lowValue,
range = this.options.minRange !== null ? this.options.minRange : this.options.step;
if (difference < range) {
if (this.tracking === 'lowValue') {
this.highValue = Math.min(newValue + range, this.maxValue);
newValue = this.highValue - range;
this.applyHighValue();
this.updateHandles('highValue', this.valueToOffset(this.highValue));
}
else {
this.lowValue = Math.max(newValue - range, this.minValue);
newValue = this.lowValue + range;
this.applyLowValue();
this.updateHandles('lowValue', this.valueToOffset(this.lowValue));
}
this.updateAriaAttributes();
}
return newValue;
},

/**
* Apply the model values using scope.$apply.
* We wrap it with the internalChange flag to avoid the watchers to be called
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.

91 changes: 60 additions & 31 deletions src/rzslider.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
precision: 0,
minRange: null,
maxRange: null,
pushRange: false,
minLimit: null,
maxLimit: null,
id: null,
Expand Down Expand Up @@ -1910,40 +1911,47 @@

newValue = this.applyMinMaxLimit(newValue);
if (this.range) {
newValue = this.applyMinMaxRange(newValue);
/* This is to check if we need to switch the min and max handles */
if (this.tracking === 'lowValue' && newValue > this.highValue) {
if (this.options.noSwitching && this.highValue !== this.minValue) {
newValue = this.applyMinMaxRange(this.highValue);
}
else {
this.lowValue = this.highValue;
this.applyLowValue();
this.updateHandles(this.tracking, this.maxH.rzsp);
this.updateAriaAttributes();
this.tracking = 'highValue';
this.minH.removeClass('rz-active');
this.maxH.addClass('rz-active');
if (this.options.keyboardSupport)
this.focusElement(this.maxH);
}
if (this.options.pushRange) {
newValue = this.applyPushRange(newValue);
valueChanged = true;
} else if (this.tracking === 'highValue' && newValue < this.lowValue) {
if (this.options.noSwitching && this.lowValue !== this.maxValue) {
newValue = this.applyMinMaxRange(this.lowValue);
}
else {
newValue = this.applyMinMaxRange(newValue);
/* This is to check if we need to switch the min and max handles */
if (this.tracking === 'lowValue' && newValue > this.highValue) {
if (this.options.noSwitching && this.highValue !== this.minValue) {
newValue = this.applyMinMaxRange(this.highValue);
}
else {
this.lowValue = this.highValue;
this.applyLowValue();
this.updateHandles(this.tracking, this.maxH.rzsp);
this.updateAriaAttributes();
this.tracking = 'highValue';
this.minH.removeClass('rz-active');
this.maxH.addClass('rz-active');
if (this.options.keyboardSupport)
this.focusElement(this.maxH);
}
valueChanged = true;
}
else {
this.highValue = this.lowValue;
this.applyHighValue();
this.updateHandles(this.tracking, this.minH.rzsp);
this.updateAriaAttributes();
this.tracking = 'lowValue';
this.maxH.removeClass('rz-active');
this.minH.addClass('rz-active');
if (this.options.keyboardSupport)
this.focusElement(this.minH);
else if (this.tracking === 'highValue' && newValue < this.lowValue) {
if (this.options.noSwitching && this.lowValue !== this.maxValue) {
newValue = this.applyMinMaxRange(this.lowValue);
}
else {
this.highValue = this.lowValue;
this.applyHighValue();
this.updateHandles(this.tracking, this.minH.rzsp);
this.updateAriaAttributes();
this.tracking = 'lowValue';
this.maxH.removeClass('rz-active');
this.minH.addClass('rz-active');
if (this.options.keyboardSupport)
this.focusElement(this.minH);
}
valueChanged = true;
}
valueChanged = true;
}
}

Expand Down Expand Up @@ -1992,6 +2000,27 @@
return newValue;
},

applyPushRange: function(newValue) {
var difference = this.tracking === 'lowValue' ? this.highValue - newValue : newValue - this.lowValue,
range = this.options.minRange !== null ? this.options.minRange : this.options.step;
if (difference < range) {
if (this.tracking === 'lowValue') {
this.highValue = Math.min(newValue + range, this.maxValue);
newValue = this.highValue - range;
this.applyHighValue();
this.updateHandles('highValue', this.valueToOffset(this.highValue));
}
else {
this.lowValue = Math.max(newValue - range, this.minValue);
newValue = this.lowValue + range;
this.applyLowValue();
this.updateHandles('lowValue', this.valueToOffset(this.lowValue));
}
this.updateAriaAttributes();
}
return newValue;
},

/**
* Apply the model values using scope.$apply.
* We wrap it with the internalChange flag to avoid the watchers to be called
Expand Down
5 changes: 5 additions & 0 deletions tests/specs/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
$timeout.flush();
};

h.mouseMoveToValue = function(value) {
var offset = h.slider.valueToOffset(value) + h.slider.handleHalfDim + h.slider.sliderElem.rzsp;
h.fireMousemove(offset);
};

return h;
});
}());
Loading