@plotdb/form 0.0.1 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/index.js +371 -46
- package/index.min.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,37 @@
|
|
|
1
1
|
# Change Logs
|
|
2
2
|
|
|
3
|
+
## v0.0.5
|
|
4
|
+
|
|
5
|
+
- bug fix: `check` is not finished immediately even if `now` is true
|
|
6
|
+
- bug fix: set value may fail due to empty value provided
|
|
7
|
+
- bug fix: `check` from `status` / `change` events of form.widget causes redundant validation.
|
|
8
|
+
- bug fix: `change` event of form.widget isn't forwarded to form.manager correctly.
|
|
9
|
+
- return invalid widgets from `check` call in ( if applicable, array of ) `{widget, path, status}` object.
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## v0.0.4
|
|
13
|
+
|
|
14
|
+
- add `root()` function to get root element of an widget
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## v0.0.3
|
|
18
|
+
|
|
19
|
+
- changes about `mode`:
|
|
20
|
+
- add `mode` in `form.manager`
|
|
21
|
+
- mode change triggers event `mode` with new mode value
|
|
22
|
+
- redefine mode values to `edit`, `view` and `config`
|
|
23
|
+
- update documentation
|
|
24
|
+
|
|
25
|
+
## v0.0.2
|
|
26
|
+
|
|
27
|
+
- add `length` opset
|
|
28
|
+
- add `is` in `number` opset
|
|
29
|
+
- add `convert` in `number` opset
|
|
30
|
+
- support ldform favor `status` and `off` ( removeListener )
|
|
31
|
+
- support form.manager based on `ldform` logic
|
|
32
|
+
- update documentation
|
|
33
|
+
|
|
34
|
+
|
|
3
35
|
## v0.0.1
|
|
4
36
|
|
|
5
37
|
- initial release
|
package/index.js
CHANGED
|
@@ -2,48 +2,291 @@
|
|
|
2
2
|
(function(){
|
|
3
3
|
var form;
|
|
4
4
|
form = {};
|
|
5
|
-
form.manager = function(){
|
|
6
|
-
this
|
|
5
|
+
form.manager = function(o){
|
|
6
|
+
var this$ = this;
|
|
7
|
+
o == null && (o = {});
|
|
8
|
+
this._ws = {
|
|
9
|
+
w: {},
|
|
10
|
+
p: new WeakMap(),
|
|
11
|
+
s: {},
|
|
12
|
+
l: {}
|
|
13
|
+
};
|
|
14
|
+
this._evthdr = {};
|
|
15
|
+
this._status = 1;
|
|
16
|
+
this._mode = 'edit';
|
|
17
|
+
this.mod = {
|
|
18
|
+
afterCheck: o.mod.afterCheck
|
|
19
|
+
} || {};
|
|
20
|
+
this.afterCheck = debounce(330, function(){
|
|
21
|
+
var args, res$, i$, to$;
|
|
22
|
+
res$ = [];
|
|
23
|
+
for (i$ = 0, to$ = arguments.length; i$ < to$; ++i$) {
|
|
24
|
+
res$.push(arguments[i$]);
|
|
25
|
+
}
|
|
26
|
+
args = res$;
|
|
27
|
+
return this$._afterCheck.apply(this$, args);
|
|
28
|
+
});
|
|
29
|
+
this._checkDebounced = debounce(10, function(){
|
|
30
|
+
var args, res$, i$, to$;
|
|
31
|
+
res$ = [];
|
|
32
|
+
for (i$ = 0, to$ = arguments.length; i$ < to$; ++i$) {
|
|
33
|
+
res$.push(arguments[i$]);
|
|
34
|
+
}
|
|
35
|
+
args = res$;
|
|
36
|
+
return this$._check.apply(this$, args);
|
|
37
|
+
});
|
|
7
38
|
return this;
|
|
8
39
|
};
|
|
9
40
|
form.manager.prototype = import$(Object.create(Object.prototype), {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
: [it]);
|
|
41
|
+
on: function(n, cb){
|
|
42
|
+
var ref$;
|
|
43
|
+
return ((ref$ = this._evthdr)[n] || (ref$[n] = [])).push(cb);
|
|
14
44
|
},
|
|
15
|
-
|
|
16
|
-
|
|
45
|
+
fire: function(n){
|
|
46
|
+
var v, res$, i$, to$, ref$, len$, cb, results$ = [];
|
|
47
|
+
res$ = [];
|
|
48
|
+
for (i$ = 1, to$ = arguments.length; i$ < to$; ++i$) {
|
|
49
|
+
res$.push(arguments[i$]);
|
|
50
|
+
}
|
|
51
|
+
v = res$;
|
|
52
|
+
for (i$ = 0, len$ = (ref$ = this._evthdr[n] || []).length; i$ < len$; ++i$) {
|
|
53
|
+
cb = ref$[i$];
|
|
54
|
+
results$.push(cb.apply(this, v));
|
|
55
|
+
}
|
|
56
|
+
return results$;
|
|
17
57
|
},
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
58
|
+
add: function(o){
|
|
59
|
+
var ref$, w, p, this$ = this;
|
|
60
|
+
if (Array.isArray(o)) {
|
|
61
|
+
return o.map(function(it){
|
|
62
|
+
return this$.add(it);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
ref$ = [o.widget, o.path], w = ref$[0], p = ref$[1];
|
|
66
|
+
if (this._ws.p.get(w)) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
this._ws.w[p] = w;
|
|
70
|
+
this._ws.p.set(w, p);
|
|
71
|
+
this._ws.l[p] = {};
|
|
72
|
+
w.on('change', this._ws.l[p].c = function(v){
|
|
73
|
+
return this$.fire('change', {
|
|
74
|
+
widget: w,
|
|
75
|
+
path: p,
|
|
76
|
+
value: v
|
|
77
|
+
});
|
|
21
78
|
});
|
|
79
|
+
return w.on('status', this._ws.l[p].s = function(s){
|
|
80
|
+
return this$.fire('status', {
|
|
81
|
+
widget: w,
|
|
82
|
+
path: p,
|
|
83
|
+
status: s
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
remove: function(o){
|
|
88
|
+
var ws, ref$, key$, ref1$, this$ = this;
|
|
89
|
+
if (Array.isArray(o)) {
|
|
90
|
+
return o.map(function(it){
|
|
91
|
+
return this$.remove(it);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (!(ws = this._ws.w[o.path])) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
o.off('change', this._ws.l[o.path].c);
|
|
98
|
+
o.off('status', this._ws.l[o.path].s);
|
|
99
|
+
this._ws.p['delete'](ws);
|
|
100
|
+
delete this._ws.w[o.path];
|
|
101
|
+
return ref1$ = (ref$ = this._ws.l)[key$ = o.path], delete ref$[key$], ref1$;
|
|
102
|
+
},
|
|
103
|
+
widget: function(p){
|
|
104
|
+
return this._ws[p];
|
|
105
|
+
},
|
|
106
|
+
status: function(v){
|
|
107
|
+
return !(v != null)
|
|
108
|
+
? this._status
|
|
109
|
+
: this._status = v;
|
|
110
|
+
},
|
|
111
|
+
progress: function(){
|
|
112
|
+
var ret, k, s;
|
|
113
|
+
ret = {
|
|
114
|
+
total: (function(){
|
|
115
|
+
var results$ = [];
|
|
116
|
+
for (k in this._ws.w) {
|
|
117
|
+
results$.push(k);
|
|
118
|
+
}
|
|
119
|
+
return results$;
|
|
120
|
+
}.call(this)).length,
|
|
121
|
+
done: (function(){
|
|
122
|
+
var ref$, results$ = [];
|
|
123
|
+
for (k in ref$ = this._ws.s) {
|
|
124
|
+
s = ref$[k];
|
|
125
|
+
results$.push(s);
|
|
126
|
+
}
|
|
127
|
+
return results$;
|
|
128
|
+
}.call(this)).filter(function(s){
|
|
129
|
+
return s != null && s === 0;
|
|
130
|
+
}).length
|
|
131
|
+
};
|
|
132
|
+
ret.percent = ret.done / ret.total;
|
|
133
|
+
return ret;
|
|
134
|
+
},
|
|
135
|
+
_afterCheck: function(){
|
|
136
|
+
var os, ret, k, v;
|
|
137
|
+
os = this._status;
|
|
138
|
+
delete this._status;
|
|
139
|
+
this.mod.afterCheck.apply(this);
|
|
140
|
+
if (!(this._status != null)) {
|
|
141
|
+
ret = (function(){
|
|
142
|
+
var ref$, results$ = [];
|
|
143
|
+
for (k in ref$ = this._ws.w) {
|
|
144
|
+
v = ref$[k];
|
|
145
|
+
results$.push(this._ws.s[k]);
|
|
146
|
+
}
|
|
147
|
+
return results$;
|
|
148
|
+
}.call(this)).filter(function(s){
|
|
149
|
+
return !(s != null && s === 0);
|
|
150
|
+
}).length;
|
|
151
|
+
this._status = ret ? 1 : 0;
|
|
152
|
+
}
|
|
153
|
+
if (os !== this._status) {
|
|
154
|
+
return this.fire('readystatechange', this._status === 0);
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
check: function(o, now){
|
|
158
|
+
var p, w;
|
|
159
|
+
now == null && (now = false);
|
|
160
|
+
if (!o) {
|
|
161
|
+
return this.check((function(){
|
|
162
|
+
var ref$, results$ = [];
|
|
163
|
+
for (p in ref$ = this._ws.w) {
|
|
164
|
+
w = ref$[p];
|
|
165
|
+
results$.push({
|
|
166
|
+
widget: w,
|
|
167
|
+
path: p
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return results$;
|
|
171
|
+
}.call(this)), now);
|
|
172
|
+
}
|
|
173
|
+
this.checkList = (this.checkList || (this.checkList = [])).concat(Array.isArray(o)
|
|
174
|
+
? o
|
|
175
|
+
: [o]);
|
|
176
|
+
return now
|
|
177
|
+
? this._check(null, true)
|
|
178
|
+
: this._checkDebounced();
|
|
22
179
|
},
|
|
23
|
-
|
|
24
|
-
var
|
|
25
|
-
if (!
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
180
|
+
_check: function(o, now){
|
|
181
|
+
var list, this$ = this;
|
|
182
|
+
if (!o) {
|
|
183
|
+
list = this.checkList || [];
|
|
184
|
+
this.checkList = [];
|
|
185
|
+
return this._check(list, now);
|
|
186
|
+
}
|
|
187
|
+
if (Array.isArray(o)) {
|
|
188
|
+
return Promise.all(o.map(function(it){
|
|
189
|
+
return this$._check(it, now);
|
|
190
|
+
})).then(function(it){
|
|
191
|
+
return it.filter(function(it){
|
|
192
|
+
return it;
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
return new Promise(function(res, rej){
|
|
197
|
+
var ref$, w, p, _now;
|
|
198
|
+
ref$ = [o.widget, o.path, o.now], w = ref$[0], p = ref$[1], _now = ref$[2];
|
|
199
|
+
if (!(w && p)) {
|
|
200
|
+
return res();
|
|
201
|
+
}
|
|
202
|
+
if (!w && !(w = this$._ws.w[p])) {
|
|
203
|
+
return res();
|
|
204
|
+
}
|
|
205
|
+
if (!p) {
|
|
206
|
+
p = this$._ws.p[w];
|
|
207
|
+
}
|
|
208
|
+
return w.validate().then(function(){
|
|
209
|
+
var os, promise;
|
|
210
|
+
os = this$._ws.s[p];
|
|
211
|
+
this$._ws.s[p] = w.status();
|
|
212
|
+
promise = now || _now
|
|
213
|
+
? Promise.resolve(this$.afterCheck().now())
|
|
214
|
+
: this$.afterCheck();
|
|
215
|
+
return promise.then(function(){
|
|
216
|
+
if (os !== this$._ws.s[p]) {
|
|
217
|
+
this$.fire('status', {
|
|
218
|
+
path: p,
|
|
219
|
+
widget: w,
|
|
220
|
+
status: this$._ws.s[p]
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return this$._ws.s[p];
|
|
224
|
+
});
|
|
225
|
+
}).then(function(){
|
|
226
|
+
if (this$._ws.s[p] === 2) {
|
|
227
|
+
return res({
|
|
228
|
+
widget: w,
|
|
229
|
+
path: p,
|
|
230
|
+
status: this$._ws.s[p]
|
|
231
|
+
});
|
|
232
|
+
} else {
|
|
233
|
+
return res();
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
},
|
|
238
|
+
formData: function(){
|
|
239
|
+
var fd, p, ref$, w, val, i$, to$, i;
|
|
240
|
+
fd = new FormData();
|
|
241
|
+
for (p in ref$ = this._ws.w) {
|
|
242
|
+
w = ref$[p];
|
|
243
|
+
val = w.value();
|
|
244
|
+
if (Array.isArray(v)) {
|
|
245
|
+
for (i$ = 0, to$ = v.length; i$ < to$; ++i$) {
|
|
246
|
+
i = i$;
|
|
247
|
+
fd.append(p + "[]", v[i]);
|
|
248
|
+
}
|
|
249
|
+
} else {
|
|
250
|
+
fd.append(p, val);
|
|
35
251
|
}
|
|
36
|
-
results$.push(f.value(v));
|
|
37
252
|
}
|
|
38
|
-
return
|
|
39
|
-
|
|
40
|
-
|
|
253
|
+
return fd;
|
|
254
|
+
},
|
|
255
|
+
value: function(v, opt){
|
|
256
|
+
var ret, p, ref$, w, results$ = [];
|
|
257
|
+
opt == null && (opt = {});
|
|
258
|
+
if (!v) {
|
|
259
|
+
ret = {};
|
|
260
|
+
for (p in ref$ = this._ws.w) {
|
|
261
|
+
w = ref$[p];
|
|
262
|
+
ret[p] = w.value();
|
|
263
|
+
}
|
|
264
|
+
return ret;
|
|
41
265
|
}
|
|
266
|
+
for (p in ref$ = this._ws.w) {
|
|
267
|
+
w = ref$[p];
|
|
268
|
+
results$.push(w.value(v[p], opt));
|
|
269
|
+
}
|
|
270
|
+
return results$;
|
|
42
271
|
},
|
|
43
272
|
mode: function(m){
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
273
|
+
var ref$, p, w, results$ = [];
|
|
274
|
+
if (!(m != null)) {
|
|
275
|
+
return this._mode;
|
|
276
|
+
}
|
|
277
|
+
if (!(m === 'edit' || m === 'view' || m === 'config')) {
|
|
278
|
+
throw ref$ = new Error(), ref$.name = 'lderror', ref$.id = 1015, ref$;
|
|
279
|
+
}
|
|
280
|
+
if (this._mode === m) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
this._mode = m;
|
|
284
|
+
this.fire('mode', m);
|
|
285
|
+
for (p in ref$ = this._ws.w) {
|
|
286
|
+
w = ref$[p];
|
|
287
|
+
results$.push(w.mode(m));
|
|
288
|
+
}
|
|
289
|
+
return results$;
|
|
47
290
|
}
|
|
48
291
|
});
|
|
49
292
|
form.op = function(opt){
|
|
@@ -52,13 +295,16 @@
|
|
|
52
295
|
this.name = opt.name;
|
|
53
296
|
this.config = opt.config;
|
|
54
297
|
this.func = opt.func;
|
|
55
|
-
this.
|
|
298
|
+
this.opset = opt.opset;
|
|
56
299
|
return this;
|
|
57
300
|
};
|
|
58
301
|
form.op.prototype = import$(Object.create(Object.prototype), {
|
|
59
302
|
validate: function(val, cfg){
|
|
60
303
|
var ret;
|
|
61
304
|
cfg == null && (cfg = {});
|
|
305
|
+
if (this.opset.convert) {
|
|
306
|
+
val = this.opset.convert(val);
|
|
307
|
+
}
|
|
62
308
|
if ((ret = this.func(val, cfg)) instanceof Promise) {
|
|
63
309
|
return ret;
|
|
64
310
|
} else {
|
|
@@ -81,6 +327,7 @@
|
|
|
81
327
|
this.name = opt.name;
|
|
82
328
|
this.id = opt.id;
|
|
83
329
|
this.i18n = opt.i18n;
|
|
330
|
+
this.convert = opt.convert;
|
|
84
331
|
this.ops = {};
|
|
85
332
|
ops = Array.isArray(opt.ops)
|
|
86
333
|
? opt.ops.map(function(it){
|
|
@@ -108,7 +355,7 @@
|
|
|
108
355
|
: k
|
|
109
356
|
? new form.op((ref$ = typeof v === 'function' ? {
|
|
110
357
|
func: v
|
|
111
|
-
} : v, ref$.id = k, ref$))
|
|
358
|
+
} : v, ref$.id = k, ref$.opset = this$, ref$))
|
|
112
359
|
: (function(){
|
|
113
360
|
throw new Error('[@plotdb/form/opset] invalid op when initializing opset.');
|
|
114
361
|
}());
|
|
@@ -116,7 +363,7 @@
|
|
|
116
363
|
v = ref$[k];
|
|
117
364
|
results$.push(this$.ops[k] = new form.op((ref1$ = typeof v === 'function' ? {
|
|
118
365
|
func: v
|
|
119
|
-
} : v, ref1$.name = k, ref1$.id = k, ref1$)));
|
|
366
|
+
} : v, ref1$.name = k, ref1$.id = k, ref1$.opset = this$, ref1$)));
|
|
120
367
|
}
|
|
121
368
|
return results$;
|
|
122
369
|
});
|
|
@@ -196,6 +443,30 @@
|
|
|
196
443
|
config: {}
|
|
197
444
|
}
|
|
198
445
|
}
|
|
446
|
+
}, {
|
|
447
|
+
id: 'length',
|
|
448
|
+
i18n: {
|
|
449
|
+
"zh-TW": {
|
|
450
|
+
length: "長度",
|
|
451
|
+
lte: "≦ 小於或等於",
|
|
452
|
+
number: "數字",
|
|
453
|
+
"maximal length": "長度上限"
|
|
454
|
+
}
|
|
455
|
+
},
|
|
456
|
+
ops: {
|
|
457
|
+
lte: {
|
|
458
|
+
func: function(v, c){
|
|
459
|
+
c == null && (c = {});
|
|
460
|
+
return (v + "").length <= +c.val;
|
|
461
|
+
},
|
|
462
|
+
config: {
|
|
463
|
+
val: {
|
|
464
|
+
type: 'number',
|
|
465
|
+
hint: "maximal length"
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
199
470
|
}, {
|
|
200
471
|
id: 'number',
|
|
201
472
|
i18n: {
|
|
@@ -204,9 +475,13 @@
|
|
|
204
475
|
lte: "≦ 小於或等於",
|
|
205
476
|
gte: "≧ 大於或等於",
|
|
206
477
|
ne: "≠ 不等於",
|
|
207
|
-
eq: "= 等於"
|
|
478
|
+
eq: "= 等於",
|
|
479
|
+
is: "任何數字"
|
|
208
480
|
}
|
|
209
481
|
},
|
|
482
|
+
convert: function(v){
|
|
483
|
+
return +(v + "").replace(/,/g, '');
|
|
484
|
+
},
|
|
210
485
|
ops: {
|
|
211
486
|
lte: {
|
|
212
487
|
func: function(v, c){
|
|
@@ -271,6 +546,12 @@
|
|
|
271
546
|
hint: "number for comparison"
|
|
272
547
|
}
|
|
273
548
|
}
|
|
549
|
+
},
|
|
550
|
+
is: {
|
|
551
|
+
func: function(v){
|
|
552
|
+
return !isNaN(v);
|
|
553
|
+
},
|
|
554
|
+
config: {}
|
|
274
555
|
}
|
|
275
556
|
}
|
|
276
557
|
}
|
|
@@ -347,20 +628,21 @@
|
|
|
347
628
|
form.widget = function(opt){
|
|
348
629
|
var this$ = this;
|
|
349
630
|
opt == null && (opt = {});
|
|
350
|
-
this.
|
|
631
|
+
this._root = typeof opt.root === 'string'
|
|
351
632
|
? document.querySelector(opt.root)
|
|
352
633
|
: opt.root;
|
|
353
634
|
this.evtHandler = {};
|
|
354
635
|
this.mod = opt.mod || null;
|
|
355
636
|
this.i18n = {};
|
|
356
637
|
this._custom = {};
|
|
638
|
+
this._status = 1;
|
|
357
639
|
this._meta = {
|
|
358
640
|
config: {},
|
|
359
641
|
key: Math.random().toString(36).substring(2)
|
|
360
642
|
};
|
|
361
643
|
this._value = null;
|
|
362
644
|
this._empty = true;
|
|
363
|
-
this._mode = opt.mode || '
|
|
645
|
+
this._mode = opt.mode || 'edit';
|
|
364
646
|
this._validate = opt.validate || null;
|
|
365
647
|
this._opsets = (opt.opsets || []).map(function(opset){
|
|
366
648
|
if (typeof opset === 'string') {
|
|
@@ -397,6 +679,18 @@
|
|
|
397
679
|
return ((ref$ = this$.evtHandler)[n] || (ref$[n] = [])).push(cb);
|
|
398
680
|
});
|
|
399
681
|
},
|
|
682
|
+
off: function(n, cb){
|
|
683
|
+
var this$ = this;
|
|
684
|
+
return (Array.isArray(n)
|
|
685
|
+
? n
|
|
686
|
+
: [n]).map(function(n){
|
|
687
|
+
var l, ref$;
|
|
688
|
+
l = (ref$ = this$.evtHandler)[n] || (ref$[n] = []);
|
|
689
|
+
if (l.indexOf(cb) >= 0) {
|
|
690
|
+
return l.splice(l.indexOf(cb), 1);
|
|
691
|
+
}
|
|
692
|
+
});
|
|
693
|
+
},
|
|
400
694
|
fire: function(n){
|
|
401
695
|
var v, res$, i$, to$, ref$, len$, cb, results$ = [];
|
|
402
696
|
res$ = [];
|
|
@@ -410,6 +704,9 @@
|
|
|
410
704
|
}
|
|
411
705
|
return results$;
|
|
412
706
|
},
|
|
707
|
+
root: function(){
|
|
708
|
+
return this._root;
|
|
709
|
+
},
|
|
413
710
|
_init: function(){
|
|
414
711
|
return Promise.resolve(this.mod && this.mod.init ? this.mod.init.apply(this) : '');
|
|
415
712
|
},
|
|
@@ -419,6 +716,17 @@
|
|
|
419
716
|
? this._meta.key
|
|
420
717
|
: this._meta.alias || this._meta.key;
|
|
421
718
|
},
|
|
719
|
+
status: function(v){
|
|
720
|
+
var ov;
|
|
721
|
+
if (!(v != null)) {
|
|
722
|
+
return this._status;
|
|
723
|
+
}
|
|
724
|
+
ov = this._status;
|
|
725
|
+
this._status = v;
|
|
726
|
+
if (ov !== v) {
|
|
727
|
+
return this.fire('status', v);
|
|
728
|
+
}
|
|
729
|
+
},
|
|
422
730
|
serialize: function(){
|
|
423
731
|
var ref$, ret, ref1$;
|
|
424
732
|
ret = (ref1$ = {}, ref1$.key = (ref$ = this._meta).key, ref1$.title = ref$.title, ref1$.desc = ref$.desc, ref1$);
|
|
@@ -438,22 +746,34 @@
|
|
|
438
746
|
this._meta.term = (v.term || []).map(function(it){
|
|
439
747
|
return new form.term(it);
|
|
440
748
|
});
|
|
441
|
-
this.validate(
|
|
749
|
+
this.validate({
|
|
750
|
+
init: true
|
|
751
|
+
});
|
|
442
752
|
return this.render();
|
|
443
753
|
},
|
|
444
|
-
mode: function(
|
|
445
|
-
|
|
754
|
+
mode: function(m){
|
|
755
|
+
var ref$;
|
|
756
|
+
if (!(m != null)) {
|
|
446
757
|
return this._mode;
|
|
447
758
|
}
|
|
448
|
-
|
|
449
|
-
|
|
759
|
+
if (!(m === 'edit' || m === 'view' || m === 'config')) {
|
|
760
|
+
throw ref$ = new Error(), ref$.name = 'lderror', ref$.id = 1015, ref$;
|
|
761
|
+
}
|
|
762
|
+
if (this._mode === m) {
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
this._mode = m;
|
|
766
|
+
this.fire('mode', m);
|
|
767
|
+
this.validate({
|
|
768
|
+
init: true
|
|
769
|
+
});
|
|
450
770
|
return this.render();
|
|
451
771
|
},
|
|
452
772
|
errors: function(){
|
|
453
773
|
return this._errors;
|
|
454
774
|
},
|
|
455
|
-
value: function(v,
|
|
456
|
-
|
|
775
|
+
value: function(v, opt){
|
|
776
|
+
opt == null && (opt = {});
|
|
457
777
|
if (!(v != null)) {
|
|
458
778
|
return this._value;
|
|
459
779
|
}
|
|
@@ -461,13 +781,16 @@
|
|
|
461
781
|
this._empty = this.mod && this.mod.isEmpty
|
|
462
782
|
? this.mod.isEmpty.apply(this, v)
|
|
463
783
|
: !v;
|
|
464
|
-
this.validate(
|
|
465
|
-
|
|
784
|
+
this.validate({
|
|
785
|
+
init: opt.init
|
|
786
|
+
});
|
|
787
|
+
if (!opt.fromSource) {
|
|
466
788
|
return this.fire('change', this._value);
|
|
467
789
|
}
|
|
468
790
|
},
|
|
469
|
-
validate: function(){
|
|
791
|
+
validate: function(opt){
|
|
470
792
|
var this$ = this;
|
|
793
|
+
opt == null && (opt = {});
|
|
471
794
|
if (this.mod && this.mod.validate) {
|
|
472
795
|
return this.mod.validate.apply(this, this._value);
|
|
473
796
|
}
|
|
@@ -476,6 +799,7 @@
|
|
|
476
799
|
}
|
|
477
800
|
if (this._empty && this._meta.config.isRequired) {
|
|
478
801
|
this._errors = ["required"];
|
|
802
|
+
this.status(opt.init ? 1 : 2);
|
|
479
803
|
return this.render();
|
|
480
804
|
}
|
|
481
805
|
return Promise.all(this._meta.term.filter(function(t){
|
|
@@ -490,6 +814,7 @@
|
|
|
490
814
|
}).map(function(it){
|
|
491
815
|
return it[0].msg || 'error';
|
|
492
816
|
});
|
|
817
|
+
this$.status(this$._errors.length ? 2 : 0);
|
|
493
818
|
return this$.render();
|
|
494
819
|
}).then(function(){
|
|
495
820
|
return this$._errors;
|
package/index.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(){var form;form={};form.manager=function(){this._fields=[];return this};form.manager.prototype=import$(Object.create(Object.prototype),{add:function(it){return this._fields=this._fields.concat(Array.isArray(it)?it:[it])},fields:function(){return this._fields},serialize:function(){return this._fields.map(function(it){return it.serialize()})},value:function(vs){var k,v,f,results$=[];if(!vs){return Object.fromEntries(this._fields.map(function(it){return[it.key(),it.value()]}))}for(k in vs){v=vs[k];f=this._fields.filter(fn$)[0];if(!f){continue}results$.push(f.value(v))}return results$;function fn$(it){return it.key()===k}},mode:function(m){return this._fields.map(function(it){return it.mode(m)})}});form.op=function(opt){opt==null&&(opt={});this.id=opt.id;this.name=opt.name;this.config=opt.config;this.func=opt.func;this.i18n=opt.i18n;return this};form.op.prototype=import$(Object.create(Object.prototype),{validate:function(val,cfg){var ret;cfg==null&&(cfg={});if((ret=this.func(val,cfg))instanceof Promise){return ret}else{return Promise.resolve(!!ret)}},configDefault:function(){var cfg,k,ref$,v;cfg={};for(k in ref$=this.config){v=ref$[k];cfg[k]=v["default"]}return cfg}});form.opset=function(opt){var ops,k,v,this$=this;opt==null&&(opt={});this.name=opt.name;this.id=opt.id;this.i18n=opt.i18n;this.ops={};ops=Array.isArray(opt.ops)?opt.ops.map(function(it){return{v:it,k:it.id}}):function(){var ref$,results$=[];for(k in ref$=opt.ops){v=ref$[k];results$.push({k:k,v:v})}return results$}();ops.map(function(arg$){var k,v,ref$,ref1$,results$=[];k=arg$.k,v=arg$.v;this$.ops[k]=v instanceof form.op?v:k?new form.op((ref$=typeof v==="function"?{func:v}:v,ref$.id=k,ref$)):function(){throw new Error("[@plotdb/form/opset] invalid op when initializing opset.")}();for(k in ref$=opt.ops){v=ref$[k];results$.push(this$.ops[k]=new form.op((ref1$=typeof v==="function"?{func:v}:v,ref1$.name=k,ref1$.id=k,ref1$)))}return results$});this.defaultOp=this.ops[opt.defaultOp]?opt.defaultOp:function(){var ref$,results$=[];for(k in ref$=this.ops){v=ref$[k];results$.push(k)}return results$}.call(this)[0];return this};form.opset.prototype=import$(Object.create(Object.prototype),{getOp:function(id){return this.ops[id||this.defaultOp]},getOps:function(){var k,ref$,v,results$=[];for(k in ref$=this.ops){v=ref$[k];results$.push(v)}return results$}});form.opset.register=function(it){return(this.list||(this.list=[])).push(it instanceof form.opset?it:new form.opset(it))};form.opset.get=function(id){return(this.list||(this.list=[])).filter(function(it){return(it.id||it.name)===id})[0]};form.opset["default"]=[{id:"string",i18n:{"zh-TW":{string:"文字",include:"包含",exclude:"排除",email:"電子郵件"}},ops:{include:{func:function(v,c){c==null&&(c={});return~(""+(v||"")).indexOf(c.str||"")},config:{str:{type:"text"}}},exclude:{func:function(v,c){c==null&&(c={});return!~(""+(v||"")).indexOf(c.str||"")},config:{str:{type:"text"}}},email:{func:function(v){return/^[^@]+@[^@]+$/.exec(v)},config:{}}}},{id:"number",i18n:{"zh-TW":{number:"數字",lte:"≦ 小於或等於",gte:"≧ 大於或等於",ne:"≠ 不等於",eq:"= 等於"}},ops:{lte:{func:function(v,c){c==null&&(c={});if(isNaN(v)||isNaN(c.val)){return false}else{return+v<=+c.val}},config:{val:{type:"text",hint:"number for comparison"}}},gte:{func:function(v,c){c==null&&(c={});if(isNaN(v)||isNaN(c.val)){return false}else{return+v>=+c.val}},config:{val:{type:"text",hint:"number for comparison"}}},ne:{func:function(v,c){c==null&&(c={});if(isNaN(v)||isNaN(c.val)){return false}else{return+v!==+c.val}},config:{val:{type:"text",hint:"number for comparison"}}},eq:{func:function(v,c){c==null&&(c={});if(isNaN(v)||isNaN(c.val)){return false}else{return+v===+c.val}},config:{val:{type:"text",hint:"number for comparison"}}}}}];form.opset["default"].map(function(it){return form.opset.register(it)});form.term=function(opt){opt==null&&(opt={});import$((this.enabled=true,this.opset=null,this.op=null,this.config={},this),opt);this.setOpset(opt.opset,opt.op,opt.config);return this};form.term.prototype=import$(Object.create(Object.prototype),{toggle:function(it){return this.enabled=it!=null?it:!this.enabled},setOpset:function(opset,op,cfg){if(typeof opset==="string"){if(!(this.opset=form.opset.get(opset))){throw new Error("no such opset '"+opset+"'")}}else if(opset instanceof form.opset){this.opset=opset}else{throw new Error("invalid opset")}return this.setOp(op,cfg)},setOp:function(id,cfg){if(!this.opset){throw new Error("opset not set")}if(!(this.op=this.opset.getOp(id))){throw new Error("no such op '"+id+"'")}return this.setConfig(cfg)},setConfig:function(cfg){if(!this.op){throw new Error("op not set")}return this.config=!cfg?this.op.configDefault():cfg},validate:function(v){if(!this.op){Promis.reject(new Error("op not set"))}return this.op.validate(v,this.config)},serialize:function(){return{enabled:this.enabled,opset:this.opset.id,op:this.op.id,config:this.config}},deserialize:function(v){this.toggle(v.enabled);return this.setOpset(v.opset,v.op,v.config)}});form.widget=function(opt){var this$=this;opt==null&&(opt={});this.root=typeof opt.root==="string"?document.querySelector(opt.root):opt.root;this.evtHandler={};this.mod=opt.mod||null;this.i18n={};this._custom={};this._meta={config:{},key:Math.random().toString(36).substring(2)};this._value=null;this._empty=true;this._mode=opt.mode||"view";this._validate=opt.validate||null;this._opsets=(opt.opsets||[]).map(function(opset){if(typeof opset==="string"){return form.opset.get(opset)}else if(typeof opset===form.opset){return opset}else{return new form.opset(opset)}});this._opsets.filter(function(it){return it.i18n}).map(function(it){var k,ref$,v,ref1$,results$=[];for(k in ref$=it.i18n){v=ref$[k];results$.push(import$((ref1$=this$.i18n)[k]||(ref1$[k]={}),v))}return results$});this._errors=[];this.init=proxise.once(function(){return this$._init()});return this};form.widget.prototype=import$(Object.create(Object.prototype),{on:function(n,cb){var this$=this;return(Array.isArray(n)?n:[n]).map(function(n){var ref$;return((ref$=this$.evtHandler)[n]||(ref$[n]=[])).push(cb)})},fire:function(n){var v,res$,i$,to$,ref$,len$,cb,results$=[];res$=[];for(i$=1,to$=arguments.length;i$<to$;++i$){res$.push(arguments[i$])}v=res$;for(i$=0,len$=(ref$=this.evtHandler[n]||[]).length;i$<len$;++i$){cb=ref$[i$];results$.push(cb.apply(this,v))}return results$},_init:function(){return Promise.resolve(this.mod&&this.mod.init?this.mod.init.apply(this):"")},key:function(keyonly){keyonly==null&&(keyonly=false);return keyonly?this._meta.key:this._meta.alias||this._meta.key},serialize:function(){var ref$,ret,ref1$;ret=(ref1$={},ref1$.key=(ref$=this._meta).key,ref1$.title=ref$.title,ref1$.desc=ref$.desc,ref1$);ret.config=JSON.parse(JSON.stringify(this._meta.config||{}));ret.term=this._meta.term.map(function(it){return it.serialize()});return ret},deserialize:function(v){var ref$;ref$=this._meta;ref$.key=v.key;ref$.title=v.title;ref$.desc=v.desc;this._meta.config=JSON.parse(JSON.stringify(v.config||{}));this._meta.term=(v.term||[]).map(function(it){return new form.term(it)});this.validate();return this.render()},mode:function(it){if(!(it!=null)){return this._mode}this._mode=it;this.validate();return this.render()},errors:function(){return this._errors},value:function(v,fromSource){fromSource==null&&(fromSource=false);if(!(v!=null)){return this._value}this._value=v;this._empty=this.mod&&this.mod.isEmpty?this.mod.isEmpty.apply(this,v):!v;this.validate();if(!fromSource){return this.fire("change",this._value)}},validate:function(){var this$=this;if(this.mod&&this.mod.validate){return this.mod.validate.apply(this,this._value)}if(this._validate){return Promise.resolve(this._validate(this._value))}if(this._empty&&this._meta.config.isRequired){this._errors=["required"];return this.render()}return Promise.all(this._meta.term.filter(function(t){return t.enabled}).map(function(t){return t.validate(this$._value).then(function(v){return[t,v]})})).then(function(it){this$._errors=it.filter(function(it){return!it[1]}).map(function(it){return it[0].msg||"error"});return this$.render()}).then(function(){return this$._errors})},render:function(){this.fire("render");if(this.mod&&this.mod.render){return this.mod.render.apply(this)}},adapt:function(){var args,res$,i$,to$;res$=[];for(i$=0,to$=arguments.length;i$<to$;++i$){res$.push(arguments[i$])}args=res$;if(this.mod&&this.mod.adapt){return this.mod.adapt.apply(this,args)}}});if(typeof module!="undefined"&&module!==null){module.exports=form}else if(typeof window!="undefined"&&window!==null){window.form=form}function import$(obj,src){var own={}.hasOwnProperty;for(var key in src)if(own.call(src,key))obj[key]=src[key];return obj}}).call(this);
|
|
1
|
+
(function(){var form;form={};form.manager=function(o){var this$=this;o==null&&(o={});this._ws={w:{},p:new WeakMap,s:{},l:{}};this._evthdr={};this._status=1;this._mode="edit";this.mod={afterCheck:o.mod.afterCheck}||{};this.afterCheck=debounce(330,function(){var args,res$,i$,to$;res$=[];for(i$=0,to$=arguments.length;i$<to$;++i$){res$.push(arguments[i$])}args=res$;return this$._afterCheck.apply(this$,args)});this._checkDebounced=debounce(10,function(){var args,res$,i$,to$;res$=[];for(i$=0,to$=arguments.length;i$<to$;++i$){res$.push(arguments[i$])}args=res$;return this$._check.apply(this$,args)});return this};form.manager.prototype=import$(Object.create(Object.prototype),{on:function(n,cb){var ref$;return((ref$=this._evthdr)[n]||(ref$[n]=[])).push(cb)},fire:function(n){var v,res$,i$,to$,ref$,len$,cb,results$=[];res$=[];for(i$=1,to$=arguments.length;i$<to$;++i$){res$.push(arguments[i$])}v=res$;for(i$=0,len$=(ref$=this._evthdr[n]||[]).length;i$<len$;++i$){cb=ref$[i$];results$.push(cb.apply(this,v))}return results$},add:function(o){var ref$,w,p,this$=this;if(Array.isArray(o)){return o.map(function(it){return this$.add(it)})}ref$=[o.widget,o.path],w=ref$[0],p=ref$[1];if(this._ws.p.get(w)){return}this._ws.w[p]=w;this._ws.p.set(w,p);this._ws.l[p]={};w.on("change",this._ws.l[p].c=function(v){return this$.fire("change",{widget:w,path:p,value:v})});return w.on("status",this._ws.l[p].s=function(s){return this$.fire("status",{widget:w,path:p,status:s})})},remove:function(o){var ws,ref$,key$,ref1$,this$=this;if(Array.isArray(o)){return o.map(function(it){return this$.remove(it)})}if(!(ws=this._ws.w[o.path])){return}o.off("change",this._ws.l[o.path].c);o.off("status",this._ws.l[o.path].s);this._ws.p["delete"](ws);delete this._ws.w[o.path];return ref1$=(ref$=this._ws.l)[key$=o.path],delete ref$[key$],ref1$},widget:function(p){return this._ws[p]},status:function(v){return!(v!=null)?this._status:this._status=v},progress:function(){var ret,k,s;ret={total:function(){var results$=[];for(k in this._ws.w){results$.push(k)}return results$}.call(this).length,done:function(){var ref$,results$=[];for(k in ref$=this._ws.s){s=ref$[k];results$.push(s)}return results$}.call(this).filter(function(s){return s!=null&&s===0}).length};ret.percent=ret.done/ret.total;return ret},_afterCheck:function(){var os,ret,k,v;os=this._status;delete this._status;this.mod.afterCheck.apply(this);if(!(this._status!=null)){ret=function(){var ref$,results$=[];for(k in ref$=this._ws.w){v=ref$[k];results$.push(this._ws.s[k])}return results$}.call(this).filter(function(s){return!(s!=null&&s===0)}).length;this._status=ret?1:0}if(os!==this._status){return this.fire("readystatechange",this._status===0)}},check:function(o,now){var p,w;now==null&&(now=false);if(!o){return this.check(function(){var ref$,results$=[];for(p in ref$=this._ws.w){w=ref$[p];results$.push({widget:w,path:p})}return results$}.call(this),now)}this.checkList=(this.checkList||(this.checkList=[])).concat(Array.isArray(o)?o:[o]);return now?this._check(null,true):this._checkDebounced()},_check:function(o,now){var list,this$=this;if(!o){list=this.checkList||[];this.checkList=[];return this._check(list,now)}if(Array.isArray(o)){return Promise.all(o.map(function(it){return this$._check(it,now)})).then(function(it){return it.filter(function(it){return it})})}return new Promise(function(res,rej){var ref$,w,p,_now;ref$=[o.widget,o.path,o.now],w=ref$[0],p=ref$[1],_now=ref$[2];if(!(w&&p)){return res()}if(!w&&!(w=this$._ws.w[p])){return res()}if(!p){p=this$._ws.p[w]}return w.validate().then(function(){var os,promise;os=this$._ws.s[p];this$._ws.s[p]=w.status();promise=now||_now?Promise.resolve(this$.afterCheck().now()):this$.afterCheck();return promise.then(function(){if(os!==this$._ws.s[p]){this$.fire("status",{path:p,widget:w,status:this$._ws.s[p]})}return this$._ws.s[p]})}).then(function(){if(this$._ws.s[p]===2){return res({widget:w,path:p,status:this$._ws.s[p]})}else{return res()}})})},formData:function(){var fd,p,ref$,w,val,i$,to$,i;fd=new FormData;for(p in ref$=this._ws.w){w=ref$[p];val=w.value();if(Array.isArray(v)){for(i$=0,to$=v.length;i$<to$;++i$){i=i$;fd.append(p+"[]",v[i])}}else{fd.append(p,val)}}return fd},value:function(v,opt){var ret,p,ref$,w,results$=[];opt==null&&(opt={});if(!v){ret={};for(p in ref$=this._ws.w){w=ref$[p];ret[p]=w.value()}return ret}for(p in ref$=this._ws.w){w=ref$[p];results$.push(w.value(v[p],opt))}return results$},mode:function(m){var ref$,p,w,results$=[];if(!(m!=null)){return this._mode}if(!(m==="edit"||m==="view"||m==="config")){throw ref$=new Error,ref$.name="lderror",ref$.id=1015,ref$}if(this._mode===m){return}this._mode=m;this.fire("mode",m);for(p in ref$=this._ws.w){w=ref$[p];results$.push(w.mode(m))}return results$}});form.op=function(opt){opt==null&&(opt={});this.id=opt.id;this.name=opt.name;this.config=opt.config;this.func=opt.func;this.opset=opt.opset;return this};form.op.prototype=import$(Object.create(Object.prototype),{validate:function(val,cfg){var ret;cfg==null&&(cfg={});if(this.opset.convert){val=this.opset.convert(val)}if((ret=this.func(val,cfg))instanceof Promise){return ret}else{return Promise.resolve(!!ret)}},configDefault:function(){var cfg,k,ref$,v;cfg={};for(k in ref$=this.config){v=ref$[k];cfg[k]=v["default"]}return cfg}});form.opset=function(opt){var ops,k,v,this$=this;opt==null&&(opt={});this.name=opt.name;this.id=opt.id;this.i18n=opt.i18n;this.convert=opt.convert;this.ops={};ops=Array.isArray(opt.ops)?opt.ops.map(function(it){return{v:it,k:it.id}}):function(){var ref$,results$=[];for(k in ref$=opt.ops){v=ref$[k];results$.push({k:k,v:v})}return results$}();ops.map(function(arg$){var k,v,ref$,ref1$,results$=[];k=arg$.k,v=arg$.v;this$.ops[k]=v instanceof form.op?v:k?new form.op((ref$=typeof v==="function"?{func:v}:v,ref$.id=k,ref$.opset=this$,ref$)):function(){throw new Error("[@plotdb/form/opset] invalid op when initializing opset.")}();for(k in ref$=opt.ops){v=ref$[k];results$.push(this$.ops[k]=new form.op((ref1$=typeof v==="function"?{func:v}:v,ref1$.name=k,ref1$.id=k,ref1$.opset=this$,ref1$)))}return results$});this.defaultOp=this.ops[opt.defaultOp]?opt.defaultOp:function(){var ref$,results$=[];for(k in ref$=this.ops){v=ref$[k];results$.push(k)}return results$}.call(this)[0];return this};form.opset.prototype=import$(Object.create(Object.prototype),{getOp:function(id){return this.ops[id||this.defaultOp]},getOps:function(){var k,ref$,v,results$=[];for(k in ref$=this.ops){v=ref$[k];results$.push(v)}return results$}});form.opset.register=function(it){return(this.list||(this.list=[])).push(it instanceof form.opset?it:new form.opset(it))};form.opset.get=function(id){return(this.list||(this.list=[])).filter(function(it){return(it.id||it.name)===id})[0]};form.opset["default"]=[{id:"string",i18n:{"zh-TW":{string:"文字",include:"包含",exclude:"排除",email:"電子郵件"}},ops:{include:{func:function(v,c){c==null&&(c={});return~(""+(v||"")).indexOf(c.str||"")},config:{str:{type:"text"}}},exclude:{func:function(v,c){c==null&&(c={});return!~(""+(v||"")).indexOf(c.str||"")},config:{str:{type:"text"}}},email:{func:function(v){return/^[^@]+@[^@]+$/.exec(v)},config:{}}}},{id:"length",i18n:{"zh-TW":{length:"長度",lte:"≦ 小於或等於",number:"數字","maximal length":"長度上限"}},ops:{lte:{func:function(v,c){c==null&&(c={});return(v+"").length<=+c.val},config:{val:{type:"number",hint:"maximal length"}}}}},{id:"number",i18n:{"zh-TW":{number:"數字",lte:"≦ 小於或等於",gte:"≧ 大於或等於",ne:"≠ 不等於",eq:"= 等於",is:"任何數字"}},convert:function(v){return+(v+"").replace(/,/g,"")},ops:{lte:{func:function(v,c){c==null&&(c={});if(isNaN(v)||isNaN(c.val)){return false}else{return+v<=+c.val}},config:{val:{type:"text",hint:"number for comparison"}}},gte:{func:function(v,c){c==null&&(c={});if(isNaN(v)||isNaN(c.val)){return false}else{return+v>=+c.val}},config:{val:{type:"text",hint:"number for comparison"}}},ne:{func:function(v,c){c==null&&(c={});if(isNaN(v)||isNaN(c.val)){return false}else{return+v!==+c.val}},config:{val:{type:"text",hint:"number for comparison"}}},eq:{func:function(v,c){c==null&&(c={});if(isNaN(v)||isNaN(c.val)){return false}else{return+v===+c.val}},config:{val:{type:"text",hint:"number for comparison"}}},is:{func:function(v){return!isNaN(v)},config:{}}}}];form.opset["default"].map(function(it){return form.opset.register(it)});form.term=function(opt){opt==null&&(opt={});import$((this.enabled=true,this.opset=null,this.op=null,this.config={},this),opt);this.setOpset(opt.opset,opt.op,opt.config);return this};form.term.prototype=import$(Object.create(Object.prototype),{toggle:function(it){return this.enabled=it!=null?it:!this.enabled},setOpset:function(opset,op,cfg){if(typeof opset==="string"){if(!(this.opset=form.opset.get(opset))){throw new Error("no such opset '"+opset+"'")}}else if(opset instanceof form.opset){this.opset=opset}else{throw new Error("invalid opset")}return this.setOp(op,cfg)},setOp:function(id,cfg){if(!this.opset){throw new Error("opset not set")}if(!(this.op=this.opset.getOp(id))){throw new Error("no such op '"+id+"'")}return this.setConfig(cfg)},setConfig:function(cfg){if(!this.op){throw new Error("op not set")}return this.config=!cfg?this.op.configDefault():cfg},validate:function(v){if(!this.op){Promis.reject(new Error("op not set"))}return this.op.validate(v,this.config)},serialize:function(){return{enabled:this.enabled,opset:this.opset.id,op:this.op.id,config:this.config}},deserialize:function(v){this.toggle(v.enabled);return this.setOpset(v.opset,v.op,v.config)}});form.widget=function(opt){var this$=this;opt==null&&(opt={});this._root=typeof opt.root==="string"?document.querySelector(opt.root):opt.root;this.evtHandler={};this.mod=opt.mod||null;this.i18n={};this._custom={};this._status=1;this._meta={config:{},key:Math.random().toString(36).substring(2)};this._value=null;this._empty=true;this._mode=opt.mode||"edit";this._validate=opt.validate||null;this._opsets=(opt.opsets||[]).map(function(opset){if(typeof opset==="string"){return form.opset.get(opset)}else if(typeof opset===form.opset){return opset}else{return new form.opset(opset)}});this._opsets.filter(function(it){return it.i18n}).map(function(it){var k,ref$,v,ref1$,results$=[];for(k in ref$=it.i18n){v=ref$[k];results$.push(import$((ref1$=this$.i18n)[k]||(ref1$[k]={}),v))}return results$});this._errors=[];this.init=proxise.once(function(){return this$._init()});return this};form.widget.prototype=import$(Object.create(Object.prototype),{on:function(n,cb){var this$=this;return(Array.isArray(n)?n:[n]).map(function(n){var ref$;return((ref$=this$.evtHandler)[n]||(ref$[n]=[])).push(cb)})},off:function(n,cb){var this$=this;return(Array.isArray(n)?n:[n]).map(function(n){var l,ref$;l=(ref$=this$.evtHandler)[n]||(ref$[n]=[]);if(l.indexOf(cb)>=0){return l.splice(l.indexOf(cb),1)}})},fire:function(n){var v,res$,i$,to$,ref$,len$,cb,results$=[];res$=[];for(i$=1,to$=arguments.length;i$<to$;++i$){res$.push(arguments[i$])}v=res$;for(i$=0,len$=(ref$=this.evtHandler[n]||[]).length;i$<len$;++i$){cb=ref$[i$];results$.push(cb.apply(this,v))}return results$},root:function(){return this._root},_init:function(){return Promise.resolve(this.mod&&this.mod.init?this.mod.init.apply(this):"")},key:function(keyonly){keyonly==null&&(keyonly=false);return keyonly?this._meta.key:this._meta.alias||this._meta.key},status:function(v){var ov;if(!(v!=null)){return this._status}ov=this._status;this._status=v;if(ov!==v){return this.fire("status",v)}},serialize:function(){var ref$,ret,ref1$;ret=(ref1$={},ref1$.key=(ref$=this._meta).key,ref1$.title=ref$.title,ref1$.desc=ref$.desc,ref1$);ret.config=JSON.parse(JSON.stringify(this._meta.config||{}));ret.term=this._meta.term.map(function(it){return it.serialize()});return ret},deserialize:function(v){var ref$;ref$=this._meta;ref$.key=v.key;ref$.title=v.title;ref$.desc=v.desc;this._meta.config=JSON.parse(JSON.stringify(v.config||{}));this._meta.term=(v.term||[]).map(function(it){return new form.term(it)});this.validate({init:true});return this.render()},mode:function(m){var ref$;if(!(m!=null)){return this._mode}if(!(m==="edit"||m==="view"||m==="config")){throw ref$=new Error,ref$.name="lderror",ref$.id=1015,ref$}if(this._mode===m){return}this._mode=m;this.fire("mode",m);this.validate({init:true});return this.render()},errors:function(){return this._errors},value:function(v,opt){opt==null&&(opt={});if(!(v!=null)){return this._value}this._value=v;this._empty=this.mod&&this.mod.isEmpty?this.mod.isEmpty.apply(this,v):!v;this.validate({init:opt.init});if(!opt.fromSource){return this.fire("change",this._value)}},validate:function(opt){var this$=this;opt==null&&(opt={});if(this.mod&&this.mod.validate){return this.mod.validate.apply(this,this._value)}if(this._validate){return Promise.resolve(this._validate(this._value))}if(this._empty&&this._meta.config.isRequired){this._errors=["required"];this.status(opt.init?1:2);return this.render()}return Promise.all(this._meta.term.filter(function(t){return t.enabled}).map(function(t){return t.validate(this$._value).then(function(v){return[t,v]})})).then(function(it){this$._errors=it.filter(function(it){return!it[1]}).map(function(it){return it[0].msg||"error"});this$.status(this$._errors.length?2:0);return this$.render()}).then(function(){return this$._errors})},render:function(){this.fire("render");if(this.mod&&this.mod.render){return this.mod.render.apply(this)}},adapt:function(){var args,res$,i$,to$;res$=[];for(i$=0,to$=arguments.length;i$<to$;++i$){res$.push(arguments[i$])}args=res$;if(this.mod&&this.mod.adapt){return this.mod.adapt.apply(this,args)}}});if(typeof module!="undefined"&&module!==null){module.exports=form}else if(typeof window!="undefined"&&window!==null){window.form=form}function import$(obj,src){var own={}.hasOwnProperty;for(var key in src)if(own.call(src,key))obj[key]=src[key];return obj}}).call(this);
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"author":"zbryikt","name":"@plotdb/form","license":"MIT","description":"form blocks","version":"0.0.
|
|
1
|
+
{"author":"zbryikt","name":"@plotdb/form","license":"MIT","description":"form blocks","version":"0.0.5","browser":"index.min.js","main":"index.min.js","homepage":"https://github.com/plotdb/form","repository":{"type":"git","url":"https://github.com/plotdb/form"},"engines":{"node":">=10.14.1","npm":">=6.4.1"},"scripts":{"start":"npx server -r web -o true"},"devDependencies":{"@loadingio/bootstrap.ext":"^0.0.4","@loadingio/debounce.js":"^1.0.1","@loadingio/ldquery":"^3.0.2","@plotdb/datadom":"^0.2.0","@plotdb/datahub":"^0.2.2","@plotdb/semver":"^0.0.2","@zbryikt/deploy":"^0.0.1","@zbryikt/template":"^2.3.33","bootstrap":"^4.5.3","bootstrap.native":"^3.0.13","fedep":"^1.1.2","i18next":"^19.9.2","ldcover":"^3.0.1","ldiconfont":"^0.1.10","ldview":"^1.0.0","livescript":"^1.6.0","stylus":"^0.55.0","uglify-js":"^3.13.1","uglifycss":"^0.0.29"},"frontendDependencies":{"root":"web/static/assets/lib","modules":["bootstrap","@loadingio/bootstrap.ext","bootstrap.native","ldcover","@loadingio/ldquery","@loadingio/debounce.js","@loadingio/ldcolorpicker","ldview","ldiconfont","ldslider","ldcolor","ldpalettepicker","curegex","dompurify","proxise","i18next","@plotdb/sheet","@plotdb/block","@plotdb/datadom","@plotdb/datahub","@plotdb/semver","@plotdb/rescope","@plotdb/suuid","@plotdb/konfig","@plotdb/csscope"]},"dependencies":{"@loadingio/ldcolorpicker":"^3.0.0","@plotdb/block":"^4.1.0","@plotdb/csscope":"^4.0.0","@plotdb/datadom":"^0.2.0","@plotdb/konfig":"^4.0.1","@plotdb/rescope":"^4.1.0","@plotdb/sheet":"^0.2.0","@plotdb/suuid":"^0.5.0","curegex":"^0.0.4","dompurify":"^2.2.6","ldcolor":"^1.0.0","ldpalettepicker":"^4.0.0","ldslider":"^3.0.2","proxise":"^1.0.1"}}
|