Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('transition', function (Y, NAME) {
2
 
3
/**
4
* Provides the transition method for Node.
5
* Transition has no API of its own, but adds the transition method to Node.
6
*
7
* @module transition
8
* @requires node-style
9
*/
10
 
11
var CAMEL_VENDOR_PREFIX = '',
12
    VENDOR_PREFIX = '',
13
    DOCUMENT = Y.config.doc,
14
    DOCUMENT_ELEMENT = 'documentElement',
15
    DOCUMENT_STYLE = DOCUMENT[DOCUMENT_ELEMENT].style,
16
    TRANSITION_CAMEL = 'transition',
17
    TRANSITION_PROPERTY_CAMEL = 'transitionProperty',
18
    TRANSITION_PROPERTY,
19
    TRANSITION_DURATION,
20
    TRANSITION_TIMING_FUNCTION,
21
    TRANSITION_DELAY,
22
    TRANSITION_END,
23
    ON_TRANSITION_END,
24
 
25
    EMPTY_OBJ = {},
26
 
27
    VENDORS = [
28
        'Webkit',
29
        'Moz'
30
    ],
31
 
32
    VENDOR_TRANSITION_END = {
33
        Webkit: 'webkitTransitionEnd'
34
    },
35
 
36
/**
37
 * A class for constructing transition instances.
38
 * Adds the "transition" method to Node.
39
 * @class Transition
40
 * @constructor
41
 */
42
 
43
Transition = function() {
44
    this.init.apply(this, arguments);
45
};
46
 
47
// One off handling of transform-prefixing.
48
Transition._TRANSFORM = 'transform';
49
 
50
Transition._toCamel = function(property) {
51
    property = property.replace(/-([a-z])/gi, function(m0, m1) {
52
        return m1.toUpperCase();
53
    });
54
 
55
    return property;
56
};
57
 
58
Transition._toHyphen = function(property) {
59
    property = property.replace(/([A-Z]?)([a-z]+)([A-Z]?)/g, function(m0, m1, m2, m3) {
60
        var str = ((m1) ? '-' + m1.toLowerCase() : '') + m2;
61
 
62
        if (m3) {
63
            str += '-' + m3.toLowerCase();
64
        }
65
 
66
        return str;
67
    });
68
 
69
    return property;
70
};
71
 
72
Transition.SHOW_TRANSITION = 'fadeIn';
73
Transition.HIDE_TRANSITION = 'fadeOut';
74
 
75
Transition.useNative = false;
76
 
77
// Map transition properties to vendor-specific versions.
78
if ('transition' in DOCUMENT_STYLE
79
    && 'transitionProperty' in DOCUMENT_STYLE
80
    && 'transitionDuration' in DOCUMENT_STYLE
81
    && 'transitionTimingFunction' in DOCUMENT_STYLE
82
    && 'transitionDelay' in DOCUMENT_STYLE) {
83
    Transition.useNative = true;
84
    Transition.supported = true; // TODO: remove
85
} else {
86
    Y.Array.each(VENDORS, function(val) { // then vendor specific
87
        var property = val + 'Transition';
88
        if (property in DOCUMENT[DOCUMENT_ELEMENT].style) {
89
            CAMEL_VENDOR_PREFIX = val;
90
            VENDOR_PREFIX       = Transition._toHyphen(val) + '-';
91
 
92
            Transition.useNative = true;
93
            Transition.supported = true; // TODO: remove
94
            Transition._VENDOR_PREFIX = val;
95
        }
96
    });
97
}
98
 
99
// Map transform property to vendor-specific versions.
100
// One-off required for cssText injection.
101
if (typeof DOCUMENT_STYLE.transform === 'undefined') {
102
    Y.Array.each(VENDORS, function(val) { // then vendor specific
103
        var property = val + 'Transform';
104
        if (typeof DOCUMENT_STYLE[property] !== 'undefined') {
105
            Transition._TRANSFORM = property;
106
        }
107
    });
108
}
109
 
110
if (CAMEL_VENDOR_PREFIX) {
111
    TRANSITION_CAMEL          = CAMEL_VENDOR_PREFIX + 'Transition';
112
    TRANSITION_PROPERTY_CAMEL = CAMEL_VENDOR_PREFIX + 'TransitionProperty';
113
}
114
 
115
TRANSITION_PROPERTY        = VENDOR_PREFIX + 'transition-property';
116
TRANSITION_DURATION        = VENDOR_PREFIX + 'transition-duration';
117
TRANSITION_TIMING_FUNCTION = VENDOR_PREFIX + 'transition-timing-function';
118
TRANSITION_DELAY           = VENDOR_PREFIX + 'transition-delay';
119
 
120
TRANSITION_END    = 'transitionend';
121
ON_TRANSITION_END = 'on' + CAMEL_VENDOR_PREFIX.toLowerCase() + 'transitionend';
122
TRANSITION_END    = VENDOR_TRANSITION_END[CAMEL_VENDOR_PREFIX] || TRANSITION_END;
123
 
124
Transition.fx = {};
125
Transition.toggles = {};
126
 
127
Transition._hasEnd = {};
128
 
129
Transition._reKeywords = /^(?:node|duration|iterations|easing|delay|on|onstart|onend)$/i;
130
 
131
Y.Node.DOM_EVENTS[TRANSITION_END] = 1;
132
 
133
Transition.NAME = 'transition';
134
 
135
Transition.DEFAULT_EASING = 'ease';
136
Transition.DEFAULT_DURATION = 0.5;
137
Transition.DEFAULT_DELAY = 0;
138
 
139
Transition._nodeAttrs = {};
140
 
141
Transition.prototype = {
142
    constructor: Transition,
143
    init: function(node, config) {
144
        var anim = this;
145
        anim._node = node;
146
        if (!anim._running && config) {
147
            anim._config = config;
148
            node._transition = anim; // cache for reuse
149
 
150
            anim._duration = ('duration' in config) ?
151
                config.duration: anim.constructor.DEFAULT_DURATION;
152
 
153
            anim._delay = ('delay' in config) ?
154
                config.delay: anim.constructor.DEFAULT_DELAY;
155
 
156
            anim._easing = config.easing || anim.constructor.DEFAULT_EASING;
157
            anim._count = 0; // track number of animated properties
158
            anim._running = false;
159
 
160
        }
161
 
162
        return anim;
163
    },
164
 
165
    addProperty: function(prop, config) {
166
        var anim = this,
167
            node = this._node,
168
            uid = Y.stamp(node),
169
            nodeInstance = Y.one(node),
170
            attrs = Transition._nodeAttrs[uid],
171
            computed,
172
            compareVal,
173
            dur,
174
            attr,
175
            val;
176
 
177
        if (!attrs) {
178
            attrs = Transition._nodeAttrs[uid] = {};
179
        }
180
 
181
        attr = attrs[prop];
182
 
183
        // might just be a value
184
        if (config && config.value !== undefined) {
185
            val = config.value;
186
        } else if (config !== undefined) {
187
            val = config;
188
            config = EMPTY_OBJ;
189
        }
190
 
191
        if (typeof val === 'function') {
192
            val = val.call(nodeInstance, nodeInstance);
193
        }
194
 
195
        if (attr && attr.transition) {
196
            // take control if another transition owns this property
197
            if (attr.transition !== anim) {
198
                attr.transition._count--; // remapping attr to this transition
199
            }
200
        }
201
 
202
        anim._count++; // properties per transition
203
 
204
        // make 0 async and fire events
205
        dur = ((typeof config.duration !== 'undefined') ? config.duration :
206
                    anim._duration) || 0.0001;
207
 
208
        attrs[prop] = {
209
            value: val,
210
            duration: dur,
211
            delay: (typeof config.delay !== 'undefined') ? config.delay :
212
                    anim._delay,
213
 
214
            easing: config.easing || anim._easing,
215
 
216
            transition: anim
217
        };
218
 
219
        // native end event doesnt fire when setting to same value
220
        // supplementing with timer
221
        // val may be a string or number (height: 0, etc), but computedStyle is always string
222
        computed = Y.DOM.getComputedStyle(node, prop);
223
        compareVal = (typeof val === 'string') ? computed : parseFloat(computed);
224
 
225
        if (Transition.useNative && compareVal === val) {
226
            setTimeout(function() {
227
                anim._onNativeEnd.call(node, {
228
                    propertyName: prop,
229
                    elapsedTime: dur
230
                });
231
            }, dur * 1000);
232
        }
233
    },
234
 
235
    removeProperty: function(prop) {
236
        var anim = this,
237
            attrs = Transition._nodeAttrs[Y.stamp(anim._node)];
238
 
239
        if (attrs && attrs[prop]) {
240
            delete attrs[prop];
241
            anim._count--;
242
        }
243
 
244
    },
245
 
246
    initAttrs: function(config) {
247
        var attr,
248
            node = this._node;
249
 
250
        if (config.transform && !config[Transition._TRANSFORM]) {
251
            config[Transition._TRANSFORM] = config.transform;
252
            delete config.transform; // TODO: copy
253
        }
254
 
255
        for (attr in config) {
256
            if (config.hasOwnProperty(attr) && !Transition._reKeywords.test(attr)) {
257
                this.addProperty(attr, config[attr]);
258
 
259
                // when size is auto or % webkit starts from zero instead of computed
260
                // (https://bugs.webkit.org/show_bug.cgi?id=16020)
261
                // TODO: selective set
262
                if (node.style[attr] === '') {
263
                    Y.DOM.setStyle(node, attr, Y.DOM.getComputedStyle(node, attr));
264
                }
265
            }
266
        }
267
    },
268
 
269
    /**
270
     * Starts or an animation.
271
     * @method run
272
     * @chainable
273
     * @private
274
     */
275
    run: function(callback) {
276
        var anim = this,
277
            node = anim._node,
278
            config = anim._config,
279
            data = {
280
                type: 'transition:start',
281
                config: config
282
            };
283
 
284
 
285
        if (!anim._running) {
286
            anim._running = true;
287
 
288
            if (config.on && config.on.start) {
289
                config.on.start.call(Y.one(node), data);
290
            }
291
 
292
            anim.initAttrs(anim._config);
293
 
294
            anim._callback = callback;
295
            anim._start();
296
        }
297
 
298
 
299
        return anim;
300
    },
301
 
302
    _start: function() {
303
        this._runNative();
304
    },
305
 
306
    _prepDur: function(dur) {
307
        dur = parseFloat(dur) * 1000;
308
 
309
        return dur + 'ms';
310
    },
311
 
312
    _runNative: function() {
313
        var anim = this,
314
            node = anim._node,
315
            uid = Y.stamp(node),
316
            style = node.style,
317
            computed = node.ownerDocument.defaultView.getComputedStyle(node),
318
            attrs = Transition._nodeAttrs[uid],
319
            cssText = '',
320
            cssTransition = computed[Transition._toCamel(TRANSITION_PROPERTY)],
321
 
322
            transitionText = TRANSITION_PROPERTY + ': ',
323
            duration = TRANSITION_DURATION + ': ',
324
            easing = TRANSITION_TIMING_FUNCTION + ': ',
325
            delay = TRANSITION_DELAY + ': ',
326
            hyphy,
327
            attr,
328
            name;
329
 
330
        // preserve existing transitions
331
        if (cssTransition !== 'all') {
332
            transitionText += cssTransition + ',';
333
            duration += computed[Transition._toCamel(TRANSITION_DURATION)] + ',';
334
            easing += computed[Transition._toCamel(TRANSITION_TIMING_FUNCTION)] + ',';
335
            delay += computed[Transition._toCamel(TRANSITION_DELAY)] + ',';
336
 
337
        }
338
 
339
        // run transitions mapped to this instance
340
        for (name in attrs) {
341
            hyphy = Transition._toHyphen(name);
342
            attr = attrs[name];
343
            if ((attr = attrs[name]) && attr.transition === anim) {
344
                if (name in node.style) { // only native styles allowed
345
                    duration += anim._prepDur(attr.duration) + ',';
346
                    delay += anim._prepDur(attr.delay) + ',';
347
                    easing += (attr.easing) + ',';
348
 
349
                    transitionText += hyphy + ',';
350
                    cssText += hyphy + ': ' + attr.value + '; ';
351
                } else {
352
                    this.removeProperty(name);
353
                }
354
            }
355
        }
356
 
357
        transitionText = transitionText.replace(/,$/, ';');
358
        duration = duration.replace(/,$/, ';');
359
        easing = easing.replace(/,$/, ';');
360
        delay = delay.replace(/,$/, ';');
361
 
362
        // only one native end event per node
363
        if (!Transition._hasEnd[uid]) {
364
            node.addEventListener(TRANSITION_END, anim._onNativeEnd, '');
365
            Transition._hasEnd[uid] = true;
366
 
367
        }
368
 
369
        style.cssText += transitionText + duration + easing + delay + cssText;
370
 
371
    },
372
 
373
    _end: function(elapsed) {
374
        var anim = this,
375
            node = anim._node,
376
            callback = anim._callback,
377
            config = anim._config,
378
            data = {
379
                type: 'transition:end',
380
                config: config,
381
                elapsedTime: elapsed
382
            },
383
 
384
            nodeInstance = Y.one(node);
385
 
386
        anim._running = false;
387
        anim._callback = null;
388
 
389
        if (node) {
390
            if (config.on && config.on.end) {
391
                setTimeout(function() { // IE: allow previous update to finish
392
                    config.on.end.call(nodeInstance, data);
393
 
394
                    // nested to ensure proper fire order
395
                    if (callback) {
396
                        callback.call(nodeInstance, data);
397
                    }
398
 
399
                }, 1);
400
            } else if (callback) {
401
                setTimeout(function() { // IE: allow previous update to finish
402
                    callback.call(nodeInstance, data);
403
                }, 1);
404
            }
405
        }
406
 
407
    },
408
 
409
    _endNative: function(name) {
410
        var node = this._node,
411
            value = node.ownerDocument.defaultView.getComputedStyle(node, '')[Transition._toCamel(TRANSITION_PROPERTY)];
412
 
413
        name = Transition._toHyphen(name);
414
        if (typeof value === 'string') {
415
            value = value.replace(new RegExp('(?:^|,\\s)' + name + ',?'), ',');
416
            value = value.replace(/^,|,$/, '');
417
            node.style[TRANSITION_CAMEL] = value;
418
        }
419
    },
420
 
421
    _onNativeEnd: function(e) {
422
        var node = this,
423
            uid = Y.stamp(node),
424
            event = e,//e._event,
425
            name = Transition._toCamel(event.propertyName),
426
            elapsed = event.elapsedTime,
427
            attrs = Transition._nodeAttrs[uid],
428
            attr = attrs[name],
429
            anim = (attr) ? attr.transition : null,
430
            data,
431
            config;
432
 
433
        if (anim) {
434
            anim.removeProperty(name);
435
            anim._endNative(name);
436
            config = anim._config[name];
437
 
438
            data = {
439
                type: 'propertyEnd',
440
                propertyName: name,
441
                elapsedTime: elapsed,
442
                config: config
443
            };
444
 
445
            if (config && config.on && config.on.end) {
446
                config.on.end.call(Y.one(node), data);
447
            }
448
 
449
            if (anim._count <= 0)  { // after propertyEnd fires
450
                anim._end(elapsed);
451
                node.style[TRANSITION_PROPERTY_CAMEL] = ''; // clean up style
452
            }
453
        }
454
    },
455
 
456
    destroy: function() {
457
        var anim = this,
458
            node = anim._node;
459
 
460
        if (node) {
461
            node.removeEventListener(TRANSITION_END, anim._onNativeEnd, false);
462
            anim._node = null;
463
        }
464
    }
465
};
466
 
467
Y.Transition = Transition;
468
Y.TransitionNative = Transition; // TODO: remove
469
 
470
/**
471
 *   Animate one or more css properties to a given value. Requires the "transition" module.
472
 *   <pre>example usage:
473
 *       Y.one('#demo').transition({
474
 *           duration: 1, // in seconds, default is 0.5
475
 *           easing: 'ease-out', // default is 'ease'
476
 *           delay: '1', // delay start for 1 second, default is 0
477
 *
478
 *           height: '10px',
479
 *           width: '10px',
480
 *
481
 *           opacity: { // per property
482
 *               value: 0,
483
 *               duration: 2,
484
 *               delay: 2,
485
 *               easing: 'ease-in'
486
 *           }
487
 *       });
488
 *   </pre>
489
 *   @for Node
490
 *   @method transition
491
 *   @param {Object} config An object containing one or more style properties, a duration and an easing.
492
 *   @param {Function} callback A function to run after the transition has completed.
493
 *   @chainable
494
*/
495
Y.Node.prototype.transition = function(name, config, callback) {
496
    var
497
        transitionAttrs = Transition._nodeAttrs[Y.stamp(this._node)],
498
        anim = (transitionAttrs) ? transitionAttrs.transition || null : null,
499
        fxConfig,
500
        prop;
501
 
502
    if (typeof name === 'string') { // named effect, pull config from registry
503
        if (typeof config === 'function') {
504
            callback = config;
505
            config = null;
506
        }
507
 
508
        fxConfig = Transition.fx[name];
509
 
510
        if (config && typeof config === 'object') {
511
            config = Y.clone(config);
512
 
513
            for (prop in fxConfig) {
514
                if (fxConfig.hasOwnProperty(prop)) {
515
                    if (! (prop in config)) {
516
                        config[prop] = fxConfig[prop];
517
                    }
518
                }
519
            }
520
        } else {
521
            config = fxConfig;
522
        }
523
 
524
    } else { // name is a config, config is a callback or undefined
525
        callback = config;
526
        config = name;
527
    }
528
 
529
    if (anim && !anim._running) {
530
        anim.init(this, config);
531
    } else {
532
        anim = new Transition(this._node, config);
533
    }
534
 
535
    anim.run(callback);
536
    return this;
537
};
538
 
539
Y.Node.prototype.show = function(name, config, callback) {
540
    this._show(); // show prior to transition
541
    if (name && Y.Transition) {
542
        if (typeof name !== 'string' && !name.push) { // named effect or array of effects supercedes default
543
            if (typeof config === 'function') {
544
                callback = config;
545
                config = name;
546
            }
547
            name = Transition.SHOW_TRANSITION;
548
        }
549
        this.transition(name, config, callback);
550
    }
551
    return this;
552
};
553
 
554
Y.NodeList.prototype.show = function(name, config, callback) {
555
    var nodes = this._nodes,
556
        i = 0,
557
        node;
558
 
559
    while ((node = nodes[i++])) {
560
        Y.one(node).show(name, config, callback);
561
    }
562
 
563
    return this;
564
};
565
 
566
 
567
 
568
var _wrapCallBack = function(anim, fn, callback) {
569
    return function() {
570
        if (fn) {
571
            fn.call(anim);
572
        }
573
        if (callback && typeof callback === 'function') {
574
            callback.apply(anim._node, arguments);
575
        }
576
    };
577
};
578
 
579
Y.Node.prototype.hide = function(name, config, callback) {
580
    if (name && Y.Transition) {
581
        if (typeof config === 'function') {
582
            callback = config;
583
            config = null;
584
        }
585
 
586
        callback = _wrapCallBack(this, this._hide, callback); // wrap with existing callback
587
        if (typeof name !== 'string' && !name.push) { // named effect or array of effects supercedes default
588
            if (typeof config === 'function') {
589
                callback = config;
590
                config = name;
591
            }
592
            name = Transition.HIDE_TRANSITION;
593
        }
594
        this.transition(name, config, callback);
595
    } else {
596
        this._hide();
597
    }
598
    return this;
599
};
600
 
601
Y.NodeList.prototype.hide = function(name, config, callback) {
602
    var nodes = this._nodes,
603
        i = 0,
604
        node;
605
 
606
    while ((node = nodes[i++])) {
607
        Y.one(node).hide(name, config, callback);
608
    }
609
 
610
    return this;
611
};
612
 
613
/**
614
 *   Animate one or more css properties to a given value. Requires the "transition" module.
615
 *   <pre>example usage:
616
 *       Y.all('.demo').transition({
617
 *           duration: 1, // in seconds, default is 0.5
618
 *           easing: 'ease-out', // default is 'ease'
619
 *           delay: '1', // delay start for 1 second, default is 0
620
 *
621
 *           height: '10px',
622
 *           width: '10px',
623
 *
624
 *           opacity: { // per property
625
 *               value: 0,
626
 *               duration: 2,
627
 *               delay: 2,
628
 *               easing: 'ease-in'
629
 *           }
630
 *       });
631
 *   </pre>
632
 *   @for NodeList
633
 *   @method transition
634
 *   @param {Object} config An object containing one or more style properties, a duration and an easing.
635
 *   @param {Function} callback A function to run after the transition has completed. The callback fires
636
 *       once per item in the NodeList.
637
 *   @param {Boolean} callbackOnce If true, the callback will be called only after the
638
 *       last transition has completed
639
 *   @chainable
640
*/
641
Y.NodeList.prototype.transition = function(config, callback, callbackOnce) {
642
    var nodes = this._nodes,
643
        size = this.size(),
644
         i = 0,
645
        callbackOnce = callbackOnce === true,
646
        node;
647
 
648
    while ((node = nodes[i++])) {
649
        if (i < size && callbackOnce){
650
            Y.one(node).transition(config);
651
        } else {
652
            Y.one(node).transition(config, callback);
653
        }
654
    }
655
 
656
    return this;
657
};
658
 
659
Y.Node.prototype.toggleView = function(name, on, callback) {
660
    this._toggles = this._toggles || [];
661
    callback = arguments[arguments.length - 1];
662
 
663
    if (typeof name !== 'string') { // no transition, just toggle
664
        on = name;
665
        this._toggleView(on, callback); // call original _toggleView in Y.Node
666
        return;
667
    }
668
 
669
    if (typeof on === 'function') { // Ignore "on" if used for callback argument.
670
        on = undefined;
671
    }
672
 
673
    if (typeof on === 'undefined' && name in this._toggles) { // reverse current toggle
674
        on = ! this._toggles[name];
675
    }
676
 
677
    on = (on) ? 1 : 0;
678
    if (on) {
679
        this._show();
680
    }  else {
681
        callback = _wrapCallBack(this, this._hide, callback);
682
    }
683
 
684
    this._toggles[name] = on;
685
    this.transition(Y.Transition.toggles[name][on], callback);
686
 
687
    return this;
688
};
689
 
690
Y.NodeList.prototype.toggleView = function(name, on, callback) {
691
    var nodes = this._nodes,
692
        i = 0,
693
        node;
694
 
695
    while ((node = nodes[i++])) {
696
        node = Y.one(node);
697
        node.toggleView.apply(node, arguments);
698
    }
699
 
700
    return this;
701
};
702
 
703
Y.mix(Transition.fx, {
704
    fadeOut: {
705
        opacity: 0,
706
        duration: 0.5,
707
        easing: 'ease-out'
708
    },
709
 
710
    fadeIn: {
711
        opacity: 1,
712
        duration: 0.5,
713
        easing: 'ease-in'
714
    },
715
 
716
    sizeOut: {
717
        height: 0,
718
        width: 0,
719
        duration: 0.75,
720
        easing: 'ease-out'
721
    },
722
 
723
    sizeIn: {
724
        height: function(node) {
725
            return node.get('scrollHeight') + 'px';
726
        },
727
        width: function(node) {
728
            return node.get('scrollWidth') + 'px';
729
        },
730
        duration: 0.5,
731
        easing: 'ease-in',
732
 
733
        on: {
734
            start: function() {
735
                var overflow = this.getStyle('overflow');
736
                if (overflow !== 'hidden') { // enable scrollHeight/Width
737
                    this.setStyle('overflow', 'hidden');
738
                    this._transitionOverflow = overflow;
739
                }
740
            },
741
 
742
            end: function() {
743
                if (this._transitionOverflow) { // revert overridden value
744
                    this.setStyle('overflow', this._transitionOverflow);
745
                    delete this._transitionOverflow;
746
                }
747
            }
748
        }
749
    }
750
});
751
 
752
Y.mix(Transition.toggles, {
753
    size: ['sizeOut', 'sizeIn'],
754
    fade: ['fadeOut', 'fadeIn']
755
});
756
 
757
 
758
}, '3.18.1', {"requires": ["node-style"]});