Upgrade to ExtJS 3.1.1 - Released 02/08/2010
[extjs.git] / src / widgets / layout / AccordionLayout.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.AccordionLayout\r
9  * @extends Ext.layout.FitLayout\r
10  * <p>This is a layout that manages multiple Panels in an expandable accordion style such that only\r
11  * <b>one Panel can be expanded at any given time</b>. Each Panel has built-in support for expanding and collapsing.</p>\r
12  * <p>Note: Only Ext.Panels <b>and all subclasses of Ext.Panel</b> may be used in an accordion layout Container.</p>\r
13  * <p>This class is intended to be extended or created via the <tt><b>{@link Ext.Container#layout layout}</b></tt>\r
14  * configuration property.  See <tt><b>{@link Ext.Container#layout}</b></tt> for additional details.</p>\r
15  * <p>Example usage:</p>\r
16  * <pre><code>\r
17 var accordion = new Ext.Panel({\r
18     title: 'Accordion Layout',\r
19     layout:'accordion',\r
20     defaults: {\r
21         // applied to each contained panel\r
22         bodyStyle: 'padding:15px'\r
23     },\r
24     layoutConfig: {\r
25         // layout-specific configs go here\r
26         titleCollapse: false,\r
27         animate: true,\r
28         activeOnTop: true\r
29     },\r
30     items: [{\r
31         title: 'Panel 1',\r
32         html: '&lt;p&gt;Panel content!&lt;/p&gt;'\r
33     },{\r
34         title: 'Panel 2',\r
35         html: '&lt;p&gt;Panel content!&lt;/p&gt;'\r
36     },{\r
37         title: 'Panel 3',\r
38         html: '&lt;p&gt;Panel content!&lt;/p&gt;'\r
39     }]\r
40 });\r
41 </code></pre>\r
42  */\r
43 Ext.layout.AccordionLayout = Ext.extend(Ext.layout.FitLayout, {\r
44     /**\r
45      * @cfg {Boolean} fill\r
46      * True to adjust the active item's height to fill the available space in the container, false to use the\r
47      * item's current height, or auto height if not explicitly set (defaults to true).\r
48      */\r
49     fill : true,\r
50     /**\r
51      * @cfg {Boolean} autoWidth\r
52      * True to set each contained item's width to 'auto', false to use the item's current width (defaults to true).\r
53      * Note that some components, in particular the {@link Ext.grid.GridPanel grid}, will not function properly within\r
54      * layouts if they have auto width, so in such cases this config should be set to false.\r
55      */\r
56     autoWidth : true,\r
57     /**\r
58      * @cfg {Boolean} titleCollapse\r
59      * True to allow expand/collapse of each contained panel by clicking anywhere on the title bar, false to allow\r
60      * expand/collapse only when the toggle tool button is clicked (defaults to true).  When set to false,\r
61      * {@link #hideCollapseTool} should be false also.\r
62      */\r
63     titleCollapse : true,\r
64     /**\r
65      * @cfg {Boolean} hideCollapseTool\r
66      * True to hide the contained panels' collapse/expand toggle buttons, false to display them (defaults to false).\r
67      * When set to true, {@link #titleCollapse} should be true also.\r
68      */\r
69     hideCollapseTool : false,\r
70     /**\r
71      * @cfg {Boolean} collapseFirst\r
72      * True to make sure the collapse/expand toggle button always renders first (to the left of) any other tools\r
73      * in the contained panels' title bars, false to render it last (defaults to false).\r
74      */\r
75     collapseFirst : false,\r
76     /**\r
77      * @cfg {Boolean} animate\r
78      * True to slide the contained panels open and closed during expand/collapse using animation, false to open and\r
79      * close directly with no animation (defaults to false).  Note: to defer to the specific config setting of each\r
80      * contained panel for this property, set this to undefined at the layout level.\r
81      */\r
82     animate : false,\r
83     /**\r
84      * @cfg {Boolean} sequence\r
85      * <b>Experimental</b>. If animate is set to true, this will result in each animation running in sequence.\r
86      */\r
87     sequence : false,\r
88     /**\r
89      * @cfg {Boolean} activeOnTop\r
90      * True to swap the position of each panel as it is expanded so that it becomes the first item in the container,\r
91      * false to keep the panels in the rendered order. <b>This is NOT compatible with "animate:true"</b> (defaults to false).\r
92      */\r
93     activeOnTop : false,\r
94 \r
95     type: 'accordion',\r
96 \r
97     renderItem : function(c){\r
98         if(this.animate === false){\r
99             c.animCollapse = false;\r
100         }\r
101         c.collapsible = true;\r
102         if(this.autoWidth){\r
103             c.autoWidth = true;\r
104         }\r
105         if(this.titleCollapse){\r
106             c.titleCollapse = true;\r
107         }\r
108         if(this.hideCollapseTool){\r
109             c.hideCollapseTool = true;\r
110         }\r
111         if(this.collapseFirst !== undefined){\r
112             c.collapseFirst = this.collapseFirst;\r
113         }\r
114         if(!this.activeItem && !c.collapsed){\r
115             this.setActiveItem(c, true);\r
116         }else if(this.activeItem && this.activeItem != c){\r
117             c.collapsed = true;\r
118         }\r
119         Ext.layout.AccordionLayout.superclass.renderItem.apply(this, arguments);\r
120         c.header.addClass('x-accordion-hd');\r
121         c.on('beforeexpand', this.beforeExpand, this);\r
122     },\r
123 \r
124     onRemove: function(c){\r
125         Ext.layout.AccordionLayout.superclass.onRemove.call(this, c);\r
126         if(c.rendered){\r
127             c.header.removeClass('x-accordion-hd');\r
128         }\r
129         c.un('beforeexpand', this.beforeExpand, this);\r
130     },\r
131 \r
132     // private\r
133     beforeExpand : function(p, anim){\r
134         var ai = this.activeItem;\r
135         if(ai){\r
136             if(this.sequence){\r
137                 delete this.activeItem;\r
138                 if (!ai.collapsed){\r
139                     ai.collapse({callback:function(){\r
140                         p.expand(anim || true);\r
141                     }, scope: this});\r
142                     return false;\r
143                 }\r
144             }else{\r
145                 ai.collapse(this.animate);\r
146             }\r
147         }\r
148         this.setActive(p);\r
149         if(this.activeOnTop){\r
150             p.el.dom.parentNode.insertBefore(p.el.dom, p.el.dom.parentNode.firstChild);\r
151         }\r
152         // Items have been hidden an possibly rearranged, we need to get the container size again.\r
153         this.layout();\r
154     },\r
155 \r
156     // private\r
157     setItemSize : function(item, size){\r
158         if(this.fill && item){\r
159             var hh = 0, i, ct = this.getRenderedItems(this.container), len = ct.length, p;\r
160             // Add up all the header heights\r
161             for (i = 0; i < len; i++) {\r
162                 if((p = ct[i]) != item){\r
163                     hh += p.header.getHeight();\r
164                 }\r
165             };\r
166             // Subtract the header heights from the container size\r
167             size.height -= hh;\r
168             // Call setSize on the container to set the correct height.  For Panels, deferedHeight\r
169             // will simply store this size for when the expansion is done.\r
170             item.setSize(size);\r
171         }\r
172     },\r
173 \r
174     /**\r
175      * Sets the active (expanded) item in the layout.\r
176      * @param {String/Number} item The string component id or numeric index of the item to activate\r
177      */\r
178     setActiveItem : function(item){\r
179         this.setActive(item, true);\r
180     },\r
181 \r
182     // private\r
183     setActive : function(item, expand){\r
184         var ai = this.activeItem;\r
185         item = this.container.getComponent(item);\r
186         if(ai != item){\r
187             if(item.rendered && item.collapsed && expand){\r
188                 item.expand();\r
189             }else{\r
190                 if(ai){\r
191                    ai.fireEvent('deactivate', ai);\r
192                 }\r
193                 this.activeItem = item;\r
194                 item.fireEvent('activate', item);\r
195             }\r
196         }\r
197     }\r
198 });\r
199 Ext.Container.LAYOUTS.accordion = Ext.layout.AccordionLayout;\r
200 \r
201 //backwards compat\r
202 Ext.layout.Accordion = Ext.layout.AccordionLayout;