transition-native.js revision 416ff6e8477a5950c56e04adea6abf838140a852
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington/**
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington* Provides the transition method for Node.
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington* Transition has no API of its own, but adds the transition method to Node.
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington*
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington* @module transition
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington* @requires node-style
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington*/
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellingtonvar CAMEL_VENDOR_PREFIX = '',
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington VENDOR_PREFIX = '',
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington DOCUMENT = Y.config.doc,
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington DOCUMENT_ELEMENT = 'documentElement',
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington TRANSITION = 'transition',
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington TRANSITION_CAMEL = 'Transition',
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington TRANSITION_PROPERTY_CAMEL,
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington TRANSITION_PROPERTY,
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington TRANSITION_DURATION,
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington TRANSITION_TIMING_FUNCTION,
99776003811a413457a2c35a808ad860df877d24Mark Andrews TRANSITION_DELAY,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington TRANSITION_END,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington ON_TRANSITION_END,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington TRANSFORM_CAMEL,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington EMPTY_OBJ = {},
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington VENDORS = [
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington 'Webkit',
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington 'Moz'
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington ],
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington VENDOR_TRANSITION_END = {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington Webkit: 'webkitTransitionEnd'
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington },
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington/**
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington * A class for constructing transition instances.
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington * Adds the "transition" method to Node.
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington * @class Transition
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington * @constructor
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington */
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition = function() {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington this.init.apply(this, arguments);
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington};
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition._toCamel = function(property) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington property = property.replace(/-([a-z])/gi, function(m0, m1) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington return m1.toUpperCase();
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington });
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington return property;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington};
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition.SHOW_TRANSITION = 'fadeIn';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition.HIDE_TRANSITION = 'fadeOut';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition._toHyphen = function(property) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington property = property.replace(/([A-Z]?)([a-z]+)([A-Z]?)/g, function(m0, m1, m2, m3) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington var str = ((m1) ? '-' + m1.toLowerCase() : '') + m2;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (m3) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington str += '-' + m3.toLowerCase();
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington return str;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington });
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington return property;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington};
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition.useNative = false;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonY.Array.each(VENDORS, function(val) { // then vendor specific
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington var property = val + TRANSITION_CAMEL;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (property in DOCUMENT[DOCUMENT_ELEMENT].style) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington CAMEL_VENDOR_PREFIX = val;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington VENDOR_PREFIX = Transition._toHyphen(val) + '-';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington Transition.useNative = true;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington Transition.supported = true; // TODO: remove
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington});
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTRANSITION_CAMEL = CAMEL_VENDOR_PREFIX + TRANSITION_CAMEL;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTRANSITION_PROPERTY_CAMEL = CAMEL_VENDOR_PREFIX + 'TransitionProperty';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTRANSITION_PROPERTY = VENDOR_PREFIX + 'transition-property';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTRANSITION_DURATION = VENDOR_PREFIX + 'transition-duration';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTRANSITION_TIMING_FUNCTION = VENDOR_PREFIX + 'transition-timing-function';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTRANSITION_DELAY = VENDOR_PREFIX + 'transition-delay';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTRANSITION_END = 'transitionend';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonON_TRANSITION_END = 'on' + CAMEL_VENDOR_PREFIX.toLowerCase() + 'transitionend';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTRANSITION_END = VENDOR_TRANSITION_END[CAMEL_VENDOR_PREFIX] || TRANSITION_END;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTRANSFORM_CAMEL = CAMEL_VENDOR_PREFIX + 'Transform';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition.fx = {};
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition.toggles = {};
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition._hasEnd = {};
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition._reKeywords = /^(?:node|duration|iterations|easing|delay|on|onstart|onend)$/i;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonY.Node.DOM_EVENTS[TRANSITION_END] = 1;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition.NAME = 'transition';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition.DEFAULT_EASING = 'ease';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition.DEFAULT_DURATION = 0.5;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition.DEFAULT_DELAY = 0;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition._nodeAttrs = {};
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian WellingtonTransition.prototype = {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington constructor: Transition,
99776003811a413457a2c35a808ad860df877d24Mark Andrews init: function(node, config) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington var anim = this;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._node = node;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (!anim._running && config) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._config = config;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington node._transition = anim; // cache for reuse
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._duration = ('duration' in config) ?
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington config.duration: anim.constructor.DEFAULT_DURATION;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._delay = ('delay' in config) ?
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington config.delay: anim.constructor.DEFAULT_DELAY;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._easing = config.easing || anim.constructor.DEFAULT_EASING;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._count = 0; // track number of animated properties
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._running = false;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington return anim;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington },
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington addProperty: function(prop, config) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington var anim = this,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington node = this._node,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington uid = Y.stamp(node),
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington nodeInstance = Y.one(node),
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington attrs = Transition._nodeAttrs[uid],
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington computed,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington compareVal,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington dur,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington attr,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington val;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (!attrs) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington attrs = Transition._nodeAttrs[uid] = {};
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington attr = attrs[prop];
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington // might just be a value
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (config && config.value !== undefined) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington val = config.value;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington } else if (config !== undefined) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington val = config;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington config = EMPTY_OBJ;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (typeof val === 'function') {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington val = val.call(nodeInstance, nodeInstance);
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (attr && attr.transition) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington // take control if another transition owns this property
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (attr.transition !== anim) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington attr.transition._count--; // remapping attr to this transition
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._count++; // properties per transition
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington // make 0 async and fire events
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington dur = ((typeof config.duration != 'undefined') ? config.duration :
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._duration) || 0.0001;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington attrs[prop] = {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington value: val,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington duration: dur,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington delay: (typeof config.delay != 'undefined') ? config.delay :
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._delay,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington easing: config.easing || anim._easing,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington transition: anim
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington };
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington // native end event doesnt fire when setting to same value
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington // supplementing with timer
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington // val may be a string or number (height: 0, etc), but computedStyle is always string
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington computed = Y.DOM.getComputedStyle(node, prop);
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington compareVal = (typeof val === 'string') ? computed : parseFloat(computed);
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (Transition.useNative && compareVal === val) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington setTimeout(function() {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._onNativeEnd.call(node, {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington propertyName: prop,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington elapsedTime: dur
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington });
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }, dur * 1000);
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington },
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington removeProperty: function(prop) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington var anim = this,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington attrs = Transition._nodeAttrs[Y.stamp(anim._node)];
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (attrs && attrs[prop]) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington delete attrs[prop];
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._count--;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington },
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington initAttrs: function(config) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington var attr,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington node = this._node;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (config.transform && !config[TRANSFORM_CAMEL]) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington config[TRANSFORM_CAMEL] = config.transform;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington delete config.transform; // TODO: copy
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington for (attr in config) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (config.hasOwnProperty(attr) && !Transition._reKeywords.test(attr)) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington this.addProperty(attr, config[attr]);
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington // when size is auto or % webkit starts from zero instead of computed
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington // (https://bugs.webkit.org/show_bug.cgi?id=16020)
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington // TODO: selective set
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (node.style[attr] === '') {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington Y.DOM.setStyle(node, attr, Y.DOM.getComputedStyle(node, attr));
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington },
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington /**
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington * Starts or an animation.
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington * @method run
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington * @chainable
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington * @private
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington */
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington run: function(callback) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington var anim = this,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington node = anim._node,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington config = anim._config,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington data = {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington type: 'transition:start',
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington config: config
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington };
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (!anim._running) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._running = true;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington //anim._node.fire('transition:start', data);
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (config.on && config.on.start) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington config.on.start.call(Y.one(node), data);
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim.initAttrs(anim._config);
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._callback = callback;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington anim._start();
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington return anim;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington },
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington _start: function() {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington this._runNative();
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington },
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington _prepDur: function(dur) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington dur = parseFloat(dur);
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington return dur + 's';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington },
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington _runNative: function(time) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington var anim = this,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington node = anim._node,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington uid = Y.stamp(node),
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington style = node.style,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington computed = getComputedStyle(node),
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington attrs = Transition._nodeAttrs[uid],
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington cssText = '',
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington cssTransition = computed[Transition._toCamel(TRANSITION_PROPERTY)],
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington transitionText = TRANSITION_PROPERTY + ': ',
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington duration = TRANSITION_DURATION + ': ',
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington easing = TRANSITION_TIMING_FUNCTION + ': ',
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington delay = TRANSITION_DELAY + ': ',
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington hyphy,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington attr,
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington name;
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington // preserve existing transitions
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (cssTransition !== 'all') {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington transitionText += cssTransition + ',';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington duration += computed[Transition._toCamel(TRANSITION_DURATION)] + ',';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington easing += computed[Transition._toCamel(TRANSITION_TIMING_FUNCTION)] + ',';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington delay += computed[Transition._toCamel(TRANSITION_DELAY)] + ',';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington }
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington // run transitions mapped to this instance
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington for (name in attrs) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington hyphy = Transition._toHyphen(name);
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington attr = attrs[name];
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if ((attr = attrs[name]) && attr.transition === anim) {
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington if (name in node.style) { // only native styles allowed
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington duration += anim._prepDur(attr.duration) + ',';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington delay += anim._prepDur(attr.delay) + ',';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington easing += (attr.easing) + ',';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington transitionText += hyphy + ',';
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington cssText += hyphy + ': ' + attr.value + '; ';
} else {
this.removeProperty(name);
}
}
}
transitionText = transitionText.replace(/,$/, ';');
duration = duration.replace(/,$/, ';');
easing = easing.replace(/,$/, ';');
delay = delay.replace(/,$/, ';');
// only one native end event per node
if (!Transition._hasEnd[uid]) {
//anim._detach = Y.on(TRANSITION_END, anim._onNativeEnd, node);
//node[ON_TRANSITION_END] = anim._onNativeEnd;
node.addEventListener(TRANSITION_END, anim._onNativeEnd, '');
Transition._hasEnd[uid] = true;
}
//setTimeout(function() { // allow updates to apply (size fix, onstart, etc)
style.cssText += transitionText + duration + easing + delay + cssText;
//}, 1);
},
_end: function(elapsed) {
var anim = this,
node = anim._node,
callback = anim._callback,
config = anim._config,
data = {
type: 'transition:end',
config: config,
elapsedTime: elapsed
},
nodeInstance = Y.one(node);
anim._running = false;
anim._callback = null;
if (node) {
if (config.on && config.on.end) {
setTimeout(function() { // IE: allow previous update to finish
config.on.end.call(nodeInstance, data);
// nested to ensure proper fire order
if (callback) {
callback.call(nodeInstance, data);
}
}, 1);
} else if (callback) {
setTimeout(function() { // IE: allow previous update to finish
callback.call(nodeInstance, data);
}, 1);
}
//node.fire('transition:end', data);
}
},
_endNative: function(name) {
var node = this._node,
value = node.ownerDocument.defaultView.getComputedStyle(node, '')[Transition._toCamel(TRANSITION_PROPERTY)];
if (typeof value === 'string') {
value = value.replace(new RegExp('(?:^|,\\s)' + name + ',?'), ',');
value = value.replace(/^,|,$/, '');
node.style[TRANSITION_CAMEL] = value;
}
},
_onNativeEnd: function(e) {
var node = this,
uid = Y.stamp(node),
event = e,//e._event,
name = Transition._toCamel(event.propertyName),
elapsed = event.elapsedTime,
attrs = Transition._nodeAttrs[uid],
attr = attrs[name],
anim = (attr) ? attr.transition : null,
data,
config;
if (anim) {
anim.removeProperty(name);
anim._endNative(name);
config = anim._config[name];
data = {
type: 'propertyEnd',
propertyName: name,
elapsedTime: elapsed,
config: config
};
if (config && config.on && config.on.end) {
config.on.end.call(Y.one(node), data);
}
//node.fire('transition:propertyEnd', data);
if (anim._count <= 0) { // after propertyEnd fires
anim._end(elapsed);
}
}
},
destroy: function() {
var anim = this,
node = anim._node;
/*
if (anim._detach) {
anim._detach.detach();
}
*/
//anim._node[ON_TRANSITION_END] = null;
if (node) {
node.removeEventListener(TRANSITION_END, anim._onNativeEnd, false);
anim._node = null;
}
}
};
Y.Transition = Transition;
Y.TransitionNative = Transition; // TODO: remove
/**
* Animate one or more css properties to a given value. Requires the "transition" module.
* <pre>example usage:
* Y.one('#demo').transition({
* duration: 1, // in seconds, default is 0.5
* easing: 'ease-out', // default is 'ease'
* delay: '1', // delay start for 1 second, default is 0
*
* height: '10px',
* width: '10px',
*
* opacity: { // per property
* value: 0,
* duration: 2,
* delay: 2,
* easing: 'ease-in'
* }
* });
* </pre>
* @for Node
* @method transition
* @param {Object} config An object containing one or more style properties, a duration and an easing.
* @param {Function} callback A function to run after the transition has completed.
* @chainable
*/
Y.Node.prototype.transition = function(name, config, callback) {
var
transitionAttrs = Transition._nodeAttrs[Y.stamp(this._node)],
anim = (transitionAttrs) ? transitionAttrs.transition || null : null,
fxConfig,
prop;
if (typeof name === 'string') { // named effect, pull config from registry
if (typeof config === 'function') {
callback = config;
config = null;
}
fxConfig = Transition.fx[name];
if (config && typeof config !== 'boolean') {
config = Y.clone(config);
for (prop in fxConfig) {
if (fxConfig.hasOwnProperty(prop)) {
if (! (prop in config)) {
config[prop] = fxConfig[prop];
}
}
}
} else {
config = fxConfig;
}
} else { // name is a config, config is a callback or undefined
callback = config;
config = name;
}
if (anim && !anim._running) {
anim.init(this, config);
} else {
anim = new Transition(this._node, config);
}
anim.run(callback);
return this;
};
Y.Node.prototype.show = function(name, config, callback) {
this._show(); // show prior to transition
if (name && Y.Transition) {
if (typeof name !== 'string' && !name.push) { // named effect or array of effects supercedes default
if (typeof config === 'function') {
callback = config;
config = name;
}
name = Transition.SHOW_TRANSITION;
}
this.transition(name, config, callback);
}
else if (name && !Y.Transition) { Y.log('unable to transition show; missing transition module', 'warn', 'node'); }
return this;
};
var _wrapCallBack = function(anim, fn, callback) {
return function() {
if (fn) {
fn.call(anim);
}
if (callback) {
callback.apply(anim._node, arguments);
}
};
};
Y.Node.prototype.hide = function(name, config, callback) {
if (name && Y.Transition) {
if (typeof config === 'function') {
callback = config;
config = null;
}
callback = _wrapCallBack(this, this._hide, callback); // wrap with existing callback
if (typeof name !== 'string' && !name.push) { // named effect or array of effects supercedes default
if (typeof config === 'function') {
callback = config;
config = name;
}
name = Transition.HIDE_TRANSITION;
}
this.transition(name, config, callback);
} else if (name && !Y.Transition) { Y.log('unable to transition hide; missing transition module', 'warn', 'node'); // end if on nex
} else {
this._hide();
}
return this;
};
/**
* Animate one or more css properties to a given value. Requires the "transition" module.
* <pre>example usage:
* Y.all('.demo').transition({
* duration: 1, // in seconds, default is 0.5
* easing: 'ease-out', // default is 'ease'
* delay: '1', // delay start for 1 second, default is 0
*
* height: '10px',
* width: '10px',
*
* opacity: { // per property
* value: 0,
* duration: 2,
* delay: 2,
* easing: 'ease-in'
* }
* });
* </pre>
* @for NodeList
* @method transition
* @param {Object} config An object containing one or more style properties, a duration and an easing.
* @param {Function} callback A function to run after the transition has completed. The callback fires
* once per item in the NodeList.
* @chainable
*/
Y.NodeList.prototype.transition = function(config, callback) {
var nodes = this._nodes,
i = 0,
node;
while ((node = nodes[i++])) {
Y.one(node).transition(config, callback);
}
return this;
};
Y.Node.prototype.toggleView = function(name, on, callback) {
this._toggles = this._toggles || [];
callback = arguments[arguments.length - 1];
if (typeof name == 'boolean') { // no transition, just toggle
on = name;
name = null;
}
name = name || Y.Transition.DEFAULT_TOGGLE;
if (typeof on == 'undefined' && name in this._toggles) { // reverse current toggle
on = ! this._toggles[name];
}
on = (on) ? 1 : 0;
if (on) {
this._show();
} else {
callback = _wrapCallBack(this, this._hide, callback);
}
this._toggles[name] = on;
this.transition(Y.Transition.toggles[name][on], callback);
return this;
};
Y.NodeList.prototype.toggleView = function(name, on, callback) {
var nodes = this._nodes,
i = 0,
node;
while ((node = nodes[i++])) {
Y.one(node).toggleView(name, on, callback);
}
return this;
};
Y.mix(Transition.fx, {
fadeOut: {
opacity: 0,
duration: 0.5,
easing: 'ease-out'
},
fadeIn: {
opacity: 1,
duration: 0.5,
easing: 'ease-in'
},
sizeOut: {
height: 0,
width: 0,
duration: 0.75,
easing: 'ease-out'
},
sizeIn: {
height: function(node) {
return node.get('scrollHeight') + 'px';
},
width: function(node) {
return node.get('scrollWidth') + 'px';
},
duration: 0.5,
easing: 'ease-in',
on: {
start: function() {
var overflow = this.getStyle('overflow');
if (overflow !== 'hidden') { // enable scrollHeight/Width
this.setStyle('overflow', 'hidden');
this._transitionOverflow = overflow;
}
},
end: function() {
if (this._transitionOverflow) { // revert overridden value
this.setStyle('overflow', this._transitionOverflow);
delete this._transitionOverflow;
}
}
}
}
});
Y.mix(Transition.toggles, {
size: ['sizeOut', 'sizeIn'],
fade: ['fadeOut', 'fadeIn']
});
Transition.DEFAULT_TOGGLE = 'fade';