Skip to content

Commit b84e866

Browse files
MasadowValentinH
authored andcommitted
feat: Add selectionBarGradient option to customize the selection bar (#473)
* feat: Add selectionBarGradient option to customize the selection bar Add selectionBarGradient option which create a linear gradient for the selection bar. The option accepts an object that contains `from` and `to` properties which are colors describing the gradient * Add tests to the gradient feature * Handle vertical + reverse case
1 parent 36a012c commit b84e866

10 files changed

+121
-14
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ The default options are:
242242
customTemplateScope: null,
243243
logScale: false,
244244
customValueToPosition: null,
245-
customPositionToValue: null
245+
customPositionToValue: null,
246+
selectionBarGradient: null
246247
}
247248
````
248249

@@ -395,6 +396,7 @@ For custom scales:
395396

396397
**customPositionToValue** - _Function(percent, minVal, maxVal): value_: Function that returns the value for a given position on the slider. The position is a percentage between 0 and 1.
397398

399+
**selectionBarGradient** - _Object (default to null)_: Use to display the selection bar as a gradient. The given object must contain `from` and `to` properties which are colors.
398400

399401
## Change default options
400402
If you want the change the default options for all the sliders displayed in your application, you can set them using the `RzSliderOptions.options()` method:

demo/demo.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $uibModal) {
9494
}
9595
};
9696

97+
//Slider with selection bar gradient
98+
$scope.gradient_slider_bar = {
99+
minValue: 0,
100+
maxValue: 33,
101+
options: {
102+
ceil: 100,
103+
showSelectionBar: true,
104+
selectionBarGradient: {
105+
from: 'white',
106+
to: '#0db9f0'
107+
}
108+
}
109+
};
110+
97111
//Slider with selection bar color
98112
$scope.color_slider_bar = {
99113
value: 12,
@@ -412,7 +426,12 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $uibModal) {
412426
options: {
413427
floor: 0,
414428
ceil: 10,
415-
vertical: true
429+
vertical: true,
430+
showSelectionBarEnd: true,
431+
selectionBarGradient: {
432+
from: 'white',
433+
to: '#0db9f0'
434+
}
416435
}
417436
};
418437
$scope.verticalSlider2 = {
@@ -421,7 +440,11 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $uibModal) {
421440
options: {
422441
floor: 0,
423442
ceil: 100,
424-
vertical: true
443+
vertical: true,
444+
selectionBarGradient: {
445+
from: 'white',
446+
to: '#0db9f0'
447+
}
425448
}
426449
};
427450
$scope.verticalSlider3 = {

demo/index.html

+9
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ <h2>Slider with visible selection bar from a value</h2>
9595
></rzslider>
9696
</article>
9797

98+
<article>
99+
<h2>Slider with selection bar gradient</h2>
100+
<rzslider
101+
rz-slider-model="gradient_slider_bar.minValue"
102+
rz-slider-high="gradient_slider_bar.maxValue"
103+
rz-slider-options="gradient_slider_bar.options"
104+
></rzslider>
105+
</article>
106+
98107
<article>
99108
<h2>Slider with dynamic selection bar colors</h2>
100109
<rzslider

dist/rzslider.css

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rzslider.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*! angularjs-slider - v5.8.9 -
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-12-11 */
4+
2016-12-12 */
55
/*jslint unparam: true */
66
/*global angular: false, console: false, define, module */
77
(function(root, factory) {
@@ -80,7 +80,8 @@
8080
customTemplateScope: null,
8181
logScale: false,
8282
customValueToPosition: null,
83-
customPositionToValue: null
83+
customPositionToValue: null,
84+
selectionBarGradient: null
8485
};
8586
var globalOptions = {};
8687

@@ -1268,6 +1269,20 @@
12681269
this.scope.barStyle = {
12691270
backgroundColor: color
12701271
};
1272+
} else if (this.options.selectionBarGradient) {
1273+
var offset = this.options.showSelectionBarFromValue !== null ? this.valueToPosition(this.options.showSelectionBarFromValue) : 0,
1274+
reversed = offset - position > 0 ^ isSelectionBarFromRight,
1275+
direction = this.options.vertical ? (reversed ? 'bottom' : 'top') : (reversed ? 'left' : 'right');
1276+
this.scope.barStyle = {
1277+
backgroundImage: 'linear-gradient(to ' + direction + ', ' + this.options.selectionBarGradient.from + ' 0%,' + this.options.selectionBarGradient.to + ' 100%)'
1278+
};
1279+
if (this.options.vertical) {
1280+
this.scope.barStyle.backgroundPosition = 'center ' + (offset + dimension + position + (reversed ? -this.handleHalfDim : 0)) + 'px';
1281+
this.scope.barStyle.backgroundSize = '100% ' + (this.barDimension - this.handleHalfDim) + 'px';
1282+
} else {
1283+
this.scope.barStyle.backgroundPosition = (offset - position + (reversed ? this.handleHalfDim : 0)) + 'px center';
1284+
this.scope.barStyle.backgroundSize = (this.barDimension - this.handleHalfDim) + 'px 100%';
1285+
}
12711286
}
12721287
},
12731288

