~ted/lazr-js/annoying-debug-message

« back to all changes in this revision

Viewing changes to src-js/lazrjs/yui/transition/transition-native-debug.js

  • Committer: Launchpad Patch Queue Manager
  • Date: 2010-09-09 14:20:30 UTC
  • mfrom: (182.1.3 yui-3.2)
  • Revision ID: launchpad@pqm.canonical.com-20100909142030-13w6vo0ixfysxc15
[r=beuno] Update lazr-js to yui-3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
 
3
Code licensed under the BSD License:
 
4
http://developer.yahoo.com/yui/license.html
 
5
version: 3.2.0
 
6
build: 2676
 
7
*/
 
8
YUI.add('transition-native', function(Y) {
 
9
 
 
10
/**
 
11
* Provides the transition method for Node.
 
12
* Transition has no API of its own, but adds the transition method to Node.
 
13
*
 
14
* @module transition
 
15
* @requires node
 
16
*/
 
17
 
 
18
var TRANSITION = '-webkit-transition',
 
19
    TRANSITION_PROPERTY_CAMEL = 'WebkitTransition',
 
20
    TRANSITION_PROPERTY = '-webkit-transition-property',
 
21
    TRANSITION_DURATION = '-webkit-transition-duration',
 
22
    TRANSITION_TIMING_FUNCTION = '-webkit-transition-timing-function',
 
23
    TRANSITION_DELAY = '-webkit-transition-delay',
 
24
    TRANSITION_END = 'webkitTransitionEnd',
 
25
    TRANSFORM_CAMEL = 'WebkitTransform',
 
26
 
 
27
    EMPTY_OBJ = {},
 
28
 
 
29
/**
 
30
 * A class for constructing transition instances.
 
31
 * Adds the "transition" method to Node.
 
32
 * @class Transition
 
33
 * @constructor
 
34
 */
 
35
 
 
36
Transition = function() {
 
37
    this.init.apply(this, arguments);
 
38
};
 
39
 
 
40
Transition._toCamel = function(property) {
 
41
    property = property.replace(/-([a-z])/gi, function(m0, m1) {
 
42
        return m1.toUpperCase();
 
43
    });
 
44
 
 
45
    return property;
 
46
};
 
47
 
 
48
Transition._toHyphen = function(property) {
 
49
    property = property.replace(/([A-Z]?)([a-z]+)([A-Z]?)/g, function(m0, m1, m2, m3) {
 
50
        var str = '';
 
51
        if (m1) {
 
52
            str += '-' + m1.toLowerCase();
 
53
        }
 
54
        str += m2;
 
55
        
 
56
        if (m3) {
 
57
            str += '-' + m3.toLowerCase();
 
58
        }
 
59
 
 
60
        return str;
 
61
    }); 
 
62
 
 
63
    return property;
 
64
};
 
65
 
 
66
 
 
67
Transition._reKeywords = /^(?:node|duration|iterations|easing|delay)$/;
 
68
 
 
69
Transition.useNative = false;
 
70
 
 
71
if (TRANSITION in Y.config.doc.documentElement.style) {
 
72
    Transition.useNative = true;
 
73
    Transition.supported = true; // TODO: remove
 
74
}
 
75
 
 
76
Y.Node.DOM_EVENTS[TRANSITION_END] = 1; 
 
77
 
 
78
Transition.NAME = 'transition';
 
79
 
 
80
Transition.DEFAULT_EASING = 'ease';
 
81
Transition.DEFAULT_DURATION = 0.5;
 
82
Transition.DEFAULT_DELAY = 0;
 
83
 
 
84
Transition._nodeAttrs = {};
 
85
 
 
86
Transition.prototype = {
 
87
    constructor: Transition,
 
88
    init: function(node, config) {
 
89
        var anim = this;
 
90
        if (!anim._running) {
 
91
            anim._node = node;
 
92
            anim._config = config;
 
93
            node._transition = anim; // cache for reuse
 
94
 
 
95
            anim._duration = ('duration' in config) ?
 
96
                config.duration: anim.constructor.DEFAULT_DURATION;
 
97
 
 
98
            anim._delay = ('delay' in config) ?
 
99
                config.delay: anim.constructor.DEFAULT_DELAY;
 
100
 
 
101
            anim._easing = config.easing || anim.constructor.DEFAULT_EASING;
 
102
            anim._count = 0; // track number of animated properties
 
103
            anim._running = false;
 
104
 
 
105
            anim.initAttrs(config);
 
106
 
 
107
        }
 
108
 
 
109
        return anim;
 
110
    },
 
111
 
 
112
    addProperty: function(prop, config) {
 
113
        var anim = this,
 
114
            node = this._node,
 
115
            uid = Y.stamp(node),
 
116
            attrs = Transition._nodeAttrs[uid],
 
117
            attr,
 
118
            val;
 
119
 
 
120
        if (!attrs) {
 
121
            attrs = Transition._nodeAttrs[uid] = {};
 
122
        }
 
123
 
 
124
        attr = attrs[prop];
 
125
 
 
126
        // might just be a value
 
127
        if (config && config.value !== undefined) {
 
128
            val = config.value;
 
129
        } else if (config !== undefined) {
 
130
            val = config; 
 
131
            config = EMPTY_OBJ;
 
132
        }
 
133
 
 
134
        if (typeof val === 'function') {
 
135
            val = val.call(node, node);
 
136
        }
 
137
 
 
138
        // take control if another transition owns this property
 
139
        if (attr && attr.transition && attr.transition !== anim) {
 
140
            attr.transition._count--; // remapping attr to this transition
 
141
        }
 
142
 
 
143
        anim._count++; // properties per transition
 
144
 
 
145
        attrs[prop] = {
 
146
            value: val,
 
147
            duration: ((typeof config.duration !== 'undefined') ? config.duration :
 
148
                    anim._duration) || 0.0001, // make 0 async and fire events
 
149
 
 
150
            delay: (typeof config.delay !== 'undefined') ? config.delay :
 
151
                    anim._delay,
 
152
 
 
153
            easing: config.easing || anim._easing,
 
154
 
 
155
            transition: anim
 
156
        };
 
157
    },
 
158
 
 
159
    removeProperty: function(prop) {
 
160
        var anim = this,
 
161
            attrs = Transition._nodeAttrs[Y.stamp(anim._node)];
 
162
 
 
163
        if (attrs && attrs[prop]) {
 
164
            delete attrs[prop];
 
165
            anim._count--;
 
166
        }
 
167
 
 
168
    },
 
169
 
 
170
    initAttrs: function(config) {
 
171
        var attr;
 
172
 
 
173
        if (config.transform && !config[TRANSFORM_CAMEL]) {
 
174
            config[TRANSFORM_CAMEL] = config.transform;
 
175
            delete config.transform; // TODO: copy
 
176
        }
 
177
 
 
178
        for (attr in config) {
 
179
            if (config.hasOwnProperty(attr) && !Transition._reKeywords.test(attr)) {
 
180
                this.addProperty(attr, config[attr]);
 
181
            }
 
182
 
 
183
        }
 
184
    },
 
185
 
 
186
    /**
 
187
     * Starts or an animation.
 
188
     * @method run
 
189
     * @chainable
 
190
     * @private
 
191
     */    
 
192
    run: function(callback) {
 
193
        var anim = this;
 
194
 
 
195
        if (!anim._running) {
 
196
            anim._running = true;
 
197
 
 
198
            anim._node.fire('transition:start', {
 
199
                type: 'transition:start',
 
200
                config: anim._config
 
201
            });
 
202
 
 
203
            anim._start();
 
204
            anim._callback = callback;
 
205
        }
 
206
 
 
207
        return anim;
 
208
    },
 
209
 
 
210
    _start: function() {
 
211
        this._runNative();
 
212
    },
 
213
 
 
214
    _prepDur: function(dur) {
 
215
        dur = parseFloat(dur);
 
216
 
 
217
        return dur + 's';
 
218
    },
 
219
 
 
220
    _runNative: function(time) {
 
221
        var anim = this,
 
222
            node = anim._node,
 
223
            uid = Y.stamp(node),
 
224
            domNode = node._node,
 
225
            style = domNode.style,
 
226
            computed = getComputedStyle(domNode),
 
227
            attrs = Transition._nodeAttrs[uid],
 
228
            cssText = '',
 
229
            cssTransition = computed[TRANSITION_PROPERTY],
 
230
 
 
231
            transitionText = TRANSITION_PROPERTY + ': ',
 
232
            duration = TRANSITION_DURATION + ': ',
 
233
            easing = TRANSITION_TIMING_FUNCTION + ': ',
 
234
            delay = TRANSITION_DELAY + ': ',
 
235
            hyphy,
 
236
            attr,
 
237
            name;
 
238
 
 
239
        // preserve existing transitions
 
240
        if (cssTransition !== 'all') {
 
241
            transitionText += cssTransition + ',';
 
242
            duration += computed[TRANSITION_DURATION] + ',';
 
243
            easing += computed[TRANSITION_TIMING_FUNCTION] + ',';
 
244
            delay += computed[TRANSITION_DELAY] + ',';
 
245
 
 
246
        }
 
247
 
 
248
        // run transitions mapped to this instance
 
249
        for (name in attrs) {
 
250
            hyphy = Transition._toHyphen(name);
 
251
            attr = attrs[name];
 
252
            if (attrs.hasOwnProperty(name) && attr.transition === anim) {
 
253
                if (name in domNode.style) { // only native styles allowed
 
254
                    duration += anim._prepDur(attr.duration) + ',';
 
255
                    delay += anim._prepDur(attr.delay) + ',';
 
256
                    easing += (attr.easing) + ',';
 
257
 
 
258
                    transitionText += hyphy + ',';
 
259
                    cssText += hyphy + ': ' + attr.value + '; ';
 
260
                } else {
 
261
                    this.removeProperty(name);
 
262
                }
 
263
            }
 
264
        }
 
265
 
 
266
        transitionText = transitionText.replace(/,$/, ';');
 
267
        duration = duration.replace(/,$/, ';');
 
268
        easing = easing.replace(/,$/, ';');
 
269
        delay = delay.replace(/,$/, ';');
 
270
 
 
271
        // only one native end event per node
 
272
        if (!node._hasTransitionEnd) {
 
273
            anim._detach = node.on(TRANSITION_END, anim._onNativeEnd);
 
274
            node._hasTransitionEnd = true;
 
275
 
 
276
        }
 
277
 
 
278
        style.cssText += transitionText + duration + easing + delay + cssText;
 
279
 
 
280
    },
 
281
 
 
282
    _end: function(elapsed) {
 
283
        var anim = this,
 
284
            node = anim._node,
 
285
            callback = anim._callback,
 
286
            data = {
 
287
                type: 'transition:end',
 
288
                config: anim._config,
 
289
                elapsedTime: elapsed 
 
290
            };
 
291
 
 
292
        anim._running = false;
 
293
        if (callback) {
 
294
            anim._callback = null;
 
295
            setTimeout(function() { // IE: allow previous update to finish
 
296
                callback.call(node, data);
 
297
            }, 1);
 
298
        }
 
299
 
 
300
        node.fire('transition:end', data);
 
301
    },
 
302
 
 
303
    _endNative: function(name) {
 
304
        var node = this._node,
 
305
            value = node.getComputedStyle(TRANSITION_PROPERTY);
 
306
 
 
307
        if (typeof value === 'string') {
 
308
            value = value.replace(new RegExp('(?:^|,\\s)' + name + ',?'), ',');
 
309
            value = value.replace(/^,|,$/, '');
 
310
            node.setStyle(TRANSITION_PROPERTY_CAMEL, value);
 
311
        }
 
312
    },
 
313
 
 
314
    _onNativeEnd: function(e) {
 
315
        var node = this,
 
316
            uid = Y.stamp(node),
 
317
            event = e._event,
 
318
            name = Transition._toCamel(event.propertyName),
 
319
            elapsed = event.elapsedTime,
 
320
            attrs = Transition._nodeAttrs[uid],
 
321
            attr = attrs[name],
 
322
            anim = (attr) ? attr.transition : null;
 
323
 
 
324
        if (anim) {
 
325
            anim.removeProperty(name);
 
326
            anim._endNative(name);
 
327
 
 
328
            node.fire('transition:propertyEnd', {
 
329
                type: 'propertyEnd',
 
330
                propertyName: name,
 
331
                elapsedTime: elapsed
 
332
            });
 
333
 
 
334
            if (anim._count <= 0)  { // after propertEnd fires
 
335
                anim._end(elapsed);
 
336
            }
 
337
 
 
338
        }
 
339
    },
 
340
 
 
341
    destroy: function() {
 
342
        var anim = this;
 
343
        if (anim._detach) {
 
344
            anim._detach.detach();
 
345
        }
 
346
        anim._node = null;
 
347
    }
 
348
};
 
349
 
 
350
Y.Transition = Transition;
 
351
Y.TransitionNative = Transition; // TODO: remove
 
352
 
 
353
/** 
 
354
 *   Animate one or more css properties to a given value. Requires the "transition" module.
 
355
 *   <pre>example usage:
 
356
 *       Y.one('#demo').transition({
 
357
 *           duration: 1, // in seconds, default is 0.5
 
358
 *           easing: 'ease-out', // default is 'ease'
 
359
 *           delay: '1', // delay start for 1 second, default is 0
 
360
 *
 
361
 *           height: '10px',
 
362
 *           width: '10px',
 
363
 *
 
364
 *           opacity: { // per property
 
365
 *               value: 0,
 
366
 *               duration: 2,
 
367
 *               delay: 2,
 
368
 *               easing: 'ease-in'
 
369
 *           }
 
370
 *       });
 
371
 *   </pre>
 
372
 *   @for Node
 
373
 *   @method transition
 
374
 *   @param {Object} config An object containing one or more style properties, a duration and an easing.
 
375
 *   @param {Function} callback A function to run after the transition has completed. 
 
376
 *   @chainable
 
377
*/
 
378
Y.Node.prototype.transition = function(config, callback) {
 
379
    var anim = this._transition;
 
380
    
 
381
    if (anim && !anim._running) {
 
382
        anim.init(this, config);
 
383
    } else {
 
384
        anim = new Transition(this, config);
 
385
    }
 
386
 
 
387
    anim.run(callback);
 
388
    return this;
 
389
};
 
390
 
 
391
/** 
 
392
 *   Animate one or more css properties to a given value. Requires the "transition" module.
 
393
 *   <pre>example usage:
 
394
 *       Y.all('.demo').transition({
 
395
 *           duration: 1, // in seconds, default is 0.5
 
396
 *           easing: 'ease-out', // default is 'ease'
 
397
 *           delay: '1', // delay start for 1 second, default is 0
 
398
 *
 
399
 *           height: '10px',
 
400
 *           width: '10px',
 
401
 *
 
402
 *           opacity: { // per property
 
403
 *               value: 0,
 
404
 *               duration: 2,
 
405
 *               delay: 2,
 
406
 *               easing: 'ease-in'
 
407
 *           }
 
408
 *       });
 
409
 *   </pre>
 
410
 *   @for NodeList
 
411
 *   @method transition
 
412
 *   @param {Object} config An object containing one or more style properties, a duration and an easing.
 
413
 *   @param {Function} callback A function to run after the transition has completed. The callback fires
 
414
 *       once per item in the NodeList.
 
415
 *   @chainable
 
416
*/
 
417
Y.NodeList.prototype.transition = function(config, callback) {
 
418
    this.each(function(node) {
 
419
        node.transition(config, callback);
 
420
    });
 
421
 
 
422
    return this;
 
423
};
 
424
 
 
425
 
 
426
}, '3.2.0' ,{requires:['node-base']});