Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / layout / container / Column.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.layout.container.Column
17  * @extends Ext.layout.container.Auto
18  * <p>This is the layout style of choice for creating structural layouts in a multi-column format where the width of
19  * each column can be specified as a percentage or fixed width, but the height is allowed to vary based on the content.
20  * This class is intended to be extended or created via the layout:'column' {@link Ext.container.Container#layout} config,
21  * and should generally not need to be created directly via the new keyword.</p>
22  * <p>ColumnLayout does not have any direct config options (other than inherited ones), but it does support a
23  * specific config property of <b><tt>columnWidth</tt></b> that can be included in the config of any panel added to it.  The
24  * layout will use the columnWidth (if present) or width of each panel during layout to determine how to size each panel.
25  * If width or columnWidth is not specified for a given panel, its width will default to the panel's width (or auto).</p>
26  * <p>The width property is always evaluated as pixels, and must be a number greater than or equal to 1.
27  * The columnWidth property is always evaluated as a percentage, and must be a decimal value greater than 0 and
28  * less than 1 (e.g., .25).</p>
29  * <p>The basic rules for specifying column widths are pretty simple.  The logic makes two passes through the
30  * set of contained panels.  During the first layout pass, all panels that either have a fixed width or none
31  * specified (auto) are skipped, but their widths are subtracted from the overall container width.  During the second
32  * pass, all panels with columnWidths are assigned pixel widths in proportion to their percentages based on
33  * the total <b>remaining</b> container width.  In other words, percentage width panels are designed to fill the space
34  * left over by all the fixed-width and/or auto-width panels.  Because of this, while you can specify any number of columns
35  * with different percentages, the columnWidths must always add up to 1 (or 100%) when added together, otherwise your
36  * layout may not render as expected.  
37  * {@img Ext.layout.container.Column/Ext.layout.container.Column1.png Ext.layout.container.Column container layout}
38  * Example usage:</p>
39  * <pre><code>
40     // All columns are percentages -- they must add up to 1
41     Ext.create('Ext.panel.Panel', {
42         title: 'Column Layout - Percentage Only',
43         width: 350,
44         height: 250,
45         layout:'column',
46         items: [{
47             title: 'Column 1',
48             columnWidth: .25
49         },{
50             title: 'Column 2',
51             columnWidth: .55
52         },{
53             title: 'Column 3',
54             columnWidth: .20
55         }],
56         renderTo: Ext.getBody()
57     }); 
58
59 // {@img Ext.layout.container.Column/Ext.layout.container.Column2.png Ext.layout.container.Column container layout}
60 // Mix of width and columnWidth -- all columnWidth values must add up
61 // to 1. The first column will take up exactly 120px, and the last two
62 // columns will fill the remaining container width.
63
64     Ext.create('Ext.Panel', {
65         title: 'Column Layout - Mixed',
66         width: 350,
67         height: 250,
68         layout:'column',
69         items: [{
70             title: 'Column 1',
71             width: 120
72         },{
73             title: 'Column 2',
74             columnWidth: .7
75         },{
76             title: 'Column 3',
77             columnWidth: .3
78         }],
79         renderTo: Ext.getBody()
80     }); 
81 </code></pre>
82  */
83 Ext.define('Ext.layout.container.Column', {
84
85     extend: 'Ext.layout.container.Auto',
86     alias: ['layout.column'],
87     alternateClassName: 'Ext.layout.ColumnLayout',
88
89     type: 'column',
90
91     itemCls: Ext.baseCSSPrefix + 'column',
92
93     targetCls: Ext.baseCSSPrefix + 'column-layout-ct',
94
95     scrollOffset: 0,
96
97     bindToOwnerCtComponent: false,
98
99     getRenderTarget : function() {
100         if (!this.innerCt) {
101
102             // the innerCt prevents wrapping and shuffling while
103             // the container is resizing
104             this.innerCt = this.getTarget().createChild({
105                 cls: Ext.baseCSSPrefix + 'column-inner'
106             });
107
108             // Column layout uses natural HTML flow to arrange the child items.
109             // To ensure that all browsers (I'm looking at you IE!) add the bottom margin of the last child to the
110             // containing element height, we create a zero-sized element with style clear:both to force a "new line"
111             this.clearEl = this.innerCt.createChild({
112                 cls: Ext.baseCSSPrefix + 'clear',
113                 role: 'presentation'
114             });
115         }
116         return this.innerCt;
117     },
118
119     // private
120     onLayout : function() {
121         var me = this,
122             target = me.getTarget(),
123             items = me.getLayoutItems(),
124             len = items.length,
125             item,
126             i,
127             parallelMargins = [],
128             itemParallelMargins,
129             size,
130             availableWidth,
131             columnWidth;
132
133         size = me.getLayoutTargetSize();
134         if (size.width < len * 10) { // Don't lay out in impossibly small target (probably display:none, or initial, unsized Container)
135             return;
136         }
137
138         // On the first pass, for all except IE6-7, we lay out the items with no scrollbars visible using style overflow: hidden.
139         // If, after the layout, it is detected that there is vertical overflow,
140         // we will recurse back through here. Do not adjust overflow style at that time.
141         if (me.adjustmentPass) {
142             if (Ext.isIE6 || Ext.isIE7 || Ext.isIEQuirks) {
143                 size.width = me.adjustedWidth;
144             }
145         } else {
146             i = target.getStyle('overflow');
147             if (i && i != 'hidden') {
148                 me.autoScroll = true;
149                 if (!(Ext.isIE6 || Ext.isIE7 || Ext.isIEQuirks)) {
150                     target.setStyle('overflow', 'hidden');
151                     size = me.getLayoutTargetSize();
152                 }
153             }
154         }
155
156         availableWidth = size.width - me.scrollOffset;
157         me.innerCt.setWidth(availableWidth);
158
159         // some columns can be percentages while others are fixed
160         // so we need to make 2 passes
161         for (i = 0; i < len; i++) {
162             item = items[i];
163             itemParallelMargins = parallelMargins[i] = item.getEl().getMargin('lr');
164             if (!item.columnWidth) {
165                 availableWidth -= (item.getWidth() + itemParallelMargins);
166             }
167         }
168
169         availableWidth = availableWidth < 0 ? 0 : availableWidth;
170         for (i = 0; i < len; i++) {
171             item = items[i];
172             if (item.columnWidth) {
173                 columnWidth = Math.floor(item.columnWidth * availableWidth) - parallelMargins[i];
174                 if (item.getWidth() != columnWidth) {
175                     me.setItemSize(item, columnWidth, item.height);
176                 }
177             }
178         }
179
180         // After the first pass on an autoScroll layout, restore the overflow settings if it had been changed (only changed for non-IE6)
181         if (!me.adjustmentPass && me.autoScroll) {
182
183             // If there's a vertical overflow, relay with scrollbars
184             target.setStyle('overflow', 'auto');
185             me.adjustmentPass = (target.dom.scrollHeight > size.height);
186             if (Ext.isIE6 || Ext.isIE7 || Ext.isIEQuirks) {
187                 me.adjustedWidth = size.width - Ext.getScrollBarWidth();
188             } else {
189                 target.setStyle('overflow', 'auto');
190             }
191
192             // If the layout caused height overflow, recurse back and recalculate (with overflow setting restored on non-IE6)
193             if (me.adjustmentPass) {
194                 me.onLayout();
195             }
196         }
197         delete me.adjustmentPass;
198     },
199
200     configureItem: function(item) {
201         if (item.columnWidth) {
202             item.layoutManagedWidth = 1;
203         } else {
204             item.layoutManagedWidth = 2;
205         }
206         item.layoutManagedHeight = 2;
207         this.callParent(arguments);
208     }
209 });