Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / layout / container / Anchor.js
1 /**
2  * @class Ext.layout.container.Anchor
3  * @extends Ext.layout.container.Container
4  * <p>This is a layout that enables anchoring of contained elements relative to the container's dimensions.
5  * If the container is resized, all anchored items are automatically rerendered according to their
6  * <b><tt>{@link #anchor}</tt></b> rules.</p>
7  * <p>This class is intended to be extended or created via the layout: 'anchor' {@link Ext.layout.container.AbstractContainer#layout}
8  * config, and should generally not need to be created directly via the new keyword.</p>
9  * <p>AnchorLayout does not have any direct config options (other than inherited ones). By default,
10  * AnchorLayout will calculate anchor measurements based on the size of the container itself. However, the
11  * container using the AnchorLayout can supply an anchoring-specific config property of <b>anchorSize</b>.
12  * If anchorSize is specifed, the layout will use it as a virtual container for the purposes of calculating
13  * anchor measurements based on it instead, allowing the container to be sized independently of the anchoring
14  * logic if necessary.  
15  * {@img Ext.layout.container.Anchor/Ext.layout.container.Anchor.png Ext.layout.container.Anchor container layout}
16  * For example:
17         Ext.create('Ext.Panel', {
18                 width: 500,
19                 height: 400,
20                 title: "AnchorLayout Panel",
21                 layout: 'anchor',
22                 renderTo: Ext.getBody(),
23                 items: [{
24                         xtype: 'panel',
25                         title: '75% Width and 20% Height',
26                         anchor: '75% 20%'
27                 },{
28                         xtype: 'panel',
29                         title: 'Offset -300 Width & -200 Height',
30                         anchor: '-300 -200'             
31                 },{
32                         xtype: 'panel',
33                         title: 'Mixed Offset and Percent',
34                         anchor: '-250 20%'
35                 }]
36         });
37  */
38
39 Ext.define('Ext.layout.container.Anchor', {
40
41     /* Begin Definitions */
42
43     alias: 'layout.anchor',
44     extend: 'Ext.layout.container.Container',
45     alternateClassName: 'Ext.layout.AnchorLayout',
46
47     /* End Definitions */
48
49     /**
50      * @cfg {String} anchor
51      * <p>This configuation option is to be applied to <b>child <tt>items</tt></b> of a container managed by
52      * this layout (ie. configured with <tt>layout:'anchor'</tt>).</p><br/>
53      *
54      * <p>This value is what tells the layout how an item should be anchored to the container. <tt>items</tt>
55      * added to an AnchorLayout accept an anchoring-specific config property of <b>anchor</b> which is a string
56      * containing two values: the horizontal anchor value and the vertical anchor value (for example, '100% 50%').
57      * The following types of anchor values are supported:<div class="mdetail-params"><ul>
58      *
59      * <li><b>Percentage</b> : Any value between 1 and 100, expressed as a percentage.<div class="sub-desc">
60      * The first anchor is the percentage width that the item should take up within the container, and the
61      * second is the percentage height.  For example:<pre><code>
62 // two values specified
63 anchor: '100% 50%' // render item complete width of the container and
64                    // 1/2 height of the container
65 // one value specified
66 anchor: '100%'     // the width value; the height will default to auto
67      * </code></pre></div></li>
68      *
69      * <li><b>Offsets</b> : Any positive or negative integer value.<div class="sub-desc">
70      * This is a raw adjustment where the first anchor is the offset from the right edge of the container,
71      * and the second is the offset from the bottom edge. For example:<pre><code>
72 // two values specified
73 anchor: '-50 -100' // render item the complete width of the container
74                    // minus 50 pixels and
75                    // the complete height minus 100 pixels.
76 // one value specified
77 anchor: '-50'      // anchor value is assumed to be the right offset value
78                    // bottom offset will default to 0
79      * </code></pre></div></li>
80      *
81      * <li><b>Sides</b> : Valid values are <tt>'right'</tt> (or <tt>'r'</tt>) and <tt>'bottom'</tt>
82      * (or <tt>'b'</tt>).<div class="sub-desc">
83      * Either the container must have a fixed size or an anchorSize config value defined at render time in
84      * order for these to have any effect.</div></li>
85      *
86      * <li><b>Mixed</b> : <div class="sub-desc">
87      * Anchor values can also be mixed as needed.  For example, to render the width offset from the container
88      * right edge by 50 pixels and 75% of the container's height use:
89      * <pre><code>
90 anchor: '-50 75%'
91      * </code></pre></div></li>
92      *
93      *
94      * </ul></div>
95      */
96
97     type: 'anchor',
98
99     /**
100      * @cfg {String} defaultAnchor
101      *
102      * default anchor for all child container items applied if no anchor or specific width is set on the child item.  Defaults to '100%'.
103      *
104      */
105     defaultAnchor: '100%',
106
107     parseAnchorRE: /^(r|right|b|bottom)$/i,
108
109     // private
110     onLayout: function() {
111         this.callParent(arguments);
112
113         var me = this,
114             size = me.getLayoutTargetSize(),
115             owner = me.owner,
116             target = me.getTarget(),
117             ownerWidth = size.width,
118             ownerHeight = size.height,
119             overflow = target.getStyle('overflow'),
120             components = me.getVisibleItems(owner),
121             len = components.length,
122             boxes = [],
123             box, newTargetSize, anchorWidth, anchorHeight, component, anchorSpec, calcWidth, calcHeight,
124             anchorsArray, anchor, i, el;
125
126         if (ownerWidth < 20 && ownerHeight < 20) {
127             return;
128         }
129
130         // Anchor layout uses natural HTML flow to arrange the child items.
131         // To ensure that all browsers (I'm looking at you IE!) add the bottom margin of the last child to the
132         // containing element height, we create a zero-sized element with style clear:both to force a "new line"
133         if (!me.clearEl) {
134             me.clearEl = target.createChild({
135                 cls: Ext.baseCSSPrefix + 'clear',
136                 role: 'presentation'
137             });
138         }
139
140         // find the container anchoring size
141         if (owner.anchorSize) {
142             if (typeof owner.anchorSize == 'number') {
143                 anchorWidth = owner.anchorSize;
144             }
145             else {
146                 anchorWidth = owner.anchorSize.width;
147                 anchorHeight = owner.anchorSize.height;
148             }
149         }
150         else {
151             anchorWidth = owner.initialConfig.width;
152             anchorHeight = owner.initialConfig.height;
153         }
154
155         // Work around WebKit RightMargin bug. We're going to inline-block all the children only ONCE and remove it when we're done
156         if (!Ext.supports.RightMargin) {
157             target.addCls(Ext.baseCSSPrefix + 'inline-children');
158         }
159
160         for (i = 0; i < len; i++) {
161             component = components[i];
162             el = component.el;
163             anchor = component.anchor;
164
165             if (!component.anchor && component.items && !Ext.isNumber(component.width) && !(Ext.isIE6 && Ext.isStrict)) {
166                 component.anchor = anchor = me.defaultAnchor;
167             }
168
169             if (anchor) {
170                 anchorSpec = component.anchorSpec;
171                 // cache all anchor values
172                 if (!anchorSpec) {
173                     anchorsArray = anchor.split(' ');
174                     component.anchorSpec = anchorSpec = {
175                         right: me.parseAnchor(anchorsArray[0], component.initialConfig.width, anchorWidth),
176                         bottom: me.parseAnchor(anchorsArray[1], component.initialConfig.height, anchorHeight)
177                     };
178                 }
179                 calcWidth = anchorSpec.right ? me.adjustWidthAnchor(anchorSpec.right(ownerWidth) - el.getMargin('lr'), component) : undefined;
180                 calcHeight = anchorSpec.bottom ? me.adjustHeightAnchor(anchorSpec.bottom(ownerHeight) - el.getMargin('tb'), component) : undefined;
181
182                 boxes.push({
183                     component: component,
184                     anchor: true,
185                     width: calcWidth || undefined,
186                     height: calcHeight || undefined
187                 });
188             } else {
189                 boxes.push({
190                     component: component,
191                     anchor: false
192                 });
193             }
194         }
195
196         // Work around WebKit RightMargin bug. We're going to inline-block all the children only ONCE and remove it when we're done
197         if (!Ext.supports.RightMargin) {
198             target.removeCls(Ext.baseCSSPrefix + 'inline-children');
199         }
200
201         for (i = 0; i < len; i++) {
202             box = boxes[i];
203             me.setItemSize(box.component, box.width, box.height);
204         }
205
206         if (overflow && overflow != 'hidden' && !me.adjustmentPass) {
207             newTargetSize = me.getLayoutTargetSize();
208             if (newTargetSize.width != size.width || newTargetSize.height != size.height) {
209                 me.adjustmentPass = true;
210                 me.onLayout();
211             }
212         }
213
214         delete me.adjustmentPass;
215     },
216
217     // private
218     parseAnchor: function(a, start, cstart) {
219         if (a && a != 'none') {
220             var ratio;
221             // standard anchor
222             if (this.parseAnchorRE.test(a)) {
223                 var diff = cstart - start;
224                 return function(v) {
225                     return v - diff;
226                 };
227             }    
228             // percentage
229             else if (a.indexOf('%') != -1) {
230                 ratio = parseFloat(a.replace('%', '')) * 0.01;
231                 return function(v) {
232                     return Math.floor(v * ratio);
233                 };
234             }    
235             // simple offset adjustment
236             else {
237                 a = parseInt(a, 10);
238                 if (!isNaN(a)) {
239                     return function(v) {
240                         return v + a;
241                     };
242                 }
243             }
244         }
245         return null;
246     },
247
248     // private
249     adjustWidthAnchor: function(value, comp) {
250         return value;
251     },
252
253     // private
254     adjustHeightAnchor: function(value, comp) {
255         return value;
256     }
257
258 });