Skip to content

Expose label on scope in template #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,18 @@ $scope.slider = {

**rz-slider-tpl-url**

> If for some reason you need to use a custom template, you can do so by providing a template URL to the `rz-slider-tpl-url` attribute. The default template is [this one](https://github.com/angular-slider/angularjs-slider/blob/master/src/rzSliderTpl.html).
> If you need to use a custom template, you can do so by providing a template URL to the `rz-slider-tpl-url` attribute. The default template is [this one](https://github.com/angular-slider/angularjs-slider/blob/master/src/rzSliderTpl.html).

The following variables are available in the template as scope variables.
- `floorLabel`: The value set to `floor` in `rz-slider-options`
- `ceilLabel`: The value set to `ceil` in `rz-slider-options`
- `modelLabel`: The value set to `rz-slider-model`
- `highLabel`: The value set to `rz-slider-high`
- `cmbLabel`: The text shown when the two handlers are close to each other. (e.g. "30-40")

The library replaces the HTML contents of label elements in the template by default, if you want to stop this behaviour and tweak label HTML on your own, you need to set `no-label-injection` class on the elements you're customizing.

See the [Custom template to use angular directive for label](./demo/directiveInCustomTemplate.html) for an example.

**rz-slider-options**

Expand Down
23 changes: 23 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) {
}
}
};
//Slider config with angular directive inside custom template
$scope.slider_custom_directive_inside_template = {
minValue: 20,
maxValue: 80,
options: {
floor: 0,
ceil: 100
}
};

//Slider config with steps array of letters
$scope.slider_alphabet = {
Expand Down Expand Up @@ -515,3 +524,17 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) {
}
}
});

app.directive('clickableLabel', function() {
return {
restrict: 'E',
scope: {label: '='},
replace: true,
template: "<button ng-click='onclick(label)' style='cursor: pointer;'>click me - {{label}}</button>",
link: function(scope, elem, attrs){
scope.onclick = function(label){
alert("I'm " + label);
};
}
};
});
23 changes: 23 additions & 0 deletions demo/directiveInCustomTemplate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div class="rzslider">
<span class="rz-bar-wrapper"><span class="rz-bar"></span></span>
<span class="rz-bar-wrapper">
<span class="rz-bar rz-selection" ng-style="barStyle"></span>
</span>
<span class="rz-pointer rz-pointer-min" ng-style=minPointerStyle></span>
<span class="rz-pointer rz-pointer-max" ng-style=maxPointerStyle></span>
<span class="rz-bubble rz-limit rz-floor no-label-injection">{{floorLabel}}</span>
<span class="rz-bubble rz-limit rz-ceil no-label-injection">{{ceilLabel}}</span>
<span class="rz-bubble no-label-injection"><clickable-label label="modelLabel" /></span>
<span class="rz-bubble no-label-injection"><clickable-label label="highLabel" /></span>
<span class="rz-bubble no-label-injection">
<clickable-label label="modelLabel"/>
-
<clickable-label label="highLabel"/>
</span>
<ul ng-show="showTicks" class="rz-ticks">
<li ng-repeat="t in ticks track by $index" class="rz-tick" ng-class="{'rz-selected': t.selected}" ng-style="t.style" ng-attr-uib-tooltip="{{ t.tooltip }}" ng-attr-tooltip-placement="{{t.tooltipPlacement}}" ng-attr-tooltip-append-to-body="{{ t.tooltip ? true : undefined}}">
<span ng-if="t.value != null" class="rz-tick-value" ng-attr-uib-tooltip="{{ t.valueTooltip }}" ng-attr-tooltip-placement="{{t.valueTooltipPlacement}}">{{ t.value }}</span>
<span ng-if="t.legend != null" class="rz-tick-legend">{{ t.legend }}</span>
</li>
</ul>
</div>
10 changes: 10 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ <h2>Slider with custom display function using html formatting</h2>
></rzslider>
</article>

<article>
<h2>Slider with angular directive inside custom template</h2>
<rzslider
rz-slider-model="slider_custom_directive_inside_template.minValue"
rz-slider-high="slider_custom_directive_inside_template.maxValue"
rz-slider-options="slider_custom_directive_inside_template.options"
rz-slider-tpl-url="directiveInCustomTemplate.html"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the directiveInCustomTemplate.html template, did you forget to include it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot this branch was pullreqeusted one, it's just wip commit. Sorry for bothering

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem! ;)

></rzslider>
</article>

