Skip to content

Add a getSelectionBarColor options #197

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 4 commits into from
Dec 17, 2015
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 @@
# 2.2.0 (not released yet)
## Features
- Add a `getSelectionBarColor` options to dynamically change the selection bar color (#197).

# 2.1.0 (2015-11-29)
## Features
- Add a `vertical` options to display vertical sliders (#185).
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Make sure the report is accompanied by a reproducible demo. The ideal demo is cr

## Common issues
### My slider is not rendered correctly on load
If the slider's parent element is not visible during slider initialization, the slider can't know when its parent becomes visible.
If the slider's parent element is not visible during slider initialization, the slider can't know when its parent becomes visible.
For instance, when displaying a slider inside an element which visibility is toggled using ng-show, you need to send an event to force it to redraw when you set your ng-show to true.

Here's an example of `refreshSlider` method that you should call whenever the slider becomes visible.
Expand Down Expand Up @@ -209,6 +209,8 @@ $scope.slider = {

**showSelectionBar** - _Boolean (defaults to false)_: Set to true to always show the selection bar.

**getSelectionBarColor** - _Function(value) or Function(minVal, maxVal) (defaults to null)_: Function that returns the current color of the selection bar. If the returned color depends on a model value (either `rzScopeModel`or `'rzSliderHigh`), you should use the argument passed to the function. Indeed, when the function is called, there is no certainty that the model has already been updated.

**hideLimitLabels** - _Boolean (defaults to false)_: Set to true to hide min / max labels

**readOnly** - _Boolean (defaults to false)_: Set to true to make the slider read-only.
Expand Down
17 changes: 17 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) {
}
};

//Slider with selection bar
$scope.color_slider_bar = {
value: 12,
options: {
showSelectionBar: true,
getSelectionBarColor: function(value) {
if (value <= 3)
return 'red';
if (value <= 6)
return 'orange';
if (value <= 9)
return 'yellow';
return '#2AE02A';
}
}
};

//Slider config with floor, ceil and step
$scope.slider_floor_ceil = {
value: 12,
Expand Down
8 changes: 8 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ <h2>Slider with visible selection bar</h2>
></rzslider>
</article>

<article>
<h2>Slider with dynamic selection bar colors</h2>
<rzslider
rz-slider-model="color_slider_bar.value"
rz-slider-options="color_slider_bar.options"
></rzslider>
</article>

<article>
<h2>Slider with custom floor/ceil/step</h2>
<rzslider
Expand Down
33 changes: 28 additions & 5 deletions dist/rzslider.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
showTicksValues: false,
ticksValuesTooltip: null,
vertical: false,
selectionBarColor: null,
scale: 1,
onStart: null,
onChange: null,
Expand Down Expand Up @@ -432,6 +433,7 @@
break;
case 1:
this.selBar = jElem;
this.selBarChild = this.selBar.children('rz-selection');
break;
case 2:
this.minH = jElem;
Expand Down Expand Up @@ -472,8 +474,8 @@
this.cmbLab.rzsp = 0;
},

/** Update each elements style based on options
*
/**
* Update each elements style based on options
*/
manageElementsStyle: function() {

Expand Down Expand Up @@ -662,9 +664,14 @@
this.scope.ticks = [];
for (var i = 0; i < ticksCount; i++) {
var value = this.roundStep(this.minValue + i * this.step);
var tick = {
var tick =   {
selected: this.isTickSelected(value)
};
if (tick.selected && this.options.getSelectionBarColor) {
tick.style = {
'background-color': this.getSelectionBarColor()
};
}
if (this.options.showTicksValues) {
tick.value = this.getDisplayValue(value);
if (this.options.ticksValuesTooltip) {
Expand Down Expand Up @@ -863,6 +870,22 @@
updateSelectionBar: function() {
this.setDimension(this.selBar, Math.abs(this.maxH.rzsp - this.minH.rzsp) + this.handleHalfDim);
this.setPosition(this.selBar, this.range ? this.minH.rzsp + this.handleHalfDim : 0);
if (this.options.getSelectionBarColor) {
var color = this.getSelectionBarColor();
this.scope.barStyle = {
backgroundColor: color
};
}
},

/**
* Wrapper around the getSelectionBarColor of the user to pass to
* correct parameters
*/
getSelectionBarColor: function() {
if (this.range)
return this.options.getSelectionBarColor(this.scope.rzSliderModel, this.scope.rzSliderHigh);
return this.options.getSelectionBarColor(this.scope.rzSliderModel);
},

/**
Expand Down Expand Up @@ -1305,7 +1328,7 @@
valueChanged = true;
}

if(valueChanged) {
if (valueChanged) {
this.scope.$apply();
this.callOnChange();
}
Expand Down Expand Up @@ -1428,7 +1451,7 @@
'use strict';

$templateCache.put('rzSliderTpl.html',
"<span class=rz-bar-wrapper><span class=rz-bar></span></span> <span class=rz-bar-wrapper><span class=\"rz-bar rz-selection\"></span></span> <span class=rz-pointer></span> <span class=rz-pointer></span> <span class=\"rz-bubble rz-limit\"></span> <span class=\"rz-bubble rz-limit\"></span> <span class=rz-bubble></span> <span class=rz-bubble></span> <span class=rz-bubble></span><ul ng-show=showTicks class=rz-ticks><li ng-repeat=\"t in ticks\" class=tick ng-class=\"{selected: t.selected}\"><span ng-if=\"t.value != null && t.tooltip == null\" class=tick-value>{{ t.value }}</span> <span ng-if=\"t.value != null && t.tooltip != null\" class=tick-value uib-tooltip=\"{{ t.tooltip }}\" tooltip-placement={{t.tooltipPlacement}}>{{ t.value }}</span></li></ul>"
"<span class=rz-bar-wrapper><span class=rz-bar></span></span> <span class=rz-bar-wrapper><span class=\"rz-bar rz-selection\" ng-style=barStyle></span></span> <span class=rz-pointer></span> <span class=rz-pointer></span> <span class=\"rz-bubble rz-limit\"></span> <span class=\"rz-bubble rz-limit\"></span> <span class=rz-bubble></span> <span class=rz-bubble></span> <span class=rz-bubble></span><ul ng-show=showTicks class=rz-ticks><li ng-repeat=\"t in ticks track by $index\" class=tick ng-class=\"{selected: t.selected}\" ng-style=t.style><span ng-if=\"t.value != null && t.tooltip == null\" class=tick-value>{{ t.value }}</span> <span ng-if=\"t.value != null && t.tooltip != null\" class=tick-value uib-tooltip=\"{{ t.tooltip }}\" tooltip-placement={{t.tooltipPlacement}}>{{ t.value }}</span></li></ul>"
);

}]);
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.

7 changes: 5 additions & 2 deletions src/rzSliderTpl.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<span class="rz-bar-wrapper"><span class="rz-bar"></span></span> <!-- // 0 The slider bar -->
<span class="rz-bar-wrapper"><span class="rz-bar rz-selection"></span></span> <!-- // 1 Highlight between two handles -->
<span class="rz-bar-wrapper">
<span class="rz-bar rz-selection" ng-style="barStyle"></span>
</span> <!-- // 1 Highlight between two handles -->
<span class="rz-pointer"></span> <!-- // 2 Left slider handle -->
<span class="rz-pointer"></span> <!-- // 3 Right slider handle -->
<span class="rz-bubble rz-limit"></span> <!-- // 4 Floor label -->
Expand All @@ -8,7 +10,8 @@
<span class="rz-bubble"></span> <!-- // 7 Label above right slider handle -->
<span class="rz-bubble"></span> <!-- // 8 Range label when the slider handles are close ex. 15 - 17 -->
<ul ng-show="showTicks" class="rz-ticks"> <!-- // 9 The ticks -->
<li ng-repeat="t in ticks" class="tick" ng-class="{selected: t.selected}">
<li ng-repeat="t in ticks track by $index" class="tick" ng-class="{selected: t.selected}"
ng-style="t.style">
<span ng-if="t.value != null && t.tooltip == null" class="tick-value">{{ t.value }}</span>
<span ng-if="t.value != null && t.tooltip != null" class="tick-value"
uib-tooltip="{{ t.tooltip }}" tooltip-placement="{{t.tooltipPlacement}}">{{ t.value }}</span>
Expand Down
31 changes: 27 additions & 4 deletions src/rzslider.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
showTicksValues: false,
ticksValuesTooltip: null,
vertical: false,
selectionBarColor: null,
scale: 1,
onStart: null,
onChange: null,
Expand Down Expand Up @@ -432,6 +433,7 @@
break;
case 1:
this.selBar = jElem;
this.selBarChild = this.selBar.children('rz-selection');
break;
case 2:
this.minH = jElem;
Expand Down Expand Up @@ -472,8 +474,8 @@
this.cmbLab.rzsp = 0;
},

/** Update each elements style based on options
*
/**
* Update each elements style based on options
*/
manageElementsStyle: function() {

Expand Down Expand Up @@ -662,9 +664,14 @@
this.scope.ticks = [];
for (var i = 0; i < ticksCount; i++) {
var value = this.roundStep(this.minValue + i * this.step);
var tick = {
var tick =   {
selected: this.isTickSelected(value)
};
if (tick.selected && this.options.getSelectionBarColor) {
tick.style = {
'background-color': this.getSelectionBarColor()
};
}
if (this.options.showTicksValues) {
tick.value = this.getDisplayValue(value);
if (this.options.ticksValuesTooltip) {
Expand Down Expand Up @@ -863,6 +870,22 @@
updateSelectionBar: function() {
this.setDimension(this.selBar, Math.abs(this.maxH.rzsp - this.minH.rzsp) + this.handleHalfDim);
this.setPosition(this.selBar, this.range ? this.minH.rzsp + this.handleHalfDim : 0);
if (this.options.getSelectionBarColor) {
var color = this.getSelectionBarColor();
this.scope.barStyle = {
backgroundColor: color
};
}
},

/**
* Wrapper around the getSelectionBarColor of the user to pass to
* correct parameters
*/
getSelectionBarColor: function() {
if (this.range)
return this.options.getSelectionBarColor(this.scope.rzSliderModel, this.scope.rzSliderHigh);
return this.options.getSelectionBarColor(this.scope.rzSliderModel);
},

/**
Expand Down Expand Up @@ -1305,7 +1328,7 @@
valueChanged = true;
}

if(valueChanged) {
if (valueChanged) {
this.scope.$apply();
this.callOnChange();
}
Expand Down