]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * A FieldSet is a great way to visually separate elements of a form. It's normally used when you have a form with\r | |
3 | * fields that can be divided into groups - for example a customer's billing details in one fieldset and their shipping\r | |
4 | * address in another. A fieldset can be used inside a form or on its own elsewhere in your app. Fieldsets can\r | |
5 | * optionally have a title at the top and instructions at the bottom. Here's how we might create a FieldSet inside a\r | |
6 | * form:\r | |
7 | *\r | |
8 | * @example\r | |
9 | * Ext.create('Ext.form.Panel', {\r | |
10 | * fullscreen: true,\r | |
11 | * items: [\r | |
12 | * {\r | |
13 | * xtype: 'fieldset',\r | |
14 | * title: 'About You',\r | |
15 | * instructions: 'Tell us all about yourself',\r | |
16 | * items: [\r | |
17 | * {\r | |
18 | * xtype: 'textfield',\r | |
19 | * name : 'firstName',\r | |
20 | * label: 'First Name'\r | |
21 | * },\r | |
22 | * {\r | |
23 | * xtype: 'textfield',\r | |
24 | * name : 'lastName',\r | |
25 | * label: 'Last Name'\r | |
26 | * }\r | |
27 | * ]\r | |
28 | * }\r | |
29 | * ]\r | |
30 | * });\r | |
31 | *\r | |
32 | * Above we created a {@link Ext.form.Panel form} with a fieldset that contains two text fields. In this case, all\r | |
33 | * of the form fields are in the same fieldset, but for longer forms we may choose to use multiple fieldsets. We also\r | |
34 | * configured a {@link #title} and {@link #instructions} to give the user more information on filling out the form if\r | |
35 | * required.\r | |
36 | */\r | |
37 | Ext.define('Ext.form.FieldSet', {\r | |
38 | extend : 'Ext.Container',\r | |
39 | alias : 'widget.fieldset',\r | |
40 | requires: ['Ext.Title'],\r | |
41 | \r | |
42 | config: {\r | |
43 | /**\r | |
44 | * @cfg\r | |
45 | * @inheritdoc\r | |
46 | */\r | |
47 | baseCls: Ext.baseCSSPrefix + 'form-fieldset',\r | |
48 | \r | |
49 | /**\r | |
50 | * @cfg {String} title\r | |
51 | * Optional fieldset title, rendered just above the grouped fields.\r | |
52 | *\r | |
53 | * ## Example\r | |
54 | *\r | |
55 | * Ext.create('Ext.form.Fieldset', {\r | |
56 | * fullscreen: true,\r | |
57 | *\r | |
58 | * title: 'Login',\r | |
59 | *\r | |
60 | * items: [{\r | |
61 | * xtype: 'textfield',\r | |
62 | * label: 'Email'\r | |
63 | * }]\r | |
64 | * });\r | |
65 | * \r | |
66 | * @accessor\r | |
67 | */\r | |
68 | title: null,\r | |
69 | \r | |
70 | /**\r | |
71 | * @cfg {String} instructions\r | |
72 | * Optional fieldset instructions, rendered just below the grouped fields.\r | |
73 | *\r | |
74 | * ## Example\r | |
75 | *\r | |
76 | * Ext.create('Ext.form.Fieldset', {\r | |
77 | * fullscreen: true,\r | |
78 | *\r | |
79 | * instructions: 'Please enter your email address.',\r | |
80 | *\r | |
81 | * items: [{\r | |
82 | * xtype: 'textfield',\r | |
83 | * label: 'Email'\r | |
84 | * }]\r | |
85 | * });\r | |
86 | * \r | |
87 | * @accessor\r | |
88 | */\r | |
89 | instructions: null\r | |
90 | },\r | |
91 | \r | |
92 | /**\r | |
93 | * @private\r | |
94 | */\r | |
95 | applyTitle: function(title) {\r | |
96 | if (typeof title == 'string') {\r | |
97 | title = {title: title};\r | |
98 | }\r | |
99 | \r | |
100 | Ext.applyIf(title, {\r | |
101 | docked : 'top',\r | |
102 | baseCls: this.getBaseCls() + '-title'\r | |
103 | });\r | |
104 | \r | |
105 | return Ext.factory(title, Ext.Title, this._title);\r | |
106 | },\r | |
107 | \r | |
108 | /**\r | |
109 | * @private\r | |
110 | */\r | |
111 | updateTitle: function(newTitle, oldTitle) {\r | |
112 | if (newTitle) {\r | |
113 | this.add(newTitle);\r | |
114 | }\r | |
115 | if (oldTitle) {\r | |
116 | this.remove(oldTitle);\r | |
117 | }\r | |
118 | },\r | |
119 | \r | |
120 | /**\r | |
121 | * @private\r | |
122 | */\r | |
123 | getTitle: function() {\r | |
124 | var title = this._title;\r | |
125 | \r | |
126 | if (title && title instanceof Ext.Title) {\r | |
127 | return title.getTitle();\r | |
128 | }\r | |
129 | \r | |
130 | return title;\r | |
131 | },\r | |
132 | \r | |
133 | /**\r | |
134 | * @private\r | |
135 | */\r | |
136 | applyInstructions: function(instructions) {\r | |
137 | if (typeof instructions == 'string') {\r | |
138 | instructions = {title: instructions};\r | |
139 | }\r | |
140 | \r | |
141 | Ext.applyIf(instructions, {\r | |
142 | docked : 'bottom',\r | |
143 | baseCls: this.getBaseCls() + '-instructions'\r | |
144 | });\r | |
145 | \r | |
146 | return Ext.factory(instructions, Ext.Title, this._instructions);\r | |
147 | },\r | |
148 | \r | |
149 | /**\r | |
150 | * @private\r | |
151 | */\r | |
152 | updateInstructions: function(newInstructions, oldInstructions) {\r | |
153 | if (newInstructions) {\r | |
154 | this.add(newInstructions);\r | |
155 | }\r | |
156 | if (oldInstructions) {\r | |
157 | this.remove(oldInstructions);\r | |
158 | }\r | |
159 | },\r | |
160 | \r | |
161 | /**\r | |
162 | * @private\r | |
163 | */\r | |
164 | getInstructions: function() {\r | |
165 | var instructions = this._instructions;\r | |
166 | \r | |
167 | if (instructions && instructions instanceof Ext.Title) {\r | |
168 | return instructions.getTitle();\r | |
169 | }\r | |
170 | \r | |
171 | return instructions;\r | |
172 | },\r | |
173 | \r | |
174 | /**\r | |
175 | * A convenient method to disable all fields in this FieldSet\r | |
176 | * @return {Ext.form.FieldSet} This FieldSet\r | |
177 | */\r | |
178 | \r | |
179 | updateDisabled: function(newDisabled) {\r | |
180 | this.getFieldsAsArray().forEach(function(field) {\r | |
181 | field.setDisabled(newDisabled);\r | |
182 | });\r | |
183 | \r | |
184 | return this;\r | |
185 | },\r | |
186 | \r | |
187 | /**\r | |
188 | * @private\r | |
189 | */\r | |
190 | getFieldsAsArray: function() {\r | |
191 | var fields = [],\r | |
192 | getFieldsFrom = function(item) {\r | |
193 | if (item.isField) {\r | |
194 | fields.push(item);\r | |
195 | }\r | |
196 | \r | |
197 | if (item.isContainer) {\r | |
198 | item.getItems().each(getFieldsFrom);\r | |
199 | }\r | |
200 | };\r | |
201 | \r | |
202 | this.getItems().each(getFieldsFrom);\r | |
203 | \r | |
204 | return fields;\r | |
205 | }\r | |
206 | });\r |