<article>
<h2>Slider with Alphabet</h2>
Current letter: {{ slider_alphabet.value }}
Expand Down
2 changes: 1 addition & 1 deletion dist/rzslider.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*! angularjs-slider - v5.2.0 -
(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-07-07 */
2016-07-09 */
.rzslider {
position: relative;
display: inline-block;
Expand Down
9 changes: 6 additions & 3 deletions dist/rzslider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*! angularjs-slider - v5.2.0 -
(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-07-07 */
2016-07-09 */
/*jslint unparam: true */
/*global angular: false, console: false, define, module */
(function(root, factory) {
Expand Down Expand Up @@ -774,7 +774,8 @@
useCustomTr = useCustomTr === undefined ? true : useCustomTr;

var valStr = '',
getDimension = false;
getDimension = false,
noLabelInjection = label.hasClass('no-label-injection');

if (useCustomTr) {
if (this.options.stepsArray && !this.options.bindIndexForStepsArray)
Expand All @@ -790,7 +791,9 @@
label.rzsv = valStr;
}

label.html(valStr);
if(!noLabelInjection){ label.html(valStr); };

this.scope[which + 'Label'] = valStr;

// Update width only when length of the label have changed
if (getDimension) {
Expand Down
2 changes: 1 addition & 1 deletion dist/rzslider.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/rzslider.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function (config) {
'src/*.js',
'tests/specs/helper.js',
'tests/specs/**/*-test.js',
'tests/specs/*.html',
'dist/rzslider.css',
'src/*.html'
],
Expand All @@ -27,7 +28,8 @@ module.exports = function (config) {
// preprocess matching files before serving them to the browser
preprocessors: {
"src/*.js": ['coverage'],
"src/*Tpl.html": 'ng-html2js'
"src/*Tpl.html": 'ng-html2js',
"tests/specs/*-tpl.html": 'ng-html2js'
},

ngHtml2JsPreprocessor: {
Expand Down
7 changes: 5 additions & 2 deletions src/rzslider.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,8 @@
useCustomTr = useCustomTr === undefined ? true : useCustomTr;

var valStr = '',
getDimension = false;
getDimension = false,
noLabelInjection = label.hasClass('no-label-injection');

if (useCustomTr) {
if (this.options.stepsArray && !this.options.bindIndexForStepsArray)
Expand All @@ -794,7 +795,9 @@
label.rzsv = valStr;
}

label.html(valStr);
if(!noLabelInjection){ label.html(valStr); };

this.scope[which + 'Label'] = valStr;

// Update width only when length of the label have changed
if (getDimension) {
Expand Down
70 changes: 70 additions & 0 deletions tests/specs/custom-template-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
(function() {
"use strict";

describe('Custom templates - ', function() {
var helper,
RzSliderOptions,
$rootScope,
$timeout;

beforeEach(module('test-helper'));

beforeEach(inject(function(TestHelper, _RzSliderOptions_, _$rootScope_, _$timeout_) {
helper = TestHelper;
RzSliderOptions = _RzSliderOptions_;
$rootScope = _$rootScope_;
$timeout = _$timeout_;
}));

afterEach(function() {
helper.clean();
});

var url = 'tests/specs/custom-tpl.html';

it('should render ceil/floor labels', function() {
var sliderConf = {
min: 10,
max: 50,
options: {
floor: 0,
ceil: 100,
step: 10
}
};
helper.createRangeSliderWithCustomTemplate(sliderConf, url);
expect(helper.slider.flrLab.text()).to.equal('test- 0');
expect(helper.slider.ceilLab.text()).to.equal('test- 100');
});

it('should render min/max labels', function() {
var sliderConf = {
min: 10,
max: 50,
options: {
floor: 0,
ceil: 100,
step: 10
}
};
helper.createRangeSliderWithCustomTemplate(sliderConf, url);
expect(helper.slider.minLab.text()).to.equal('test- 10');
expect(helper.slider.maxLab.text()).to.equal('test- 50');
});

it('should render min/max labels', function() {
var sliderConf = {
min: 50,
max: 50,
options: {
floor: 0,
ceil: 100,
step: 10
}
};
helper.createRangeSliderWithCustomTemplate(sliderConf, url);
expect(helper.slider.cmbLab.text()).to.equal('test- 50 - 50');
});

});
}());
19 changes: 19 additions & 0 deletions tests/specs/custom-tpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div class="rzslider">
<span class="rz-bar-wrapper"><span class="rz-bar"></span></span>
<span class="rz-bar-wrapper">
<span class="rz-bar rz-selection" ng-style="barStyle"></span>
</span>
<span class="rz-pointer rz-pointer-min" ng-style=minPointerStyle></span>
<span class="rz-pointer rz-pointer-max" ng-style=maxPointerStyle></span>
<span class="rz-bubble rz-limit rz-floor no-label-injection">test- {{floorLabel}}</span>
<span class="rz-bubble rz-limit rz-ceil no-label-injection">test- {{ceilLabel}}</span>
<span class="rz-bubble no-label-injection">test- {{modelLabel}}</span>
<span class="rz-bubble no-label-injection">test- {{highLabel}}</span>
<span class="rz-bubble no-label-injection">test- {{cmbLabel}}</span>
<ul ng-show="showTicks" class="rz-ticks">
<li ng-repeat="t in ticks track by $index" class="rz-tick" ng-class="{'rz-selected': t.selected}" ng-style="t.style" ng-attr-uib-tooltip="{{ t.tooltip }}" ng-attr-tooltip-placement="{{t.tooltipPlacement}}" ng-attr-tooltip-append-to-body="{{ t.tooltip ? true : undefined}}">
<span ng-if="t.value != null" class="rz-tick-value" ng-attr-uib-tooltip="{{ t.valueTooltip }}" ng-attr-tooltip-placement="{{t.valueTooltipPlacement}}">{{ t.value }}</span>
<span ng-if="t.legend != null" class="rz-tick-legend">{{ t.legend }}</span>
</li>
</ul>
</div>
14 changes: 14 additions & 0 deletions tests/specs/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@
h.initSlider(sliderObj, template);
};

h.createRangeSliderWithCustomTemplate = function(sliderObj, templateUrl) {
var template = '';
var optionsExpression = sliderObj.optionsExpression || 'slider.options';
if (sliderObj.options || sliderObj.optionsExpression)
template = '<rzslider rz-slider-model="slider.min"' +
' rz-slider-high="slider.max"' +
' rz-slider-options="' + optionsExpression +
'" rz-slider-tpl-url="' + templateUrl +
'"></rzslider>';
else
template = '<rzslider rz-slider-model="slider.min" rz-slider-high="slider.max"></rzslider>';
h.initSlider(sliderObj, template);
};

h.initSlider = function(sliderObj, template) {
h.scope = $rootScope.$new();
h.scope.slider = sliderObj;
Expand Down