Skip to content

Commit 4492603

Browse files
vdiezValentinH
authored andcommitted
add the option to use ticksArray as array of objects, with legend and value properties (#662)
same as we do with steps array, but to customize the ticks without limiting the selection steps. I know this can be already done with a variety of methods, but AFAIK only with functions. In my case I store these labels in a mongo db, so storing logic is not ideal. Putting those labels in a table of objects without limiting the selection as stepsArray solves both issues, storing and displaying.
1 parent 64c3f86 commit 4492603

File tree

7 files changed

+58
-11
lines changed

7 files changed

+58
-11
lines changed

dist/rzslider.css

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rzslider.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*! angularjs-slider - v6.6.1 -
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-
2018-06-30 */
4+
2019-02-21 */
55
/*jslint unparam: true */
66
/*global angular: false, console: false, define, module */
77
;(function(root, factory) {
@@ -1033,12 +1033,19 @@
10331033
if (this.options.rightToLeft) ticksArray.reverse()
10341034

10351035
this.scope.ticks = ticksArray.map(function(value) {
1036+
var legend = null;
1037+
if (angular.isObject(value)) {
1038+
legend = value.legend;
1039+
value = value.value
1040+
}
1041+
10361042
var position = self.valueToPosition(value)
10371043

10381044
if (self.options.vertical) position = self.maxPos - position
10391045

10401046
var translation = translate + '(' + Math.round(position) + 'px)'
10411047
var tick = {
1048+
legend: legend,
10421049
selected: self.isTickSelected(value),
10431050
style: {
10441051
'-webkit-transform': translation,
@@ -1071,7 +1078,7 @@
10711078
}
10721079
}
10731080
if (self.getLegend) {
1074-
var legend = self.getLegend(value, self.options.id)
1081+
legend = self.getLegend(value, self.options.id)
10751082
if (legend) tick.legend = legend
10761083
}
10771084
return tick

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: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rzslider.scss

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

src/rzslider.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,12 +1037,19 @@
10371037
if (this.options.rightToLeft) ticksArray.reverse()
10381038

10391039
this.scope.ticks = ticksArray.map(function(value) {
1040+
var legend = null;
1041+
if (angular.isObject(value)) {
1042+
legend = value.legend;
1043+
value = value.value
1044+
}
1045+
10401046
var position = self.valueToPosition(value)
10411047

10421048
if (self.options.vertical) position = self.maxPos - position
10431049

10441050
var translation = translate + '(' + Math.round(position) + 'px)'
10451051
var tick = {
1052+
legend: legend,
10461053
selected: self.isTickSelected(value),
10471054
style: {
10481055
'-webkit-transform': translation,
@@ -1075,7 +1082,7 @@
10751082
}
10761083
}
10771084
if (self.getLegend) {
1078-
var legend = self.getLegend(value, self.options.id)
1085+
legend = self.getLegend(value, self.options.id)
10791086
if (legend) tick.legend = legend
10801087
}
10811088
return tick

tests/specs/ticks-test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,39 @@
263263
expect(lastTick.text()).to.equal('100')
264264
})
265265

266+
it('should create the correct number of ticks when ticksArray is used as array of objects', function() {
267+
var sliderConf = {
268+
value: 50,
269+
options: {
270+
floor: 0,
271+
ceil: 100,
272+
step: 10,
273+
ticksArray: [
274+
{ value: 0, legend: 'Bad' },
275+
{ value: 50, legend: 'Average' },
276+
{ value: 100, legend: 'Excellent' },
277+
],
278+
},
279+
}
280+
helper.createSlider(sliderConf)
281+
expect(helper.element[0].querySelectorAll('.rz-tick')).to.have.length(3)
282+
expect(
283+
helper.element[0].querySelectorAll('.rz-tick-value')
284+
).to.have.length(0)
285+
286+
expect(
287+
helper.element[0].querySelectorAll('.rz-tick-legend')
288+
).to.have.length(3)
289+
var firstLegend = angular.element(
290+
helper.element[0].querySelectorAll('.rz-tick-legend')[0]
291+
)
292+
expect(firstLegend.text()).to.equal('Bad')
293+
var lastLegend = angular.element(
294+
helper.element[0].querySelectorAll('.rz-tick-legend')[2]
295+
)
296+
expect(lastLegend.text()).to.equal('Excellent')
297+
})
298+
266299
it('should create the correct number of legend items when getLegend is defined', function() {
267300
var sliderConf = {
268301
value: 50,

0 commit comments

Comments
 (0)