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