Commit b84e866b authored by Jimmy Delas's avatar Jimmy Delas Committed by Valentin Hervieu

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
parent 36a012cc
......@@ -242,7 +242,8 @@ The default options are:
customTemplateScope: null,
logScale: false,
customValueToPosition: null,
customPositionToValue: null
customPositionToValue: null,
selectionBarGradient: null
}
````
......@@ -395,6 +396,7 @@ For custom scales:
**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.
**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.
## Change default options
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:
......
......@@ -94,6 +94,20 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $uibModal) {
}
};
//Slider with selection bar gradient
$scope.gradient_slider_bar = {
minValue: 0,
maxValue: 33,
options: {
ceil: 100,
showSelectionBar: true,
selectionBarGradient: {
from: 'white',
to: '#0db9f0'
}
}
};
//Slider with selection bar color
$scope.color_slider_bar = {
value: 12,
......@@ -412,7 +426,12 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $uibModal) {
options: {
floor: 0,
ceil: 10,
vertical: true
vertical: true,
showSelectionBarEnd: true,
selectionBarGradient: {
from: 'white',
to: '#0db9f0'
}
}
};
$scope.verticalSlider2 = {
......@@ -421,7 +440,11 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $uibModal) {
options: {
floor: 0,
ceil: 100,
vertical: true
vertical: true,
selectionBarGradient: {
from: 'white',
to: '#0db9f0'
}
}
};
$scope.verticalSlider3 = {
......
......@@ -95,6 +95,15 @@
></rzslider>
</article>
<article>
<h2>Slider with selection bar gradient</h2>
<rzslider
rz-slider-model="gradient_slider_bar.minValue"
rz-slider-high="gradient_slider_bar.maxValue"
rz-slider-options="gradient_slider_bar.options"
></rzslider>
</article>
<article>
<h2>Slider with dynamic selection bar colors</h2>
<rzslider
......
This diff is collapsed.
/*! angularjs-slider - v5.8.9 -
(c) Rafal Zajac <rzajac@gmail.com>, Valentin Hervieu <valentin@hervieu.me>, Jussi Saarivirta <jusasi@gmail.com>, Angelin Sirbu <angelin.sirbu@gmail.com> -
https://github.com/angular-slider/angularjs-slider -
2016-12-11 */
2016-12-12 */
/*jslint unparam: true */
/*global angular: false, console: false, define, module */
(function(root, factory) {
......@@ -80,7 +80,8 @@
customTemplateScope: null,
logScale: false,
customValueToPosition: null,
customPositionToValue: null
customPositionToValue: null,
selectionBarGradient: null
};
var globalOptions = {};
......@@ -1268,6 +1269,20 @@
this.scope.barStyle = {
backgroundColor: color
};
} else if (this.options.selectionBarGradient) {
var offset = this.options.showSelectionBarFromValue !== null ? this.valueToPosition(this.options.showSelectionBarFromValue) : 0,
reversed = offset - position > 0 ^ isSelectionBarFromRight,
direction = this.options.vertical ? (reversed ? 'bottom' : 'top') : (reversed ? 'left' : 'right');
this.scope.barStyle = {
backgroundImage: 'linear-gradient(to ' + direction + ', ' + this.options.selectionBarGradient.from + ' 0%,' + this.options.selectionBarGradient.to + ' 100%)'
};
if (this.options.vertical) {
this.scope.barStyle.backgroundPosition = 'center ' + (offset + dimension + position + (reversed ? -this.handleHalfDim : 0)) + 'px';
this.scope.barStyle.backgroundSize = '100% ' + (this.barDimension - this.handleHalfDim) + 'px';
} else {
this.scope.barStyle.backgroundPosition = (offset - position + (reversed ? this.handleHalfDim : 0)) + 'px center';
this.scope.barStyle.backgroundSize = (this.barDimension - this.handleHalfDim) + 'px 100%';
}
}
},
......
/*! angularjs-slider - v5.8.9 - (c) Rafal Zajac <rzajac@gmail.com>, Valentin Hervieu <valentin@hervieu.me>, Jussi Saarivirta <jusasi@gmail.com>, Angelin Sirbu <angelin.sirbu@gmail.com> - https://github.com/angular-slider/angularjs-slider - 2016-12-11 */
/*! angularjs-slider - v5.8.9 - (c) Rafal Zajac <rzajac@gmail.com>, Valentin Hervieu <valentin@hervieu.me>, Jussi Saarivirta <jusasi@gmail.com>, Angelin Sirbu <angelin.sirbu@gmail.com> - https://github.com/angular-slider/angularjs-slider - 2016-12-12 */
.rzslider{position:relative;display:inline-block;width:100%;height:4px;margin:35px 0 15px 0;vertical-align:middle;user-select:none}.rzslider.with-legend{margin-bottom:40px}.rzslider[disabled]{cursor:not-allowed}.rzslider[disabled] .rz-pointer{cursor:not-allowed;background-color:#d8e0f3}.rzslider[disabled] .rz-bar-wrapper.rz-draggable{cursor:not-allowed}.rzslider[disabled] .rz-bar.rz-selection{background:#8b91a2}.rzslider[disabled] .rz-ticks .rz-tick{cursor:not-allowed}.rzslider[disabled] .rz-ticks .rz-tick.rz-selected{background:#8b91a2}.rzslider span{position:absolute;display:inline-block;white-space:nowrap}.rzslider .rz-base{width:100%;height:100%;padding:0}.rzslider .rz-bar-wrapper{left:0;z-index:1;width:100%;height:32px;padding-top:16px;margin-top:-16px;box-sizing:border-box}.rzslider .rz-bar-wrapper.rz-draggable{cursor:move}.rzslider .rz-bar{left:0;z-index:1;width:100%;height:4px;background:#d8e0f3;-webkit-border-radius:2px;-moz-border-radius:2px;border-radius:2px}.rzslider .rz-bar.rz-selection{z-index:2;background:#0db9f0;-webkit-border-radius:2px;-moz-border-radius:2px;border-radius:2px}.rzslider .rz-pointer{top:-14px;z-index:3;width:32px;height:32px;cursor:pointer;background-color:#0db9f0;-webkit-border-radius:16px;-moz-border-radius:16px;border-radius:16px}.rzslider .rz-pointer:after{position:absolute;top:12px;left:12px;width:8px;height:8px;background:#fff;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;content:''}.rzslider .rz-pointer:hover:after{background-color:#fff}.rzslider .rz-pointer.rz-active{z-index:4}.rzslider .rz-pointer.rz-active:after{background-color:#451aff}.rzslider .rz-bubble{bottom:16px;padding:1px 3px;color:#55637d;cursor:default}.rzslider .rz-bubble.rz-selection{top:16px}.rzslider .rz-bubble.rz-limit{color:#55637d}.rzslider .rz-ticks{position:absolute;top:-3px;left:0;z-index:1;width:100%;height:0;margin:0;list-style:none;box-sizing:border-box}.rzslider .rz-ticks .rz-tick{position:absolute;top:0;left:0;width:10px;height:10px;margin-left:11px;text-align:center;cursor:pointer;background:#d8e0f3;border-radius:50%}.rzslider .rz-ticks .rz-tick.rz-selected{background:#0db9f0}.rzslider .rz-ticks .rz-tick .rz-tick-value{position:absolute;top:-30px;transform:translate(-50%,0)}.rzslider .rz-ticks .rz-tick .rz-tick-legend{position:absolute;top:24px;max-width:50px;white-space:normal;transform:translate(-50%,0)}.rzslider .rz-ticks.rz-ticks-values-under .rz-tick-value{top:initial;bottom:-32px}.rzslider.rz-vertical{position:relative;width:4px;height:100%;padding:0;margin:0 20px;vertical-align:baseline}.rzslider.rz-vertical .rz-base{width:100%;height:100%;padding:0}.rzslider.rz-vertical .rz-bar-wrapper{top:auto;left:0;width:32px;height:100%;padding:0 0 0 16px;margin:0 0 0 -16px}.rzslider.rz-vertical .rz-bar{bottom:0;left:auto;width:4px;height:100%}.rzslider.rz-vertical .rz-pointer{top:auto;bottom:0;left:-14px!important}.rzslider.rz-vertical .rz-bubble{bottom:0;left:16px!important;margin-left:3px}.rzslider.rz-vertical .rz-bubble.rz-selection{top:auto;left:16px!important}.rzslider.rz-vertical .rz-ticks{top:0;left:-3px;z-index:1;width:0;height:100%}.rzslider.rz-vertical .rz-ticks .rz-tick{margin-top:11px;margin-left:auto;vertical-align:middle}.rzslider.rz-vertical .rz-ticks .rz-tick .rz-tick-value{top:initial;left:24px;transform:translate(0,-28%)}.rzslider.rz-vertical .rz-ticks .rz-tick .rz-tick-legend{top:initial;right:24px;max-width:none;white-space:nowrap;transform:translate(0,-28%)}.rzslider.rz-vertical .rz-ticks.rz-ticks-values-under .rz-tick-value{right:24px;bottom:initial;left:initial}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
......@@ -84,7 +84,8 @@
customTemplateScope: null,
logScale: false,
customValueToPosition: null,
customPositionToValue: null
customPositionToValue: null,
selectionBarGradient: null
};
var globalOptions = {};
......@@ -1272,6 +1273,20 @@
this.scope.barStyle = {
backgroundColor: color
};
} else if (this.options.selectionBarGradient) {
var offset = this.options.showSelectionBarFromValue !== null ? this.valueToPosition(this.options.showSelectionBarFromValue) : 0,
reversed = offset - position > 0 ^ isSelectionBarFromRight,
direction = this.options.vertical ? (reversed ? 'bottom' : 'top') : (reversed ? 'left' : 'right');
this.scope.barStyle = {
backgroundImage: 'linear-gradient(to ' + direction + ', ' + this.options.selectionBarGradient.from + ' 0%,' + this.options.selectionBarGradient.to + ' 100%)'
};
if (this.options.vertical) {
this.scope.barStyle.backgroundPosition = 'center ' + (offset + dimension + position + (reversed ? -this.handleHalfDim : 0)) + 'px';
this.scope.barStyle.backgroundSize = '100% ' + (this.barDimension - this.handleHalfDim) + 'px';
} else {
this.scope.barStyle.backgroundPosition = (offset - position + (reversed ? this.handleHalfDim : 0)) + 'px center';
this.scope.barStyle.backgroundSize = (this.barDimension - this.handleHalfDim) + 'px 100%';
}
}
},
......
......@@ -413,6 +413,49 @@
expect(helper.slider.selBar.css('left')).to.equal(expectedPosition + 'px');
});
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() {
var sliderConf = {
min: 5,
max: 10,
options: {
floor: 0,
ceil: 20,
selectionBarGradient: {
from: 'white',
to: 'blue'
}
}
};
helper.createRangeSlider(sliderConf);
var expectedPosition = -(Math.round(helper.slider.valueToPosition(5)) + helper.slider.handleHalfDim) + 'px center',
actualPosition = helper.slider.scope.barStyle.backgroundPosition;
expect(actualPosition).to.equal(expectedPosition);
});
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() {
var sliderConf = {
value: 5,
options: {
floor: 0,
ceil: 20,
vertical: true,
showSelectionBar: true,
selectionBarGradient: {
from: 'white',
to: 'blue'
}
}
};
helper.createSlider(sliderConf);
var expectedGradient = 'linear-gradient(to top, white 0%,blue 100%)',
actualGradient = helper.slider.scope.barStyle.backgroundImage;
expect(actualGradient).to.equal(expectedGradient);
});
it('should set alwaysHide on floor/ceil when hideLimitLabels is set to true', function() {
var sliderConf = {
value: 10,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment