Commit 34eaf5ec authored by Valentin Hervieu's avatar Valentin Hervieu

feat(translate): Pass a label string as third arg to the translate function to…

feat(translate): Pass a label string as third arg to the translate function to differentiate the lab

Closes #252
parent 7a600911
# 2.7.0 (not released) # 2.7.0 (not released)
## Features ## Features
- Add a `showSelectionBarFromValue` options (#250). - Add a `showSelectionBarFromValue` options (#250).
- Pass a label string as third arg to the `translate` function to differentiate the labels (#252).
# 2.6.0 (2016-01-31) # 2.6.0 (2016-01-31)
## Features ## Features
......
...@@ -204,7 +204,15 @@ The default options are: ...@@ -204,7 +204,15 @@ The default options are:
**minRange** - _Number (defaults to 0)_: The minimum range authorized on the slider. *Applies to range slider only.* **minRange** - _Number (defaults to 0)_: The minimum range authorized on the slider. *Applies to range slider only.*
**translate** - _Function(value, sliderId)_: Custom translate function. Use this if you want to translate values displayed on the slider. For example if you want to display dollar amounts instead of just numbers: **translate** - _Function(value, sliderId, label)_: Custom translate function. Use this if you want to translate values displayed on the slider.
`sliderId` can be used to determine the slider for which we are translating the value. `label` is a string that can take the following values:
- *'model'*: the model label
- *'high'*: the high label
- *'floor'*: the floor label
- *'ceil'*: the ceil label
- *'tick-value'*: the ticks labels
For example if you want to display dollar amounts instead of just numbers:
```html ```html
<div> <div>
<rzslider <rzslider
......
...@@ -127,7 +127,11 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) { ...@@ -127,7 +127,11 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) {
options: { options: {
ceil: 500, ceil: 500,
floor: 0, floor: 0,
translate: function(value) { step: 50,
showTicksValues: true,
id: 'translate-slider',
translate: function(value, id, which) {
console.info(value, id, which);
return '$' + value; return '$' + value;
} }
} }
......
...@@ -606,10 +606,10 @@ ...@@ -606,10 +606,10 @@
* @param {boolean} [useCustomTr] * @param {boolean} [useCustomTr]
* @returns {undefined} * @returns {undefined}
*/ */
translateFn: function(value, label, useCustomTr) { translateFn: function(value, label, which, useCustomTr) {
useCustomTr = useCustomTr === undefined ? true : useCustomTr; useCustomTr = useCustomTr === undefined ? true : useCustomTr;
var valStr = String((useCustomTr ? this.customTrFn(value, this.options.id) : value)), var valStr = String((useCustomTr ? this.customTrFn(value, this.options.id, which) : value)),
getDimension = false; getDimension = false;
if (label.rzsv === undefined || label.rzsv.length !== valStr.length || (label.rzsv.length > 0 && label.rzsd === 0)) { if (label.rzsv === undefined || label.rzsv.length !== valStr.length || (label.rzsv.length > 0 && label.rzsd === 0)) {
...@@ -691,14 +691,14 @@ ...@@ -691,14 +691,14 @@
updateAriaAttributes: function() { updateAriaAttributes: function() {
this.minH.attr({ this.minH.attr({
'aria-valuenow': this.scope.rzSliderModel, 'aria-valuenow': this.scope.rzSliderModel,
'aria-valuetext': this.customTrFn(this.scope.rzSliderModel), 'aria-valuetext': this.customTrFn(this.scope.rzSliderModel, this.options.id, 'model'),
'aria-valuemin': this.minValue, 'aria-valuemin': this.minValue,
'aria-valuemax': this.maxValue 'aria-valuemax': this.maxValue
}); });
if (this.range) { if (this.range) {
this.maxH.attr({ this.maxH.attr({
'aria-valuenow': this.scope.rzSliderHigh, 'aria-valuenow': this.scope.rzSliderHigh,
'aria-valuetext': this.customTrFn(this.scope.rzSliderHigh), 'aria-valuetext': this.customTrFn(this.scope.rzSliderHigh, this.options.id, 'high'),
'aria-valuemin': this.minValue, 'aria-valuemin': this.minValue,
'aria-valuemax': this.maxValue 'aria-valuemax': this.maxValue
}); });
...@@ -756,7 +756,7 @@ ...@@ -756,7 +756,7 @@
tick.tooltipPlacement = this.options.vertical ? 'right' : 'top'; tick.tooltipPlacement = this.options.vertical ? 'right' : 'top';
} }
if (this.options.showTicksValues) { if (this.options.showTicksValues) {
tick.value = this.getDisplayValue(value); tick.value = this.getDisplayValue(value, 'tick-value');
if (this.options.ticksValuesTooltip) { if (this.options.ticksValuesTooltip) {
tick.valueTooltip = this.options.ticksValuesTooltip(value); tick.valueTooltip = this.options.ticksValuesTooltip(value);
tick.valueTooltipPlacement = this.options.vertical ? 'right' : 'top'; tick.valueTooltipPlacement = this.options.vertical ? 'right' : 'top';
...@@ -793,7 +793,7 @@ ...@@ -793,7 +793,7 @@
* @returns {undefined} * @returns {undefined}
*/ */
updateCeilLab: function() { updateCeilLab: function() {
this.translateFn(this.maxValue, this.ceilLab); this.translateFn(this.maxValue, this.ceilLab, 'ceil');
this.setPosition(this.ceilLab, this.barDimension - this.ceilLab.rzsd); this.setPosition(this.ceilLab, this.barDimension - this.ceilLab.rzsd);
this.getDimension(this.ceilLab); this.getDimension(this.ceilLab);
}, },
...@@ -804,7 +804,7 @@ ...@@ -804,7 +804,7 @@
* @returns {undefined} * @returns {undefined}
*/ */
updateFloorLab: function() { updateFloorLab: function() {
this.translateFn(this.minValue, this.flrLab); this.translateFn(this.minValue, this.flrLab, 'floor');
this.getDimension(this.flrLab); this.getDimension(this.flrLab);
}, },
...@@ -879,7 +879,7 @@ ...@@ -879,7 +879,7 @@
*/ */
updateLowHandle: function(newOffset) { updateLowHandle: function(newOffset) {
this.setPosition(this.minH, newOffset); this.setPosition(this.minH, newOffset);
this.translateFn(this.scope.rzSliderModel, this.minLab); this.translateFn(this.scope.rzSliderModel, this.minLab, 'model');
var pos = Math.min( var pos = Math.min(
Math.max( Math.max(
newOffset - this.minLab.rzsd / 2 + this.handleHalfDim, newOffset - this.minLab.rzsd / 2 + this.handleHalfDim,
...@@ -900,7 +900,7 @@ ...@@ -900,7 +900,7 @@
*/ */
updateHighHandle: function(newOffset) { updateHighHandle: function(newOffset) {
this.setPosition(this.maxH, newOffset); this.setPosition(this.maxH, newOffset);
this.translateFn(this.scope.rzSliderHigh, this.maxLab); this.translateFn(this.scope.rzSliderHigh, this.maxLab, 'high');
var pos = Math.min(newOffset - this.maxLab.rzsd / 2 + this.handleHalfDim, this.barDimension - this.ceilLab.rzsd); var pos = Math.min(newOffset - this.maxLab.rzsd / 2 + this.handleHalfDim, this.barDimension - this.ceilLab.rzsd);
this.setPosition(this.maxLab, pos); this.setPosition(this.maxLab, pos);
...@@ -1009,11 +1009,11 @@ ...@@ -1009,11 +1009,11 @@
updateCmbLabel: function() { updateCmbLabel: function() {
if (this.minLab.rzsp + this.minLab.rzsd + 10 >= this.maxLab.rzsp) { if (this.minLab.rzsp + this.minLab.rzsd + 10 >= this.maxLab.rzsp) {
var lowTr = this.getDisplayValue(this.scope.rzSliderModel), var lowTr = this.getDisplayValue(this.scope.rzSliderModel, 'model'),
highTr = this.getDisplayValue(this.scope.rzSliderHigh), highTr = this.getDisplayValue(this.scope.rzSliderHigh, 'high'),
labelVal = lowTr === highTr ? lowTr : lowTr + ' - ' + highTr; labelVal = lowTr === highTr ? lowTr : lowTr + ' - ' + highTr;
this.translateFn(labelVal, this.cmbLab, false); this.translateFn(labelVal, this.cmbLab, 'cmb', false);
var pos = Math.min( var pos = Math.min(
Math.max( Math.max(
this.selBar.rzsp + this.selBar.rzsd / 2 - this.cmbLab.rzsd / 2, this.selBar.rzsp + this.selBar.rzsd / 2 - this.cmbLab.rzsd / 2,
...@@ -1037,8 +1037,8 @@ ...@@ -1037,8 +1037,8 @@
* @param value * @param value
* @returns {*} * @returns {*}
*/ */
getDisplayValue: function(value) { getDisplayValue: function(value, which) {
return this.customTrFn(value, this.options.id); return this.customTrFn(value, this.options.id, which);
}, },
/** /**
......
This diff is collapsed.
...@@ -610,10 +610,10 @@ ...@@ -610,10 +610,10 @@
* @param {boolean} [useCustomTr] * @param {boolean} [useCustomTr]
* @returns {undefined} * @returns {undefined}
*/ */
translateFn: function(value, label, useCustomTr) { translateFn: function(value, label, which, useCustomTr) {
useCustomTr = useCustomTr === undefined ? true : useCustomTr; useCustomTr = useCustomTr === undefined ? true : useCustomTr;
var valStr = String((useCustomTr ? this.customTrFn(value, this.options.id) : value)), var valStr = String((useCustomTr ? this.customTrFn(value, this.options.id, which) : value)),
getDimension = false; getDimension = false;
if (label.rzsv === undefined || label.rzsv.length !== valStr.length || (label.rzsv.length > 0 && label.rzsd === 0)) { if (label.rzsv === undefined || label.rzsv.length !== valStr.length || (label.rzsv.length > 0 && label.rzsd === 0)) {
...@@ -695,14 +695,14 @@ ...@@ -695,14 +695,14 @@
updateAriaAttributes: function() { updateAriaAttributes: function() {
this.minH.attr({ this.minH.attr({
'aria-valuenow': this.scope.rzSliderModel, 'aria-valuenow': this.scope.rzSliderModel,
'aria-valuetext': this.customTrFn(this.scope.rzSliderModel), 'aria-valuetext': this.customTrFn(this.scope.rzSliderModel, this.options.id, 'model'),
'aria-valuemin': this.minValue, 'aria-valuemin': this.minValue,
'aria-valuemax': this.maxValue 'aria-valuemax': this.maxValue
}); });
if (this.range) { if (this.range) {
this.maxH.attr({ this.maxH.attr({
'aria-valuenow': this.scope.rzSliderHigh, 'aria-valuenow': this.scope.rzSliderHigh,
'aria-valuetext': this.customTrFn(this.scope.rzSliderHigh), 'aria-valuetext': this.customTrFn(this.scope.rzSliderHigh, this.options.id, 'high'),
'aria-valuemin': this.minValue, 'aria-valuemin': this.minValue,
'aria-valuemax': this.maxValue 'aria-valuemax': this.maxValue
}); });
...@@ -760,7 +760,7 @@ ...@@ -760,7 +760,7 @@
tick.tooltipPlacement = this.options.vertical ? 'right' : 'top'; tick.tooltipPlacement = this.options.vertical ? 'right' : 'top';
} }
if (this.options.showTicksValues) { if (this.options.showTicksValues) {
tick.value = this.getDisplayValue(value); tick.value = this.getDisplayValue(value, 'tick-value');
if (this.options.ticksValuesTooltip) { if (this.options.ticksValuesTooltip) {
tick.valueTooltip = this.options.ticksValuesTooltip(value); tick.valueTooltip = this.options.ticksValuesTooltip(value);
tick.valueTooltipPlacement = this.options.vertical ? 'right' : 'top'; tick.valueTooltipPlacement = this.options.vertical ? 'right' : 'top';
...@@ -797,7 +797,7 @@ ...@@ -797,7 +797,7 @@
* @returns {undefined} * @returns {undefined}
*/ */
updateCeilLab: function() { updateCeilLab: function() {
this.translateFn(this.maxValue, this.ceilLab); this.translateFn(this.maxValue, this.ceilLab, 'ceil');
this.setPosition(this.ceilLab, this.barDimension - this.ceilLab.rzsd); this.setPosition(this.ceilLab, this.barDimension - this.ceilLab.rzsd);
this.getDimension(this.ceilLab); this.getDimension(this.ceilLab);
}, },
...@@ -808,7 +808,7 @@ ...@@ -808,7 +808,7 @@
* @returns {undefined} * @returns {undefined}
*/ */
updateFloorLab: function() { updateFloorLab: function() {
this.translateFn(this.minValue, this.flrLab); this.translateFn(this.minValue, this.flrLab, 'floor');
this.getDimension(this.flrLab); this.getDimension(this.flrLab);
}, },
...@@ -883,7 +883,7 @@ ...@@ -883,7 +883,7 @@
*/ */
updateLowHandle: function(newOffset) { updateLowHandle: function(newOffset) {
this.setPosition(this.minH, newOffset); this.setPosition(this.minH, newOffset);
this.translateFn(this.scope.rzSliderModel, this.minLab); this.translateFn(this.scope.rzSliderModel, this.minLab, 'model');
var pos = Math.min( var pos = Math.min(
Math.max( Math.max(
newOffset - this.minLab.rzsd / 2 + this.handleHalfDim, newOffset - this.minLab.rzsd / 2 + this.handleHalfDim,
...@@ -904,7 +904,7 @@ ...@@ -904,7 +904,7 @@
*/ */
updateHighHandle: function(newOffset) { updateHighHandle: function(newOffset) {
this.setPosition(this.maxH, newOffset); this.setPosition(this.maxH, newOffset);
this.translateFn(this.scope.rzSliderHigh, this.maxLab); this.translateFn(this.scope.rzSliderHigh, this.maxLab, 'high');
var pos = Math.min(newOffset - this.maxLab.rzsd / 2 + this.handleHalfDim, this.barDimension - this.ceilLab.rzsd); var pos = Math.min(newOffset - this.maxLab.rzsd / 2 + this.handleHalfDim, this.barDimension - this.ceilLab.rzsd);
this.setPosition(this.maxLab, pos); this.setPosition(this.maxLab, pos);
...@@ -1013,11 +1013,11 @@ ...@@ -1013,11 +1013,11 @@
updateCmbLabel: function() { updateCmbLabel: function() {
if (this.minLab.rzsp + this.minLab.rzsd + 10 >= this.maxLab.rzsp) { if (this.minLab.rzsp + this.minLab.rzsd + 10 >= this.maxLab.rzsp) {
var lowTr = this.getDisplayValue(this.scope.rzSliderModel), var lowTr = this.getDisplayValue(this.scope.rzSliderModel, 'model'),
highTr = this.getDisplayValue(this.scope.rzSliderHigh), highTr = this.getDisplayValue(this.scope.rzSliderHigh, 'high'),
labelVal = lowTr === highTr ? lowTr : lowTr + ' - ' + highTr; labelVal = lowTr === highTr ? lowTr : lowTr + ' - ' + highTr;
this.translateFn(labelVal, this.cmbLab, false); this.translateFn(labelVal, this.cmbLab, 'cmb', false);
var pos = Math.min( var pos = Math.min(
Math.max( Math.max(
this.selBar.rzsp + this.selBar.rzsd / 2 - this.cmbLab.rzsd / 2, this.selBar.rzsp + this.selBar.rzsd / 2 - this.cmbLab.rzsd / 2,
...@@ -1041,8 +1041,8 @@ ...@@ -1041,8 +1041,8 @@
* @param value * @param value
* @returns {*} * @returns {*}
*/ */
getDisplayValue: function(value) { getDisplayValue: function(value, which) {
return this.customTrFn(value, this.options.id); return this.customTrFn(value, this.options.id, which);
}, },
/** /**
......
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