Skip to content

Commit 271b496

Browse files
Valentin HervieuValentin Hervieu
authored andcommitted
Add a global options method to apply custom default options to all sliders
1 parent a3aacff commit 271b496

File tree

3 files changed

+95
-59
lines changed

3 files changed

+95
-59
lines changed

dist/rzslider.js

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,47 @@
3131
'use strict';
3232
var module = angular.module('rzModule', [])
3333

34-
.value('defaultOptions', {
35-
floor: 0,
36-
ceil: null, //defaults to rz-slider-model
37-
step: 1,
38-
precision: 0,
39-
translate: null,
40-
draggableRange: false,
41-
showSelectionBar: false,
42-
hideLimitLabels: false,
43-
readOnly: false,
44-
disabled: false,
45-
interval: 350,
46-
showTicks: false,
47-
showTicksValues: false,
48-
onStart: null,
49-
onChange: null,
50-
onEnd: null
34+
.factory('RzSliderOptions', function() {
35+
var defaultOptions = {
36+
floor: 0,
37+
ceil: null, //defaults to rz-slider-model
38+
step: 1,
39+
precision: 0,
40+
translate: null,
41+
draggableRange: false,
42+
showSelectionBar: false,
43+
hideLimitLabels: false,
44+
readOnly: false,
45+
disabled: false,
46+
interval: 350,
47+
showTicks: false,
48+
showTicksValues: false,
49+
scale: 1,
50+
onStart: null,
51+
onChange: null,
52+
onEnd: null
53+
};
54+
var globalOptions = {};
55+
56+
var factory = {};
57+
/**
58+
* `options({})` allows global configuration of all sliders in the
59+
* application.
60+
*
61+
* var app = angular.module( 'App', ['rzModule'], function( RzSliderOptions ) {
62+
* // show ticks for all sliders
63+
* RzSliderOptions.options( { showTicks: true } );
64+
* });
65+
*/
66+
factory.options = function(value) {
67+
angular.extend(globalOptions, value);
68+
};
69+
70+
factory.getOptions = function(options) {
71+
return angular.extend({}, defaultOptions, globalOptions, options);
72+
};
73+
74+
return factory;
5175
})
5276

5377
.value('throttle',
@@ -97,7 +121,7 @@
97121
};
98122
})
99123

100-
.factory('RzSlider', ['$timeout', '$document', '$window', 'defaultOptions', 'throttle', function($timeout, $document, $window, defaultOptions, throttle) {
124+
.factory('RzSlider', ['$timeout', '$document', '$window', 'RzSliderOptions', 'throttle', function($timeout, $document, $window, RzSliderOptions, throttle) {
101125
'use strict';
102126

103127
/**
@@ -326,14 +350,8 @@
326350
* Read the user options and apply them to the slider model
327351
*/
328352
applyOptions: function() {
329-
var userOpts = this.scope.rzSliderOptions;
330-
this.options = {};
331-
for (var option_name in defaultOptions) {
332-
if (!userOpts || userOpts[option_name] === undefined)
333-
this.options[option_name] = defaultOptions[option_name];
334-
else
335-
this.options[option_name] = userOpts[option_name];
336-
}
353+
this.options = RzSliderOptions.getOptions(this.scope.rzSliderOptions);
354+
337355
if (this.options.step <= 0)
338356
this.options.step = 1;
339357
this.range = this.scope.rzSliderModel !== undefined && this.scope.rzSliderHigh !== undefined;
@@ -915,7 +933,7 @@
915933
*/
916934
getWidth: function(elem) {
917935
var val = elem[0].getBoundingClientRect();
918-
elem.rzsw = val.right - val.left;
936+
elem.rzsw = (val.right - val.left) * this.options.scale;
919937
return elem.rzsw;
920938
},
921939

@@ -982,7 +1000,7 @@
9821000
if (!this.range) {
9831001
return this.minH;
9841002
}
985-
var offset = this.getEventX(event) - this.sliderElem.rzsl - this.handleHalfWidth;
1003+
var offset = (this.getEventX(event) - this.sliderElem.rzsl - this.handleHalfWidth) * this.options.scale;
9861004
return Math.abs(offset - this.minH.rzsl) < Math.abs(offset - this.maxH.rzsl) ? this.minH : this.maxH;
9871005
},
9881006

@@ -1095,7 +1113,7 @@
10951113
sliderLO, newOffset, newValue;
10961114

10971115
sliderLO = this.sliderElem.rzsl;
1098-
newOffset = eventX - sliderLO - this.handleHalfWidth;
1116+
newOffset = (eventX - sliderLO - this.handleHalfWidth) * this.options.scale;
10991117

11001118
if (newOffset <= 0) {
11011119
if (pointer.rzsl === 0)

0 commit comments

Comments
 (0)