Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / fx / target / Component.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.target.Component
17  * @extends Ext.fx.target.Target
18  * 
19  * This class represents a animation target for a {@link Ext.Component}. In general this class will not be
20  * created directly, the {@link Ext.Component} will be passed to the animation and
21  * and the appropriate target will be created.
22  */
23 Ext.define('Ext.fx.target.Component', {
24
25     /* Begin Definitions */
26    
27     extend: 'Ext.fx.target.Target',
28     
29     /* End Definitions */
30
31     type: 'component',
32
33     // Methods to call to retrieve unspecified "from" values from a target Component
34     getPropMethod: {
35         top: function() {
36             return this.getPosition(true)[1];
37         },
38         left: function() {
39             return this.getPosition(true)[0];
40         },
41         x: function() {
42             return this.getPosition()[0];
43         },
44         y: function() {
45             return this.getPosition()[1];
46         },
47         height: function() {
48             return this.getHeight();
49         },
50         width: function() {
51             return this.getWidth();
52         },
53         opacity: function() {
54             return this.el.getStyle('opacity');
55         }
56     },
57
58     compMethod: {
59         top: 'setPosition',
60         left: 'setPosition',
61         x: 'setPagePosition',
62         y: 'setPagePosition',
63         height: 'setSize',
64         width: 'setSize',
65         opacity: 'setOpacity'
66     },
67
68     // Read the named attribute from the target Component. Use the defined getter for the attribute
69     getAttr: function(attr, val) {
70         return [[this.target, val !== undefined ? val : this.getPropMethod[attr].call(this.target)]];
71     },
72
73     setAttr: function(targetData, isFirstFrame, isLastFrame) {
74         var me = this,
75             target = me.target,
76             ln = targetData.length,
77             attrs, attr, o, i, j, meth, targets, left, top, w, h;
78         for (i = 0; i < ln; i++) {
79             attrs = targetData[i].attrs;
80             for (attr in attrs) {
81                 targets = attrs[attr].length;
82                 meth = {
83                     setPosition: {},
84                     setPagePosition: {},
85                     setSize: {},
86                     setOpacity: {}
87                 };
88                 for (j = 0; j < targets; j++) {
89                     o = attrs[attr][j];
90                     // We REALLY want a single function call, so push these down to merge them: eg
91                     // meth.setPagePosition.target = <targetComponent>
92                     // meth.setPagePosition['x'] = 100
93                     // meth.setPagePosition['y'] = 100
94                     meth[me.compMethod[attr]].target = o[0];
95                     meth[me.compMethod[attr]][attr] = o[1];
96                 }
97                 if (meth.setPosition.target) {
98                     o = meth.setPosition;
99                     left = (o.left === undefined) ? undefined : parseInt(o.left, 10);
100                     top = (o.top === undefined) ? undefined : parseInt(o.top, 10);
101                     o.target.setPosition(left, top);
102                 }
103                 if (meth.setPagePosition.target) {
104                     o = meth.setPagePosition;
105                     o.target.setPagePosition(o.x, o.y);
106                 }
107                 if (meth.setSize.target && meth.setSize.target.el) {
108                     o = meth.setSize;
109                     // Dimensions not being animated MUST NOT be autosized. They must remain at current value.
110                     w = (o.width === undefined) ? o.target.getWidth() : parseInt(o.width, 10);
111                     h = (o.height === undefined) ? o.target.getHeight() : parseInt(o.height, 10);
112
113                     // Only set the size of the Component on the last frame, or if the animation was
114                     // configured with dynamic: true.
115                     // In other cases, we just set the target element size.
116                     // This will result in either clipping if animating a reduction in size, or the revealing of
117                     // the inner elements of the Component if animating an increase in size.
118                     // Component's animate function initially resizes to the larger size before resizing the
119                     // outer element to clip the contents.
120                     if (isLastFrame || me.dynamic) {
121                         o.target.componentLayout.childrenChanged = true;
122
123                         // Flag if we are being called by an animating layout: use setCalculatedSize
124                         if (me.layoutAnimation) {
125                             o.target.setCalculatedSize(w, h);
126                         } else {
127                             o.target.setSize(w, h);
128                         }
129                     }
130                     else {
131                         o.target.el.setSize(w, h);
132                     }
133                 }
134                 if (meth.setOpacity.target) {
135                     o = meth.setOpacity;
136                     o.target.el.setStyle('opacity', o.opacity);
137                 }
138             }
139         }
140     }
141 });
142