|
525
|
1 |
/* |
|
|
2 |
YUI 3.10.3 (build 2fb5187) |
|
|
3 |
Copyright 2013 Yahoo! Inc. All rights reserved. |
|
|
4 |
Licensed under the BSD License. |
|
|
5 |
http://yuilibrary.com/license/ |
|
|
6 |
*/ |
|
|
7 |
|
|
|
8 |
YUI.add('slider-value-range', function (Y, NAME) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* Adds value support for Slider as a range of integers between a configured |
|
|
12 |
* minimum and maximum value. For use with <code>Y.Base.build(..)</code> to |
|
|
13 |
* add the plumbing to <code>Y.SliderBase</code>. |
|
|
14 |
* |
|
|
15 |
* @module slider |
|
|
16 |
* @submodule slider-value-range |
|
|
17 |
*/ |
|
|
18 |
|
|
|
19 |
// Constants for compression or performance |
|
|
20 |
var MIN = 'min', |
|
|
21 |
MAX = 'max', |
|
|
22 |
VALUE = 'value', |
|
|
23 |
// MINORSTEP = 'minorStep', |
|
|
24 |
// MAJORSTEP = 'majorStep', |
|
|
25 |
|
|
|
26 |
round = Math.round; |
|
|
27 |
|
|
|
28 |
/** |
|
|
29 |
* One class of value algorithm that can be built onto SliderBase. By default, |
|
|
30 |
* values range between 0 and 100, but you can configure these on the |
|
|
31 |
* built Slider class by setting the <code>min</code> and <code>max</code> |
|
|
32 |
* configurations. Set the initial value (will cause the thumb to move to the |
|
|
33 |
* appropriate location on the rail) in configuration as well if appropriate. |
|
|
34 |
* |
|
|
35 |
* @class SliderValueRange |
|
|
36 |
*/ |
|
|
37 |
function SliderValueRange() { |
|
|
38 |
this._initSliderValueRange(); |
|
|
39 |
} |
|
|
40 |
|
|
|
41 |
Y.SliderValueRange = Y.mix( SliderValueRange, { |
|
|
42 |
|
|
|
43 |
// Prototype properties and methods that will be added onto host class |
|
|
44 |
prototype: { |
|
|
45 |
|
|
|
46 |
/** |
|
|
47 |
* Factor used to translate value -> position -> value. |
|
|
48 |
* |
|
|
49 |
* @property _factor |
|
|
50 |
* @type {Number} |
|
|
51 |
* @protected |
|
|
52 |
*/ |
|
|
53 |
_factor: 1, |
|
|
54 |
|
|
|
55 |
/** |
|
|
56 |
* Stub for construction logic. Override if extending this class and |
|
|
57 |
* you need to set something up during the initializer phase. |
|
|
58 |
* |
|
|
59 |
* @method _initSliderValueRange |
|
|
60 |
* @protected |
|
|
61 |
*/ |
|
|
62 |
_initSliderValueRange: function () {}, |
|
|
63 |
|
|
|
64 |
/** |
|
|
65 |
* Override of stub method in SliderBase that is called at the end of |
|
|
66 |
* its bindUI stage of render(). Subscribes to internal events to |
|
|
67 |
* trigger UI and related state updates. |
|
|
68 |
* |
|
|
69 |
* @method _bindValueLogic |
|
|
70 |
* @protected |
|
|
71 |
*/ |
|
|
72 |
_bindValueLogic: function () { |
|
|
73 |
this.after( { |
|
|
74 |
minChange : this._afterMinChange, |
|
|
75 |
maxChange : this._afterMaxChange, |
|
|
76 |
valueChange: this._afterValueChange |
|
|
77 |
} ); |
|
|
78 |
}, |
|
|
79 |
|
|
|
80 |
/** |
|
|
81 |
* Move the thumb to appropriate position if necessary. Also resets |
|
|
82 |
* the cached offsets and recalculates the conversion factor to |
|
|
83 |
* translate position to value. |
|
|
84 |
* |
|
|
85 |
* @method _syncThumbPosition |
|
|
86 |
* @protected |
|
|
87 |
*/ |
|
|
88 |
_syncThumbPosition: function () { |
|
|
89 |
this._calculateFactor(); |
|
|
90 |
|
|
|
91 |
this._setPosition( this.get( VALUE ) ); |
|
|
92 |
}, |
|
|
93 |
|
|
|
94 |
/** |
|
|
95 |
* Calculates and caches |
|
|
96 |
* (range between max and min) / (rail length) |
|
|
97 |
* for fast runtime calculation of position -> value. |
|
|
98 |
* |
|
|
99 |
* @method _calculateFactor |
|
|
100 |
* @protected |
|
|
101 |
*/ |
|
|
102 |
_calculateFactor: function () { |
|
|
103 |
var length = this.get( 'length' ), |
|
|
104 |
thumbSize = this.thumb.getStyle( this._key.dim ), |
|
|
105 |
min = this.get( MIN ), |
|
|
106 |
max = this.get( MAX ); |
|
|
107 |
|
|
|
108 |
// The default thumb width is based on Sam skin's thumb dimension. |
|
|
109 |
// This attempts to allow for rendering off-DOM, then attaching |
|
|
110 |
// without the need to call syncUI(). It is still recommended |
|
|
111 |
// to call syncUI() in these cases though, just to be sure. |
|
|
112 |
length = parseFloat( length ) || 150; |
|
|
113 |
thumbSize = parseFloat( thumbSize ) || 15; |
|
|
114 |
|
|
|
115 |
this._factor = ( max - min ) / ( length - thumbSize ); |
|
|
116 |
|
|
|
117 |
}, |
|
|
118 |
|
|
|
119 |
/** |
|
|
120 |
* Dispatch the new position of the thumb into the value setting |
|
|
121 |
* operations. |
|
|
122 |
* |
|
|
123 |
* @method _defThumbMoveFn |
|
|
124 |
* @param e { EventFacade } The host's thumbMove event |
|
|
125 |
* @protected |
|
|
126 |
*/ |
|
|
127 |
_defThumbMoveFn: function ( e ) { |
|
|
128 |
// To prevent set('value', x) from looping back around |
|
|
129 |
if (e.source !== 'set') { |
|
|
130 |
this.set(VALUE, this._offsetToValue(e.offset)); |
|
|
131 |
} |
|
|
132 |
}, |
|
|
133 |
|
|
|
134 |
/** |
|
|
135 |
* <p>Converts a pixel position into a value. Calculates current |
|
|
136 |
* thumb offset from the leading edge of the rail multiplied by the |
|
|
137 |
* ratio of <code>(max - min) / (constraining dim)</code>.</p> |
|
|
138 |
* |
|
|
139 |
* <p>Override this if you want to use a different value mapping |
|
|
140 |
* algorithm.</p> |
|
|
141 |
* |
|
|
142 |
* @method _offsetToValue |
|
|
143 |
* @param offset { Number } X or Y pixel offset |
|
|
144 |
* @return { mixed } Value corresponding to the provided pixel offset |
|
|
145 |
* @protected |
|
|
146 |
*/ |
|
|
147 |
_offsetToValue: function ( offset ) { |
|
|
148 |
|
|
|
149 |
var value = round( offset * this._factor ) + this.get( MIN ); |
|
|
150 |
|
|
|
151 |
return round( this._nearestValue( value ) ); |
|
|
152 |
}, |
|
|
153 |
|
|
|
154 |
/** |
|
|
155 |
* Converts a value into a pixel offset for use in positioning |
|
|
156 |
* the thumb according to the reverse of the |
|
|
157 |
* <code>_offsetToValue( xy )</code> operation. |
|
|
158 |
* |
|
|
159 |
* @method _valueToOffset |
|
|
160 |
* @param val { Number } The value to map to pixel X or Y position |
|
|
161 |
* @return { Number } The pixel offset |
|
|
162 |
* @protected |
|
|
163 |
*/ |
|
|
164 |
_valueToOffset: function ( value ) { |
|
|
165 |
var offset = round( ( value - this.get( MIN ) ) / this._factor ); |
|
|
166 |
|
|
|
167 |
return offset; |
|
|
168 |
}, |
|
|
169 |
|
|
|
170 |
/** |
|
|
171 |
* Returns the current value. Override this if you want to introduce |
|
|
172 |
* output formatting. Otherwise equivalent to slider.get( "value" ); |
|
|
173 |
* |
|
|
174 |
* @method getValue |
|
|
175 |
* @return {Number} |
|
|
176 |
*/ |
|
|
177 |
getValue: function () { |
|
|
178 |
return this.get( VALUE ); |
|
|
179 |
}, |
|
|
180 |
|
|
|
181 |
/** |
|
|
182 |
* Updates the current value. Override this if you want to introduce |
|
|
183 |
* input value parsing or preprocessing. Otherwise equivalent to |
|
|
184 |
* slider.set( "value", v ); |
|
|
185 |
* |
|
|
186 |
* @method setValue |
|
|
187 |
* @param val {Number} The new value |
|
|
188 |
* @return {Slider} |
|
|
189 |
* @chainable |
|
|
190 |
*/ |
|
|
191 |
setValue: function ( val ) { |
|
|
192 |
return this.set( VALUE, val ); |
|
|
193 |
}, |
|
|
194 |
|
|
|
195 |
/** |
|
|
196 |
* Update position according to new min value. If the new min results |
|
|
197 |
* in the current value being out of range, the value is set to the |
|
|
198 |
* closer of min or max. |
|
|
199 |
* |
|
|
200 |
* @method _afterMinChange |
|
|
201 |
* @param e { EventFacade } The <code>min</code> attribute change event. |
|
|
202 |
* @protected |
|
|
203 |
*/ |
|
|
204 |
_afterMinChange: function ( e ) { |
|
|
205 |
this._verifyValue(); |
|
|
206 |
|
|
|
207 |
this._syncThumbPosition(); |
|
|
208 |
}, |
|
|
209 |
|
|
|
210 |
/** |
|
|
211 |
* Update position according to new max value. If the new max results |
|
|
212 |
* in the current value being out of range, the value is set to the |
|
|
213 |
* closer of min or max. |
|
|
214 |
* |
|
|
215 |
* @method _afterMaxChange |
|
|
216 |
* @param e { EventFacade } The <code>max</code> attribute change event. |
|
|
217 |
* @protected |
|
|
218 |
*/ |
|
|
219 |
_afterMaxChange: function ( e ) { |
|
|
220 |
this._verifyValue(); |
|
|
221 |
|
|
|
222 |
this._syncThumbPosition(); |
|
|
223 |
}, |
|
|
224 |
|
|
|
225 |
/** |
|
|
226 |
* Verifies that the current value is within the min - max range. If |
|
|
227 |
* not, value is set to either min or max, depending on which is |
|
|
228 |
* closer. |
|
|
229 |
* |
|
|
230 |
* @method _verifyValue |
|
|
231 |
* @protected |
|
|
232 |
*/ |
|
|
233 |
_verifyValue: function () { |
|
|
234 |
var value = this.get( VALUE ), |
|
|
235 |
nearest = this._nearestValue( value ); |
|
|
236 |
|
|
|
237 |
if ( value !== nearest ) { |
|
|
238 |
// @TODO Can/should valueChange, minChange, etc be queued |
|
|
239 |
// events? To make dd.set( 'min', n ); execute after minChange |
|
|
240 |
// subscribers before on/after valueChange subscribers. |
|
|
241 |
this.set( VALUE, nearest ); |
|
|
242 |
} |
|
|
243 |
}, |
|
|
244 |
|
|
|
245 |
/** |
|
|
246 |
* Propagate change to the thumb position unless the change originated |
|
|
247 |
* from the thumbMove event. |
|
|
248 |
* |
|
|
249 |
* @method _afterValueChange |
|
|
250 |
* @param e { EventFacade } The <code>valueChange</code> event. |
|
|
251 |
* @protected |
|
|
252 |
*/ |
|
|
253 |
_afterValueChange: function ( e ) { |
|
|
254 |
var val = e.newVal; |
|
|
255 |
this._setPosition( val, { source: 'set' } ); |
|
|
256 |
}, |
|
|
257 |
|
|
|
258 |
/** |
|
|
259 |
* Positions the thumb and its ARIA attributes in accordance with the |
|
|
260 |
* translated value. |
|
|
261 |
* |
|
|
262 |
* @method _setPosition |
|
|
263 |
* @param value {Number} Value to translate to a pixel position |
|
|
264 |
* @param [options] {Object} Details object to pass to `_uiMoveThumb` |
|
|
265 |
* @protected |
|
|
266 |
*/ |
|
|
267 |
_setPosition: function ( value, options ) { |
|
|
268 |
this._uiMoveThumb( this._valueToOffset( value ), options ); |
|
|
269 |
this.thumb.set('aria-valuenow', value); |
|
|
270 |
this.thumb.set('aria-valuetext', value); |
|
|
271 |
}, |
|
|
272 |
|
|
|
273 |
/** |
|
|
274 |
* Validates new values assigned to <code>min</code> attribute. Numbers |
|
|
275 |
* are acceptable. Override this to enforce different rules. |
|
|
276 |
* |
|
|
277 |
* @method _validateNewMin |
|
|
278 |
* @param value {Any} Value assigned to <code>min</code> attribute. |
|
|
279 |
* @return {Boolean} True for numbers. False otherwise. |
|
|
280 |
* @protected |
|
|
281 |
*/ |
|
|
282 |
_validateNewMin: function ( value ) { |
|
|
283 |
return Y.Lang.isNumber( value ); |
|
|
284 |
}, |
|
|
285 |
|
|
|
286 |
/** |
|
|
287 |
* Validates new values assigned to <code>max</code> attribute. Numbers |
|
|
288 |
* are acceptable. Override this to enforce different rules. |
|
|
289 |
* |
|
|
290 |
* @method _validateNewMax |
|
|
291 |
* @param value { mixed } Value assigned to <code>max</code> attribute. |
|
|
292 |
* @return { Boolean } True for numbers. False otherwise. |
|
|
293 |
* @protected |
|
|
294 |
*/ |
|
|
295 |
_validateNewMax: function ( value ) { |
|
|
296 |
return Y.Lang.isNumber( value ); |
|
|
297 |
}, |
|
|
298 |
|
|
|
299 |
/** |
|
|
300 |
* Restricts new values assigned to <code>value</code> attribute to be |
|
|
301 |
* between the configured <code>min</code> and <code>max</code>. |
|
|
302 |
* Rounds to nearest integer value. |
|
|
303 |
* |
|
|
304 |
* @method _setNewValue |
|
|
305 |
* @param value { Number } Value assigned to <code>value</code> attribute |
|
|
306 |
* @return { Number } Normalized and constrained value |
|
|
307 |
* @protected |
|
|
308 |
*/ |
|
|
309 |
_setNewValue: function ( value ) { |
|
|
310 |
return round( this._nearestValue( value ) ); |
|
|
311 |
}, |
|
|
312 |
|
|
|
313 |
/** |
|
|
314 |
* Returns the nearest valid value to the value input. If the provided |
|
|
315 |
* value is outside the min - max range, accounting for min > max |
|
|
316 |
* scenarios, the nearest of either min or max is returned. Otherwise, |
|
|
317 |
* the provided value is returned. |
|
|
318 |
* |
|
|
319 |
* @method _nearestValue |
|
|
320 |
* @param value { mixed } Value to test against current min - max range |
|
|
321 |
* @return { Number } Current min, max, or value if within range |
|
|
322 |
* @protected |
|
|
323 |
*/ |
|
|
324 |
_nearestValue: function ( value ) { |
|
|
325 |
var min = this.get( MIN ), |
|
|
326 |
max = this.get( MAX ), |
|
|
327 |
tmp; |
|
|
328 |
|
|
|
329 |
// Account for reverse value range (min > max) |
|
|
330 |
tmp = ( max > min ) ? max : min; |
|
|
331 |
min = ( max > min ) ? min : max; |
|
|
332 |
max = tmp; |
|
|
333 |
|
|
|
334 |
return ( value < min ) ? |
|
|
335 |
min : |
|
|
336 |
( value > max ) ? |
|
|
337 |
max : |
|
|
338 |
value; |
|
|
339 |
} |
|
|
340 |
|
|
|
341 |
}, |
|
|
342 |
|
|
|
343 |
/** |
|
|
344 |
* Attributes that will be added onto host class. |
|
|
345 |
* |
|
|
346 |
* @property ATTRS |
|
|
347 |
* @type {Object} |
|
|
348 |
* @static |
|
|
349 |
* @protected |
|
|
350 |
*/ |
|
|
351 |
ATTRS: { |
|
|
352 |
/** |
|
|
353 |
* The value associated with the farthest top, left position of the |
|
|
354 |
* rail. Can be greater than the configured <code>max</code> if you |
|
|
355 |
* want values to increase from right-to-left or bottom-to-top. |
|
|
356 |
* |
|
|
357 |
* @attribute min |
|
|
358 |
* @type { Number } |
|
|
359 |
* @default 0 |
|
|
360 |
*/ |
|
|
361 |
min: { |
|
|
362 |
value : 0, |
|
|
363 |
validator: '_validateNewMin' |
|
|
364 |
}, |
|
|
365 |
|
|
|
366 |
/** |
|
|
367 |
* The value associated with the farthest bottom, right position of |
|
|
368 |
* the rail. Can be less than the configured <code>min</code> if |
|
|
369 |
* you want values to increase from right-to-left or bottom-to-top. |
|
|
370 |
* |
|
|
371 |
* @attribute max |
|
|
372 |
* @type { Number } |
|
|
373 |
* @default 100 |
|
|
374 |
*/ |
|
|
375 |
max: { |
|
|
376 |
value : 100, |
|
|
377 |
validator: '_validateNewMax' |
|
|
378 |
}, |
|
|
379 |
|
|
|
380 |
/** |
|
|
381 |
* amount to increment/decrement the Slider value |
|
|
382 |
* when the arrow up/down/left/right keys are pressed |
|
|
383 |
* |
|
|
384 |
* @attribute minorStep |
|
|
385 |
* @type {Number} |
|
|
386 |
* @default 1 |
|
|
387 |
*/ |
|
|
388 |
minorStep : { |
|
|
389 |
value: 1 |
|
|
390 |
}, |
|
|
391 |
|
|
|
392 |
/** |
|
|
393 |
* amount to increment/decrement the Slider value |
|
|
394 |
* when the page up/down keys are pressed |
|
|
395 |
* |
|
|
396 |
* @attribute majorStep |
|
|
397 |
* @type {Number} |
|
|
398 |
* @default 10 |
|
|
399 |
*/ |
|
|
400 |
majorStep : { |
|
|
401 |
value: 10 |
|
|
402 |
}, |
|
|
403 |
|
|
|
404 |
/** |
|
|
405 |
* The value associated with the thumb's current position on the |
|
|
406 |
* rail. Defaults to the value inferred from the thumb's current |
|
|
407 |
* position. Specifying value in the constructor will move the |
|
|
408 |
* thumb to the position that corresponds to the supplied value. |
|
|
409 |
* |
|
|
410 |
* @attribute value |
|
|
411 |
* @type { Number } |
|
|
412 |
* @default (inferred from current thumb position) |
|
|
413 |
*/ |
|
|
414 |
value: { |
|
|
415 |
value : 0, |
|
|
416 |
setter: '_setNewValue' |
|
|
417 |
} |
|
|
418 |
} |
|
|
419 |
}, true ); |
|
|
420 |
|
|
|
421 |
|
|
|
422 |
}, '3.10.3', {"requires": ["slider-base"]}); |