Skip to content

Commit 8e5448a

Browse files
author
Valentin Hervieu
committed
Add a rz-slider-disabled attribute. Closes #117
1 parent 96d37a6 commit 8e5448a

10 files changed

+176
-22
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ $scope.priceSlider = {
146146

147147
> Display a tick for each value and the value of the tick.
148148
149+
**rz-slider-disabled**
150+
151+
> Disable the slider (apply a special style and unbind events)
152+
149153
```javascript
150154
// In your controller
151155

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angularjs-slider",
3-
"version": "0.1.34",
3+
"version": "0.1.35",
44
"homepage": "https://github.com/rzajac/angularjs-slider",
55
"authors": [
66
"Rafal Zajac <[email protected]>",

demo/index.html

+12
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ <h2>Toggle slider example</h2>
151151
</div>
152152
</article>
153153

154+
<article>
155+
<h2>Disabled slider example</h2>
156+
<label>Disable slider <input type="checkbox" ng-model="disableSlider"></label>
157+
<rzslider rz-slider-model="priceSlider8"
158+
rz-slider-floor="0"
159+
rz-slider-ceil="10"
160+
rz-slider-disabled="disableSlider"></rzslider>
161+
</article>
162+
154163
</div>
155164

156165

@@ -176,6 +185,9 @@ <h2>Toggle slider example</h2>
176185
min: 2,
177186
max: 8
178187
};
188+
$scope.priceSlider8 = 5;
189+
190+
$scope.disableSlider = true;
179191

180192
$scope.translate = function(value) {
181193
return '$' + value;

dist/rzslider.css

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ rzslider {
1818
height: 4px;
1919
margin: 30px 0 15px 0;
2020
vertical-align: middle;
21+
-webkit-user-select: none;
22+
-moz-user-select: none;
23+
-ms-user-select: none;
24+
user-select: none;
25+
}
26+
27+
rzslider[disabled] {
28+
cursor: not-allowed;
29+
}
30+
31+
rzslider[disabled] span.rz-pointer {
32+
cursor: not-allowed;
33+
background-color: #d8e0f3;
2134
}
2235

2336
rzslider span {

dist/rzslider.js

+70-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* (c) Rafal Zajac <[email protected]>
55
* http://github.com/rzajac/angularjs-slider
66
*
7-
* Version: v0.1.34
7+
* Version: v0.1.35
88
*
99
* Licensed under the MIT license
1010
*/
@@ -225,6 +225,13 @@ function throttle(func, wait, options) {
225225
*/
226226
this.showTicksValue = attributes.rzSliderShowTicksValue;
227227

228+
/**
229+
* Disable the slider
230+
*
231+
* @type {boolean}
232+
*/
233+
this.disabled = this.scope.rzSliderDisabled;
234+
228235
/**
229236
* The delta between min and max value
230237
*
@@ -284,6 +291,8 @@ function throttle(func, wait, options) {
284291
self = this;
285292

286293
this.initElemHandles();
294+
this.addAccessibility();
295+
this.setDisabledState();
287296
this.calcViewDimensions();
288297
this.setMinAndMax();
289298

@@ -295,7 +304,7 @@ function throttle(func, wait, options) {
295304
self.updateCeilLab();
296305
self.updateFloorLab();
297306
self.initHandles();
298-
if (!self.presentOnly) { self.bindEvents(); }
307+
self.bindEvents();
299308
});
300309

301310
// Recalculate slider view dimensions
@@ -384,13 +393,20 @@ function throttle(func, wait, options) {
384393
});
385394
this.deRegFuncs.push(unRegFn);
386395

396+
unRegFn = this.scope.$watch('rzSliderDisabled', function(newValue, oldValue)
397+
{
398+
if(newValue === oldValue) { return; }
399+
self.resetSlider();
400+
if(self.disabled)
401+
self.unbindEvents();
402+
else
403+
self.bindEvents();
404+
});
405+
this.deRegFuncs.push(unRegFn);
406+
387407
this.scope.$on('$destroy', function()
388408
{
389-
self.minH.off();
390-
self.maxH.off();
391-
self.fullBar.off();
392-
self.selBar.off();
393-
self.ticks.off();
409+
self.unbindEvents();
394410
angular.element($window).off('resize', calcDimFn);
395411
self.deRegFuncs.map(function(unbind) { unbind(); });
396412
});
@@ -406,9 +422,27 @@ function throttle(func, wait, options) {
406422
this.setMinAndMax();
407423
this.updateCeilLab();
408424
this.updateFloorLab();
425+
this.setDisabledState();
409426
this.calcViewDimensions();
410427
},
411428

429+
/**
430+
* Set the disabled state based on rzSliderDisabled
431+
*
432+
* @returns {undefined}
433+
*/
434+
setDisabledState: function()
435+
{
436+
this.disabled = this.scope.rzSliderDisabled;
437+
if(this.disabled) {
438+
this.sliderElem.attr('disabled', 'disabled');
439+
}
440+
else {
441+
this.sliderElem.attr('disabled', null);
442+
}
443+
444+
},
445+
412446
/**
413447
* Reset label values
414448
*
@@ -587,6 +621,18 @@ function throttle(func, wait, options) {
587621
this.selBar.addClass('rz-draggable');
588622
}
589623
},
624+
625+
/**
626+
* Adds accessibility atributes
627+
*
628+
* Run only once during initialization
629+
*
630+
* @returns {undefined}
631+
*/
632+
addAccessibility: function ()
633+
{
634+
this.sliderElem.attr("role", "slider");
635+
},
590636

591637
/**
592638
* Calculate dimensions that are dependent on view port size
@@ -1031,6 +1077,7 @@ function throttle(func, wait, options) {
10311077
*/
10321078
bindEvents: function()
10331079
{
1080+
if(this.presentOnly || this.disabled) return;
10341081
var barTracking, barStart, barMove;
10351082

10361083
if (this.dragRange)
@@ -1065,6 +1112,20 @@ function throttle(func, wait, options) {
10651112
this.ticks.on('touchstart', angular.bind(this, this.onMove, this.ticks));
10661113
},
10671114

1115+
/**
1116+
* Unbind mouse and touch events to slider handles
1117+
*
1118+
* @returns {undefined}
1119+
*/
1120+
unbindEvents: function()
1121+
{
1122+
this.minH.off();
1123+
this.maxH.off();
1124+
this.fullBar.off();
1125+
this.selBar.off();
1126+
this.ticks.off();
1127+
},
1128+
10681129
/**
10691130
* onStart event handler
10701131
*
@@ -1350,7 +1411,8 @@ function throttle(func, wait, options) {
13501411
rzSliderOnChange: '&',
13511412
rzSliderOnEnd: '&',
13521413
rzSliderShowTicks: '=?',
1353-
rzSliderShowTicksValue: '=?'
1414+
rzSliderShowTicksValue: '=?',
1415+
rzSliderDisabled: '=?',
13541416
},
13551417

13561418
/**

dist/rzslider.min.css

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

0 commit comments

Comments
 (0)