3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('transition-native', 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
92
TRANSITION_CAMEL = CAMEL_VENDOR_PREFIX + TRANSITION_CAMEL;
93
TRANSITION_PROPERTY_CAMEL = CAMEL_VENDOR_PREFIX + 'TransitionProperty';
94
TRANSITION_PROPERTY = VENDOR_PREFIX + 'transition-property';
95
TRANSITION_DURATION = VENDOR_PREFIX + 'transition-duration';
96
TRANSITION_TIMING_FUNCTION = VENDOR_PREFIX + 'transition-timing-function';
97
TRANSITION_DELAY = VENDOR_PREFIX + 'transition-delay';
98
TRANSITION_END = 'transitionend';
99
ON_TRANSITION_END = 'on' + CAMEL_VENDOR_PREFIX.toLowerCase() + 'transitionend';
101
TRANSITION_END = VENDOR_TRANSITION_END[CAMEL_VENDOR_PREFIX] || TRANSITION_END;
103
TRANSFORM_CAMEL = CAMEL_VENDOR_PREFIX + 'Transform';
106
Transition.toggles = {};
108
Transition._hasEnd = {};
110
Transition._reKeywords = /^(?:node|duration|iterations|easing|delay|on|onstart|onend)$/i;
112
Y.Node.DOM_EVENTS[TRANSITION_END] = 1;
114
Transition.NAME = 'transition';
116
Transition.DEFAULT_EASING = 'ease';
117
Transition.DEFAULT_DURATION = 0.5;
118
Transition.DEFAULT_DELAY = 0;
120
Transition._nodeAttrs = {};
122
Transition.prototype = {
123
constructor: Transition,
124
init: function(node, config) {
127
if (!anim._running && config) {
128
anim._config = config;
129
node._transition = anim; // cache for reuse
131
anim._duration = ('duration' in config) ?
132
config.duration: anim.constructor.DEFAULT_DURATION;
134
anim._delay = ('delay' in config) ?
135
config.delay: anim.constructor.DEFAULT_DELAY;
137
anim._easing = config.easing || anim.constructor.DEFAULT_EASING;
138
anim._count = 0; // track number of animated properties
139
anim._running = false;
146
addProperty: function(prop, config) {
150
nodeInstance = Y.one(node),
151
attrs = Transition._nodeAttrs[uid],
159
attrs = Transition._nodeAttrs[uid] = {};
164
// might just be a value
165
if (config && config.value !== undefined) {
167
} else if (config !== undefined) {
172
if (typeof val === 'function') {
173
val = val.call(nodeInstance, nodeInstance);
176
if (attr && attr.transition) {
177
// take control if another transition owns this property
178
if (attr.transition !== anim) {
179
attr.transition._count--; // remapping attr to this transition
183
anim._count++; // properties per transition
185
// make 0 async and fire events
186
dur = ((typeof config.duration != 'undefined') ? config.duration :
187
anim._duration) || 0.0001;
192
delay: (typeof config.delay != 'undefined') ? config.delay :
195
easing: config.easing || anim._easing,
200
// native end event doesnt fire when setting to same value
201
// supplementing with timer
202
// val may be a string or number (height: 0, etc), but computedStyle is always string
203
computed = Y.DOM.getComputedStyle(node, prop);
204
compareVal = (typeof val === 'string') ? computed : parseFloat(computed);
206
if (Transition.useNative && compareVal === val) {
207
setTimeout(function() {
208
anim._onNativeEnd.call(node, {
216
removeProperty: function(prop) {
218
attrs = Transition._nodeAttrs[Y.stamp(anim._node)];
220
if (attrs && attrs[prop]) {
227
initAttrs: function(config) {
231
if (config.transform && !config[TRANSFORM_CAMEL]) {
232
config[TRANSFORM_CAMEL] = config.transform;
233
delete config.transform; // TODO: copy
236
for (attr in config) {
237
if (config.hasOwnProperty(attr) && !Transition._reKeywords.test(attr)) {
238
this.addProperty(attr, config[attr]);
240
// when size is auto or % webkit starts from zero instead of computed
241
// (https://bugs.webkit.org/show_bug.cgi?id=16020)
242
// TODO: selective set
243
if (node.style[attr] === '') {
244
Y.DOM.setStyle(node, attr, Y.DOM.getComputedStyle(node, attr));
251
* Starts or an animation.
256
run: function(callback) {
259
config = anim._config,
261
type: 'transition:start',
266
if (!anim._running) {
267
anim._running = true;
269
//anim._node.fire('transition:start', data);
271
if (config.on && config.on.start) {
272
config.on.start.call(Y.one(node), data);
275
anim.initAttrs(anim._config);
277
anim._callback = callback;
289
_prepDur: function(dur) {
290
dur = parseFloat(dur);
295
_runNative: function(time) {
300
computed = getComputedStyle(node),
301
attrs = Transition._nodeAttrs[uid],
303
cssTransition = computed[Transition._toCamel(TRANSITION_PROPERTY)],
305
transitionText = TRANSITION_PROPERTY + ': ',
306
duration = TRANSITION_DURATION + ': ',
307
easing = TRANSITION_TIMING_FUNCTION + ': ',
308
delay = TRANSITION_DELAY + ': ',
313
// preserve existing transitions
314
if (cssTransition !== 'all') {
315
transitionText += cssTransition + ',';
316
duration += computed[Transition._toCamel(TRANSITION_DURATION)] + ',';
317
easing += computed[Transition._toCamel(TRANSITION_TIMING_FUNCTION)] + ',';
318
delay += computed[Transition._toCamel(TRANSITION_DELAY)] + ',';
322
// run transitions mapped to this instance
323
for (name in attrs) {
324
hyphy = Transition._toHyphen(name);
326
if ((attr = attrs[name]) && attr.transition === anim) {
327
if (name in node.style) { // only native styles allowed
328
duration += anim._prepDur(attr.duration) + ',';
329
delay += anim._prepDur(attr.delay) + ',';
330
easing += (attr.easing) + ',';
332
transitionText += hyphy + ',';
333
cssText += hyphy + ': ' + attr.value + '; ';
335
this.removeProperty(name);
340
transitionText = transitionText.replace(/,$/, ';');
341
duration = duration.replace(/,$/, ';');
342
easing = easing.replace(/,$/, ';');
343
delay = delay.replace(/,$/, ';');
345
// only one native end event per node
346
if (!Transition._hasEnd[uid]) {
347
//anim._detach = Y.on(TRANSITION_END, anim._onNativeEnd, node);
348
//node[ON_TRANSITION_END] = anim._onNativeEnd;
349
node.addEventListener(TRANSITION_END, anim._onNativeEnd, '');
350
Transition._hasEnd[uid] = true;
354
//setTimeout(function() { // allow updates to apply (size fix, onstart, etc)
355
style.cssText += transitionText + duration + easing + delay + cssText;
360
_end: function(elapsed) {
363
callback = anim._callback,
364
config = anim._config,
366
type: 'transition:end',
371
nodeInstance = Y.one(node);
373
anim._running = false;
374
anim._callback = null;
377
if (config.on && config.on.end) {
378
setTimeout(function() { // IE: allow previous update to finish
379
config.on.end.call(nodeInstance, data);
381
// nested to ensure proper fire order
383
callback.call(nodeInstance, data);
387
} else if (callback) {
388
setTimeout(function() { // IE: allow previous update to finish
389
callback.call(nodeInstance, data);
392
//node.fire('transition:end', data);
397
_endNative: function(name) {
398
var node = this._node,
399
value = node.ownerDocument.defaultView.getComputedStyle(node, '')[Transition._toCamel(TRANSITION_PROPERTY)];
401
if (typeof value === 'string') {
402
value = value.replace(new RegExp('(?:^|,\\s)' + name + ',?'), ',');
403
value = value.replace(/^,|,$/, '');
404
node.style[TRANSITION_CAMEL] = value;
408
_onNativeEnd: function(e) {
411
event = e,//e._event,
412
name = Transition._toCamel(event.propertyName),
413
elapsed = event.elapsedTime,
414
attrs = Transition._nodeAttrs[uid],
416
anim = (attr) ? attr.transition : null,
421
anim.removeProperty(name);
422
anim._endNative(name);
423
config = anim._config[name];
428
elapsedTime: elapsed,
432
if (config && config.on && config.on.end) {
433
config.on.end.call(Y.one(node), data);
436
//node.fire('transition:propertyEnd', data);
438
if (anim._count <= 0) { // after propertyEnd fires
444
destroy: function() {
449
anim._detach.detach();
452
//anim._node[ON_TRANSITION_END] = null;
454
node.removeEventListener(TRANSITION_END, anim._onNativeEnd, false);
460
Y.Transition = Transition;
461
Y.TransitionNative = Transition; // TODO: remove
464
* Animate one or more css properties to a given value. Requires the "transition" module.
465
* <pre>example usage:
466
* Y.one('#demo').transition({
467
* duration: 1, // in seconds, default is 0.5
468
* easing: 'ease-out', // default is 'ease'
469
* delay: '1', // delay start for 1 second, default is 0
474
* opacity: { // per property
484
* @param {Object} config An object containing one or more style properties, a duration and an easing.
485
* @param {Function} callback A function to run after the transition has completed.
488
Y.Node.prototype.transition = function(name, config, callback) {
490
transitionAttrs = Transition._nodeAttrs[Y.stamp(this._node)],
491
anim = (transitionAttrs) ? transitionAttrs.transition || null : null,
495
if (typeof name === 'string') { // named effect, pull config from registry
496
if (typeof config === 'function') {
501
fxConfig = Transition.fx[name];
503
if (config && typeof config !== 'boolean') {
504
config = Y.clone(config);
506
for (prop in fxConfig) {
507
if (fxConfig.hasOwnProperty(prop)) {
508
if (! (prop in config)) {
509
config[prop] = fxConfig[prop];
517
} else { // name is a config, config is a callback or undefined
522
if (anim && !anim._running) {
523
anim.init(this, config);
525
anim = new Transition(this._node, config);
532
Y.Node.prototype.show = function(name, config, callback) {
533
this._show(); // show prior to transition
534
if (name && Y.Transition) {
535
if (typeof name !== 'string' && !name.push) { // named effect or array of effects supercedes default
536
if (typeof config === 'function') {
540
name = Transition.SHOW_TRANSITION;
542
this.transition(name, config, callback);
544
else if (name && !Y.Transition) { Y.log('unable to transition show; missing transition module', 'warn', 'node'); }
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);
575
} else if (name && !Y.Transition) { Y.log('unable to transition hide; missing transition module', 'warn', 'node'); // end if on nex
583
* Animate one or more css properties to a given value. Requires the "transition" module.
584
* <pre>example usage:
585
* Y.all('.demo').transition({
586
* duration: 1, // in seconds, default is 0.5
587
* easing: 'ease-out', // default is 'ease'
588
* delay: '1', // delay start for 1 second, default is 0
593
* opacity: { // per property
603
* @param {Object} config An object containing one or more style properties, a duration and an easing.
604
* @param {Function} callback A function to run after the transition has completed. The callback fires
605
* once per item in the NodeList.
608
Y.NodeList.prototype.transition = function(config, callback) {
609
var nodes = this._nodes,
613
while ((node = nodes[i++])) {
614
Y.one(node).transition(config, callback);
620
Y.Node.prototype.toggleView = function(name, on, callback) {
621
this._toggles = this._toggles || [];
622
callback = arguments[arguments.length - 1];
624
if (typeof name == 'boolean') { // no transition, just toggle
629
name = name || Y.Transition.DEFAULT_TOGGLE;
631
if (typeof on == 'undefined' && name in this._toggles) { // reverse current toggle
632
on = ! this._toggles[name];
639
callback = _wrapCallBack(this, this._hide, callback);
642
this._toggles[name] = on;
643
this.transition(Y.Transition.toggles[name][on], callback);
648
Y.NodeList.prototype.toggleView = function(name, on, callback) {
649
var nodes = this._nodes,
653
while ((node = nodes[i++])) {
654
Y.one(node).toggleView(name, on, callback);
660
Y.mix(Transition.fx, {
681
height: function(node) {
682
return node.get('scrollHeight') + 'px';
684
width: function(node) {
685
return node.get('scrollWidth') + 'px';
692
var overflow = this.getStyle('overflow');
693
if (overflow !== 'hidden') { // enable scrollHeight/Width
694
this.setStyle('overflow', 'hidden');
695
this._transitionOverflow = overflow;
700
if (this._transitionOverflow) { // revert overridden value
701
this.setStyle('overflow', this._transitionOverflow);
702
delete this._transitionOverflow;
709
Y.mix(Transition.toggles, {
710
size: ['sizeOut', 'sizeIn'],
711
fade: ['fadeOut', 'fadeIn']
714
Transition.DEFAULT_TOGGLE = 'fade';
718
}, '3.4.1' ,{requires:['node-base']});