3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('transition', function(Y) {
10 * Provides the transition method for Node.
11 * Transition has no API of its own, but adds the transition method to Node.
14 * @requires node-style
17 var CAMEL_VENDOR_PREFIX = '',
19 DOCUMENT = Y.config.doc,
20 DOCUMENT_ELEMENT = 'documentElement',
21 TRANSITION = 'transition',
22 TRANSITION_CAMEL = 'Transition',
23 TRANSITION_PROPERTY_CAMEL,
26 TRANSITION_TIMING_FUNCTION,
39 VENDOR_TRANSITION_END = {
40 Webkit: 'webkitTransitionEnd'
44 * A class for constructing transition instances.
45 * Adds the "transition" method to Node.
50 Transition = function() {
51 this.init.apply(this, arguments);
54 Transition._toCamel = function(property) {
55 property = property.replace(/-([a-z])/gi, function(m0, m1) {
56 return m1.toUpperCase();
62 Transition._toHyphen = function(property) {
63 property = property.replace(/([A-Z]?)([a-z]+)([A-Z]?)/g, function(m0, m1, m2, m3) {
64 var str = ((m1) ? '-' + m1.toLowerCase() : '') + m2;
67 str += '-' + m3.toLowerCase();
76 Transition.SHOW_TRANSITION = 'fadeIn';
77 Transition.HIDE_TRANSITION = 'fadeOut';
79 Transition.useNative = false;
81 Y.Array.each(VENDORS, function(val) { // then vendor specific
82 var property = val + TRANSITION_CAMEL;
83 if (property in DOCUMENT[DOCUMENT_ELEMENT].style) {
84 CAMEL_VENDOR_PREFIX = val;
85 VENDOR_PREFIX = Transition._toHyphen(val) + '-';
87 Transition.useNative = true;
88 Transition.supported = true; // TODO: remove
89 Transition._VENDOR_PREFIX = val;
93 TRANSITION_CAMEL = CAMEL_VENDOR_PREFIX + TRANSITION_CAMEL;
94 TRANSITION_PROPERTY_CAMEL = CAMEL_VENDOR_PREFIX + 'TransitionProperty';
95 TRANSITION_PROPERTY = VENDOR_PREFIX + 'transition-property';
96 TRANSITION_DURATION = VENDOR_PREFIX + 'transition-duration';
97 TRANSITION_TIMING_FUNCTION = VENDOR_PREFIX + 'transition-timing-function';
98 TRANSITION_DELAY = VENDOR_PREFIX + 'transition-delay';
99 TRANSITION_END = 'transitionend';
100 ON_TRANSITION_END = 'on' + CAMEL_VENDOR_PREFIX.toLowerCase() + 'transitionend';
102 TRANSITION_END = VENDOR_TRANSITION_END[CAMEL_VENDOR_PREFIX] || TRANSITION_END;
104 TRANSFORM_CAMEL = CAMEL_VENDOR_PREFIX + 'Transform';
107 Transition.toggles = {};
109 Transition._hasEnd = {};
111 Transition._reKeywords = /^(?:node|duration|iterations|easing|delay|on|onstart|onend)$/i;
113 Y.Node.DOM_EVENTS[TRANSITION_END] = 1;
115 Transition.NAME = 'transition';
117 Transition.DEFAULT_EASING = 'ease';
118 Transition.DEFAULT_DURATION = 0.5;
119 Transition.DEFAULT_DELAY = 0;
121 Transition._nodeAttrs = {};
123 Transition.prototype = {
124 constructor: Transition,
125 init: function(node, config) {
128 if (!anim._running && config) {
129 anim._config = config;
130 node._transition = anim; // cache for reuse
132 anim._duration = ('duration' in config) ?
133 config.duration: anim.constructor.DEFAULT_DURATION;
135 anim._delay = ('delay' in config) ?
136 config.delay: anim.constructor.DEFAULT_DELAY;
138 anim._easing = config.easing || anim.constructor.DEFAULT_EASING;
139 anim._count = 0; // track number of animated properties
140 anim._running = false;
147 addProperty: function(prop, config) {
151 nodeInstance = Y.one(node),
152 attrs = Transition._nodeAttrs[uid],
160 attrs = Transition._nodeAttrs[uid] = {};
165 // might just be a value
166 if (config && config.value !== undefined) {
168 } else if (config !== undefined) {
173 if (typeof val === 'function') {
174 val = val.call(nodeInstance, nodeInstance);
177 if (attr && attr.transition) {
178 // take control if another transition owns this property
179 if (attr.transition !== anim) {
180 attr.transition._count--; // remapping attr to this transition
184 anim._count++; // properties per transition
186 // make 0 async and fire events
187 dur = ((typeof config.duration != 'undefined') ? config.duration :
188 anim._duration) || 0.0001;
193 delay: (typeof config.delay != 'undefined') ? config.delay :
196 easing: config.easing || anim._easing,
201 // native end event doesnt fire when setting to same value
202 // supplementing with timer
203 // val may be a string or number (height: 0, etc), but computedStyle is always string
204 computed = Y.DOM.getComputedStyle(node, prop);
205 compareVal = (typeof val === 'string') ? computed : parseFloat(computed);
207 if (Transition.useNative && compareVal === val) {
208 setTimeout(function() {
209 anim._onNativeEnd.call(node, {
217 removeProperty: function(prop) {
219 attrs = Transition._nodeAttrs[Y.stamp(anim._node)];
221 if (attrs && attrs[prop]) {
228 initAttrs: function(config) {
232 if (config.transform && !config[TRANSFORM_CAMEL]) {
233 config[TRANSFORM_CAMEL] = config.transform;
234 delete config.transform; // TODO: copy
237 for (attr in config) {
238 if (config.hasOwnProperty(attr) && !Transition._reKeywords.test(attr)) {
239 this.addProperty(attr, config[attr]);
241 // when size is auto or % webkit starts from zero instead of computed
242 // (https://bugs.webkit.org/show_bug.cgi?id=16020)
243 // TODO: selective set
244 if (node.style[attr] === '') {
245 Y.DOM.setStyle(node, attr, Y.DOM.getComputedStyle(node, attr));
252 * Starts or an animation.
257 run: function(callback) {
260 config = anim._config,
262 type: 'transition:start',
267 if (!anim._running) {
268 anim._running = true;
270 if (config.on && config.on.start) {
271 config.on.start.call(Y.one(node), data);
274 anim.initAttrs(anim._config);
276 anim._callback = callback;
288 _prepDur: function(dur) {
289 dur = parseFloat(dur);
294 _runNative: function(time) {
299 computed = node.ownerDocument.defaultView.getComputedStyle(node),
300 attrs = Transition._nodeAttrs[uid],
302 cssTransition = computed[Transition._toCamel(TRANSITION_PROPERTY)],
304 transitionText = TRANSITION_PROPERTY + ': ',
305 duration = TRANSITION_DURATION + ': ',
306 easing = TRANSITION_TIMING_FUNCTION + ': ',
307 delay = TRANSITION_DELAY + ': ',
312 // preserve existing transitions
313 if (cssTransition !== 'all') {
314 transitionText += cssTransition + ',';
315 duration += computed[Transition._toCamel(TRANSITION_DURATION)] + ',';
316 easing += computed[Transition._toCamel(TRANSITION_TIMING_FUNCTION)] + ',';
317 delay += computed[Transition._toCamel(TRANSITION_DELAY)] + ',';
321 // run transitions mapped to this instance
322 for (name in attrs) {
323 hyphy = Transition._toHyphen(name);
325 if ((attr = attrs[name]) && attr.transition === anim) {
326 if (name in node.style) { // only native styles allowed
327 duration += anim._prepDur(attr.duration) + ',';
328 delay += anim._prepDur(attr.delay) + ',';
329 easing += (attr.easing) + ',';
331 transitionText += hyphy + ',';
332 cssText += hyphy + ': ' + attr.value + '; ';
334 this.removeProperty(name);
339 transitionText = transitionText.replace(/,$/, ';');
340 duration = duration.replace(/,$/, ';');
341 easing = easing.replace(/,$/, ';');
342 delay = delay.replace(/,$/, ';');
344 // only one native end event per node
345 if (!Transition._hasEnd[uid]) {
346 node.addEventListener(TRANSITION_END, anim._onNativeEnd, '');
347 Transition._hasEnd[uid] = true;
351 style.cssText += transitionText + duration + easing + delay + cssText;
355 _end: function(elapsed) {
358 callback = anim._callback,
359 config = anim._config,
361 type: 'transition:end',
366 nodeInstance = Y.one(node);
368 anim._running = false;
369 anim._callback = null;
372 if (config.on && config.on.end) {
373 setTimeout(function() { // IE: allow previous update to finish
374 config.on.end.call(nodeInstance, data);
376 // nested to ensure proper fire order
378 callback.call(nodeInstance, data);
382 } else if (callback) {
383 setTimeout(function() { // IE: allow previous update to finish
384 callback.call(nodeInstance, data);
391 _endNative: function(name) {
392 var node = this._node,
393 value = node.ownerDocument.defaultView.getComputedStyle(node, '')[Transition._toCamel(TRANSITION_PROPERTY)];
395 name = Transition._toHyphen(name);
396 if (typeof value === 'string') {
397 value = value.replace(new RegExp('(?:^|,\\s)' + name + ',?'), ',');
398 value = value.replace(/^,|,$/, '');
399 node.style[TRANSITION_CAMEL] = value;
403 _onNativeEnd: function(e) {
406 event = e,//e._event,
407 name = Transition._toCamel(event.propertyName),
408 elapsed = event.elapsedTime,
409 attrs = Transition._nodeAttrs[uid],
411 anim = (attr) ? attr.transition : null,
416 anim.removeProperty(name);
417 anim._endNative(name);
418 config = anim._config[name];
423 elapsedTime: elapsed,
427 if (config && config.on && config.on.end) {
428 config.on.end.call(Y.one(node), data);
431 if (anim._count <= 0) { // after propertyEnd fires
433 node.style[TRANSITION_PROPERTY_CAMEL] = ''; // clean up style
438 destroy: function() {
443 node.removeEventListener(TRANSITION_END, anim._onNativeEnd, false);
449 Y.Transition = Transition;
450 Y.TransitionNative = Transition; // TODO: remove
453 * Animate one or more css properties to a given value. Requires the "transition" module.
454 * <pre>example usage:
455 * Y.one('#demo').transition({
456 * duration: 1, // in seconds, default is 0.5
457 * easing: 'ease-out', // default is 'ease'
458 * delay: '1', // delay start for 1 second, default is 0
463 * opacity: { // per property
473 * @param {Object} config An object containing one or more style properties, a duration and an easing.
474 * @param {Function} callback A function to run after the transition has completed.
477 Y.Node.prototype.transition = function(name, config, callback) {
479 transitionAttrs = Transition._nodeAttrs[Y.stamp(this._node)],
480 anim = (transitionAttrs) ? transitionAttrs.transition || null : null,
484 if (typeof name === 'string') { // named effect, pull config from registry
485 if (typeof config === 'function') {
490 fxConfig = Transition.fx[name];
492 if (config && typeof config !== 'boolean') {
493 config = Y.clone(config);
495 for (prop in fxConfig) {
496 if (fxConfig.hasOwnProperty(prop)) {
497 if (! (prop in config)) {
498 config[prop] = fxConfig[prop];
506 } else { // name is a config, config is a callback or undefined
511 if (anim && !anim._running) {
512 anim.init(this, config);
514 anim = new Transition(this._node, config);
521 Y.Node.prototype.show = function(name, config, callback) {
522 this._show(); // show prior to transition
523 if (name && Y.Transition) {
524 if (typeof name !== 'string' && !name.push) { // named effect or array of effects supercedes default
525 if (typeof config === 'function') {
529 name = Transition.SHOW_TRANSITION;
531 this.transition(name, config, callback);
533 else if (name && !Y.Transition) { Y.log('unable to transition show; missing transition module', 'warn', 'node'); }
537 var _wrapCallBack = function(anim, fn, callback) {
543 callback.apply(anim._node, arguments);
548 Y.Node.prototype.hide = function(name, config, callback) {
549 if (name && Y.Transition) {
550 if (typeof config === 'function') {
555 callback = _wrapCallBack(this, this._hide, callback); // wrap with existing callback
556 if (typeof name !== 'string' && !name.push) { // named effect or array of effects supercedes default
557 if (typeof config === 'function') {
561 name = Transition.HIDE_TRANSITION;
563 this.transition(name, config, callback);
564 } else if (name && !Y.Transition) { Y.log('unable to transition hide; missing transition module', 'warn', 'node');
572 * Animate one or more css properties to a given value. Requires the "transition" module.
573 * <pre>example usage:
574 * Y.all('.demo').transition({
575 * duration: 1, // in seconds, default is 0.5
576 * easing: 'ease-out', // default is 'ease'
577 * delay: '1', // delay start for 1 second, default is 0
582 * opacity: { // per property
592 * @param {Object} config An object containing one or more style properties, a duration and an easing.
593 * @param {Function} callback A function to run after the transition has completed. The callback fires
594 * once per item in the NodeList.
597 Y.NodeList.prototype.transition = function(config, callback) {
598 var nodes = this._nodes,
602 while ((node = nodes[i++])) {
603 Y.one(node).transition(config, callback);
609 Y.Node.prototype.toggleView = function(name, on, callback) {
610 this._toggles = this._toggles || [];
611 callback = arguments[arguments.length - 1];
613 if (typeof name == 'boolean') { // no transition, just toggle
618 name = name || Y.Transition.DEFAULT_TOGGLE;
620 if (typeof on == 'undefined' && name in this._toggles) { // reverse current toggle
621 on = ! this._toggles[name];
628 callback = _wrapCallBack(this, this._hide, callback);
631 this._toggles[name] = on;
632 this.transition(Y.Transition.toggles[name][on], callback);
637 Y.NodeList.prototype.toggleView = function(name, on, callback) {
638 var nodes = this._nodes,
642 while ((node = nodes[i++])) {
643 Y.one(node).toggleView(name, on, callback);
649 Y.mix(Transition.fx, {
670 height: function(node) {
671 return node.get('scrollHeight') + 'px';
673 width: function(node) {
674 return node.get('scrollWidth') + 'px';
681 var overflow = this.getStyle('overflow');
682 if (overflow !== 'hidden') { // enable scrollHeight/Width
683 this.setStyle('overflow', 'hidden');
684 this._transitionOverflow = overflow;
689 if (this._transitionOverflow) { // revert overridden value
690 this.setStyle('overflow', this._transitionOverflow);
691 delete this._transitionOverflow;
698 Y.mix(Transition.toggles, {
699 size: ['sizeOut', 'sizeIn'],
700 fade: ['fadeOut', 'fadeIn']
703 Transition.DEFAULT_TOGGLE = 'fade';
707 }, '3.5.1' ,{requires:['node-style']});