Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / form / Label.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
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.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @docauthor Jason Johnston <jason@sencha.com>
17  *
18  * Produces a standalone `<label />` element which can be inserted into a form and be associated with a field
19  * in that form using the {@link #forId} property.
20  * 
21  * **NOTE:** in most cases it will be more appropriate to use the {@link Ext.form.Labelable#fieldLabel fieldLabel}
22  * and associated config properties ({@link Ext.form.Labelable#labelAlign}, {@link Ext.form.Labelable#labelWidth},
23  * etc.) in field components themselves, as that allows labels to be uniformly sized throughout the form.
24  * Ext.form.Label should only be used when your layout can not be achieved with the standard
25  * {@link Ext.form.Labelable field layout}.
26  * 
27  * You will likely be associating the label with a field component that extends {@link Ext.form.field.Base}, so
28  * you should make sure the {@link #forId} is set to the same value as the {@link Ext.form.field.Base#inputId inputId}
29  * of that field.
30  * 
31  * The label's text can be set using either the {@link #text} or {@link #html} configuration properties; the
32  * difference between the two is that the former will automatically escape HTML characters when rendering, while
33  * the latter will not.
34  *
35  * # Example
36  * 
37  * This example creates a Label after its associated Text field, an arrangement that cannot currently
38  * be achieved using the standard Field layout's labelAlign.
39  * 
40  *     @example
41  *     Ext.create('Ext.form.Panel', {
42  *         title: 'Field with Label',
43  *         width: 400,
44  *         bodyPadding: 10,
45  *         renderTo: Ext.getBody(),
46  *         layout: {
47  *             type: 'hbox',
48  *             align: 'middle'
49  *         },
50  *         items: [{
51  *             xtype: 'textfield',
52  *             hideLabel: true,
53  *             flex: 1
54  *         }, {
55  *             xtype: 'label',
56  *             forId: 'myFieldId',
57  *             text: 'My Awesome Field',
58  *             margins: '0 0 0 10'
59  *         }]
60  *     });
61  */
62 Ext.define('Ext.form.Label', {
63     extend:'Ext.Component',
64     alias: 'widget.label',
65     requires: ['Ext.util.Format'],
66
67     /**
68      * @cfg {String} [text='']
69      * The plain text to display within the label. If you need to include HTML
70      * tags within the label's innerHTML, use the {@link #html} config instead.
71      */
72     /**
73      * @cfg {String} forId
74      * The id of the input element to which this label will be bound via the standard HTML 'for'
75      * attribute. If not specified, the attribute will not be added to the label. In most cases you will be
76      * associating the label with a {@link Ext.form.field.Base} component, so you should make sure this matches
77      * the {@link Ext.form.field.Base#inputId inputId} of that field.
78      */
79     /**
80      * @cfg {String} [html='']
81      * An HTML fragment that will be used as the label's innerHTML.
82      * Note that if {@link #text} is specified it will take precedence and this value will be ignored.
83      */
84     
85     maskOnDisable: false,
86     getElConfig: function(){
87         var me = this;
88         return {
89             tag: 'label', 
90             id: me.id, 
91             htmlFor: me.forId || '',
92             html: me.text ? Ext.util.Format.htmlEncode(me.text) : (me.html || '') 
93         };
94     },
95
96     /**
97      * Updates the label's innerHTML with the specified string.
98      * @param {String} text The new label text
99      * @param {Boolean} [encode=true] False to skip HTML-encoding the text when rendering it
100      * to the label. This might be useful if you want to include tags in the label's innerHTML rather
101      * than rendering them as string literals per the default logic.
102      * @return {Ext.form.Label} this
103      */
104     setText : function(text, encode){
105         var me = this;
106         
107         encode = encode !== false;
108         if(encode) {
109             me.text = text;
110             delete me.html;
111         } else {
112             me.html = text;
113             delete me.text;
114         }
115         
116         if(me.rendered){
117             me.el.dom.innerHTML = encode !== false ? Ext.util.Format.htmlEncode(text) : text;
118         }
119         return this;
120     }
121 });
122
123