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