@plotdb/form 0.0.1 → 0.0.2

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 CHANGED
@@ -1,5 +1,15 @@
1
1
  # Change Logs
2
2
 
3
+ ## v0.0.2
4
+
5
+ - add `length` opset
6
+ - add `is` in `number` opset
7
+ - add `convert` in `number` opset
8
+ - support ldform favor `status` and `off` ( removeListener )
9
+ - support form.manager based on `ldform` logic
10
+ - update documentation
11
+
12
+
3
13
  ## v0.0.1
4
14
 
5
15
  - initial release
package/index.js CHANGED
@@ -2,48 +2,264 @@
2
2
  (function(){
3
3
  var form;
4
4
  form = {};
5
- form.manager = function(){
6
- this._fields = [];
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.mod = {
17
+ afterCheck: o.mod.afterCheck
18
+ } || {};
19
+ this.afterCheck = debounce(330, function(){
20
+ var args, res$, i$, to$;
21
+ res$ = [];
22
+ for (i$ = 0, to$ = arguments.length; i$ < to$; ++i$) {
23
+ res$.push(arguments[i$]);
24
+ }
25
+ args = res$;
26
+ return this$._afterCheck.apply(this$, args);
27
+ });
28
+ this._checkDebounced = debounce(10, function(){
29
+ var args, res$, i$, to$;
30
+ res$ = [];
31
+ for (i$ = 0, to$ = arguments.length; i$ < to$; ++i$) {
32
+ res$.push(arguments[i$]);
33
+ }
34
+ args = res$;
35
+ return this$._check.apply(this$, args);
36
+ });
7
37
  return this;
8
38
  };
9
39
  form.manager.prototype = import$(Object.create(Object.prototype), {
10
- add: function(it){
11
- return this._fields = this._fields.concat(Array.isArray(it)
12
- ? it
13
- : [it]);
40
+ on: function(n, cb){
41
+ var ref$;
42
+ return ((ref$ = this._evthdr)[n] || (ref$[n] = [])).push(cb);
14
43
  },
15
- fields: function(){
16
- return this._fields;
44
+ fire: function(n){
45
+ var v, res$, i$, to$, ref$, len$, cb, results$ = [];
46
+ res$ = [];
47
+ for (i$ = 1, to$ = arguments.length; i$ < to$; ++i$) {
48
+ res$.push(arguments[i$]);
49
+ }
50
+ v = res$;
51
+ for (i$ = 0, len$ = (ref$ = this._evthdr[n] || []).length; i$ < len$; ++i$) {
52
+ cb = ref$[i$];
53
+ results$.push(cb.apply(this, v));
54
+ }
55
+ return results$;
17
56
  },
18
- serialize: function(){
19
- return this._fields.map(function(it){
20
- return it.serialize();
57
+ add: function(o){
58
+ var ref$, w, p, this$ = this;
59
+ if (Array.isArray(o)) {
60
+ return o.map(function(it){
61
+ return this$.add(it);
62
+ });
63
+ }
64
+ ref$ = [o.widget, o.path], w = ref$[0], p = ref$[1];
65
+ if (this._ws.p.get(w)) {
66
+ return;
67
+ }
68
+ this._ws.w[p] = w;
69
+ this._ws.p.set(w, p);
70
+ this._ws.l[p] = {};
71
+ w.on('change', this._ws.l[p].c = function(v){
72
+ this$.check({
73
+ widget: w,
74
+ path: p
75
+ });
76
+ return this$.fire('change', {
77
+ widget: w,
78
+ path: p,
79
+ value: v
80
+ });
81
+ });
82
+ return w.on('status', this._ws.l[p].s = function(s){
83
+ return this$.check({
84
+ widget: w,
85
+ path: p
86
+ });
21
87
  });
22
88
  },
23
- value: function(vs){
24
- var k, v, f, results$ = [];
25
- if (!vs) {
26
- return Object.fromEntries(this._fields.map(function(it){
27
- return [it.key(), it.value()];
28
- }));
89
+ remove: function(o){
90
+ var ws, ref$, key$, ref1$, this$ = this;
91
+ if (Array.isArray(o)) {
92
+ return o.map(function(it){
93
+ return this$.remove(it);
94
+ });
29
95
  }
30
- for (k in vs) {
31
- v = vs[k];
32
- f = this._fields.filter(fn$)[0];
33
- if (!f) {
34
- continue;
35
- }
36
- results$.push(f.value(v));
96
+ if (!(ws = this._ws.w[o.path])) {
97
+ return;
37
98
  }
38
- return results$;
39
- function fn$(it){
40
- return it.key() === k;
99
+ o.off('change', this._ws.l[o.path].c);
100
+ o.off('status', this._ws.l[o.path].s);
101
+ this._ws.p['delete'](ws);
102
+ delete this._ws.w[o.path];
103
+ return ref1$ = (ref$ = this._ws.l)[key$ = o.path], delete ref$[key$], ref1$;
104
+ },
105
+ widget: function(p){
106
+ return this._ws[p];
107
+ },
108
+ status: function(v){
109
+ return !(v != null)
110
+ ? this._status
111
+ : this._status = v;
112
+ },
113
+ progress: function(){
114
+ var ret, k, s;
115
+ ret = {
116
+ total: (function(){
117
+ var results$ = [];
118
+ for (k in this._ws.w) {
119
+ results$.push(k);
120
+ }
121
+ return results$;
122
+ }.call(this)).length,
123
+ done: (function(){
124
+ var ref$, results$ = [];
125
+ for (k in ref$ = this._ws.s) {
126
+ s = ref$[k];
127
+ results$.push(s);
128
+ }
129
+ return results$;
130
+ }.call(this)).filter(function(s){
131
+ return s != null && s === 0;
132
+ }).length
133
+ };
134
+ ret.percent = ret.done / ret.total;
135
+ return ret;
136
+ },
137
+ _afterCheck: function(){
138
+ var os, ret, k, v;
139
+ os = this._status;
140
+ delete this._status;
141
+ this.mod.afterCheck.apply(this);
142
+ if (!(this._status != null)) {
143
+ ret = (function(){
144
+ var ref$, results$ = [];
145
+ for (k in ref$ = this._ws.w) {
146
+ v = ref$[k];
147
+ results$.push(this._ws.s[k]);
148
+ }
149
+ return results$;
150
+ }.call(this)).filter(function(s){
151
+ return !(s != null && s === 0);
152
+ }).length;
153
+ this._status = ret ? 1 : 0;
154
+ }
155
+ if (os !== this._status) {
156
+ return this.fire('readystatechange', this._status === 0);
157
+ }
158
+ },
159
+ check: function(o, now){
160
+ var p, w;
161
+ now == null && (now = false);
162
+ if (!o) {
163
+ return this.check((function(){
164
+ var ref$, results$ = [];
165
+ for (p in ref$ = this._ws.w) {
166
+ w = ref$[p];
167
+ results$.push({
168
+ widget: w,
169
+ path: p
170
+ });
171
+ }
172
+ return results$;
173
+ }.call(this)));
174
+ }
175
+ this.checkList = (this.checkList || (this.checkList = [])).concat(Array.isArray(o)
176
+ ? o
177
+ : [o]);
178
+ if (now) {
179
+ return this._check();
180
+ } else {
181
+ return this._checkDebounced();
41
182
  }
42
183
  },
43
- mode: function(m){
44
- return this._fields.map(function(it){
45
- return it.mode(m);
184
+ _check: function(o){
185
+ var list, this$ = this;
186
+ if (!o) {
187
+ list = this.checkList || [];
188
+ this.checkList = [];
189
+ return this._check(list);
190
+ }
191
+ if (Array.isArray(o)) {
192
+ return Promise.all(o.map(function(it){
193
+ return this$._check(it);
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;
210
+ os = this$._ws.s[p];
211
+ this$._ws.s[p] = w.status();
212
+ if (os !== this$._ws.s[p]) {
213
+ this$.fire('status', {
214
+ path: p,
215
+ widget: w,
216
+ status: this$._ws.s[p]
217
+ });
218
+ }
219
+ if (now) {
220
+ return this$.afterCheck.now();
221
+ } else {
222
+ return this$.afterCheck();
223
+ }
224
+ }).then(function(){
225
+ return res();
226
+ });
46
227
  });
228
+ },
229
+ formData: function(){
230
+ var fd, p, ref$, w, val, i$, to$, i;
231
+ fd = new FormData();
232
+ for (p in ref$ = this._ws.w) {
233
+ w = ref$[p];
234
+ val = w.value();
235
+ if (Array.isArray(v)) {
236
+ for (i$ = 0, to$ = v.length; i$ < to$; ++i$) {
237
+ i = i$;
238
+ fd.append(p + "[]", v[i]);
239
+ }
240
+ } else {
241
+ fd.append(p, val);
242
+ }
243
+ }
244
+ return fd;
245
+ },
246
+ value: function(v){
247
+ var ret, p, ref$, w, results$ = [];
248
+ if (!v) {
249
+ ret = {};
250
+ for (p in ref$ = this._ws.w) {
251
+ w = ref$[p];
252
+ ret[p] = w.value();
253
+ }
254
+ return ret;
255
+ }
256
+ for (p in ref$ = this._ws.w) {
257
+ w = ref$[p];
258
+ if (v[p]) {
259
+ results$.push(w.value(v[p]));
260
+ }
261
+ }
262
+ return results$;
47
263
  }
48
264
  });
49
265
  form.op = function(opt){
@@ -52,13 +268,16 @@
52
268
  this.name = opt.name;
53
269
  this.config = opt.config;
54
270
  this.func = opt.func;
55
- this.i18n = opt.i18n;
271
+ this.opset = opt.opset;
56
272
  return this;
57
273
  };
58
274
  form.op.prototype = import$(Object.create(Object.prototype), {
59
275
  validate: function(val, cfg){
60
276
  var ret;
61
277
  cfg == null && (cfg = {});
278
+ if (this.opset.convert) {
279
+ val = this.opset.convert(val);
280
+ }
62
281
  if ((ret = this.func(val, cfg)) instanceof Promise) {
63
282
  return ret;
64
283
  } else {
@@ -81,6 +300,7 @@
81
300
  this.name = opt.name;
82
301
  this.id = opt.id;
83
302
  this.i18n = opt.i18n;
303
+ this.convert = opt.convert;
84
304
  this.ops = {};
85
305
  ops = Array.isArray(opt.ops)
86
306
  ? opt.ops.map(function(it){
@@ -108,7 +328,7 @@
108
328
  : k
109
329
  ? new form.op((ref$ = typeof v === 'function' ? {
110
330
  func: v
111
- } : v, ref$.id = k, ref$))
331
+ } : v, ref$.id = k, ref$.opset = this$, ref$))
112
332
  : (function(){
113
333
  throw new Error('[@plotdb/form/opset] invalid op when initializing opset.');
114
334
  }());
@@ -116,7 +336,7 @@
116
336
  v = ref$[k];
117
337
  results$.push(this$.ops[k] = new form.op((ref1$ = typeof v === 'function' ? {
118
338
  func: v
119
- } : v, ref1$.name = k, ref1$.id = k, ref1$)));
339
+ } : v, ref1$.name = k, ref1$.id = k, ref1$.opset = this$, ref1$)));
120
340
  }
121
341
  return results$;
122
342
  });
@@ -196,6 +416,30 @@
196
416
  config: {}
197
417
  }
198
418
  }
419
+ }, {
420
+ id: 'length',
421
+ i18n: {
422
+ "zh-TW": {
423
+ length: "長度",
424
+ lte: "≦ 小於或等於",
425
+ number: "數字",
426
+ "maximal length": "長度上限"
427
+ }
428
+ },
429
+ ops: {
430
+ lte: {
431
+ func: function(v, c){
432
+ c == null && (c = {});
433
+ return (v + "").length <= +c.val;
434
+ },
435
+ config: {
436
+ val: {
437
+ type: 'number',
438
+ hint: "maximal length"
439
+ }
440
+ }
441
+ }
442
+ }
199
443
  }, {
200
444
  id: 'number',
201
445
  i18n: {
@@ -204,9 +448,13 @@
204
448
  lte: "≦ 小於或等於",
205
449
  gte: "≧ 大於或等於",
206
450
  ne: "≠ 不等於",
207
- eq: "= 等於"
451
+ eq: "= 等於",
452
+ is: "任何數字"
208
453
  }
209
454
  },
455
+ convert: function(v){
456
+ return +(v + "").replace(/,/g, '');
457
+ },
210
458
  ops: {
211
459
  lte: {
212
460
  func: function(v, c){
@@ -271,6 +519,12 @@
271
519
  hint: "number for comparison"
272
520
  }
273
521
  }
522
+ },
523
+ is: {
524
+ func: function(v){
525
+ return !isNaN(v);
526
+ },
527
+ config: {}
274
528
  }
275
529
  }
276
530
  }
@@ -354,6 +608,7 @@
354
608
  this.mod = opt.mod || null;
355
609
  this.i18n = {};
356
610
  this._custom = {};
611
+ this._status = 1;
357
612
  this._meta = {
358
613
  config: {},
359
614
  key: Math.random().toString(36).substring(2)
@@ -397,6 +652,18 @@
397
652
  return ((ref$ = this$.evtHandler)[n] || (ref$[n] = [])).push(cb);
398
653
  });
399
654
  },
655
+ off: function(n, cb){
656
+ var this$ = this;
657
+ return (Array.isArray(n)
658
+ ? n
659
+ : [n]).map(function(n){
660
+ var l, ref$;
661
+ l = (ref$ = this$.evtHandler)[n] || (ref$[n] = []);
662
+ if (l.indexOf(cb) >= 0) {
663
+ return l.splice(l.indexOf(cb), 1);
664
+ }
665
+ });
666
+ },
400
667
  fire: function(n){
401
668
  var v, res$, i$, to$, ref$, len$, cb, results$ = [];
402
669
  res$ = [];
@@ -419,6 +686,17 @@
419
686
  ? this._meta.key
420
687
  : this._meta.alias || this._meta.key;
421
688
  },
689
+ status: function(v){
690
+ var ov;
691
+ if (!(v != null)) {
692
+ return this._status;
693
+ }
694
+ ov = this._status;
695
+ this._status = v;
696
+ if (ov !== v) {
697
+ return this.fire('status', v);
698
+ }
699
+ },
422
700
  serialize: function(){
423
701
  var ref$, ret, ref1$;
424
702
  ret = (ref1$ = {}, ref1$.key = (ref$ = this._meta).key, ref1$.title = ref$.title, ref1$.desc = ref$.desc, ref1$);
@@ -438,7 +716,9 @@
438
716
  this._meta.term = (v.term || []).map(function(it){
439
717
  return new form.term(it);
440
718
  });
441
- this.validate();
719
+ this.validate({
720
+ init: true
721
+ });
442
722
  return this.render();
443
723
  },
444
724
  mode: function(it){
@@ -466,8 +746,9 @@
466
746
  return this.fire('change', this._value);
467
747
  }
468
748
  },
469
- validate: function(){
749
+ validate: function(opt){
470
750
  var this$ = this;
751
+ opt == null && (opt = {});
471
752
  if (this.mod && this.mod.validate) {
472
753
  return this.mod.validate.apply(this, this._value);
473
754
  }
@@ -476,6 +757,7 @@
476
757
  }
477
758
  if (this._empty && this._meta.config.isRequired) {
478
759
  this._errors = ["required"];
760
+ this.status(opt.init && this.status() === 1 ? 1 : 2);
479
761
  return this.render();
480
762
  }
481
763
  return Promise.all(this._meta.term.filter(function(t){
@@ -490,6 +772,7 @@
490
772
  }).map(function(it){
491
773
  return it[0].msg || 'error';
492
774
  });
775
+ this$.status(this$._errors.length ? 2 : 0);
493
776
  return this$.render();
494
777
  }).then(function(){
495
778
  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.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){this$.check({widget:w,path:p});return this$.fire("change",{widget:w,path:p,value:v})});return w.on("status",this._ws.l[p].s=function(s){return this$.check({widget:w,path:p})})},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))}this.checkList=(this.checkList||(this.checkList=[])).concat(Array.isArray(o)?o:[o]);if(now){return this._check()}else{return this._checkDebounced()}},_check:function(o){var list,this$=this;if(!o){list=this.checkList||[];this.checkList=[];return this._check(list)}if(Array.isArray(o)){return Promise.all(o.map(function(it){return this$._check(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;os=this$._ws.s[p];this$._ws.s[p]=w.status();if(os!==this$._ws.s[p]){this$.fire("status",{path:p,widget:w,status:this$._ws.s[p]})}if(now){return this$.afterCheck.now()}else{return this$.afterCheck()}}).then(function(){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){var ret,p,ref$,w,results$=[];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];if(v[p]){results$.push(w.value(v[p]))}}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||"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)})},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$},_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(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(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&&this.status()===1?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","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"}}
1
+ {"author":"zbryikt","name":"@plotdb/form","license":"MIT","description":"form blocks","version":"0.0.2","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"}}