dist/rzslider.min.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rzslider.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rzslider.scss

+2-2
Large diffs are not rendered by default.

src/rzslider.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
customTemplateScope: null,
8585
logScale: false,
8686
customValueToPosition: null,
87-
customPositionToValue: null
87+
customPositionToValue: null,
88+
selectionBarGradient: null
8889
};
8990
var globalOptions = {};
9091

@@ -1272,6 +1273,20 @@
12721273
this.scope.barStyle = {
12731274
backgroundColor: color
12741275
};
1276+
} else if (this.options.selectionBarGradient) {
1277+
var offset = this.options.showSelectionBarFromValue !== null ? this.valueToPosition(this.options.showSelectionBarFromValue) : 0,
1278+
reversed = offset - position > 0 ^ isSelectionBarFromRight,
1279+
direction = this.options.vertical ? (reversed ? 'bottom' : 'top') : (reversed ? 'left' : 'right');
1280+
this.scope.barStyle = {
1281+
backgroundImage: 'linear-gradient(to ' + direction + ', ' + this.options.selectionBarGradient.from + ' 0%,' + this.options.selectionBarGradient.to + ' 100%)'
1282+
};
1283+
if (this.options.vertical) {
1284+
this.scope.barStyle.backgroundPosition = 'center ' + (offset + dimension + position + (reversed ? -this.handleHalfDim : 0)) + 'px';
1285+
this.scope.barStyle.backgroundSize = '100% ' + (this.barDimension - this.handleHalfDim) + 'px';
1286+
} else {
1287+
this.scope.barStyle.backgroundPosition = (offset - position + (reversed ? this.handleHalfDim : 0)) + 'px center';
1288+
this.scope.barStyle.backgroundSize = (this.barDimension - this.handleHalfDim) + 'px 100%';
1289+
}
12751290
}
12761291
},
12771292

tests/specs/options-handling-test.js

+43
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,49 @@
413413
expect(helper.slider.selBar.css('left')).to.equal(expectedPosition + 'px');
414414
});
415415

416+
it('should set the correct background position for selection bar for range slider when selectionBarGradient is used with a value {from: "white"; to:"blue"}', function() {
417+
var sliderConf = {
418+
min: 5,
419+
max: 10,
420+
options: {
421+
floor: 0,
422+
ceil: 20,
423+
selectionBarGradient: {
424+
from: 'white',
425+
to: 'blue'
426+
}
427+
}
428+
};
429+
430+
helper.createRangeSlider(sliderConf);
431+
432+
var expectedPosition = -(Math.round(helper.slider.valueToPosition(5)) + helper.slider.handleHalfDim) + 'px center',
433+
actualPosition = helper.slider.scope.barStyle.backgroundPosition;
434+
expect(actualPosition).to.equal(expectedPosition);
435+
});
436+
437+
it('should set the correct gradient for selection bar for slider when selectionBarGradient is used with a value {from: "white"; to:"blue"} and vertical is used with a value true', function() {
438+
var sliderConf = {
439+
value: 5,
440+
options: {
441+
floor: 0,
442+
ceil: 20,
443+
vertical: true,
444+
showSelectionBar: true,
445+
selectionBarGradient: {
446+
from: 'white',
447+
to: 'blue'
448+
}
449+
}
450+
};
451+
452+
helper.createSlider(sliderConf);
453+
454+
var expectedGradient = 'linear-gradient(to top, white 0%,blue 100%)',
455+
actualGradient = helper.slider.scope.barStyle.backgroundImage;
456+
expect(actualGradient).to.equal(expectedGradient);
457+
});
458+
416459
it('should set alwaysHide on floor/ceil when hideLimitLabels is set to true', function() {
417460
var sliderConf = {
418461
value: 10,

0 commit comments

Comments
 (0)