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