Skip to content

Commit b29b7c2

Browse files
amit777ValentinH
authored andcommitted
Added test for new tick colors (#372)
* feat(directive): Add function to customize color of ticks Pass a custom function to the slider options to allow for dynamic tick color determination
1 parent be65196 commit b29b7c2

File tree

9 files changed

+83
-5
lines changed

9 files changed

+83
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ The default options are:
223223
ticksValuesTooltip: null,
224224
vertical: false,
225225
getSelectionBarColor: null,
226+
getTickColor: null,
226227
getPointerColor: null,
227228
keyboardSupport: true,
228229
scale: 1,
@@ -318,6 +319,8 @@ Just pass an array with each slider value and that's it; the floor, ceil and ste
318319
319320
**getSelectionBarColor** - _Function(value) or Function(minVal, maxVal) (defaults to null)_: Function that returns the current color of the selection bar. *If your color won't changed, don't use this option but set it through CSS.* 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.
320321
322+
**getTickColor** - _Function(value) (defaults to null)_: Function that returns the color of a tick. showTicks must be enabled.
323+
321324
**getPointerColor** - _Function(value, pointerType) (defaults to null)_: Function that returns the current color of a pointer. *If your color won't changed, don't use this option but set it through CSS.* 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. To handle range slider pointers independently, you should evaluate pointerType within the given function where "min" stands for `rzScopeModel` and "max" for `rzScopeHigh` values.
322325
323326
**hidePointerLabels** - _Boolean (defaults to false)_: Set to true to hide pointer labels

demo/demo.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,24 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) {
333333
}
334334
};
335335

336+
//Slider with custom tick formating
337+
$scope.slider_custom_tick_format = {
338+
value: 30,
339+
options: {
340+
ceil: 1000,
341+
floor: 0,
342+
showSelectionBar: true,
343+
showTicks: true,
344+
getTickColor: function(value){
345+
if(value > 100){
346+
return 'red';
347+
}
348+
return 'blue';
349+
}
350+
}
351+
};
352+
353+
336354
//Vertical sliders
337355
$scope.verticalSlider1 = {
338356
value: 0,

demo/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,14 @@ <h2>Slider with draggable range only</h2>
252252
></rzslider>
253253
</article>
254254

255+
<article>
256+
<h2>Slider with custom tick formatting</h2>
257+
<rzslider
258+
rz-slider-model="slider_custom_tick_format.value"
259+
rz-slider-options="slider_custom_tick_format.options"
260+
></rzslider>
261+
</article>
262+
255263
<article>
256264
<h2>Vertical sliders</h2>
257265
<div class="row vertical-sliders" style="margin: 20px;">

dist/rzslider.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*! angularjs-slider - v5.3.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-07-11 */
4+
2016-07-12 */
55
.rzslider {
66
position: relative;
77
display: inline-block;

dist/rzslider.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*! angularjs-slider - v5.3.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-07-11 */
4+
2016-07-12 */
55
/*jslint unparam: true */
66
/*global angular: false, console: false, define, module */
77
(function(root, factory) {
@@ -57,6 +57,7 @@
5757
ticksValuesTooltip: null,
5858
vertical: false,
5959
getSelectionBarColor: null,
60+
getTickColor: null,
6061
getPointerColor: null,
6162
keyboardSupport: true,
6263
scale: 1,
@@ -932,6 +933,11 @@
932933
'background-color': this.getSelectionBarColor()
933934
};
934935
}
936+
if(!tick.selected && this.options.getTickColor){
937+
tick.style = {
938+
'background-color': this.getTickColor(value)
939+
}
940+
}
935941
if (this.options.ticksTooltip) {
936942
tick.tooltip = this.options.ticksTooltip(value);
937943
tick.tooltipPlacement = this.options.vertical ? 'right' : 'top';
@@ -1214,6 +1220,14 @@
12141220
return this.options.getPointerColor(this.scope.rzSliderModel, pointerType);
12151221
},
12161222

1223+
/**
1224+
* Wrapper around the getTickColor of the user to pass to
1225+
* correct parameters
1226+
*/
1227+
getTickColor: function(value) {
1228+
return this.options.getTickColor(value);
1229+
},
1230+
12171231
/**
12181232
* Update combined label position and value
12191233
*

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
ticksValuesTooltip: null,
6262
vertical: false,
6363
getSelectionBarColor: null,
64+
getTickColor: null,
6465
getPointerColor: null,
6566
keyboardSupport: true,
6667
scale: 1,
@@ -936,6 +937,11 @@
936937
'background-color': this.getSelectionBarColor()
937938
};
938939
}
940+
if(!tick.selected && this.options.getTickColor){
941+
tick.style = {
942+
'background-color': this.getTickColor(value)
943+
}
944+
}
939945
if (this.options.ticksTooltip) {
940946
tick.tooltip = this.options.ticksTooltip(value);
941947
tick.tooltipPlacement = this.options.vertical ? 'right' : 'top';
@@ -1218,6 +1224,14 @@
12181224
return this.options.getPointerColor(this.scope.rzSliderModel, pointerType);
12191225
},
12201226

1227+
/**
1228+
* Wrapper around the getTickColor of the user to pass to
1229+
* correct parameters
1230+
*/
1231+
getTickColor: function(value) {
1232+
return this.options.getTickColor(value);
1233+
},
1234+
12211235
/**
12221236
* Update combined label position and value
12231237
*

tests/specs/options-handling-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,27 @@
639639

640640
});
641641

642+
it('should set the correct background-color on tick', function() {
643+
var sliderConf = {
644+
value: 3,
645+
options: {
646+
floor: 0,
647+
ceil: 10,
648+
showTicks: true,
649+
getTickColor: function(v) {
650+
if (v < 5) return 'red';
651+
return 'green';
652+
}
653+
}
654+
};
655+
helper.createRangeSlider(sliderConf);
656+
expect(helper.element[0].querySelectorAll('.rz-tick')).to.have.length(11);
657+
var firstTick = angular.element(helper.element[0].querySelectorAll('.rz-tick')[0]);
658+
var lastTick = angular.element(helper.element[0].querySelectorAll('.rz-tick')[10]);
659+
expect(firstTick[0].style['background-color']).to.equal('red');
660+
expect(lastTick[0].style['background-color']).to.equal('green');
661+
});
662+
642663
it('should set the correct position for labels for single slider with boundPointerLabels=false', function() {
643664
var sliderConf = {
644665
min: 100000000,

0 commit comments

Comments
 (0)