|
@@ -1,8 +1,3 @@
|
1
|
|
-// Consts for readability
|
2
|
|
-const REQUIRED = true;
|
3
|
|
-const NOT_REQUIRED = false;
|
4
|
|
-
|
5
|
|
-
|
6
|
1
|
var prefix = 'form'
|
7
|
2
|
|
8
|
3
|
var items = {
|
|
@@ -111,29 +106,33 @@ function generateForm(id) {
|
111
|
106
|
name: getField({
|
112
|
107
|
name: items.name,
|
113
|
108
|
label: lang.form_name_label,
|
114
|
|
- type: 'text'
|
115
|
|
- }, REQUIRED),
|
|
109
|
+ type: 'text',
|
|
110
|
+ required: true
|
|
111
|
+ }),
|
116
|
112
|
addr: getField({
|
117
|
113
|
name: items.addr,
|
118
|
114
|
label: lang.form_addr_label,
|
119
|
|
- type: 'email'
|
120
|
|
- }, REQUIRED),
|
|
115
|
+ type: 'email',
|
|
116
|
+ required: true
|
|
117
|
+ }),
|
121
|
118
|
subj: getField({
|
122
|
119
|
name: items.subj,
|
123
|
120
|
label: lang.form_subj_label,
|
124
|
|
- type: 'text'
|
125
|
|
- }, REQUIRED),
|
|
121
|
+ type: 'text',
|
|
122
|
+ required: true
|
|
123
|
+ }),
|
126
|
124
|
text: getField({
|
127
|
125
|
name: items.text,
|
128
|
126
|
label: lang.form_mesg_label,
|
129
|
|
- type: 'textarea'
|
130
|
|
- }, REQUIRED)
|
|
127
|
+ type: 'textarea',
|
|
128
|
+ required: true
|
|
129
|
+ })
|
131
|
130
|
};
|
132
|
131
|
|
133
|
132
|
// Adding custom fields
|
134
|
133
|
for(let fieldName in customFields) {
|
135
|
134
|
let field = customFields[fieldName];
|
136
|
|
- DOMFields[fieldName] = getField(field, NOT_REQUIRED);
|
|
135
|
+ DOMFields[fieldName] = getField(field);
|
137
|
136
|
}
|
138
|
137
|
|
139
|
138
|
// Adding all nodes to document
|
|
@@ -153,7 +152,7 @@ function generateForm(id) {
|
153
|
152
|
// fieldInfos: object describing the field
|
154
|
153
|
// required: boolean on whether the field is required or optional
|
155
|
154
|
// return: a block containing the field and a label describing it (if enabled)
|
156
|
|
-function getField(fieldInfos, required) {
|
|
155
|
+function getField(fieldInfos) {
|
157
|
156
|
var block = document.createElement('div');
|
158
|
157
|
block.setAttribute('id', fieldInfos.name);
|
159
|
158
|
|
|
@@ -162,13 +161,11 @@ function getField(fieldInfos, required) {
|
162
|
161
|
|
163
|
162
|
// Easily add new supported input types
|
164
|
163
|
switch(fieldInfos.type) {
|
165
|
|
- case 'text': field = getTextField(fieldInfos, required);
|
|
164
|
+ case 'textarea': field = getTextarea(fieldInfos);
|
166
|
165
|
break;
|
167
|
|
- case 'textarea': field = getTextarea(fieldInfos, required);
|
|
166
|
+ case 'select': field = getSelectField(fieldInfos);
|
168
|
167
|
break;
|
169
|
|
- case 'email': field = getEmailField(fieldInfos, required);
|
170
|
|
- break;
|
171
|
|
- case 'select': field = getSelectField(fieldInfos, required);
|
|
168
|
+ default: field = getInputField(fieldInfos);
|
172
|
169
|
break;
|
173
|
170
|
}
|
174
|
171
|
|
|
@@ -202,17 +199,25 @@ function getLabel(content, id) {
|
202
|
199
|
// fieldInfos: object describing the field
|
203
|
200
|
// required: boolean on whether the field is required or optional
|
204
|
201
|
// return: a <select> element corresponding to the info passed as input
|
205
|
|
-function getSelectField(fieldInfos, required) {
|
|
202
|
+function getSelectField(fieldInfos) {
|
206
|
203
|
let field = document.createElement('select');
|
207
|
204
|
|
208
|
205
|
// Set attributes when necessary
|
209
|
|
- if(required) {
|
|
206
|
+ if(fieldInfos.required) {
|
210
|
207
|
field.setAttribute('required', 'required');
|
211
|
208
|
}
|
212
|
209
|
field.setAttribute('id', prefix + '_' + fieldInfos.name + '_select');
|
213
|
210
|
|
214
|
211
|
let index = 0;
|
215
|
212
|
|
|
213
|
+ // Add header option, useful if the field is required
|
|
214
|
+ let header = document.createElement('option');
|
|
215
|
+ // The value must be an empty string so the browser can block the submit
|
|
216
|
+ // event if the field is required
|
|
217
|
+ header.setAttribute('value', '');
|
|
218
|
+ header.innerHTML = lang.form_select_header_option;
|
|
219
|
+ field.appendChild(header);
|
|
220
|
+
|
216
|
221
|
// Add all options to select
|
217
|
222
|
for(let choice of fieldInfos.options) {
|
218
|
223
|
let option = document.createElement('option');
|
|
@@ -229,32 +234,14 @@ function getSelectField(fieldInfos, required) {
|
229
|
234
|
}
|
230
|
235
|
|
231
|
236
|
|
232
|
|
-// Returns a <input> HTML element with 'text' type
|
|
237
|
+// Returns a <input> HTML element with desired type
|
233
|
238
|
// fieldInfos: object describing the field
|
234
|
239
|
// required: boolean on whether the field is required or optional
|
|
240
|
+// type: type of the input field (text, email, date...)
|
235
|
241
|
// return: a <input> HTML element corresponding to the info passed as input
|
236
|
|
-function getTextField(fieldInfos, required) {
|
237
|
|
- return getBaseInputField(fieldInfos, required, 'text');
|
238
|
|
-}
|
239
|
|
-
|
240
|
|
-
|
241
|
|
-// Returns a <input> HTML element with 'email' type
|
242
|
|
-// fieldInfos: object describing the field
|
243
|
|
-// required: boolean on whether the field is required or optional
|
244
|
|
-// return: a <input> HTML element corresponding to the info passed as input
|
245
|
|
-function getEmailField(fieldInfos, required) {
|
246
|
|
- return getBaseInputField(fieldInfos, required, 'email');
|
247
|
|
-}
|
248
|
|
-
|
249
|
|
-
|
250
|
|
-// Returns a basic <input> HTML element with generic info to be processed by
|
251
|
|
-// functions at higher level
|
252
|
|
-// fieldInfos: object describing the field
|
253
|
|
-// required: boolean on whether the field is required or optional
|
254
|
|
-// return: a basic <input> HTML element with generic info
|
255
|
|
-function getBaseInputField(fieldInfos, required, type) {
|
|
242
|
+function getInputField(fieldInfos, required) {
|
256
|
243
|
let field = getBaseField(fieldInfos, required, 'input')
|
257
|
|
- field.setAttribute('type', type);
|
|
244
|
+ field.setAttribute('type', fieldInfos.type);
|
258
|
245
|
return field;
|
259
|
246
|
}
|
260
|
247
|
|
|
@@ -277,7 +264,7 @@ function getTextarea(fieldInfos, required) {
|
277
|
264
|
function getBaseField(fieldInfos, required, tag) {
|
278
|
265
|
let field = document.createElement(tag);
|
279
|
266
|
|
280
|
|
- if(required) {
|
|
267
|
+ if(fieldInfos.required) {
|
281
|
268
|
field.setAttribute('required', 'required');
|
282
|
269
|
}
|
283
|
270
|
field.setAttribute('placeholder', fieldInfos.label);
|