3
Copyright 2011 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
//anim._node.fire('transition:start', data);
272
if (config.on && config.on.start) {
273
config.on.start.call(Y.one(node), data);
276
anim.initAttrs(anim._config);
278
anim._callback = callback;
290
_prepDur: function(dur) {
291
dur = parseFloat(dur);
296
_runNative: function(time) {
301
computed = getComputedStyle(node),
302
attrs = Transition._nodeAttrs[uid],
304
cssTransition = computed[Transition._toCamel(TRANSITION_PROPERTY)],
306
transitionText = TRANSITION_PROPERTY + ': ',
307
duration = TRANSITION_DURATION + ': ',
308
easing = TRANSITION_TIMING_FUNCTION + ': ',
309
delay = TRANSITION_DELAY + ': ',
314
// preserve existing transitions
315
if (cssTransition !== 'all') {
316
transitionText += cssTransition + ',';
317
duration += computed[Transition._toCamel(TRANSITION_DURATION)] + ',';
318
easing += computed[Transition._toCamel(TRANSITION_TIMING_FUNCTION)] + ',';
319
delay += computed[Transition._toCamel(TRANSITION_DELAY)] + ',';
323
// run transitions mapped to this instance
324
for (name in attrs) {
325
hyphy = Transition._toHyphen(name);
327
if ((attr = attrs[name]) && attr.transition === anim) {
328
if (name in node.style) { // only native styles allowed
329
duration += anim._prepDur(attr.duration) + ',';
330
delay += anim._prepDur(attr.delay) + ',';
331
easing += (attr.easing) + ',';
333
transitionText += hyphy + ',';
334
cssText += hyphy + ': ' + attr.value + '; ';
336
this.removeProperty(name);
341
transitionText = transitionText.replace(/,$/, ';');
342
duration = duration.replace(/,$/, ';');
343
easing = easing.replace(/,$/, ';');
344
delay = delay.replace(/,$/, ';');
346
// only one native end event per node
347
if (!Transition._hasEnd[uid]) {
348
//anim._detach = Y.on(TRANSITION_END, anim._onNativeEnd, node);
349
//node[ON_TRANSITION_END] = anim._onNativeEnd;
350
node.addEventListener(TRANSITION_END, anim._onNativeEnd, '');
351
Transition._hasEnd[uid] = true;
355
//setTimeout(function() { // allow updates to apply (size fix, onstart, etc)
356
style.cssText += transitionText + duration + easing + delay + cssText;
361
_end: function(elapsed) {
364
callback = anim._callback,
365
config = anim._config,
367
type: 'transition:end',
372
nodeInstance = Y.one(node);
374
anim._running = false;
375
anim._callback = null;
378
if (config.on && config.on.end) {
379
setTimeout(function() { // IE: allow previous update to finish
380
config.on.end.call(nodeInstance, data);
382
// nested to ensure proper fire order
384
callback.call(nodeInstance, data);
388
} else if (callback) {
389
setTimeout(function() { // IE: allow previous update to finish
390
callback.call(nodeInstance, data);
393
//node.fire('transition:end', data);
398
_endNative: function(name) {
399
var node = this._node,
400
value = node.ownerDocument.defaultView.getComputedStyle(node, '')[Transition._toCamel(TRANSITION_PROPERTY)];
402
if (typeof value === 'string') {
403
value = value.replace(new RegExp('(?:^|,\\s)' + name + ',?'), ',');
404
value = value.replace(/^,|,$/, '');
405
node.style[TRANSITION_CAMEL] = value;
409
_onNativeEnd: function(e) {
412
event = e,//e._event,
413
name = Transition._toCamel(event.propertyName),
414
elapsed = event.elapsedTime,
415
attrs = Transition._nodeAttrs[uid],
417
anim = (attr) ? attr.transition : null,
422
anim.removeProperty(name);
423
anim._endNative(name);
424
config = anim._config[name];
429
elapsedTime: elapsed,
433
if (config && config.on && config.on.end) {
434
config.on.end.call(Y.one(node), data);
437
//node.fire('transition:propertyEnd', data);
439
if (anim._count <= 0) { // after propertyEnd fires
445
destroy: function() {
450
anim._detach.detach();
453
//anim._node[ON_TRANSITION_END] = null;
455
node.removeEventListener(TRANSITION_END, anim._onNativeEnd, false);
461
Y.Transition = Transition;
462
Y.TransitionNative = Transition; // TODO: remove
465
* Animate one or more css properties to a given value. Requires the "transition" module.
466
* <pre>example usage:
467
* Y.one('#demo').transition({
468
* duration: 1, // in seconds, default is 0.5
469
* easing: 'ease-out', // default is 'ease'
470
* delay: '1', // delay start for 1 second, default is 0
475
* opacity: { // per property
485
* @param {Object} config An object containing one or more style properties, a duration and an easing.
486
* @param {Function} callback A function to run after the transition has completed.
489
Y.Node.prototype.transition = function(name, config, callback) {
491
transitionAttrs = Transition._nodeAttrs[Y.stamp(this._node)],
492
anim = (transitionAttrs) ? transitionAttrs.transition || null : null,
496
if (typeof name === 'string') { // named effect, pull config from registry
497
if (typeof config === 'function') {
502
fxConfig = Transition.fx[name];
504
if (config && typeof config !== 'boolean') {
505
config = Y.clone(config);
507
for (prop in fxConfig) {
508
if (fxConfig.hasOwnProperty(prop)) {
509
if (! (prop in config)) {
510
config[prop] = fxConfig[prop];
518
} else { // name is a config, config is a callback or undefined
523
if (anim && !anim._running) {
524
anim.init(this, config);
526
anim = new Transition(this._node, config);
533
Y.Node.prototype.show = function(name, config, callback) {
534
this._show(); // show prior to transition
535
if (name && Y.Transition) {
536
if (typeof name !== 'string' && !name.push) { // named effect or array of effects supercedes default
537
if (typeof config === 'function') {
541
name = Transition.SHOW_TRANSITION;
543
this.transition(name, config, callback);
548
var _wrapCallBack = function(anim, fn, callback) {
554
callback.apply(anim._node, arguments);
559
Y.Node.prototype.hide = function(name, config, callback) {
560
if (name && Y.Transition) {
561
if (typeof config === 'function') {
566
callback = _wrapCallBack(this, this._hide, callback); // wrap with existing callback
567
if (typeof name !== 'string' && !name.push) { // named effect or array of effects supercedes default
568
if (typeof config === 'function') {
572
name = Transition.HIDE_TRANSITION;
574
this.transition(name, config, callback);
582
* Animate one or more css properties to a given value. Requires the "transition" module.
583
* <pre>example usage:
584
* Y.all('.demo').transition({
585
* duration: 1, // in seconds, default is 0.5
586
* easing: 'ease-out', // default is 'ease'
587
* delay: '1', // delay start for 1 second, default is 0
592
* opacity: { // per property
602
* @param {Object} config An object containing one or more style properties, a duration and an easing.
603
* @param {Function} callback A function to run after the transition has completed. The callback fires
604
* once per item in the NodeList.
607
Y.NodeList.prototype.transition = function(config, callback) {
608
var nodes = this._nodes,
612
while ((node = nodes[i++])) {
613
Y.one(node).transition(config, callback);
619
Y.Node.prototype.toggleView = function(name, on, callback) {
620
this._toggles = this._toggles || [];
621
callback = arguments[arguments.length - 1];
623
if (typeof name == 'boolean') { // no transition, just toggle
628
name = name || Y.Transition.DEFAULT_TOGGLE;
630
if (typeof on == 'undefined' && name in this._toggles) { // reverse current toggle
631
on = ! this._toggles[name];
638
callback = _wrapCallBack(this, this._hide, callback);
641
this._toggles[name] = on;
642
this.transition(Y.Transition.toggles[name][on], callback);
647
Y.NodeList.prototype.toggleView = function(name, on, callback) {
648
var nodes = this._nodes,
652
while ((node = nodes[i++])) {
653
Y.one(node).toggleView(name, on, callback);
659
Y.mix(Transition.fx, {
680
height: function(node) {
681
return node.get('scrollHeight') + 'px';
683
width: function(node) {
684
return node.get('scrollWidth') + 'px';
691
var overflow = this.getStyle('overflow');
692
if (overflow !== 'hidden') { // enable scrollHeight/Width
693
this.setStyle('overflow', 'hidden');
694
this._transitionOverflow = overflow;
699
if (this._transitionOverflow) { // revert overridden value
700
this.setStyle('overflow', this._transitionOverflow);
701
delete this._transitionOverflow;
708
Y.mix(Transition.toggles, {
709
size: ['sizeOut', 'sizeIn'],
710
fade: ['fadeOut', 'fadeIn']
713
Transition.DEFAULT_TOGGLE = 'fade';
717
}, '3.4.1' ,{requires:['node-style']});