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