-
-
Notifications
You must be signed in to change notification settings - Fork 660
/
scope.js
309 lines (237 loc) · 8.78 KB
/
scope.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Test suggested values and apply margin, step.
function setHandle ( handle, to, noLimitOption ) {
var trigger = handle !== scope_Handles[0] ? 1 : 0,
lowerMargin = scope_Locations[0] + options.margin,
upperMargin = scope_Locations[1] - options.margin,
lowerLimit = scope_Locations[0] + options.limit,
upperLimit = scope_Locations[1] - options.limit;
// For sliders with multiple handles,
// limit movement to the other handle.
// Apply the margin option by adding it to the handle positions.
if ( scope_Handles.length > 1 ) {
to = trigger ? Math.max( to, lowerMargin ) : Math.min( to, upperMargin );
}
// The limit option has the opposite effect, limiting handles to a
// maximum distance from another. Limit must be > 0, as otherwise
// handles would be unmoveable. 'noLimitOption' is set to 'false'
// for the .val() method, except for pass 4/4.
if ( noLimitOption !== false && options.limit && scope_Handles.length > 1 ) {
to = trigger ? Math.min ( to, lowerLimit ) : Math.max( to, upperLimit );
}
// Handle the step option.
to = scope_Spectrum.getStep( to );
// Limit to 0/100 for .val input, trim anything beyond 7 digits, as
// JavaScript has some issues in its floating point implementation.
to = limit(parseFloat(to.toFixed(7)));
// Return false if handle can't move
if ( to === scope_Locations[trigger] ) {
return false;
}
// Set the handle to the new position.
// Use requestAnimationFrame for efficient painting.
// No significant effect in Chrome, Edge sees dramatic
// performace improvements.
if ( window.requestAnimationFrame ) {
window.requestAnimationFrame(function(){
handle.style[options.style] = to + '%';
});
} else {
handle.style[options.style] = to + '%';
}
// Force proper handle stacking
if ( !handle.previousSibling ) {
removeClass(handle, cssClasses[17]);
if ( to > 50 ) {
addClass(handle, cssClasses[17]);
}
}
// Update locations.
scope_Locations[trigger] = to;
// Convert the value to the slider stepping/range.
scope_Values[trigger] = scope_Spectrum.fromStepping( to );
fireEvent('update', trigger);
return true;
}
// Loop values from value method and apply them.
function setValues ( count, values ) {
var i, trigger, to;
// With the limit option, we'll need another limiting pass.
if ( options.limit ) {
count += 1;
}
// If there are multiple handles to be set run the setting
// mechanism twice for the first handle, to make sure it
// can be bounced of the second one properly.
for ( i = 0; i < count; i += 1 ) {
trigger = i%2;
// Get the current argument from the array.
to = values[trigger];
// Setting with null indicates an 'ignore'.
// Inputting 'false' is invalid.
if ( to !== null && to !== false ) {
// If a formatted number was passed, attemt to decode it.
if ( typeof to === 'number' ) {
to = String(to);
}
to = options.format.from( to );
// Request an update for all links if the value was invalid.
// Do so too if setting the handle fails.
if ( to === false || isNaN(to) || setHandle( scope_Handles[trigger], scope_Spectrum.toStepping( to ), i === (3 - options.dir) ) === false ) {
fireEvent('update', trigger);
}
}
}
}
// Set the slider value.
function valueSet ( input ) {
var count, values = asArray( input ), i;
// The RTL settings is implemented by reversing the front-end,
// internal mechanisms are the same.
if ( options.dir && options.handles > 1 ) {
values.reverse();
}
// Animation is optional.
// Make sure the initial values where set before using animated placement.
if ( options.animate && scope_Locations[0] !== -1 ) {
addClassFor( scope_Target, cssClasses[14], 300 );
}
// Determine how often to set the handles.
count = scope_Handles.length > 1 ? 3 : 1;
if ( values.length === 1 ) {
count = 1;
}
setValues ( count, values );
// Fire the 'set' event for both handles.
for ( i = 0; i < scope_Handles.length; i++ ) {
// Fire the event only for handles that received a new value, as per #579
if ( values[i] !== null ) {
fireEvent('set', i);
}
}
}
// Get the slider value.
function valueGet ( ) {
var i, retour = [];
// Get the value from all handles.
for ( i = 0; i < options.handles; i += 1 ){
retour[i] = options.format.to( scope_Values[i] );
}
return inSliderOrder( retour );
}
// Removes classes from the root and empties it.
function destroy ( ) {
cssClasses.forEach(function(cls){
if ( !cls ) { return; } // Ignore empty classes
removeClass(scope_Target, cls);
});
while (scope_Target.firstChild) {
scope_Target.removeChild(scope_Target.firstChild);
}
delete scope_Target.noUiSlider;
}
// Get the current step size for the slider.
function getCurrentStep ( ) {
// Check all locations, map them to their stepping point.
// Get the step point, then find it in the input list.
var retour = scope_Locations.map(function( location, index ){
var step = scope_Spectrum.getApplicableStep( location ),
// As per #391, the comparison for the decrement step can have some rounding issues.
// Round the value to the precision used in the step.
stepDecimals = countDecimals(String(step[2])),
// Get the current numeric value
value = scope_Values[index],
// To move the slider 'one step up', the current step value needs to be added.
// Use null if we are at the maximum slider value.
increment = location === 100 ? null : step[2],
// Going 'one step down' might put the slider in a different sub-range, so we
// need to switch between the current or the previous step.
prev = Number((value - step[2]).toFixed(stepDecimals)),
// If the value fits the step, return the current step value. Otherwise, use the
// previous step. Return null if the slider is at its minimum value.
decrement = location === 0 ? null : (prev >= step[1]) ? step[2] : (step[0] || false);
return [decrement, increment];
});
// Return values in the proper order.
return inSliderOrder( retour );
}
// Attach an event to this slider, possibly including a namespace
function bindEvent ( namespacedEvent, callback ) {
scope_Events[namespacedEvent] = scope_Events[namespacedEvent] || [];
scope_Events[namespacedEvent].push(callback);
// If the event bound is 'update,' fire it immediately for all handles.
if ( namespacedEvent.split('.')[0] === 'update' ) {
scope_Handles.forEach(function(a, index){
fireEvent('update', index);
});
}
}
// Undo attachment of event
function removeEvent ( namespacedEvent ) {
var event = namespacedEvent.split('.')[0],
namespace = namespacedEvent.substring(event.length);
Object.keys(scope_Events).forEach(function( bind ){
var tEvent = bind.split('.')[0],
tNamespace = bind.substring(tEvent.length);
if ( (!event || event === tEvent) && (!namespace || namespace === tNamespace) ) {
delete scope_Events[bind];
}
});
}
// Updateable: margin, limit, step, range, animate, snap
function updateOptions ( optionsToUpdate ) {
var v = valueGet(), i, newOptions = testOptions({
start: [0, 0],
margin: optionsToUpdate.margin,
limit: optionsToUpdate.limit,
step: optionsToUpdate.step,
range: optionsToUpdate.range,
animate: optionsToUpdate.animate,
snap: optionsToUpdate.snap === undefined ? options.snap : optionsToUpdate.snap
});
['margin', 'limit', 'step', 'range', 'animate'].forEach(function(name){
if ( optionsToUpdate[name] !== undefined ) {
options[name] = optionsToUpdate[name];
}
});
// Save current spectrum direction as testOptions in testRange call
// doesn't rely on current direction
newOptions.spectrum.direction = scope_Spectrum.direction;
scope_Spectrum = newOptions.spectrum;
// Invalidate the current positioning so valueSet forces an update.
scope_Locations = [-1, -1];
valueSet(v);
for ( i = 0; i < scope_Handles.length; i++ ) {
fireEvent('update', i);
}
}
// Throw an error if the slider was already initialized.
if ( scope_Target.noUiSlider ) {
throw new Error('Slider was already initialized.');
}
// Create the base element, initialise HTML and set classes.
// Add handles and links.
scope_Base = addSlider( options.dir, options.ort, scope_Target );
scope_Handles = addHandles( options.handles, options.dir, scope_Base );
// Set the connect classes.
addConnection ( options.connect, scope_Target, scope_Handles );
if ( options.pips ) {
pips(options.pips);
}
if ( options.tooltips ) {
tooltips();
}
scope_Self = {
destroy: destroy,
steps: getCurrentStep,
on: bindEvent,
off: removeEvent,
get: valueGet,
set: valueSet,
updateOptions: updateOptions,
options: options, // Issue #600
target: scope_Target, // Issue #597
pips: pips // Issue #594
};
// Attach user events.
events( options.events );
return scope_Self;