@plotdb/form 0.0.2 → 0.0.6

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,34 @@
1
1
  # Change Logs
2
2
 
3
+ ## v0.0.6
4
+
5
+ - bug fix: widget status change isn't logged in form manager.
6
+ - bug fix: widget init status isn't logged in form manager.
7
+ - bug fix: progress NaN when there is no widget.
8
+
9
+
10
+ ## v0.0.5
11
+
12
+ - bug fix: `check` is not finished immediately even if `now` is true
13
+ - bug fix: set value may fail due to empty value provided
14
+ - bug fix: `check` from `status` / `change` events of form.widget causes redundant validation.
15
+ - bug fix: `change` event of form.widget isn't forwarded to form.manager correctly.
16
+ - return invalid widgets from `check` call in ( if applicable, array of ) `{widget, path, status}` object.
17
+
18
+
19
+ ## v0.0.4
20
+
21
+ - add `root()` function to get root element of an widget
22
+
23
+
24
+ ## v0.0.3
25
+
26
+ - changes about `mode`:
27
+ - add `mode` in `form.manager`
28
+ - mode change triggers event `mode` with new mode value
29
+ - redefine mode values to `edit`, `view` and `config`
30
+ - update documentation
31
+
3
32
  ## v0.0.2
4
33
 
5
34
  - add `length` opset
package/index.js CHANGED
@@ -13,6 +13,7 @@
13
13
  };
14
14
  this._evthdr = {};
15
15
  this._status = 1;
16
+ this._mode = 'edit';
16
17
  this.mod = {
17
18
  afterCheck: o.mod.afterCheck
18
19
  } || {};
@@ -68,11 +69,8 @@
68
69
  this._ws.w[p] = w;
69
70
  this._ws.p.set(w, p);
70
71
  this._ws.l[p] = {};
72
+ this._ws.s[p] = w.status();
71
73
  w.on('change', this._ws.l[p].c = function(v){
72
- this$.check({
73
- widget: w,
74
- path: p
75
- });
76
74
  return this$.fire('change', {
77
75
  widget: w,
78
76
  path: p,
@@ -80,9 +78,11 @@
80
78
  });
81
79
  });
82
80
  return w.on('status', this._ws.l[p].s = function(s){
83
- return this$.check({
81
+ this$._ws.s[p] = s;
82
+ return this$.fire('status', {
84
83
  widget: w,
85
- path: p
84
+ path: p,
85
+ status: s
86
86
  });
87
87
  });
88
88
  },
@@ -131,7 +131,7 @@
131
131
  return s != null && s === 0;
132
132
  }).length
133
133
  };
134
- ret.percent = ret.done / ret.total;
134
+ ret.percent = ret.done / (ret.total || 1);
135
135
  return ret;
136
136
  },
137
137
  _afterCheck: function(){
@@ -170,32 +170,34 @@
170
170
  });
171
171
  }
172
172
  return results$;
173
- }.call(this)));
173
+ }.call(this)), now);
174
174
  }
175
175
  this.checkList = (this.checkList || (this.checkList = [])).concat(Array.isArray(o)
176
176
  ? o
177
177
  : [o]);
178
- if (now) {
179
- return this._check();
180
- } else {
181
- return this._checkDebounced();
182
- }
178
+ return now
179
+ ? this._check(null, true)
180
+ : this._checkDebounced();
183
181
  },
184
- _check: function(o){
182
+ _check: function(o, now){
185
183
  var list, this$ = this;
186
184
  if (!o) {
187
185
  list = this.checkList || [];
188
186
  this.checkList = [];
189
- return this._check(list);
187
+ return this._check(list, now);
190
188
  }
191
189
  if (Array.isArray(o)) {
192
190
  return Promise.all(o.map(function(it){
193
- return this$._check(it);
194
- }));
191
+ return this$._check(it, now);
192
+ })).then(function(it){
193
+ return it.filter(function(it){
194
+ return it;
195
+ });
196
+ });
195
197
  }
196
198
  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
+ var ref$, w, p, _now;
200
+ ref$ = [o.widget, o.path, o.now], w = ref$[0], p = ref$[1], _now = ref$[2];
199
201
  if (!(w && p)) {
200
202
  return res();
201
203
  }
@@ -206,23 +208,32 @@
206
208
  p = this$._ws.p[w];
207
209
  }
208
210
  return w.validate().then(function(){
209
- var os;
211
+ var os, promise;
210
212
  os = this$._ws.s[p];
211
213
  this$._ws.s[p] = w.status();
212
- if (os !== this$._ws.s[p]) {
213
- this$.fire('status', {
214
- path: p,
214
+ promise = now || _now
215
+ ? Promise.resolve(this$.afterCheck().now())
216
+ : this$.afterCheck();
217
+ return promise.then(function(){
218
+ if (os !== this$._ws.s[p]) {
219
+ this$.fire('status', {
220
+ path: p,
221
+ widget: w,
222
+ status: this$._ws.s[p]
223
+ });
224
+ }
225
+ return this$._ws.s[p];
226
+ });
227
+ }).then(function(){
228
+ if (this$._ws.s[p] === 2) {
229
+ return res({
215
230
  widget: w,
231
+ path: p,
216
232
  status: this$._ws.s[p]
217
233
  });
218
- }
219
- if (now) {
220
- return this$.afterCheck.now();
221
234
  } else {
222
- return this$.afterCheck();
235
+ return res();
223
236
  }
224
- }).then(function(){
225
- return res();
226
237
  });
227
238
  });
228
239
  },
