Upgrade to ExtJS 4.0.2 - Released 06/09/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  * @class Ext.form.Label
17  * @extends Ext.Component
18
19 Produces a standalone `<label />` element which can be inserted into a form and be associated with a field
20 in that form using the {@link #forId} property.
21
22 **NOTE:** in most cases it will be more appropriate to use the {@link Ext.form.Labelable#fieldLabel fieldLabel}
23 and associated config properties ({@link Ext.form.Labelable#labelAlign}, {@link Ext.form.Labelable#labelWidth},
24 etc.) in field components themselves, as that allows labels to be uniformly sized throughout the form.
25 Ext.form.Label should only be used when your layout can not be achieved with the standard
26 {@link Ext.form.Labelable field layout}.
27
28 You will likely be associating the label with a field component that extends {@link Ext.form.field.Base}, so
29 you should make sure the {@link #forId} is set to the same value as the {@link Ext.form.field.Base#inputId inputId}
30 of that field.
31
32 The label's text can be set using either the {@link #text} or {@link #html} configuration properties; the
33 difference between the two is that the former will automatically escape HTML characters when rendering, while
34 the latter will not.
35 {@img Ext.form.Label/Ext.form.Label.png Ext.form.Label component}
36 #Example usage:#
37
38 This example creates a Label after its associated Text field, an arrangement that cannot currently
39 be achieved using the standard Field layout's labelAlign.
40
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  * @markdown
63  * @docauthor Jason Johnston <jason@sencha.com>
64  */
65 Ext.define('Ext.form.Label', {
66     extend:'Ext.Component',
67     alias: 'widget.label',
68     requires: ['Ext.util.Format'],
69
70     /**
71      * @cfg {String} text The plain text to display within the label (defaults to ''). If you need to include HTML
72      * tags within the label's innerHTML, use the {@link #html} config instead.
73      */
74     /**
75      * @cfg {String} forId The id of the input element to which this label will be bound via the standard HTML 'for'
76      * attribute. If not specified, the attribute will not be added to the label. In most cases you will be
77      * associating the label with a {@link Ext.form.field.Base} component, so you should make sure this matches
78      * the {@link Ext.form.field.Base#inputId inputId} of that field.
79      */
80     /**
81      * @cfg {String} html An HTML fragment that will be used as the label's innerHTML (defaults to '').
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 (optional) False to skip HTML-encoding the text when rendering it
100      * to the label (defaults to true which encodes the value). This might be useful if you want to include
101      * tags in the label's innerHTML rather than rendering them as string literals per the default logic.
102      * @return {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