Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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