Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / widgets / layout / FormLayout.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.layout.FormLayout
9  * @extends Ext.layout.AnchorLayout
10  * <p>This layout manager is specifically designed for rendering and managing child Components of
11  * {@link Ext.form.FormPanel forms}. It is responsible for rendering the labels of
12  * {@link Ext.form.Field Field}s.</p>
13  *
14  * <p>This layout manager is used when a Container is configured with the <tt>layout:'form'</tt>
15  * {@link Ext.Container#layout layout} config option, and should generally not need to be created directly
16  * via the new keyword. See <tt><b>{@link Ext.Container#layout}</b></tt> for additional details.</p>
17  *
18  * <p>In an application, it will usually be preferrable to use a {@link Ext.form.FormPanel FormPanel}
19  * (which is configured with FormLayout as its layout class by default) since it also provides built-in
20  * functionality for {@link Ext.form.BasicForm#doAction loading, validating and submitting} the form.</p>
21  *
22  * <p>A {@link Ext.Container Container} <i>using</i> the FormLayout layout manager (e.g.
23  * {@link Ext.form.FormPanel} or specifying <tt>layout:'form'</tt>) can also accept the following
24  * layout-specific config properties:<div class="mdetail-params"><ul>
25  * <li><b><tt>{@link Ext.form.FormPanel#hideLabels hideLabels}</tt></b></li>
26  * <li><b><tt>{@link Ext.form.FormPanel#labelAlign labelAlign}</tt></b></li>
27  * <li><b><tt>{@link Ext.form.FormPanel#labelPad labelPad}</tt></b></li>
28  * <li><b><tt>{@link Ext.form.FormPanel#labelSeparator labelSeparator}</tt></b></li>
29  * <li><b><tt>{@link Ext.form.FormPanel#labelWidth labelWidth}</tt></b></li>
30  * </ul></div></p>
31  *
32  * <p>Any Component (including Fields) managed by FormLayout accepts the following as a config option:
33  * <div class="mdetail-params"><ul>
34  * <li><b><tt>{@link Ext.Component#anchor anchor}</tt></b></li>
35  * </ul></div></p>
36  *
37  * <p>Any Component managed by FormLayout may be rendered as a form field (with an associated label) by
38  * configuring it with a non-null <b><tt>{@link Ext.Component#fieldLabel fieldLabel}</tt></b>. Components configured
39  * in this way may be configured with the following options which affect the way the FormLayout renders them:
40  * <div class="mdetail-params"><ul>
41  * <li><b><tt>{@link Ext.Component#clearCls clearCls}</tt></b></li>
42  * <li><b><tt>{@link Ext.Component#fieldLabel fieldLabel}</tt></b></li>
43  * <li><b><tt>{@link Ext.Component#hideLabel hideLabel}</tt></b></li>
44  * <li><b><tt>{@link Ext.Component#itemCls itemCls}</tt></b></li>
45  * <li><b><tt>{@link Ext.Component#labelSeparator labelSeparator}</tt></b></li>
46  * <li><b><tt>{@link Ext.Component#labelStyle labelStyle}</tt></b></li>
47  * </ul></div></p>
48  *
49  * <p>Example usage:</p>
50  * <pre><code>
51 // Required if showing validation messages
52 Ext.QuickTips.init();
53
54 // While you can create a basic Panel with layout:'form', practically
55 // you should usually use a FormPanel to also get its form functionality
56 // since it already creates a FormLayout internally.
57 var form = new Ext.form.FormPanel({
58     title: 'Form Layout',
59     bodyStyle: 'padding:15px',
60     width: 350,
61     defaultType: 'textfield',
62     defaults: {
63         // applied to each contained item
64         width: 230,
65         msgTarget: 'side'
66     },
67     items: [{
68             fieldLabel: 'First Name',
69             name: 'first',
70             allowBlank: false,
71             {@link Ext.Component#labelSeparator labelSeparator}: ':' // override labelSeparator layout config
72         },{
73             fieldLabel: 'Last Name',
74             name: 'last'
75         },{
76             fieldLabel: 'Email',
77             name: 'email',
78             vtype:'email'
79         }, {
80             xtype: 'textarea',
81             hideLabel: true,     // override hideLabels layout config
82             name: 'msg',
83             anchor: '100% -53'
84         }
85     ],
86     buttons: [
87         {text: 'Save'},
88         {text: 'Cancel'}
89     ],
90     layoutConfig: {
91         {@link #labelSeparator}: '~' // superseded by assignment below
92     },
93     // config options applicable to container when layout='form':
94     hideLabels: false,
95     labelAlign: 'left',   // or 'right' or 'top'
96     {@link Ext.form.FormPanel#labelSeparator labelSeparator}: '>>', // takes precedence over layoutConfig value
97     labelWidth: 65,       // defaults to 100
98     labelPad: 8           // defaults to 5, must specify labelWidth to be honored
99 });
100 </code></pre>
101  */
102 Ext.layout.FormLayout = Ext.extend(Ext.layout.AnchorLayout, {
103
104     /**
105      * @cfg {String} labelSeparator
106      * See {@link Ext.form.FormPanel}.{@link Ext.form.FormPanel#labelSeparator labelSeparator}.  Configuration
107      * of this property at the <b>container</b> level takes precedence.
108      */
109     labelSeparator : ':',
110
111     /**
112      * Read only. The CSS style specification string added to field labels in this layout if not
113      * otherwise {@link Ext.Component#labelStyle specified by each contained field}.
114      * @type String
115      * @property labelStyle
116      */
117     
118     /**
119      * @cfg {Boolean} trackLabels
120      * True to show/hide the field label when the field is hidden. Defaults to <tt>false</tt>.
121      */
122     trackLabels: false,
123     
124
125     onRemove: function(c){
126         Ext.layout.FormLayout.superclass.onRemove.call(this, c);
127         if(this.trackLabels && !this.isHide(c)){
128             c.un('show', this.onFieldShow, this);
129             c.un('hide', this.onFieldHide, this);
130         }
131         // check for itemCt, since we may be removing a fieldset or something similar
132         var el = c.getPositionEl(),
133                 ct = c.getItemCt && c.getItemCt();
134         if(c.rendered && ct){
135             el.insertAfter(ct);
136             Ext.destroy(ct);
137             Ext.destroyMembers(c, 'label', 'itemCt');
138             if(c.customItemCt){
139                 Ext.destroyMembers(c, 'getItemCt', 'customItemCt');
140             }
141         }
142     },
143     
144     // private
145     setContainer : function(ct){
146         Ext.layout.FormLayout.superclass.setContainer.call(this, ct);
147         if(ct.labelAlign){
148             ct.addClass('x-form-label-'+ct.labelAlign);
149         }
150
151         if(ct.hideLabels){
152             Ext.apply(this, {
153                 labelStyle: 'display:none',
154                 elementStyle: 'padding-left:0;',
155                 labelAdjust: 0
156             });
157         }else{
158             this.labelSeparator = ct.labelSeparator || this.labelSeparator;
159             ct.labelWidth = ct.labelWidth || 100;
160             if(Ext.isNumber(ct.labelWidth)){
161                 var pad = Ext.isNumber(ct.labelPad) ? ct.labelPad : 5;
162                 Ext.apply(this, {
163                     labelAdjust: ct.labelWidth + pad,
164                     labelStyle: 'width:' + ct.labelWidth + 'px;',
165                     elementStyle: 'padding-left:' + (ct.labelWidth + pad) + 'px'
166                 });
167             }
168             if(ct.labelAlign == 'top'){
169                 Ext.apply(this, {
170                     labelStyle: 'width:auto;',
171                     labelAdjust: 0,
172                     elementStyle: 'padding-left:0;'
173                 });
174             }
175         }
176     },
177     
178     isHide: function(c){
179         return c.hideLabel || this.container.hideLabels;
180     },
181     
182     onFieldShow: function(c){
183         c.getItemCt().removeClass('x-hide-' + c.hideMode);
184     },
185     
186     onFieldHide: function(c){
187         c.getItemCt().addClass('x-hide-' + c.hideMode);   
188     },
189
190     //private
191     getLabelStyle: function(s){
192         var ls = '', items = [this.labelStyle, s];
193         for (var i = 0, len = items.length; i < len; ++i){
194             if (items[i]){
195                 ls += items[i];
196                 if (ls.substr(-1, 1) != ';'){
197                     ls += ';'
198                 }
199             }
200         }
201         return ls;
202     },
203
204     /**
205      * @cfg {Ext.Template} fieldTpl
206      * A {@link Ext.Template#compile compile}d {@link Ext.Template} for rendering
207      * the fully wrapped, labeled and styled form Field. Defaults to:</p><pre><code>
208 new Ext.Template(
209     &#39;&lt;div class="x-form-item {itemCls}" tabIndex="-1">&#39;,
210         &#39;&lt;&#108;abel for="{id}" style="{labelStyle}" class="x-form-item-&#108;abel">{&#108;abel}{labelSeparator}&lt;/&#108;abel>&#39;,
211         &#39;&lt;div class="x-form-element" id="x-form-el-{id}" style="{elementStyle}">&#39;,
212         &#39;&lt;/div>&lt;div class="{clearCls}">&lt;/div>&#39;,
213     '&lt;/div>'
214 );
215 </code></pre>
216      * <p>This may be specified to produce a different DOM structure when rendering form Fields.</p>
217      * <p>A description of the properties within the template follows:</p><div class="mdetail-params"><ul>
218      * <li><b><tt>itemCls</tt></b> : String<div class="sub-desc">The CSS class applied to the outermost div wrapper
219      * that contains this field label and field element (the default class is <tt>'x-form-item'</tt> and <tt>itemCls</tt>
220      * will be added to that). If supplied, <tt>itemCls</tt> at the field level will override the default <tt>itemCls</tt>
221      * supplied at the container level.</div></li>
222      * <li><b><tt>id</tt></b> : String<div class="sub-desc">The id of the Field</div></li>
223      * <li><b><tt>{@link #labelStyle}</tt></b> : String<div class="sub-desc">
224      * A CSS style specification string to add to the field label for this field (defaults to <tt>''</tt> or the
225      * {@link #labelStyle layout's value for <tt>labelStyle</tt>}).</div></li>
226      * <li><b><tt>label</tt></b> : String<div class="sub-desc">The text to display as the label for this
227      * field (defaults to <tt>''</tt>)</div></li>
228      * <li><b><tt>{@link #labelSeparator}</tt></b> : String<div class="sub-desc">The separator to display after
229      * the text of the label for this field (defaults to a colon <tt>':'</tt> or the
230      * {@link #labelSeparator layout's value for labelSeparator}). To hide the separator use empty string ''.</div></li>
231      * <li><b><tt>elementStyle</tt></b> : String<div class="sub-desc">The styles text for the input element's wrapper.</div></li>
232      * <li><b><tt>clearCls</tt></b> : String<div class="sub-desc">The CSS class to apply to the special clearing div
233      * rendered directly after each form field wrapper (defaults to <tt>'x-form-clear-left'</tt>)</div></li>
234      * </ul></div>
235      * <p>Also see <tt>{@link #getTemplateArgs}</tt></p>
236      */
237
238     // private
239     renderItem : function(c, position, target){
240         if(c && (c.isFormField || c.fieldLabel) && c.inputType != 'hidden'){
241             var args = this.getTemplateArgs(c);
242             if(Ext.isNumber(position)){
243                 position = target.dom.childNodes[position] || null;
244             }
245             if(position){
246                 c.itemCt = this.fieldTpl.insertBefore(position, args, true);
247             }else{
248                 c.itemCt = this.fieldTpl.append(target, args, true);
249             }
250             if(!c.rendered){
251                 c.render('x-form-el-' + c.id);
252             }else if(!this.isValidParent(c, target)){
253                 Ext.fly('x-form-el-' + c.id).appendChild(c.getPositionEl());
254             }
255             if(!c.getItemCt){
256                 // Non form fields don't have getItemCt, apply it here
257                 // This will get cleaned up in onRemove
258                 Ext.apply(c, {
259                     getItemCt: function(){
260                         return c.itemCt;
261                     },
262                     customItemCt: true
263                 });
264             }
265             c.label = c.getItemCt().child('label.x-form-item-label');
266             if(this.trackLabels && !this.isHide(c)){
267                 if(c.hidden){
268                     this.onFieldHide(c);
269                 }
270                 c.on({
271                     scope: this,
272                     show: this.onFieldShow,
273                     hide: this.onFieldHide
274                 });
275             }
276             this.configureItem(c);
277         }else {
278             Ext.layout.FormLayout.superclass.renderItem.apply(this, arguments);
279         }
280     },
281
282     /**
283      * <p>Provides template arguments for rendering the fully wrapped, labeled and styled form Field.</p>
284      * <p>This method returns an object hash containing properties used by the layout's {@link #fieldTpl}
285      * to create a correctly wrapped, labeled and styled form Field. This may be overriden to
286      * create custom layouts. The properties which must be returned are:</p><div class="mdetail-params"><ul>
287      * <li><b><tt>itemCls</tt></b> : String<div class="sub-desc">The CSS class applied to the outermost div wrapper
288      * that contains this field label and field element (the default class is <tt>'x-form-item'</tt> and <tt>itemCls</tt>
289      * will be added to that). If supplied, <tt>itemCls</tt> at the field level will override the default <tt>itemCls</tt>
290      * supplied at the container level.</div></li>
291      * <li><b><tt>id</tt></b> : String<div class="sub-desc">The id of the Field</div></li>
292      * <li><b><tt>{@link #labelStyle}</tt></b> : String<div class="sub-desc">
293      * A CSS style specification string to add to the field label for this field (defaults to <tt>''</tt> or the
294      * {@link #labelStyle layout's value for <tt>labelStyle</tt>}).</div></li>
295      * <li><b><tt>label</tt></b> : String<div class="sub-desc">The text to display as the label for this
296      * field (defaults to <tt>''</tt>)</div></li>
297      * <li><b><tt>{@link #labelSeparator}</tt></b> : String<div class="sub-desc">The separator to display after
298      * the text of the label for this field (defaults to a colon <tt>':'</tt> or the
299      * {@link #labelSeparator layout's value for labelSeparator}). To hide the separator use empty string ''.</div></li>
300      * <li><b><tt>elementStyle</tt></b> : String<div class="sub-desc">The styles text for the input element's wrapper.</div></li>
301      * <li><b><tt>clearCls</tt></b> : String<div class="sub-desc">The CSS class to apply to the special clearing div
302      * rendered directly after each form field wrapper (defaults to <tt>'x-form-clear-left'</tt>)</div></li>
303      * </ul></div>
304      * @param field The {@link Field Ext.form.Field} being rendered.
305      * @return An object hash containing the properties required to render the Field.
306      */
307     getTemplateArgs: function(field) {
308         var noLabelSep = !field.fieldLabel || field.hideLabel;
309         return {
310             id: field.id,
311             label: field.fieldLabel,
312             labelStyle: this.getLabelStyle(field.labelStyle),
313             elementStyle: this.elementStyle||'',
314             labelSeparator: noLabelSep ? '' : (Ext.isDefined(field.labelSeparator) ? field.labelSeparator : this.labelSeparator),
315             itemCls: (field.itemCls||this.container.itemCls||'') + (field.hideLabel ? ' x-hide-label' : ''),
316             clearCls: field.clearCls || 'x-form-clear-left'
317         };
318     },
319
320     // private
321     adjustWidthAnchor: function(value, c){
322         if(c.label && !this.isHide(c) && (this.container.labelAlign != 'top')){
323             var adjust = Ext.isIE6 || (Ext.isIE && !Ext.isStrict);
324             return value - this.labelAdjust + (adjust ? -3 : 0);
325         }
326         return value;
327     },
328     
329     adjustHeightAnchor : function(value, c){
330         if(c.label && !this.isHide(c) && (this.container.labelAlign == 'top')){
331             return value - c.label.getHeight();
332         }
333         return value;
334     },
335
336     // private
337     isValidParent : function(c, target){
338         return target && this.container.getEl().contains(c.getDomPositionEl());
339     }
340
341     /**
342      * @property activeItem
343      * @hide
344      */
345 });
346
347 Ext.Container.LAYOUTS['form'] = Ext.layout.FormLayout;