3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.form.FieldSet
17 * @extends Ext.container.Container
19 * A container for grouping sets of fields, rendered as a HTML `fieldset` element. The {@link #title}
20 * config will be rendered as the fieldset's `legend`.
22 * While FieldSets commonly contain simple groups of fields, they are general {@link Ext.container.Container Containers}
23 * and may therefore contain any type of components in their {@link #items}, including other nested containers.
24 * The default {@link #layout} for the FieldSet's items is `'anchor'`, but it can be configured to use any other
27 * FieldSets may also be collapsed if configured to do so; this can be done in two ways:
29 * 1. Set the {@link #collapsible} config to true; this will result in a collapse button being rendered next to
30 * the {@link #title legend title}, or:
31 * 2. Set the {@link #checkboxToggle} config to true; this is similar to using {@link #collapsible} but renders
32 * a {@link Ext.form.field.Checkbox checkbox} in place of the toggle button. The fieldset will be expanded when the
33 * checkbox is checked and collapsed when it is unchecked. The checkbox will also be included in the
34 * {@link Ext.form.Basic#submit form submit parameters} using the {@link #checkboxName} as its parameter name.
36 * {@img Ext.form.FieldSet/Ext.form.FieldSet.png Ext.form.FieldSet component}
40 * Ext.create('Ext.form.Panel', {
41 * title: 'Simple Form with FieldSets',
42 * labelWidth: 75, // label settings here cascade unless overridden
43 * url: 'save-form.php',
45 * bodyStyle: 'padding:5px 5px 0',
47 * renderTo: Ext.getBody(),
48 * layout: 'column', // arrange fieldsets side by side
53 * // Fieldset in Column 1 - collapsible via toggle button
56 * title: 'Fieldset 1',
58 * defaultType: 'textfield',
59 * defaults: {anchor: '100%'},
62 * fieldLabel: 'Field 1',
65 * fieldLabel: 'Field 2',
69 * // Fieldset in Column 2 - collapsible via checkbox, collapsed by default, contains a panel
71 * title: 'Show Panel', // title or checkboxToggle creates fieldset header
73 * checkboxToggle: true,
74 * collapsed: true, // fieldset initially collapsed
79 * title: 'Panel inside a fieldset',
86 * @docauthor Jason Johnston <jason@sencha.com>
88 Ext.define('Ext.form.FieldSet', {
89 extend: 'Ext.container.Container',
90 alias: 'widget.fieldset',
91 uses: ['Ext.form.field.Checkbox', 'Ext.panel.Tool', 'Ext.layout.container.Anchor', 'Ext.layout.component.FieldSet'],
95 * A title to be displayed in the fieldset's legend. May contain HTML markup.
99 * @cfg {Boolean} checkboxToggle
100 * Set to <tt>true</tt> to render a checkbox into the fieldset frame just
101 * in front of the legend to expand/collapse the fieldset when the checkbox is toggled. (defaults
102 * to <tt>false</tt>). This checkbox will be included in form submits using the {@link #checkboxName}.
106 * @cfg {String} checkboxName
107 * The name to assign to the fieldset's checkbox if <tt>{@link #checkboxToggle} = true</tt>
108 * (defaults to <tt>'[fieldset id]-checkbox'</tt>).
112 * @cfg {Boolean} collapsible
113 * Set to <tt>true</tt> to make the fieldset collapsible and have the expand/collapse toggle button automatically
114 * rendered into the legend element, <tt>false</tt> to keep the fieldset statically sized with no collapse
115 * button (defaults to <tt>false</tt>). Another option is to configure <tt>{@link #checkboxToggle}</tt>.
116 * Use the {@link #collapsed} config to collapse the fieldset by default.
120 * @cfg {Boolean} collapsed
121 * Set to <tt>true</tt> to render the fieldset as collapsed by default. If {@link #checkboxToggle} is specified,
122 * the checkbox will also be unchecked by default.
128 * @type Ext.Component
129 * The component for the fieldset's legend. Will only be defined if the configuration requires a legend
130 * to be created, by setting the {@link #title} or {@link #checkboxToggle} options.
134 * @cfg {String} baseCls The base CSS class applied to the fieldset (defaults to <tt>'x-fieldset'</tt>).
136 baseCls: Ext.baseCSSPrefix + 'fieldset',
139 * @cfg {String} layout The {@link Ext.container.Container#layout} for the fieldset's immediate child items.
140 * Defaults to <tt>'anchor'</tt>.
144 componentLayout: 'fieldset',
146 // No aria role necessary as fieldset has its own recognized semantics
149 renderTpl: ['<div class="{baseCls}-body"></div>'],
151 maskOnDisable: false,
153 getElConfig: function(){
154 return {tag: 'fieldset', id: this.id};
157 initComponent: function() {
159 baseCls = me.baseCls;
163 // Create the Legend component if needed
166 // Add body el selector
167 Ext.applyIf(me.renderSelectors, {
168 body: '.' + baseCls + '-body'
172 me.addCls(baseCls + '-collapsed');
178 onRender: function(container, position) {
179 this.callParent(arguments);
180 // Make sure the legend is created and rendered
186 * Initialize and render the legend component if necessary
188 initLegend: function() {
193 // Create the legend component if needed and it hasn't been already
194 if (!legend && (me.title || me.checkboxToggle || me.collapsible)) {
198 if (me.checkboxToggle) {
199 legendItems.push(me.createCheckboxCmp());
202 else if (me.collapsible) {
203 legendItems.push(me.createToggleCmp());
207 legendItems.push(me.createTitleCmp());
209 legend = me.legend = Ext.create('Ext.container.Container', {
210 baseCls: me.baseCls + '-header',
212 getElConfig: function(){
213 return {tag: 'legend', cls: this.baseCls};
219 // Make sure legend is rendered if the fieldset is rendered
220 if (legend && !legend.rendered && me.rendered) {
221 me.legend.render(me.el, me.body); //insert before body element
227 * Creates the legend title component. This is only called internally, but could be overridden in subclasses
228 * to customize the title component.
229 * @return Ext.Component
231 createTitleCmp: function() {
233 me.titleCmp = Ext.create('Ext.Component', {
235 cls: me.baseCls + '-header-text'
242 * @property checkboxCmp
243 * @type Ext.form.field.Checkbox
244 * Refers to the {@link Ext.form.field.Checkbox} component that is added next to the title in the legend. Only
245 * populated if the fieldset is configured with <tt>{@link #checkboxToggle}:true</tt>.
250 * Creates the checkbox component. This is only called internally, but could be overridden in subclasses
251 * to customize the checkbox's configuration or even return an entirely different component type.
252 * @return Ext.Component
254 createCheckboxCmp: function() {
256 suffix = '-checkbox';
258 me.checkboxCmp = Ext.create('Ext.form.field.Checkbox', {
259 name: me.checkboxName || me.id + suffix,
260 cls: me.baseCls + '-header' + suffix,
261 checked: !me.collapsed,
263 change: me.onCheckChange,
267 return me.checkboxCmp;
271 * @property toggleCmp
272 * @type Ext.panel.Tool
273 * Refers to the {@link Ext.panel.Tool} component that is added as the collapse/expand button next
274 * to the title in the legend. Only populated if the fieldset is configured with <tt>{@link #collapsible}:true</tt>.
279 * Creates the toggle button component. This is only called internally, but could be overridden in
280 * subclasses to customize the toggle component.
281 * @return Ext.Component
283 createToggleCmp: function() {
285 me.toggleCmp = Ext.create('Ext.panel.Tool', {
294 * Sets the title of this fieldset
295 * @param {String} title The new title
296 * @return {Ext.form.FieldSet} this
298 setTitle: function(title) {
302 me.titleCmp.update(title);
306 getTargetEl : function() {
307 return this.body || this.frameBody || this.el;
310 getContentTarget: function() {
316 * Include the legend component in the items for ComponentQuery
318 getRefItems: function(deep) {
319 var refItems = this.callParent(arguments),
320 legend = this.legend;
322 // Prepend legend items to ensure correct order
324 refItems.unshift(legend);
326 refItems.unshift.apply(refItems, legend.getRefItems(true));
333 * Expands the fieldset.
334 * @return {Ext.form.FieldSet} this
337 return this.setExpanded(true);
341 * Collapses the fieldset.
342 * @return {Ext.form.FieldSet} this
344 collapse : function() {
345 return this.setExpanded(false);
349 * @private Collapse or expand the fieldset
351 setExpanded: function(expanded) {
353 checkboxCmp = me.checkboxCmp;
355 expanded = !!expanded;
358 checkboxCmp.setValue(expanded);
362 me.removeCls(me.baseCls + '-collapsed');
364 me.addCls(me.baseCls + '-collapsed');
366 me.collapsed = !expanded;
368 // ensure subitems will get rendered and layed out when expanding
369 me.getComponentLayout().childrenChanged = true;
371 me.doComponentLayout();
376 * Toggle the fieldset's collapsed state to the opposite of what it is currently
379 this.setExpanded(!!this.collapsed);
383 * @private Handle changes in the checkbox checked state
385 onCheckChange: function(cmp, checked) {
386 this.setExpanded(checked);
389 beforeDestroy : function() {
390 var legend = this.legend;