-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathwidgets.js
More file actions
287 lines (263 loc) · 8.72 KB
/
Copy pathwidgets.js
File metadata and controls
287 lines (263 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
'use strict';
var is = require('is');
var tag = require('./tag');
var dataRegExp = /^data-[a-z]+([-][a-z]+)*$/;
var ariaRegExp = /^aria-[a-z]+$/;
var legalAttrs = ['autocomplete', 'autocorrect', 'autofocus', 'autosuggest', 'checked', 'dirname', 'disabled', 'tabindex', 'list', 'max', 'maxlength', 'min', 'multiple', 'novalidate', 'pattern', 'placeholder', 'readonly', 'required', 'size', 'step'];
var ignoreAttrs = ['id', 'name', 'class', 'classes', 'type', 'value'];
var getUserAttrs = function (opt) {
return Object.keys(opt).reduce(function (attrs, k) {
if ((ignoreAttrs.indexOf(k) === -1 && legalAttrs.indexOf(k) > -1) || dataRegExp.test(k) || ariaRegExp.test(k)) {
attrs[k] = opt[k];
}
return attrs;
}, {});
};
// used to generate different input elements varying only by type attribute
var input = function (type) {
return function (opts) {
var opt = opts || {};
var userAttrs = getUserAttrs(opt);
var w = {
classes: opt.classes,
type: type,
formatValue: function (value) {
return (value || value === 0) ? value : null;
}
};
w.toHTML = function (name, field) {
var f = field || {};
var attrs = {
type: type,
name: name,
id: f.id === false ? false : (f.id || true),
classes: w.classes,
value: w.formatValue(f.value)
};
return tag('input', [attrs, userAttrs, w.attrs || {}]);
};
return w;
};
};
exports.text = input('text');
exports.email = input('email');
exports.number = input('number');
exports.hidden = input('hidden');
exports.color = input('color');
exports.tel = input('tel');
var passwordWidget = input('password');
var passwordFormatValue = function () { return null; };
exports.password = function (opt) {
var w = passwordWidget(opt);
w.formatValue = passwordFormatValue;
return w;
};
var dateWidget = input('date');
exports.date = function (opt) {
var w = dateWidget(opt);
w.formatValue = function (value) {
if (!value) {
return null;
}
var date = is.date(value) ? value : new Date(value);
if (isNaN(date.getTime())) {
return null;
}
return date.toISOString().slice(0, 10);
};
return w;
};
exports.checkbox = function (options) {
var opt = options || {};
var w = {
classes: opt.classes,
type: 'checkbox'
};
var userAttrs = getUserAttrs(opt);
w.toHTML = function (name, field) {
var f = field || {};
var attrs = {
type: 'checkbox',
name: name,
id: f.id === false ? false : (f.id || true),
classes: w.classes,
checked: !!f.value,
value: 'on'
};
return tag('input', [attrs, userAttrs, w.attrs || {}]);
};
return w;
};
exports.select = function (options) {
var opt = options || {};
var w = {
classes: opt.classes,
type: 'select'
};
var choicesHTML;
var groupHTML = function (group, label, value) {
return tag('optgroup', {
label: label
}, choicesHTML(group, value));
};
choicesHTML = function (choices, value) {
return Object.keys(choices).reduce(function (html, k) {
if (is.object(choices[k])) {
return html + groupHTML(choices[k], String(k), value);
} else {
return html + tag('option', {
value: k,
selected: !!(value && String(value) === String(k))
}, choices[k]);
}
}, '');
};
var userAttrs = getUserAttrs(opt);
w.toHTML = function (name, field) {
var f = field || {};
var optionsHTML = choicesHTML(f.choices, f.value, '');
var attrs = {
name: name,
id: f.id === false ? false : (f.id || true),
classes: w.classes
};
return tag('select', [attrs, userAttrs, w.attrs || {}], optionsHTML);
};
return w;
};
exports.textarea = function (options) {
var opt = options || {};
var w = {
classes: opt.classes,
type: 'textarea'
};
var userAttrs = getUserAttrs(opt);
w.toHTML = function (name, field) {
var f = field || {};
var attrs = {
name: name,
id: f.id === false ? false : (f.id || true),
classes: w.classes,
rows: opt.rows || null,
cols: opt.cols || null
};
return tag('textarea', [attrs, userAttrs, w.attrs || {}], f.value || '');
};
return w;
};
exports.multipleCheckbox = function (options) {
var opt = options || {};
var w = {
classes: opt.classes,
labelClasses: opt.labelClasses,
type: 'multipleCheckbox'
};
var userAttrs = getUserAttrs(opt);
w.toHTML = function (name, field) {
var f = field || {};
return Object.keys(f.choices).reduce(function (html, k) {
// input element
var id = f.id === false ? false : (f.id ? f.id + '_' + k : 'id_' + name + '_' + k);
var checked = f.value && (Array.isArray(f.value) ? f.value.some(function (v) { return String(v) === String(k); }) : String(f.value) === String(k));
var attrs = {
type: 'checkbox',
name: name,
id: id,
classes: w.classes,
value: k,
checked: !!checked
};
var inputHTML = tag('input', [attrs, userAttrs, w.attrs || {}]);
// label element
var labelHTML = tag('label', { 'for': id, classes: w.labelClasses }, f.choices[k]);
return html + inputHTML + labelHTML;
}, '');
};
return w;
};
exports.label = function (options) {
var opt = options || {};
var w = {
classes: opt.classes || []
};
var userAttrs = getUserAttrs(opt);
w.toHTML = function (forID) {
var attrs = {
'for': forID,
classes: w.classes
};
return tag('label', [attrs, userAttrs, w.attrs || {}], opt.content);
};
return w;
};
exports.multipleRadio = function (options) {
var opt = options || {};
var w = {
classes: opt.classes,
labelClasses: opt.labelClasses,
type: 'multipleRadio'
};
var userAttrs = getUserAttrs(opt);
w.toHTML = function (name, field) {
var f = field || {};
return Object.keys(f.choices).reduce(function (html, k) {
// input element
var id = f.id === false ? false : (f.id ? f.id + '_' + k : 'id_' + name + '_' + k);
var checked = f.value && (Array.isArray(f.value) ? f.value.some(function (v) { return String(v) === String(k); }) : String(f.value) === String(k));
var attrs = {
type: 'radio',
name: name,
id: id,
classes: w.classes,
value: k,
checked: !!checked
};
var inputHTML = tag('input', [attrs, userAttrs, w.attrs || {}]);
// label element
var labelHTML = tag('label', { 'for': id, classes: w.labelClasses }, f.choices[k]);
return html + inputHTML + labelHTML;
}, '');
};
return w;
};
exports.multipleSelect = function (options) {
var opt = options || {};
var w = {
classes: opt.classes,
type: 'multipleSelect'
};
var choicesHTML;
var groupHTML = function (group, label, value) {
return tag('optgroup', {
label: label
}, choicesHTML(group, value));
};
choicesHTML = function (choices, value) {
return Object.keys(choices).reduce(function (html, k) {
if (is.object(choices[k])) {
return html + groupHTML(choices[k], String(k), value);
} else {
var selected = value && (Array.isArray(value) ? value.some(function (v) {
return String(v) === String(k);
}) : String(value) === String(k));
return html + tag('option', {
value: k,
selected: !!selected
}, choices[k]);
}
}, '');
};
var userAttrs = getUserAttrs(opt);
w.toHTML = function (name, field) {
var f = field || {};
var optionsHTML = choicesHTML(f.choices, f.value);
var attrs = {
multiple: true,
name: name,
id: f.id === false ? false : (f.id || true),
classes: w.classes
};
return tag('select', [attrs, userAttrs, w.attrs || {}], optionsHTML);
};
return w;
};