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