Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / src / widgets / layout / ColumnLayout.js
1 /*!
2  * Ext JS Library 3.2.2
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.layout.ColumnLayout
9  * @extends Ext.layout.ContainerLayout
10  * <p>This is the layout style of choice for creating structural layouts in a multi-column format where the width of
11  * each column can be specified as a percentage or fixed width, but the height is allowed to vary based on the content.
12  * This class is intended to be extended or created via the layout:'column' {@link Ext.Container#layout} config,
13  * and should generally not need to be created directly via the new keyword.</p>
14  * <p>ColumnLayout does not have any direct config options (other than inherited ones), but it does support a
15  * specific config property of <b><tt>columnWidth</tt></b> that can be included in the config of any panel added to it.  The
16  * layout will use the columnWidth (if present) or width of each panel during layout to determine how to size each panel.
17  * If width or columnWidth is not specified for a given panel, its width will default to the panel's width (or auto).</p>
18  * <p>The width property is always evaluated as pixels, and must be a number greater than or equal to 1.
19  * The columnWidth property is always evaluated as a percentage, and must be a decimal value greater than 0 and
20  * less than 1 (e.g., .25).</p>
21  * <p>The basic rules for specifying column widths are pretty simple.  The logic makes two passes through the
22  * set of contained panels.  During the first layout pass, all panels that either have a fixed width or none
23  * specified (auto) are skipped, but their widths are subtracted from the overall container width.  During the second
24  * pass, all panels with columnWidths are assigned pixel widths in proportion to their percentages based on
25  * the total <b>remaining</b> container width.  In other words, percentage width panels are designed to fill the space
26  * left over by all the fixed-width and/or auto-width panels.  Because of this, while you can specify any number of columns
27  * with different percentages, the columnWidths must always add up to 1 (or 100%) when added together, otherwise your
28  * layout may not render as expected.  Example usage:</p>
29  * <pre><code>
30 // All columns are percentages -- they must add up to 1
31 var p = new Ext.Panel({
32     title: 'Column Layout - Percentage Only',
33     layout:'column',
34     items: [{
35         title: 'Column 1',
36         columnWidth: .25
37     },{
38         title: 'Column 2',
39         columnWidth: .6
40     },{
41         title: 'Column 3',
42         columnWidth: .15
43     }]
44 });
45
46 // Mix of width and columnWidth -- all columnWidth values must add up
47 // to 1. The first column will take up exactly 120px, and the last two
48 // columns will fill the remaining container width.
49 var p = new Ext.Panel({
50     title: 'Column Layout - Mixed',
51     layout:'column',
52     items: [{
53         title: 'Column 1',
54         width: 120
55     },{
56         title: 'Column 2',
57         columnWidth: .8
58     },{
59         title: 'Column 3',
60         columnWidth: .2
61     }]
62 });
63 </code></pre>
64  */
65 Ext.layout.ColumnLayout = Ext.extend(Ext.layout.ContainerLayout, {
66     // private
67     monitorResize:true,
68
69     type: 'column',
70
71     extraCls: 'x-column',
72
73     scrollOffset : 0,
74
75     // private
76
77     targetCls: 'x-column-layout-ct',
78
79     isValidParent : function(c, target){
80         return this.innerCt && c.getPositionEl().dom.parentNode == this.innerCt.dom;
81     },
82
83     getLayoutTargetSize : function() {
84         var target = this.container.getLayoutTarget(), ret;
85         if (target) {
86             ret = target.getViewSize();
87
88             // IE in strict mode will return a width of 0 on the 1st pass of getViewSize.
89             // Use getStyleSize to verify the 0 width, the adjustment pass will then work properly
90             // with getViewSize
91             if (Ext.isIE && Ext.isStrict && ret.width == 0){
92                 ret =  target.getStyleSize();
93             }
94
95             ret.width -= target.getPadding('lr');
96             ret.height -= target.getPadding('tb');
97         }
98         return ret;
99     },
100
101     renderAll : function(ct, target) {
102         if(!this.innerCt){
103             // the innerCt prevents wrapping and shuffling while
104             // the container is resizing
105             this.innerCt = target.createChild({cls:'x-column-inner'});
106             this.innerCt.createChild({cls:'x-clear'});
107         }
108         Ext.layout.ColumnLayout.superclass.renderAll.call(this, ct, this.innerCt);
109     },
110
111     // private
112     onLayout : function(ct, target){
113         var cs = ct.items.items,
114             len = cs.length,
115             c,
116             i,
117             m,
118             margins = [];
119
120         this.renderAll(ct, target);
121
122         var size = this.getLayoutTargetSize();
123
124         if(size.width < 1 && size.height < 1){ // display none?
125             return;
126         }
127
128         var w = size.width - this.scrollOffset,
129             h = size.height,
130             pw = w;
131
132         this.innerCt.setWidth(w);
133
134         // some columns can be percentages while others are fixed
135         // so we need to make 2 passes
136
137         for(i = 0; i < len; i++){
138             c = cs[i];
139             m = c.getPositionEl().getMargins('lr');
140             margins[i] = m;
141             if(!c.columnWidth){
142                 pw -= (c.getWidth() + m);
143             }
144         }
145
146         pw = pw < 0 ? 0 : pw;
147
148         for(i = 0; i < len; i++){
149             c = cs[i];
150             m = margins[i];
151             if(c.columnWidth){
152                 c.setSize(Math.floor(c.columnWidth * pw) - m);
153             }
154         }
155
156         // Browsers differ as to when they account for scrollbars.  We need to re-measure to see if the scrollbar
157         // spaces were accounted for properly.  If not, re-layout.
158         if (Ext.isIE) {
159             if (i = target.getStyle('overflow') && i != 'hidden' && !this.adjustmentPass) {
160                 var ts = this.getLayoutTargetSize();
161                 if (ts.width != size.width){
162                     this.adjustmentPass = true;
163                     this.onLayout(ct, target);
164                 }
165             }
166         }
167         delete this.adjustmentPass;
168     }
169
170     /**
171      * @property activeItem
172      * @hide
173      */
174 });
175
176 Ext.Container.LAYOUTS['column'] = Ext.layout.ColumnLayout;