Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / fx / Queue.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.fx.Queue
17  * Animation Queue mixin to handle chaining and queueing by target.
18  * @private
19  */
20
21 Ext.define('Ext.fx.Queue', {
22
23     requires: ['Ext.util.HashMap'],
24
25     constructor: function() {
26         this.targets = Ext.create('Ext.util.HashMap');
27         this.fxQueue = {};
28     },
29
30     // @private
31     getFxDefaults: function(targetId) {
32         var target = this.targets.get(targetId);
33         if (target) {
34             return target.fxDefaults;
35         }
36         return {};
37     },
38
39     // @private
40     setFxDefaults: function(targetId, obj) {
41         var target = this.targets.get(targetId);
42         if (target) {
43             target.fxDefaults = Ext.apply(target.fxDefaults || {}, obj);
44         }
45     },
46
47     // @private
48     stopAnimation: function(targetId) {
49         var me = this,
50             queue = me.getFxQueue(targetId),
51             ln = queue.length;
52         while (ln) {
53             queue[ln - 1].end();
54             ln--;
55         }
56     },
57
58     /**
59      * @private
60      * Returns current animation object if the element has any effects actively running or queued, else returns false.
61      */
62     getActiveAnimation: function(targetId) {
63         var queue = this.getFxQueue(targetId);
64         return (queue && !!queue.length) ? queue[0] : false;
65     },
66
67     // @private
68     hasFxBlock: function(targetId) {
69         var queue = this.getFxQueue(targetId);
70         return queue && queue[0] && queue[0].block;
71     },
72
73     // @private get fx queue for passed target, create if needed.
74     getFxQueue: function(targetId) {
75         if (!targetId) {
76             return false;
77         }
78         var me = this,
79             queue = me.fxQueue[targetId],
80             target = me.targets.get(targetId);
81
82         if (!target) {
83             return false;
84         }
85
86         if (!queue) {
87             me.fxQueue[targetId] = [];
88             // GarbageCollector will need to clean up Elements since they aren't currently observable
89             if (target.type != 'element') {
90                 target.target.on('destroy', function() {
91                     me.fxQueue[targetId] = [];
92                 });
93             }
94         }
95         return me.fxQueue[targetId];
96     },
97
98     // @private
99     queueFx: function(anim) {
100         var me = this,
101             target = anim.target,
102             queue, ln;
103
104         if (!target) {
105             return;
106         }
107
108         queue = me.getFxQueue(target.getId());
109         ln = queue.length;
110
111         if (ln) {
112             if (anim.concurrent) {
113                 anim.paused = false;
114             }
115             else {
116                 queue[ln - 1].on('afteranimate', function() {
117                     anim.paused = false;
118                 });
119             }
120         }
121         else {
122             anim.paused = false;
123         }
124         anim.on('afteranimate', function() {
125             Ext.Array.remove(queue, anim);
126             if (anim.remove) {
127                 if (target.type == 'element') {
128                     var el = Ext.get(target.id);
129                     if (el) {
130                         el.remove();
131                     }
132                 }
133             }
134         }, this);
135         queue.push(anim);
136     }
137 });