@@ -243,8 +254,9 @@
243
254
  }
244
255
  return fd;
245
256
  },
246
- value: function(v){
257
+ value: function(v, opt){
247
258
  var ret, p, ref$, w, results$ = [];
259
+ opt == null && (opt = {});
248
260
  if (!v) {
249
261
  ret = {};
250
262
  for (p in ref$ = this._ws.w) {
@@ -255,9 +267,26 @@
255
267
  }
256
268
  for (p in ref$ = this._ws.w) {
257
269
  w = ref$[p];
258
- if (v[p]) {
259
- results$.push(w.value(v[p]));
260
- }
270
+ results$.push(w.value(v[p], opt));
271
+ }
272
+ return results$;
273
+ },
274
+ mode: function(m){
275
+ var ref$, p, w, results$ = [];
276
+ if (!(m != null)) {
277
+ return this._mode;
278
+ }
279
+ if (!(m === 'edit' || m === 'view' || m === 'config')) {
280
+ throw ref$ = new Error(), ref$.name = 'lderror', ref$.id = 1015, ref$;
281
+ }
282
+ if (this._mode === m) {
283
+ return;
284
+ }
285
+ this._mode = m;
286
+ this.fire('mode', m);
287
+ for (p in ref$ = this._ws.w) {
288
+ w = ref$[p];
289
+ results$.push(w.mode(m));
261
290
  }
262
291
  return results$;
263
292
  }
@@ -601,7 +630,7 @@
601
630
  form.widget = function(opt){
602
631
  var this$ = this;
603
632
  opt == null && (opt = {});
604
- this.root = typeof opt.root === 'string'
633
+ this._root = typeof opt.root === 'string'
605
634
  ? document.querySelector(opt.root)
606
635
  : opt.root;
607
636
  this.evtHandler = {};
@@ -615,7 +644,7 @@
615
644
  };
616
645
  this._value = null;
617
646
  this._empty = true;
618
- this._mode = opt.mode || 'view';
647
+ this._mode = opt.mode || 'edit';
619
648
  this._validate = opt.validate || null;
620
649
  this._opsets = (opt.opsets || []).map(function(opset){
621
650
  if (typeof opset === 'string') {
@@ -677,6 +706,9 @@
677
706
  }
678
707
  return results$;
679
708
  },
709
+ root: function(){
710
+ return this._root;
711
+ },
680
712
  _init: function(){
681
713
  return Promise.resolve(this.mod && this.mod.init ? this.mod.init.apply(this) : '');
682
714
  },
@@ -721,19 +753,29 @@
721
753
  });
722
754
  return this.render();
723
755
  },
724
- mode: function(it){
725
- if (!(it != null)) {
756
+ mode: function(m){
757
+ var ref$;
758
+ if (!(m != null)) {
726
759
  return this._mode;
727
760
  }
728
- this._mode = it;
729
- this.validate();
761
+ if (!(m === 'edit' || m === 'view' || m === 'config')) {
762
+ throw ref$ = new Error(), ref$.name = 'lderror', ref$.id = 1015, ref$;
763
+ }
764
+ if (this._mode === m) {
765
+ return;
766
+ }
767
+ this._mode = m;
768
+ this.fire('mode', m);
769
+ this.validate({
770
+ init: true
771
+ });
730
772
  return this.render();
731
773
  },
732
774
  errors: function(){
733
775
  return this._errors;
734
776
  },
735
- value: function(v, fromSource){
736
- fromSource == null && (fromSource = false);
777
+ value: function(v, opt){
778
+ opt == null && (opt = {});
737
779
  if (!(v != null)) {
738
780
  return this._value;
739
781
  }
@@ -741,8 +783,10 @@
741
783
  this._empty = this.mod && this.mod.isEmpty
742
784
  ? this.mod.isEmpty.apply(this, v)
743
785
  : !v;
744
- this.validate();
745
- if (!fromSource) {
786
+ this.validate({
787
+ init: opt.init
788
+ });
789
+ if (!opt.fromSource) {
746
790
  return this.fire('change', this._value);
747
791
  }
748
792
  },
@@ -757,7 +801,7 @@
757
801
  }
758
802
  if (this._empty && this._meta.config.isRequired) {
759
803
  this._errors = ["required"];
760
- this.status(opt.init && this.status() === 1 ? 1 : 2);
804
+ this.status(opt.init ? 1 : 2);
761
805
  return this.render();
762
806
  }
763
807
  return Promise.all(this._meta.term.filter(function(t){
package/index.min.js CHANGED
@@ -1 +1 @@
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);
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]={};this._ws.s[p]=w.status();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){this$._ws.s[p]=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||1);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.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"}}
1
+ {"author":"zbryikt","name":"@plotdb/form","license":"MIT","description":"form blocks","version":"0.0.6","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"}}