@plotdb/form 0.2.5 → 0.3.1

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,21 @@
1
1
  # Change Logs
2
2
 
3
+ ## v0.3.1
4
+
5
+ - `conditor`: disabled widgets are read as usual instead of as empty, so results depend on values only.
6
+ rules relying on an upstream condition should state it explicitly, via `precond` or in `when`.
7
+
8
+
9
+ ## v0.3.0
10
+
11
+ - rewrite `conditor`: serializable rules with JSONLogic expressions ( `when` ) and paths with selectors ( `effect.targets` ).
12
+ rules are re-evaluated only when values they read change. effects from all conditors on a widget are merged.
13
+ see `doc/conditor/index.md`.
14
+ - add `manager.conditor()`
15
+ - `manager.resolve` supports selectors in path segments: `*`, `#<key>`, `[tag~=<tag>]`
16
+ - deprecate `condctrl`. `manager.condition()` now creates it on first call.
17
+
18
+
3
19
  ## v0.2.5
4
20
 
5
21
  - fix bug: `manager.manager` throws ReferenceError since it uses undefined `it` instead of loop key
package/index.js CHANGED
@@ -353,210 +353,481 @@
353
353
  var this$ = this;
354
354
  opt == null && (opt = {});
355
355
  this._ = {
356
+ id: "c" + Math.random().toString(36).substring(2),
356
357
  manager: opt.manager,
357
- conditions: [],
358
- results: {},
359
- virtual: {}
358
+ rules: [],
359
+ hash: {},
360
+ result: {},
361
+ dep: {},
362
+ tgt: {},
363
+ touched: new Set(),
364
+ pending: null
360
365
  };
361
- this._.manager.on('change', debounce(50, function(){
362
- return this$.run();
363
- }));
366
+ this._.manager.on('change', function(arg$){
367
+ var path;
368
+ path = (arg$ != null
369
+ ? arg$
370
+ : {}).path;
371
+ return this$._schedule(path);
372
+ });
364
373
  return this;
365
374
  };
366
- form.conditor.prototype = (ref$ = Object.create(Object.prototype), ref$.init = function(conditions){
367
- conditions == null && (conditions = []);
368
- this._.conditions = conditions.map(function(c, i){
375
+ form.conditor.prototype = (ref$ = Object.create(Object.prototype), ref$.init = function(rules){
376
+ var ref$, i$, len$, r, j$, ref1$, len1$, k, ref2$, p;
377
+ rules == null && (rules = []);
378
+ this.clear();
379
+ this._.rules = rules.map(function(r, i){
369
380
  var ref$;
370
- return ref$ = import$({}, c), ref$.id = c.id || "_c" + i, ref$;
381
+ return ref$ = import$({}, r), ref$.id = r.id || "_r" + (i + 1), ref$;
371
382
  });
372
- this._.results = {};
373
- return this;
374
- }, ref$.eval = function(cond){
375
- var sub, cached, src, widgets, vals, terms, this$ = this;
376
- if (cond == null) {
377
- return Promise.resolve(false);
383
+ ref$ = this._;
384
+ ref$.hash = {};
385
+ ref$.result = {};
386
+ ref$.dep = {};
387
+ ref$.tgt = {};
388
+ for (i$ = 0, len$ = (ref$ = this._.rules).length; i$ < len$; ++i$) {
389
+ r = ref$[i$];
390
+ this._.hash[r.id] = r;
391
+ for (j$ = 0, len1$ = (ref1$ = form.conditor.deps(r)).length; j$ < len1$; ++j$) {
392
+ k = ref1$[j$];
393
+ ((ref2$ = this._.dep)[k] || (ref2$[k] = [])).push(r.id);
394
+ }
395
+ for (j$ = 0, len1$ = (ref1$ = form.conditor.targets(r)).length; j$ < len1$; ++j$) {
396
+ p = ref1$[j$];
397
+ k = typeof p[0] === 'string' && !form.conditor.isSelector(p[0]) ? p[0] : '*';
398
+ ((ref2$ = this._.tgt)[k] || (ref2$[k] = [])).push(r.id);
399
+ }
378
400
  }
379
- if (typeof cond === 'function') {
380
- return Promise.resolve().then(function(){
381
- return !!cond.call(this$);
382
- });
401
+ return this.run();
402
+ }, ref$.rules = function(){
403
+ return this._.rules;
404
+ }, ref$.result = function(id){
405
+ if (arguments.length) {
406
+ return this._.result[id];
407
+ } else {
408
+ return import$({}, this._.result);
383
409
  }
384
- if (Array.isArray(cond)) {
385
- return Promise.all(cond.map(function(it){
386
- return this$.eval(it);
387
- })).then(function(it){
388
- return it.every(function(it){
389
- return it;
390
- });
391
- });
410
+ }, ref$.clear = function(){
411
+ var this$ = this;
412
+ this._.touched.forEach(function(w){
413
+ var k;
414
+ if (!(w._conditor && w._conditor.fx)) {
415
+ return;
416
+ }
417
+ for (k in w._conditor.fx) {
418
+ if (k.startsWith(this$._.id + "/")) {
419
+ delete w._conditor.fx[k];
420
+ }
421
+ }
422
+ return form.conditor.merge(w);
423
+ });
424
+ return this._.touched = new Set();
425
+ }, ref$._schedule = function(key){
426
+ var this$ = this;
427
+ this._.pending = this._.pending || new Set();
428
+ this._.pending.add(key != null ? key : '*');
429
+ if (this._.pending.size > 1) {
430
+ return;
392
431
  }
393
- if (cond.logic) {
394
- sub = Array.isArray(cond.cond)
395
- ? cond.cond
396
- : [cond.cond];
397
- return Promise.all(sub.map(function(it){
398
- return this$.eval(it);
399
- })).then(function(rs){
400
- switch (cond.logic) {
401
- case 'and':
402
- return rs.every(function(it){
403
- return it;
404
- });
405
- case 'or':
406
- return rs.some(function(it){
407
- return it;
408
- });
409
- case 'xor':
410
- return rs.filter(function(it){
411
- return it;
412
- }).length === 1;
413
- case 'not':
414
- return !rs[0];
415
- default:
416
- return false;
432
+ return Promise.resolve().then(function(){
433
+ var keys, ids, i$, ref$, len$, k, j$, ref1$, len1$, id;
434
+ keys = Array.from(this$._.pending);
435
+ this$._.pending = null;
436
+ if (in$('*', keys)) {
437
+ return this$.run();
438
+ }
439
+ ids = new Set();
440
+ for (i$ = 0, len$ = (ref$ = keys.concat(['*'])).length; i$ < len$; ++i$) {
441
+ k = ref$[i$];
442
+ for (j$ = 0, len1$ = (ref1$ = (this$._.dep[k] || []).concat(this$._.tgt[k] || [])).length; j$ < len1$; ++j$) {
443
+ id = ref1$[j$];
444
+ ids.add(id);
417
445
  }
418
- });
446
+ }
447
+ return this$.run(Array.from(ids));
448
+ });
449
+ }, ref$.run = function(ids){
450
+ var list, queue, seen, r, i$, ref$, len$, o, result, stack, evalRule, this$ = this;
451
+ list = ids
452
+ ? ids.map(function(it){
453
+ return this$._.hash[it];
454
+ }).filter(function(it){
455
+ return it;
456
+ })
457
+ : this._.rules.slice();
458
+ if (ids) {
459
+ queue = list.slice();
460
+ seen = new Set(list.map(function(it){
461
+ return it.id;
462
+ }));
463
+ while (queue.length) {
464
+ r = queue.shift();
465
+ for (i$ = 0, len$ = (ref$ = this._.rules).length; i$ < len$; ++i$) {
466
+ o = ref$[i$];
467
+ if (!seen.has(o.id) && (o.precond === r.id || in$(r.id, form.conditor.refs(o)))) {
468
+ seen.add(o.id);
469
+ list.push(o);
470
+ queue.push(o);
471
+ }
472
+ }
473
+ }
419
474
  }
420
- if (cond.src != null && typeof cond.src === 'string' && (cached = this._.results[cond.src]) != null) {
421
- return Promise.resolve(cached);
475
+ result = {};
476
+ stack = [];
477
+ evalRule = function(r){
478
+ var ret, e;
479
+ if (result[r.id] != null) {
480
+ return result[r.id];
481
+ }
482
+ if (in$(r.id, stack)) {
483
+ console.error("[@plotdb/form] conditor: circular reference in rule `" + r.id + "`");
484
+ return false;
485
+ }
486
+ stack.push(r.id);
487
+ ret = r.precond && this$._.hash[r.precond] && !evalRule(this$._.hash[r.precond])
488
+ ? false
489
+ : (function(){
490
+ try {
491
+ return form.conditor.truthy(this.eval(r.when, {
492
+ rule: evalRule
493
+ }));
494
+ } catch (e$) {
495
+ e = e$;
496
+ console.error("[@plotdb/form] conditor: rule `" + r.id + "` failed:", e);
497
+ return false;
498
+ }
499
+ }.call(this$));
500
+ stack.pop();
501
+ return this$._.result[r.id] = result[r.id] = ret;
502
+ };
503
+ for (i$ = 0, len$ = list.length; i$ < len$; ++i$) {
504
+ r = list[i$];
505
+ this._apply(r, evalRule(r));
422
506
  }
423
- if (cond.func != null) {
424
- return Promise.resolve().then(function(){
425
- return !!cond.func.call(this$);
507
+ return result;
508
+ }, ref$.eval = function(x, ctx){
509
+ var op, a, v, r, i$, to$, i, ret, b, c, list, test, this$ = this;
510
+ ctx == null && (ctx = {});
511
+ if (typeof x === 'function') {
512
+ return x.call(this, ctx);
513
+ }
514
+ if (Array.isArray(x)) {
515
+ return x.map(function(it){
516
+ return this$.eval(it, ctx);
426
517
  });
427
518
  }
428
- if (cond.src != null) {
429
- src = Array.isArray(cond.src)
430
- ? cond.src
431
- : [cond.src];
432
- widgets = this._.manager.resolve(src);
433
- if (!widgets.length) {
434
- return Promise.resolve(false);
435
- }
436
- if (widgets.some(function(w){
437
- return w.disabled();
438
- })) {
439
- return Promise.resolve(false);
440
- }
441
- if (cond.values != null) {
442
- vals = Array.isArray(cond.values)
443
- ? cond.values
444
- : [cond.values];
445
- return Promise.resolve(widgets.every(function(w){
446
- var content;
447
- content = w.content();
448
- content = Array.isArray(content)
449
- ? content
450
- : [content];
451
- return content.some(function(it){
452
- return in$(it, vals);
453
- });
454
- }));
519
+ if (!(x && typeof x === 'object')) {
520
+ return x;
521
+ }
522
+ op = Object.keys(x)[0];
523
+ a = x[op];
524
+ a = Array.isArray(a)
525
+ ? a
526
+ : [a];
527
+ v = function(i){
528
+ return this$.eval(a[i], ctx);
529
+ };
530
+ switch (op) {
531
+ case 'var':
532
+ if (ctx.data != null) {
533
+ return form.conditor.dig(ctx.data, a.length === 1 ? a[0] : a);
534
+ }
535
+ return this.value(a);
536
+ case 'rule':
537
+ if (!(r = this._.hash[v(0)])) {
538
+ return false;
539
+ } else if (ctx.rule) {
540
+ return ctx.rule(r);
541
+ } else {
542
+ return !!this._.result[r.id];
455
543
  }
456
- if (cond.term && cond.term.length) {
457
- terms = cond.term.map(function(t){
458
- if (t instanceof form.term) {
459
- return t;
460
- } else {
461
- return new form.term(t);
462
- }
463
- });
464
- return Promise.all(widgets.map(function(w){
465
- var v;
466
- v = w.content();
467
- return Promise.all(terms.map(function(t){
468
- return t.validate(v);
469
- })).then(function(it){
470
- return it.every(function(it){
471
- return it !== false;
472
- });
473
- });
474
- })).then(function(it){
475
- return it.every(function(it){
476
- return it;
477
- });
478
- });
544
+ case 'and':
545
+ for (i$ = 0, to$ = a.length; i$ < to$; ++i$) {
546
+ i = i$;
547
+ if (!form.conditor.truthy(ret = v(i))) {
548
+ return ret;
549
+ }
479
550
  }
480
- return Promise.resolve(widgets.every(function(w){
481
- return !w.isEmpty();
482
- }));
483
- }
484
- return Promise.resolve(false);
485
- }, ref$.apply = function(effect, active){
486
- var targets, i$, len$, target, lresult$, path, j$, ref$, len1$, w, cur, next, changed, results$ = [];
487
- targets = effect.targets || (effect.target
488
- ? [effect.target]
489
- : []);
490
- for (i$ = 0, len$ = targets.length; i$ < len$; ++i$) {
491
- target = targets[i$];
492
- lresult$ = [];
493
- path = Array.isArray(target)
494
- ? target
495
- : [target];
496
- if (typeof path[0] === 'string' && path[0].startsWith('$')) {
497
- this._.virtual[path[0]] = active;
498
- continue;
499
- }
500
- for (j$ = 0, len1$ = (ref$ = this._.manager.resolve(path)).length; j$ < len1$; ++j$) {
501
- w = ref$[j$];
502
- cur = w.serialize();
503
- next = JSON.parse(JSON.stringify(cur));
504
- if (effect.enabled != null) {
505
- next.disabled = !!(!effect.enabled !== !active && (effect.enabled || active));
506
- }
507
- if (effect.isRequired != null) {
508
- next.isRequired = !(!effect.isRequired !== !active && (effect.isRequired || active));
509
- }
510
- if (effect.readonly != null) {
511
- next.readonly = !(!effect.readonly !== !active && (effect.readonly || active));
512
- }
513
- changed = !!cur.disabled !== !!next.disabled || !!cur.isRequired !== !!next.isRequired || !!cur.readonly !== !!next.readonly;
514
- if (changed) {
515
- w.deserialize(next, {
516
- init: true
517
- });
551
+ return ret;
552
+ case 'or':
553
+ for (i$ = 0, to$ = a.length; i$ < to$; ++i$) {
554
+ i = i$;
555
+ if (form.conditor.truthy(ret = v(i))) {
556
+ return ret;
518
557
  }
519
- if (effect.value != null && active) {
520
- lresult$.push(w.value(effect.value));
558
+ }
559
+ return ret;
560
+ case '!':
561
+ return !form.conditor.truthy(v(0));
562
+ case '!!':
563
+ return form.conditor.truthy(v(0));
564
+ case 'if':
565
+ for (i$ = 0, to$ = a.length - 1; i$ < to$; i$ += 2) {
566
+ i = i$;
567
+ if (form.conditor.truthy(v(i))) {
568
+ return v(i + 1);
521
569
  }
522
570
  }
571
+ if (a.length % 2) {
572
+ return v(a.length - 1);
573
+ } else {
574
+ return null;
575
+ }
576
+ case '==':
577
+ return v(0) == v(1);
578
+ case '!=':
579
+ return v(0) != v(1);
580
+ case '===':
581
+ return v(0) === v(1);
582
+ case '!==':
583
+ return v(0) !== v(1);
584
+ case '<':
585
+ return v(0) < v(1) && (a.length < 3 || v(1) < v(2));
586
+ case '<=':
587
+ return v(0) <= v(1) && (a.length < 3 || v(1) <= v(2));
588
+ case '>':
589
+ return v(0) > v(1);
590
+ case '>=':
591
+ return v(0) >= v(1);
592
+ case 'in':
593
+ b = v(1);
594
+ if (Array.isArray(b) || typeof b === 'string') {
595
+ return b.indexOf(v(0)) >= 0;
596
+ } else {
597
+ return false;
598
+ }
599
+ case 'empty':
600
+ c = v(0);
601
+ return c == null || c === '' || (Array.isArray(c) && !c.length);
602
+ case 'some':
603
+ case 'all':
604
+ case 'none':
605
+ list = a[0] && typeof a[0] === 'object' && a[0]['var'] != null
606
+ ? ctx.data != null
607
+ ? form.conditor.dig(ctx.data, a[0]['var'])
608
+ : this.values(a[0]['var'])
609
+ : v(0);
610
+ list = Array.isArray(list)
611
+ ? list
612
+ : list != null
613
+ ? [list]
614
+ : [];
615
+ test = function(item){
616
+ var ref$;
617
+ return form.conditor.truthy(this$.eval(a[1], (ref$ = import$({}, ctx), ref$.data = item, ref$)));
618
+ };
619
+ if (op === 'some') {
620
+ return list.some(test);
621
+ } else if (op === 'none') {
622
+ return !list.some(test);
623
+ } else {
624
+ return !!list.length && list.every(test);
625
+ }
626
+ default:
627
+ throw new Error("unsupported operator `" + op + "`");
628
+ }
629
+ }, ref$.value = function(path){
630
+ var ws;
631
+ path = Array.isArray(path)
632
+ ? path
633
+ : [path];
634
+ if (form.conditor.fan(path)) {
635
+ return this.values(path);
636
+ }
637
+ ws = this._.manager.resolve(path);
638
+ if (ws.length === 1) {
639
+ return ws[0].content();
640
+ } else if (ws.length) {
641
+ return this.values(path);
642
+ } else {
643
+ return null;
644
+ }
645
+ }, ref$.values = function(path){
646
+ var ret, i$, ref$, len$, w, c;
647
+ ret = [];
648
+ for (i$ = 0, len$ = (ref$ = this._.manager.resolve(Array.isArray(path)
649
+ ? path
650
+ : [path])).length; i$ < len$; ++i$) {
651
+ w = ref$[i$];
652
+ c = w.content();
653
+ if (Array.isArray(c)) {
654
+ ret = ret.concat(c);
655
+ } else if (c != null) {
656
+ ret.push(c);
657
+ }
658
+ }
659
+ return ret;
660
+ }, ref$._apply = function(rule, active){
661
+ var fx, state, i$, ref$, len$, k, key, p, lresult$, j$, ref1$, len1$, w, ref2$, results$ = [];
662
+ fx = rule.effect || {};
663
+ state = {};
664
+ if (fx.enabled != null) {
665
+ state.disabled = !!(!fx.enabled !== !active && (fx.enabled || active));
666
+ } else if (fx.disabled != null) {
667
+ state.disabled = !(!fx.disabled !== !active && (fx.disabled || active));
668
+ }
669
+ for (i$ = 0, len$ = (ref$ = ['isRequired', 'readonly']).length; i$ < len$; ++i$) {
670
+ k = ref$[i$];
671
+ if (fx[k] != null) {
672
+ state[k] = !(!fx[k] !== !active && (fx[k] || active));
673
+ }
674
+ }
675
+ key = this._.id + "/" + rule.id;
676
+ for (i$ = 0, len$ = (ref$ = form.conditor.targets(rule)).length; i$ < len$; ++i$) {
677
+ p = ref$[i$];
678
+ lresult$ = [];
679
+ for (j$ = 0, len1$ = (ref1$ = this._.manager.resolve(p)).length; j$ < len1$; ++j$) {
680
+ w = ref1$[j$];
681
+ ((ref2$ = w._conditor || (w._conditor = {})).fx || (ref2$.fx = {}))[key] = state;
682
+ this._.touched.add(w);
683
+ lresult$.push(form.conditor.merge(w));
684
+ }
523
685
  results$.push(lresult$);
524
686
  }
525
687
  return results$;
526
- }, ref$.run = function(){
527
- var results, evalWithDeps, this$ = this;
528
- results = this._.results = {};
529
- evalWithDeps = function(cond){
530
- var pre, parent;
531
- if (results[cond.id] != null) {
532
- return Promise.resolve(results[cond.id]);
533
- }
534
- pre = Promise.resolve();
535
- if (cond.precond) {
536
- parent = this$._.conditions.find(function(it){
537
- return it.id === cond.precond;
538
- });
539
- if (parent) {
540
- pre = evalWithDeps(parent);
688
+ }, ref$);
689
+ form.conditor.merge = function(w){
690
+ var lc, cur, next, i$, ref$, len$, k, vs, res$, id, ref1$, s;
691
+ lc = w._conditor;
692
+ lc.base = lc.base || {};
693
+ cur = w.serialize();
694
+ next = JSON.parse(JSON.stringify(cur));
695
+ for (i$ = 0, len$ = (ref$ = ['disabled', 'isRequired', 'readonly']).length; i$ < len$; ++i$) {
696
+ k = ref$[i$];
697
+ res$ = [];
698
+ for (id in ref1$ = lc.fx) {
699
+ s = ref1$[id];
700
+ if (s[k] != null) {
701
+ res$.push(s[k]);
541
702
  }
542
703
  }
543
- return pre.then(function(){
544
- if (cond.precond != null && !results[cond.precond]) {
545
- return results[cond.id] = false;
704
+ vs = res$;
705
+ if (vs.length) {
706
+ if (!(k in lc.base)) {
707
+ lc.base[k] = !!cur[k];
546
708
  }
547
- return this$.eval(cond.when).then(function(active){
548
- results[cond.id] = active;
549
- if (cond.effect) {
550
- this$.apply(cond.effect, active);
551
- }
552
- return active;
709
+ next[k] = vs.some(fn$);
710
+ } else if (k in lc.base) {
711
+ next[k] = lc.base[k];
712
+ delete lc.base[k];
713
+ }
714
+ }
715
+ if (['disabled', 'isRequired', 'readonly'].every(function(k){
716
+ return !!cur[k] === !!next[k];
717
+ })) {
718
+ return;
719
+ }
720
+ return w.deserialize(next, {
721
+ init: true
722
+ });
723
+ function fn$(it){
724
+ return it;
725
+ }
726
+ };
727
+ form.conditor.truthy = function(v){
728
+ if (Array.isArray(v)) {
729
+ return !!v.length;
730
+ } else {
731
+ return !!v;
732
+ }
733
+ };
734
+ form.conditor.dig = function(o, p){
735
+ var i$, ref$, len$, k;
736
+ if (p === '' || p == null) {
737
+ return o;
738
+ }
739
+ for (i$ = 0, len$ = (ref$ = Array.isArray(p)
740
+ ? p
741
+ : (p + "").split('.')).length; i$ < len$; ++i$) {
742
+ k = ref$[i$];
743
+ if (!(o != null && typeof o === 'object')) {
744
+ return null;
745
+ }
746
+ o = o[k];
747
+ }
748
+ return o;
749
+ };
750
+ form.conditor.isSelector = function(s){
751
+ return typeof s === 'number' || (typeof s === 'string' && !!/^(\*$|#|:|\[)/.exec(s));
752
+ };
753
+ form.conditor.fan = function(p){
754
+ return p.some(function(s){
755
+ return form.conditor.isSelector(s) && typeof s !== 'number' && !/^#/.exec(s);
756
+ });
757
+ };
758
+ form.conditor.targets = function(r){
759
+ return ((r.effect || {}).targets || []).map(function(it){
760
+ if (Array.isArray(it)) {
761
+ return it;
762
+ } else {
763
+ return [it];
764
+ }
765
+ });
766
+ };
767
+ form.conditor.deps = function(r){
768
+ var ret, walk;
769
+ ret = new Set();
770
+ walk = function(x, inner){
771
+ var op, a, p;
772
+ if (typeof x === 'function') {
773
+ return ret.add('*');
774
+ }
775
+ if (Array.isArray(x)) {
776
+ return x.map(function(it){
777
+ return walk(it, inner);
553
778
  });
554
- });
779
+ }
780
+ if (!(x && typeof x === 'object')) {
781
+ return;
782
+ }
783
+ op = Object.keys(x)[0];
784
+ a = x[op];
785
+ if (op === 'var') {
786
+ if (inner) {
787
+ return;
788
+ }
789
+ p = Array.isArray(a)
790
+ ? a
791
+ : [a];
792
+ return ret.add(typeof p[0] === 'string' && !form.conditor.isSelector(p[0]) ? p[0] : '*');
793
+ }
794
+ a = Array.isArray(a)
795
+ ? a
796
+ : [a];
797
+ if (op === 'some' || op === 'all' || op === 'none') {
798
+ walk(a[0], inner);
799
+ return walk(a.slice(1), true);
800
+ } else {
801
+ return walk(a, inner);
802
+ }
555
803
  };
556
- return Promise.all(this._.conditions.map(function(c){
557
- return evalWithDeps(c);
558
- }));
559
- }, ref$);
804
+ walk(r.when, false);
805
+ return Array.from(ret);
806
+ };
807
+ form.conditor.refs = function(r){
808
+ var ret, walk;
809
+ ret = [];
810
+ walk = function(x){
811
+ var k, v, results$ = [];
812
+ if (Array.isArray(x)) {
813
+ return x.map(walk);
814
+ }
815
+ if (!(x && typeof x === 'object')) {
816
+ return;
817
+ }
818
+ for (k in x) {
819
+ v = x[k];
820
+ if (k === 'rule' && typeof v === 'string') {
821
+ results$.push(ret.push(v));
822
+ } else {
823
+ results$.push(walk(v));
824
+ }
825
+ }
826
+ return results$;
827
+ };
828
+ walk(r.when);
829
+ return ret;
830
+ };
560
831
  form.manager = function(o){
561
832
  var this$ = this;
562
833
  o == null && (o = {});
@@ -571,9 +842,6 @@
571
842
  this._status = 1;
572
843
  this._mode = 'edit';
573
844
  this._mod = {};
574
- this._cond = new form.condctrl({
575
- manager: this
576
- });
577
845
  this._checkDebounced = debounce(10, function(){
578
846
  var args, res$, i$, to$;
579
847
  res$ = [];
@@ -605,7 +873,14 @@
605
873
  });
606
874
  },
607
875
  condition: function(){
608
- return this._cond;
876
+ return this._cond || (this._cond = new form.condctrl({
877
+ manager: this
878
+ }));
879
+ },
880
+ conditor: function(){
881
+ return this._conditor || (this._conditor = new form.conditor({
882
+ manager: this
883
+ }));
609
884
  },
610
885
  disabled: function(){
611
886
  return !!this._disabled;
@@ -1175,19 +1450,58 @@
1175
1450
  return ret;
1176
1451
  },
1177
1452
  resolve: function(path){
1178
- var head, tail, w;
1453
+ var head, tail, ws, that;
1179
1454
  if (!path || !path.length) {
1180
1455
  return [];
1181
1456
  }
1182
1457
  head = path[0], tail = slice$.call(path, 1);
1183
- w = this._ws.w[head];
1184
- if (!w) {
1458
+ ws = form.conditor.isSelector(head)
1459
+ ? this._select(head)
1460
+ : (that = this._ws.w[head])
1461
+ ? [that]
1462
+ : [];
1463
+ if (!tail.length) {
1464
+ return ws;
1465
+ }
1466
+ return ws.map(function(it){
1467
+ return it.resolve(tail);
1468
+ }).reduce(function(a, b){
1469
+ return a.concat(b);
1470
+ }, []);
1471
+ },
1472
+ _select: function(s){
1473
+ var k, v, m, that;
1474
+ if (s === '*') {
1475
+ return (function(){
1476
+ var ref$, results$ = [];
1477
+ for (k in ref$ = this._ws.w) {
1478
+ v = ref$[k];
1479
+ results$.push(v);
1480
+ }
1481
+ return results$;
1482
+ }.call(this));
1483
+ }
1484
+ if (typeof s !== 'string') {
1185
1485
  return [];
1186
1486
  }
1187
- if (!tail.length) {
1188
- return [w];
1487
+ if (m = /^#(.+)$/.exec(s)) {
1488
+ return (that = this._ws.w[m[1]])
1489
+ ? [that]
1490
+ : [];
1491
+ }
1492
+ if (m = /^\[tag~=["']?([^"'\]]+)["']?\]$/.exec(s)) {
1493
+ return (function(){
1494
+ var ref$, results$ = [];
1495
+ for (k in ref$ = this._ws.w) {
1496
+ v = ref$[k];
1497
+ results$.push(v);
1498
+ }
1499
+ return results$;
1500
+ }.call(this)).filter(function(it){
1501
+ return in$(m[1], it._meta.tag || []);
1502
+ });
1189
1503
  }
1190
- return w.resolve(tail);
1504
+ return [];
1191
1505
  },
1192
1506
  paths: function(path){
1193
1507
  var k, v, head, tail, w;
package/index.min.js CHANGED
@@ -1 +1 @@
1
- (function(){var form,ref$,wordLen,countOps,slice$=[].slice;form={};form.value={toString:function(v){var list2str,parts;list2str=function(list){if(list[0]&&list[0].key!=null&&list[0].value!=null){return list.map(function(it){return form.value.toString(it.value)}).join(" / ")}else{return list.map(function(it){return form.value.toString(it)}).join(" / ")}};if(typeof v==="string"){return v}else if(v==null){return""}else if(Array.isArray(v)){return list2str(v)}else if(v.filename){return v.filename}else if(v.key!=null&&v.value!=null){return form.value.toString(v.value)}else if(v.list&&v.other){parts=(v.list||[]).map(function(it){return form.value.toString(it)});if(v.other.enabled&&v.other.text){parts=parts.concat([v.other.text])}return parts.join(" / ")}else if(v.list&&Array.isArray(v.list)){return list2str(v.list)}else if(v.v!=null){return form.value.toString(v.v)}else{return JSON.stringify(v)}}};form.utils={lexCompare:function(a,b){var i$,to$,i;for(i$=0,to$=Math.max(a.length,b.length);i$<to$;++i$){i=i$;if(a[i]==null&&b[i]!=null){return-1}if(a[i]!=null&&b[i]==null){return 1}if(a[i]==null&&b[i]==null){return 0}if(a[i]!==b[i]){return a[i]-b[i]}}return 0},mergeExports:function(records){var uidMap,headers,k,v,rows;uidMap={};records.map(function(cols){return cols.map(function(col){if(!uidMap[col.uid]){return uidMap[col.uid]={uid:col.uid,header:col.header,sortKey:col.sortKey}}})});headers=function(){var ref$,results$=[];for(k in ref$=uidMap){v=ref$[k];results$.push({k:k,v:v})}return results$}().sort(function(a,b){return form.utils.lexCompare(a.v.sortKey,b.v.sortKey)});rows=records.map(function(cols){var valMap;valMap={};cols.map(function(it){return valMap[it.uid]=it.value});return headers.map(function(it){return valMap[it.k]||""})});return{headers:headers.map(function(it){return it.v.header}),rows:rows}}};form.condctrl=function(opt){var ref$,this$=this;opt==null&&(opt={});ref$=this._||(this._={});ref$.hash={};ref$.enabled={};ref$.manager=opt.manager;ref$.list=opt.conditions||[];ref$.autorun=false;ref$.applyBaseRule=opt.baseRule||function(){};this._.manager.on("change",function(){if(this$._.autorun){return this$.run()}});return this};form.condctrl.prototype=(ref$=Object.create(Object.prototype),ref$.isEnabled=function(it){return this._.enabled[it]},ref$.get=function(it){return this._.hash[it]},ref$.reset=function(opt){var fields,ref$,i$,to$,i,cond,results$=[];opt==null&&(opt={});this._.autorun=opt.autorun!=null&&!opt.autorun?false:true;this._.list=opt.conditions||this._.list||[];fields=this._.manager.widgets();ref$=this._||(this._={});ref$.hash={};ref$.enabled={};for(i$=0,to$=this._.list.length;i$<to$;++i$){i=i$;cond=this._.list[i];cond.id=cond.id||"_"+(i+1);this._.hash[cond.id]=cond;if(!Array.isArray(cond.config)){cond.config=[cond.config]}cond.config=cond.config.map(fn$);results$.push(cond.config.forEach(fn1$))}return results$;function fn$(it){var ref$;return ref$=import$({},JSON.parse(JSON.stringify(it))),ref$.src=cond.src,ref$.func=cond.func,ref$}function fn1$(cfg){var k,v;if(!cfg.path&&cfg.targets){cfg.path=cfg.targets}if(!cfg.value&&cfg.values){cfg.value=cfg.values}if(!cfg.tag&&cfg.tags){cfg.tag=cfg.tags}return cfg.path=Array.from(new Set((cfg.prefix?cfg.prefix:[]).concat(cfg.path||[],function(){var ref$,results$=[];for(k in ref$=fields){v=ref$[k];results$.push({k:k,v:v})}return results$}().filter(function(arg$){var k,v,ref$;k=arg$.k,v=arg$.v;return(!cfg.prefix?0:cfg.prefix.filter(function(p){return k.startsWith(p)}).length)+((v!=null?(ref$=v.meta)!=null?ref$.tag:void 8:void 8)||[]).filter(function(t){return in$(t,cfg.tag||[])}).length>0}).map(function(it){return it.k}))))}},ref$.subcond=function(arg$){var path,active,config,itf,ms;path=arg$.path,active=arg$.active,config=arg$.config;if(!((itf=this._.manager.widget(path))&&!itf.disabled()&&(ms=itf.manager({depth:1})||[]).length)){return}return ms.forEach(function(m){var cond;cond=m.condition();cond.apply(import$({active:active},config));return cond.run()})},ref$.apply=function(opt){var path,active,enabled,isRequired,readonly,widget,curMeta,newMeta;opt==null&&(opt={});path=opt.path,active=opt.active,enabled=opt.enabled,isRequired=opt.isRequired,readonly=opt.readonly;if(Array.isArray(path)){if(path[1]){return this.subcond({path:path[0],active:active,config:{path:path.slice(1),enabled:enabled,isRequired:isRequired,readonly:readonly}})}path=path[0]}if(enabled!=null){this._.enabled[path]=!(!enabled!==!active&&(enabled||active))}if(widget=this._.manager.widget(path)){curMeta=widget.serialize();newMeta=JSON.parse(JSON.stringify(curMeta));if(enabled!=null){newMeta.disabled=!!(!enabled!==!active&&(enabled||active))}if(readonly!=null){newMeta.readonly=!(!readonly!==!active&&(readonly||active))}if(isRequired!=null){newMeta.isRequired=!(!isRequired!==!active&&(isRequired||active))}this._.applyBaseRule({path:path,widget:widget,meta:newMeta,opt:opt});if(!!curMeta.disabled===!!newMeta.disabled&&!!curMeta.isRequired===!!newMeta.isRequired&&!!curMeta.readonly===!!newMeta.readonly){return}return widget.deserialize(newMeta,{init:true})}},ref$._run=function(cfg,precond){var src,values,paths,isRequired,enabled,readonly,func,result,i$,len$,path,active,ref$,itf,content;cfg==null&&(cfg={});src=cfg.src,values=cfg.value,paths=cfg.path,isRequired=cfg.isRequired,enabled=cfg.enabled,readonly=cfg.readonly,func=cfg.func;if(func){result=true;for(i$=0,len$=paths.length;i$<len$;++i$){path=paths[i$];active=!!func.apply(this,[(ref$=import$({},cfg),ref$.path=path,ref$)])&&!(precond!=null&&!precond);result=result&&active;if(Array.isArray(path)&&path[1]){this.subcond({path:path[0],config:(ref$=import$({},cfg),ref$.path=path.slice(1),ref$),active:active});continue}this.apply({path:path,enabled:enabled,active:active,isRequired:isRequired,readonly:readonly})}}else{values=Array.isArray(values)?values:[values];if(!(itf=this._.manager.widget(src))){return console.error("[@plotdb/form] condctrl: field '"+src+"' not found")}content=itf.content();content=Array.isArray(content)?content:[content];active=!!content.filter(function(it){return in$(it,values)}).length;if(precond!=null&&!precond){active=false}for(i$=0,len$=paths.length;i$<len$;++i$){path=paths[i$];if(Array.isArray(path)&&path[1]){this.subcond({path:path[0],config:(ref$=import$({},cfg),ref$.path=path.slice(1),ref$),active:active});continue}this.apply({path:path,enabled:enabled,active:active,isRequired:isRequired,readonly:readonly})}result=active}return result},ref$.run=function(){var result,_,this$=this;result={};_=function(list,ref){var i$,to$,i,lresult$,cond,j$,ref$,len$,cfg,r,results$=[];ref==null&&(ref=[]);for(i$=0,to$=list.length;i$<to$;++i$){i=i$;lresult$=[];cond=list[i];if(result[cond.id]!=null){continue}if(cond.precond&&this$._.hash[cond.precond]){if(in$(cond.id,ref)){console.error("[@plotdb/form] condctrl: circular ref id "+cond.id)}else{_([this$._.hash[cond.precond]],ref.concat([cond.id]))}}for(j$=0,len$=(ref$=cond.config).length;j$<len$;++j$){cfg=ref$[j$];r=this$._run(cfg,result[cond.precond]);lresult$.push(result[cond.id]=result[cond.id]!=null?result[cond.id]&&r:r)}results$.push(lresult$)}return results$};return _(this._.list||[])},ref$);form.conditor=function(opt){var this$=this;opt==null&&(opt={});this._={manager:opt.manager,conditions:[],results:{},virtual:{}};this._.manager.on("change",debounce(50,function(){return this$.run()}));return this};form.conditor.prototype=(ref$=Object.create(Object.prototype),ref$.init=function(conditions){conditions==null&&(conditions=[]);this._.conditions=conditions.map(function(c,i){var ref$;return ref$=import$({},c),ref$.id=c.id||"_c"+i,ref$});this._.results={};return this},ref$.eval=function(cond){var sub,cached,src,widgets,vals,terms,this$=this;if(cond==null){return Promise.resolve(false)}if(typeof cond==="function"){return Promise.resolve().then(function(){return!!cond.call(this$)})}if(Array.isArray(cond)){return Promise.all(cond.map(function(it){return this$.eval(it)})).then(function(it){return it.every(function(it){return it})})}if(cond.logic){sub=Array.isArray(cond.cond)?cond.cond:[cond.cond];return Promise.all(sub.map(function(it){return this$.eval(it)})).then(function(rs){switch(cond.logic){case"and":return rs.every(function(it){return it});case"or":return rs.some(function(it){return it});case"xor":return rs.filter(function(it){return it}).length===1;case"not":return!rs[0];default:return false}})}if(cond.src!=null&&typeof cond.src==="string"&&(cached=this._.results[cond.src])!=null){return Promise.resolve(cached)}if(cond.func!=null){return Promise.resolve().then(function(){return!!cond.func.call(this$)})}if(cond.src!=null){src=Array.isArray(cond.src)?cond.src:[cond.src];widgets=this._.manager.resolve(src);if(!widgets.length){return Promise.resolve(false)}if(widgets.some(function(w){return w.disabled()})){return Promise.resolve(false)}if(cond.values!=null){vals=Array.isArray(cond.values)?cond.values:[cond.values];return Promise.resolve(widgets.every(function(w){var content;content=w.content();content=Array.isArray(content)?content:[content];return content.some(function(it){return in$(it,vals)})}))}if(cond.term&&cond.term.length){terms=cond.term.map(function(t){if(t instanceof form.term){return t}else{return new form.term(t)}});return Promise.all(widgets.map(function(w){var v;v=w.content();return Promise.all(terms.map(function(t){return t.validate(v)})).then(function(it){return it.every(function(it){return it!==false})})})).then(function(it){return it.every(function(it){return it})})}return Promise.resolve(widgets.every(function(w){return!w.isEmpty()}))}return Promise.resolve(false)},ref$.apply=function(effect,active){var targets,i$,len$,target,lresult$,path,j$,ref$,len1$,w,cur,next,changed,results$=[];targets=effect.targets||(effect.target?[effect.target]:[]);for(i$=0,len$=targets.length;i$<len$;++i$){target=targets[i$];lresult$=[];path=Array.isArray(target)?target:[target];if(typeof path[0]==="string"&&path[0].startsWith("$")){this._.virtual[path[0]]=active;continue}for(j$=0,len1$=(ref$=this._.manager.resolve(path)).length;j$<len1$;++j$){w=ref$[j$];cur=w.serialize();next=JSON.parse(JSON.stringify(cur));if(effect.enabled!=null){next.disabled=!!(!effect.enabled!==!active&&(effect.enabled||active))}if(effect.isRequired!=null){next.isRequired=!(!effect.isRequired!==!active&&(effect.isRequired||active))}if(effect.readonly!=null){next.readonly=!(!effect.readonly!==!active&&(effect.readonly||active))}changed=!!cur.disabled!==!!next.disabled||!!cur.isRequired!==!!next.isRequired||!!cur.readonly!==!!next.readonly;if(changed){w.deserialize(next,{init:true})}if(effect.value!=null&&active){lresult$.push(w.value(effect.value))}}results$.push(lresult$)}return results$},ref$.run=function(){var results,evalWithDeps,this$=this;results=this._.results={};evalWithDeps=function(cond){var pre,parent;if(results[cond.id]!=null){return Promise.resolve(results[cond.id])}pre=Promise.resolve();if(cond.precond){parent=this$._.conditions.find(function(it){return it.id===cond.precond});if(parent){pre=evalWithDeps(parent)}}return pre.then(function(){if(cond.precond!=null&&!results[cond.precond]){return results[cond.id]=false}return this$.eval(cond.when).then(function(active){results[cond.id]=active;if(cond.effect){this$.apply(cond.effect,active)}return active})})};return Promise.all(this._.conditions.map(function(c){return evalWithDeps(c)}))},ref$);form.manager=function(o){var this$=this;o==null&&(o={});this._ws={w:{},p:new WeakMap,s:{},l:{}};this._disabled=false;this._evthdr={};this._status=1;this._mode="edit";this._mod={};this._cond=new form.condctrl({manager:this});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)});this._restatusDebounced=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$._restatus.apply(this$,args)});return this};form.manager.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$._evthdr)[n]||(ref$[n]=[])).push(cb)})},condition:function(){return this._cond},disabled:function(){return!!this._disabled},disable:function(it){return this._disabled=it!=null?!!it:true},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.mode(this._mode);w.on("meta",this._ws.l[p].m=function(v){return this$.fire("meta",{widget:w,path:p,meta:v})});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;if(this$._ws.w[p]._meta.disabled){return}this$.fire("status",{widget:w,path:p,status:s});return this$._restatusDebounced()})},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}ws.off("meta",this._ws.l[o.path].m);ws.off("change",this._ws.l[o.path].c);ws.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$},widgets:function(){return import$({},this._ws.w||{})},widget:function(p){return this._ws.w[p]},content:function(p){var that;if(that=this._ws.w[p]){return that.content()}else{return null}},status:function(){return this._status},progress:function(arg$){var _recurred,ref$,ret,list,k,s,this$=this;_recurred=(ref$=(arg$!=null?arg$:{})._recurred)!=null?ref$:false;ret={total:0,invalid:0,done:0};list=function(){var ref$,results$=[];for(k in ref$=this._ws.s){s=ref$[k];results$.push({k:k,s:s})}return results$}.call(this).filter(function(it){return this$._ws.w[it.k]&&this$._ws.w[it.k]._meta&&!this$._ws.w[it.k]._meta.disabled});list.forEach(function(o){var w,ms,e;w=this$._ws.w[o.k];if(!_recurred&&(ms=w.manager()).length){ms=ms.filter(function(it){return!it.disabled()}).filter(function(m){var p;p=m.progress({_recurred:true});ret.total+=p.total;ret.invalid+=p.invalid;ret.done+=p.done;return!!p.invalid});if(ms.length){return}}ret.total+=1;e=w.errors();if(e.length===1&&e[0]==="nested"){return}if(o.s!=null&&o.s===2){ret.invalid+=1}if(o.s!=null&&o.s===0){return ret.done+=1}});ret.percent=!ret.total?1:ret.done/(ret.total||1);return ret},invalidWidgets:function(){var ret,list,k,s,this$=this;ret=[];list=function(){var ref$,results$=[];for(k in ref$=this._ws.s){s=ref$[k];results$.push({k:k,s:s})}return results$}.call(this).filter(function(it){return this$._ws.w[it.k]&&this$._ws.w[it.k]._meta&&!this$._ws.w[it.k]._meta.disabled});list.forEach(function(o){var w,ms,e;w=this$._ws.w[o.k];if((ms=w.manager()).length){ms.filter(function(it){return!it.disabled()}).forEach(function(m){return ret=ret.concat(m.invalidWidgets())});e=w.errors();if(o.s!=null&&o.s===2&&!(e.length===1&&e[0]==="nested")){ret.push({widget:w,path:o.k,status:2})}return}e=w.errors();if(e.length===1&&e[0]==="nested"){return}if(o.s!=null&&o.s===2){return ret.push({widget:w,path:o.k,status:2})}});return ret},_restatus:function(){var os,ret,k,v,this$=this;os=this._status;delete this._status;ret=function(){var ref$,results$=[];for(k in ref$=this._ws.w){v=ref$[k];results$.push({k:k,v:v})}return results$}.call(this).filter(function(arg$){var k,v;k=arg$.k,v=arg$.v;return!v._meta.disabled}).map(function(arg$){var k,v,s;k=arg$.k,v=arg$.v;s=this$._ws.s[k];if(s==null){return true}if(s===0){return false}if(s===1&&!v._meta.isRequired){return false}return true}).filter(function(it){return it}).length;this._status=ret?1:0;if(os!==this._status){return this.fire("readystatechange",this._status===0)}},check:function(o,opt){var this$=this;if(typeof opt!=="object"){opt={now:opt}}return Promise.resolve().then(function(){var p,w;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$).filter(function(arg$){var widget;widget=arg$.widget;if(widget._meta.disabled){return false}return!opt.skipEmpty?true:!widget.isEmpty()||widget.status()===2}),opt)}this$.checkList=(this$.checkList||(this$.checkList=[])).concat(Array.isArray(o)?o:[o]);return opt.now?this$._check(null,opt):this$._checkDebounced(null,opt)}).then(function(it){this$._restatus();return it})},_check:function(o,opt){var list,this$=this;opt==null&&(opt={});if(!o){list=this.checkList||[];this.checkList=[];return this._check(list,opt)}if(Array.isArray(o)){return Promise.all(o.map(function(it){return this$._check(it,opt)})).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({force:opt.force,init:opt.init,skipEmpty:opt.skipEmpty}).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]})}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];if(w._meta.disabled){continue}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,ps,res$,e;opt==null&&(opt={});if(!arguments.length){ret={};for(p in ref$=this._ws.w){w=ref$[p];if(!(opt.partial&&w._meta.disabled)){ret[p]=w.value()}}return ret}v=v!=null?JSON.parse(JSON.stringify(v)):{};try{res$=[];for(p in ref$=this._ws.w){w=ref$[p];if(!v.hasOwnProperty(p)&&opt.partial){continue}res$.push(w.value(v[p],opt))}ps=res$;return Promise.all(ps)}catch(e$){e=e$;return Promise.reject(e)}},mode:function(m){var this$=this;if(!(m!=null)){return this._mode}return Promise.resolve().then(function(){var ref$,p,w;if(!(m==="edit"||m==="view"||m==="config")){return Promise.reject((ref$=new Error,ref$.name="lderror",ref$.id=1015,ref$))}if(this$._mode===m){return}this$._mode=m;this$.fire("mode",m);return Promise.all(function(){var ref$,results$=[];for(p in ref$=this._ws.w){w=ref$[p];results$.push(w)}return results$}.call(this$).map(function(w){return w.mode(m)}))})},order:function(){var nodeIdx,_,obj;nodeIdx=function(r,n,opt){var i$,to$,i,v;opt.idx++;if(r===n){return true}if(!(r&&r.childNodes)){return}for(i$=0,to$=r.childNodes.length;i$<to$;++i$){i=i$;v=nodeIdx(r.childNodes[i],n,opt);if(v){return true}}};_=function(m,obj){var ws,k,v,ms,child,opt,results$=[];ws=m._ws.w;for(k in ws){v=ws[k];ms=v.manager()||[];if(ms.length){child={};ms.map(fn$)}else{child=null}opt={idx:0};nodeIdx(document.body,v._root,opt);results$.push(obj[k]={title:v._meta.title,idx:opt.idx,child:child,meta:v.serialize()})}return results$;function fn$(m){return _(m,child)}};_(this,obj={});return obj},export:function(){var attached,ws,res$,k,ref$,v,cols,i$,len$,rank;attached=function(w){return w._root&&w._root.isConnected};res$=[];for(k in ref$=this._ws.w){v=ref$[k];res$.push({k:k,v:v})}ws=res$;ws.sort(function(a,b){var ref$,ra,rb;ref$=[attached(a.v),attached(b.v)],ra=ref$[0],rb=ref$[1];if(!(ra&&rb)){return ra?-1:rb?1:0}if(a.v._root===b.v._root){return 0}if(a.v._root.compareDocumentPosition(b.v._root)&Node.DOCUMENT_POSITION_FOLLOWING){return-1}else{return 1}});cols=[];for(i$=0,len$=ws.length;i$<len$;++i$){rank=i$;ref$=ws[i$],k=ref$.k,v=ref$.v;cols=cols.concat(v["export"]().map(fn$))}return cols.sort(function(a,b){return form.utils.lexCompare(a.sortKey,b.sortKey)});function fn$(col){col.sortKey=[rank].concat(col.sortKey);col.uid=col.uid?k+"|"+col.uid:k;return col}},exportable:function(){var k,v;return function(){var ref$,results$=[];for(k in ref$=this._ws.w){v=ref$[k];results$.push(v)}return results$}.call(this).every(function(it){return it.exportable()})},settle:function(){var k,v;return Promise.all(function(){var ref$,results$=[];for(k in ref$=this._ws.w){v=ref$[k];results$.push(v.settle())}return results$}.call(this))},manager:function(opt){var ret,k,ref$,s,w,mgrs;ret=[];for(k in ref$=this._ws.s){s=ref$[k];if(!(this._ws.w[k]&&this._ws.w[k]._meta&&!this._ws.w[k]._meta.disabled)){continue}if(!(w=this.widget(k))){continue}if(!(mgrs=w.manager(opt)||[]).length){continue}ret=ret.concat(mgrs)}if(this.disabled()){ret.map(function(it){return it.disable()})}return ret},resolve:function(path){var head,tail,w;if(!path||!path.length){return[]}head=path[0],tail=slice$.call(path,1);w=this._ws.w[head];if(!w){return[]}if(!tail.length){return[w]}return w.resolve(tail)},paths:function(path){var k,v,head,tail,w;if(!path||!path.length){return function(){var ref$,results$=[];for(k in ref$=this._ws.w){v=ref$[k];results$.push({id:k,name:v._meta.title||k})}return results$}.call(this)}head=path[0],tail=slice$.call(path,1);w=this._ws.w[head];if(!w){return[]}return w.paths(tail)}});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),{getConfig:function(valspec){if(typeof this.config==="function"){return this.config(valspec)}else{return this.config}},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(valspec){var cfg,k,ref$,v;cfg={};for(k in ref$=this.getConfig(valspec)){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.valdef=opt.valdef||null;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.list=function(opt){var list,vd;opt==null&&(opt={});list=(this._list||[]).map(function(it){return it});if(!opt.valdef){return list}vd=Array.isArray(opt.valdef)?opt.valdef:[opt.valdef];return list.filter(function(os){if(!os.valdef){return!vd.length||vd.some(function(it){return!it||it==="@plotdb/form:valdef/generic"})}return vd.some(function(v){return in$(v,os.valdef)})})};wordLen=function(v,method){v==null&&(v="");return method==="simple-word"?v.split(/\s|[,.;:!?,。;:︰!?、. "]/).filter(function(it){return it}).map(function(it){return it.split(/[\u1000-\uffff]/).map(function(it){if(it.length){return 2}else{return 1}}).reduce(function(a,b){return a+b},0)-1}).reduce(function(a,b){return a+b},0):v.length};countOps={"count-max":{func:function(v,c){c==null&&(c={});return v.length<=c.val},config:{val:{type:"number",name:"count-max",hint:"maximal amount"}}},"count-min":{func:function(v,c){c==null&&(c={});return v.length>=c.val},config:{val:{type:"number",name:"count-min",hint:"minimal amount"}}},"count-range":{func:function(v,c){c==null&&(c={});return v.length>=c.min&&v.length<=c.max},config:{min:{type:"number",name:"count-min",hint:"minimal amount"},max:{type:"number",name:"count-max",hint:"maximal amount"}}},count:{func:function(v,c){c==null&&(c={});return v.length===c.val},config:{val:{type:"number",name:"count",hint:"number of entries"}}}};form.opset["default"]=[{id:"list",i18n:{"zh-TW":{list:"列表","count-max":"數量上限","count-min":"數量下限","count-range":"數量範圍",count:"數量要求"},en:{list:"List","count-max":"Maximal Count","count-min":"Minimal Count","count-range":"Count Range",count:"Required Count"}},convert:function(v){var ret;ret=Array.isArray(v)?v:v&&Array.isArray(v.list)?v.list.concat(v.other&&v.other.enabled?[v.other.text]:[]):[v];return ret.filter(function(it){return it})},ops:import$({},countOps)},{id:"file",i18n:{"zh-TW":{file:"檔案","max-size":"檔案大小上限(位元數)","min-size":"檔案大小下限(位元數)","count-limit":"檔案數量上限",extension:"副檔名限制","count-max":"檔案數量上限","count-min":"檔案數量下限","count-range":"檔案數量範圍"},en:{file:"File","max-size":"Max File Size (bytes)","min-size":"Min File Size (bytes)","count-limit":"Max File Count",extension:"File Extension","count-max":"Max File Count","count-min":"Min File Count","count-range":"File Count Range"}},convert:function(v){return(Array.isArray(v)?v:[v]).filter(function(it){return it})},ops:import$({"size-limit":{func:function(v,c){var max;c==null&&(c={});max=c.max!=null?c.max:c.size!=null?c.size:c.val;return!v.filter(function(it){return max!=null&&it.size>max||c.min!=null&&it.size<c.min}).length},config:{max:{type:"number",name:"max-size"},min:{type:"number",name:"min-size"}}},extension:{func:function(v,c){var exts;c==null&&(c={});exts=(c.str.split(",")||[]).map(function(it){return(it||"").trim().toLowerCase()}).filter(function(it){return it});return!v.filter(function(it){var ext,ref$;return!in$(((ext=(ref$=(it.filename||"").split("."))[ref$.length-1])||"").trim().toLowerCase(),exts)}).length},config:{str:{type:"text",name:"extension",hint:"extension, comma separated, without dot"}}},"count-limit":{func:function(v,c){c==null&&(c={});return v.length<c.val},config:{val:{type:"number",name:"count-limit",hint:"maximal file amount"}}}},countOps)},{id:"image",i18n:{"zh-TW":{image:"圖像","long-side":"長邊尺寸限制","short-side":"短邊尺寸限制",width:"寬度限制",height:"高度限制","pixel-count":"像素量限制","min-size":"最小尺寸","max-size":"最大尺寸"},en:{image:"Image","long-side":"Long Side","short-side":"Short Side",width:"Width",height:"Height","pixel-count":"Max Pixel Count","min-size":"Min Size","max-size":"Max Size"}},convert:function(v){return(Array.isArray(v)?v:[v]).filter(function(it){return it})},ops:Object.fromEntries([["long-side","long"],["short-side","short"],["width","width"],["height","height"],["pixel-count","pixels"]].map(function(n){return[n[0],function(k){return{func:function(v,c){v==null&&(v=[]);c==null&&(c={});return!v.filter(function(it){return!((c.min==null||it[k]>=(c.min||0))&&(c.max==null||it[k]<=c.max))}).length},config:{min:{type:"number",name:"min-size",hint:"minimal size"},max:{type:"number",name:"max-size",hint:"maximal size"}}}}(n[1])]}))},{id:"string",i18n:{"zh-TW":{string:"文字",include:"包含",exclude:"排除",email:"電子郵件",url:"網址",regex:"正規表達式"},en:{string:"Text",include:"Include",exclude:"Exclude",email:"Email",url:"URL",regex:"Regular Expression"}},ops:{include:{func:function(v,c){c==null&&(c={});return~(""+(v||"")).indexOf(c.str||"")},config:{str:{type:"text",name:"include"}}},exclude:{func:function(v,c){c==null&&(c={});return!~(""+(v||"")).indexOf(c.str||"")},config:{str:{type:"text",name:"exclude"}}},email:{func:function(v){return/^[^@]+@[^@]+$/.exec(v)},config:{}},url:{func:function(v){return/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,15}\b([-a-zA-Z0-9@:%_\+.~#()?&//=]*)$/.exec(v)},config:{}},regex:{func:function(v,c){c==null&&(c={});if(!c.rule){return true}if(typeof c.rule==="object"&&c.rule.exec){return c.rule.exec(v)}return new RegExp(c.rule).exec(v)},config:{rule:{type:"text",name:"regex"}}}}},{id:"length",i18n:{"zh-TW":{length:"字串長度",lte:"≦ 小於或等於",range:"範圍",number:"數字",method:"計算方式",char:"字元","simple-word":"簡易計字/詞","minimal length":"長度下限","maximal length":"長度上限"},en:{length:"String Length",lte:"≦",range:"Range",number:"Number",method:"Method",char:"By Character","simple-word":"Count Word","minimal length":"Min Length","maximal length":"Max Length"}},ops:{range:{func:function(v,c){var len;c==null&&(c={});len=wordLen(v+"",c.method);return len>=+c.min&&len<=+c.max},config:{min:{type:"number",name:"minimal length"},max:{type:"number",name:"maximal length"},method:{type:"choice",default:"char",values:["char","simple-word"]}}},lte:{func:function(v,c){c==null&&(c={});return wordLen(v+"",c.method)<=+c.val},config:{val:{type:"number",name:"maximal length"},method:{type:"choice",default:"char",values:["char","simple-word"]}}},gte:{func:function(v,c){c==null&&(c={});return wordLen(v+"",c.method)>=+c.val},config:{val:{type:"number",name:"minimal length"},method:{type:"choice",default:"char",values:["char","simple-word"]}}},eq:{func:function(v,c){c==null&&(c={});return wordLen(v+"",c.method)===+c.val},config:{val:{type:"number",name:"length"},method:{type:"choice",default:"char",values:["char","simple-word"]}}}}},{id:"number",i18n:{"zh-TW":{number:"數字",lte:"≦ 小於或等於",gte:"≧ 大於或等於",ne:"≠ 不等於",eq:"= 等於",is:"任何數字","ref-val":"參考值"},en:{number:"Number",lte:"≦ Equal/Less Than",gte:"≧ Equal/Greater Than",ne:"≠ Not Equals",eq:"= Equals",is:"Is Number","ref-val":"Reference Value"}},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",name:"ref-val"}}},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",name:"ref-val"}}},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",name:"ref-val"}}},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",name:"ref-val"}}},is:{func:function(v){return!isNaN(v)},config:{}}}},{id:"date",i18n:{"zh-TW":{date:"日期","min age":"最小年齡","max age":"最大年齡",age:"年齡"},en:{date:"Date","min age":"Min Age","max age":"Max Age",age:"Age"}},convert:function(v){return new Date(v)},ops:{age:{config:{min:{type:"number",name:"min age"},max:{type:"number",name:"max age"}},func:function(v,c){var dmin,dmax,dnow;c==null&&(c={});dmin=new Date(v.getYear()+1900+(c.min||0),v.getMonth(),v.getDate()).getTime();dmax=new Date(v.getYear()+1900+(c.max||0),v.getMonth(),v.getDate()).getTime();dnow=Date.now();if(c.min!=null&&dmin>dnow){return false}if(c.max!=null&&dmax<dnow){return false}return true}}}},{id:"choice",valdef:["@plotdb/form:valdef/choice"],i18n:{"zh-TW":{choice:"選擇",is:"選了","is-not":"沒選","is-any":"選了其中一個",val:"選項值",vals:"選項值(任一)"},en:{choice:"Choice",is:"Is","is-not":"Is Not","is-any":"Is Any Of",val:"Option value",vals:"Option values (any)"}},convert:function(v){var toKey,list;toKey=function(item){if(typeof item==="string"){return item}else{return item.key||item.value||""}};if(!v){return{list:[],other:{}}}if(typeof v==="string"){return{list:v?[v]:[],other:{}}}list=(v.list||v||[]).filter(function(it){return it}).map(toKey);return{list:list,other:v.other||{}}},ops:{is:{func:function(v,c){c==null&&(c={});return!!(c.val&&in$(c.val,v.list))},config:function(valspec){var values;values=((valspec!=null?valspec.values:void 8)||[]).map(function(v){return{value:v.key||v.value||v,name:v.label||v.value||v.key||v}});return{val:values.length?{type:"choice",name:"val",values:values}:{type:"text",name:"val"}}}},"is-not":{func:function(v,c){c==null&&(c={});return!(c.val&&in$(c.val,v.list))},config:function(valspec){var values;values=((valspec!=null?valspec.values:void 8)||[]).map(function(v){return{value:v.key||v.value||v,name:v.label||v.value||v.key||v}});return{val:values.length?{type:"choice",name:"val",values:values}:{type:"text",name:"val"}}}},"is-any":{func:function(v,c){var vals;c==null&&(c={});vals=Array.isArray(c.vals)?c.vals:typeof c.vals==="string"?c.vals.split(",").map(function(it){return it.trim()}).filter(function(it){return it}):[];return vals.some(function(it){return in$(it,v.list)})},config:function(valspec){var values;values=((valspec!=null?valspec.values:void 8)||[]).map(function(v){return{value:v.key||v.value||v,name:v.label||v.value||v.key||v}});return{vals:values.length?{type:"choice",name:"vals",values:values}:{type:"text",name:"vals",hint:"comma separated"}}}}}}];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);if(!this.id){this.id=Math.random().toString(36).substring(2)}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{id:this.id,enabled:this.enabled,opset:this.opset.id,op:this.op.id,config:this.config,msg:this.msg}},deserialize:function(v){this.toggle(v.enabled);return this.setOpset(v.opset,v.op,v.config)}});form.widget=function(opt){var o,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._meta_dig="";this._value=undefined;this._empty=true;this._mode=opt.mode||"edit";this._validate=opt.validate||null;this._opsets=(opt.opsets||[]).concat((opt.mod||{}).opsets||[]).map(function(opset){if(typeof opset==="string"){return form.opset.get(opset)}else if(opset instanceof 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._opsets=Object.fromEntries(function(){var i$,ref$,len$,results$=[];for(i$=0,len$=(ref$=this._opsets).length;i$<len$;++i$){o=ref$[i$];results$.push([o.id||o.name||"",o])}return results$}.call(this));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(){var this$=this;return Promise.resolve(this.mod&&this.mod.init?this.mod.init.apply(this):"").then(function(){return this$.fire("init")})},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$,i$,len$,k;ret=(ref1$={},ref1$.key=(ref$=this._meta).key,ref1$.title=ref$.title,ref1$.desc=ref$.desc,ref1$.isRequired=ref$.isRequired,ref1$.disabled=ref$.disabled,ref1$.readonly=ref$.readonly,ref1$.defaultValue=ref$.defaultValue,ref1$);ret.config=JSON.parse(JSON.stringify(this._meta.config||{}));ret.term=(this._meta.term||[]).map(function(it){return it.serialize()});for(i$=0,len$=(ref1$=["condition","tag"]).length;i$<len$;++i$){k=ref1$[i$];if(this._meta[k]!=null){ret[k]=JSON.parse(JSON.stringify(this._meta[k]))}}return ret},deserialize:function(v,o){var ref$,i$,len$,k,dig,this$=this;o==null&&(o={});ref$=this._meta;ref$.key=v.key;ref$.title=v.title;ref$.desc=v.desc;ref$.isRequired=v.isRequired;ref$.disabled=v.disabled;ref$.readonly=v.readonly;ref$.defaultValue=v.defaultValue;this._meta.config=JSON.parse(JSON.stringify(v.config||{}));for(i$=0,len$=(ref$=["condition","tag"]).length;i$<len$;++i$){k=ref$[i$];this._meta[k]=v[k]!=null?JSON.parse(JSON.stringify(v[k])):undefined}this._meta.term=(v.term||[]).map(function(t){var that;return new form.term(import$(import$({},t),(that=this$._opsets[t.opset||""])?{opset:that}:{}))});dig=JSON.stringify(v);if(this._meta_dig!==dig){this.fire("meta",this.serialize(),{init:o.init})}this.manager();this._meta_dig=dig;return Promise.resolve().then(function(){if(!o.init){return}if(!(this$._meta.defaultValue!=null&&this$.isEmpty())){return}return this$.value(this$._meta.defaultValue,{init:true,fromSource:true})}).then(function(){return this$.validate({init:o.init})}).then(function(){return this$.render()})},mode:function(m){var this$=this;if(!(m!=null)){return this._mode}return Promise.resolve().then(function(){var ref$;if(!(m==="edit"||m==="view"||m==="config")){return Promise.reject((ref$=new Error,ref$.name="lderror",ref$.id=1015,ref$))}if(this$._mode===m){return}this$._mode=m;this$.fire("mode",m);return this$.validate({init:true}).then(function(){return this$.render()})})},errors:function(){return this._errors},value:function(v,opt){var _v,_vs,p,this$=this;opt==null&&(opt={});if(arguments.length===0){return this._value!=null?JSON.parse(JSON.stringify(this._value)):this._value}if(this.isEqual(v,this._value)){return Promise.resolve()}_v=v!=null?JSON.parse(_vs=JSON.stringify(v)):v;this._value=_v;this._empty=this.isEmpty(_v);p=this.mod&&this.mod.value?Promise.resolve(this.mod.value.call(this,_vs!=null?JSON.parse(_vs):_v)):Promise.resolve();return p.then(function(){return this$.validate({init:opt.init}).then(function(){if(opt.fromSource){return}return this$.fire("change",this$._value!=null?JSON.parse(JSON.stringify(this$._value)):undefined)})})},disabled:function(){return!!this._meta.disabled},readonly:function(){return!!this._meta.readonly},isRequired:function(){return!!this._meta.isRequired},isEmpty:function(v){if(!arguments.length){v=this._value}if(this.mod&&this.mod.isEmpty){return this.mod.isEmpty.call(this,v)}else{return v===null||typeof v==="undefined"||v===""}},isEqual:function(u,v){var eu,ev;if(arguments.length===1){v=this._value}if(this.mod&&this.mod.isEqual){return this.mod.isEqual.call(this,u,v)}eu=this.isEmpty(u);ev=this.isEmpty(v);if(!eu!==!ev&&(eu||ev)){return false}if(eu&&ev){return true}return JSON.stringify(u)===JSON.stringify(v)},content:function(v){if(!arguments.length){v=this._value}if(this.mod&&this.mod.content){return this.mod.content.call(this,v)}else if(typeof v==="object"&&v&&v.hasOwnProperty(v)){return v.v}else{return v}},valdef:function(){if(this.mod&&this.mod.valdef){if(typeof this.mod.valdef==="function"){return this.mod.valdef.call(this)}return this.mod.valdef}return null},valspec:function(){if(this.mod&&this.mod.valspec){if(typeof this.mod.valspec==="function"){return this.mod.valspec.call(this)}return this.mod.valspec}return null},validate:function(opt){var lc,getVal,this$=this;opt==null&&(opt={});lc={v:null,_value:this._value};getVal=function(){if(lc.v){return lc.v}else{return lc.v=this$.content()}};return Promise.resolve().then(function(){if(this$.mod&&this$.mod.validate){return this$.mod.validate.call(this$,opt)}if(this$._validate){return this$._validate(getVal())}}).then(function(ret){var ref$;ret==null&&(ret=[]);if(ret.status===3){this$._errors=ret.errors||[];this$.status(3);this$.render();return this$._errors}if(ret&&ret.length){this$._errors=ret;this$.status(opt.init?1:2);this$.render();return this$._errors}if(this$._empty){if(this$._meta.isRequired){this$._errors=["required"];this$.status(opt.init?1:2);this$.render();return this$._errors}else{this$.status(0);this$.render();return this$._errors=[]}}return Promise.all((((ref$=this$._meta)!=null?ref$.term:void 8)||[]).filter(function(t){return t.enabled}).map(function(t){return t.validate(getVal()).then(function(v){return[t,v]})})).then(function(ret){var ref$,ref1$;if((ref$=this$._meta)!=null&&((ref1$=ref$.term)!=null&&ref1$.length)&&lc._value!==this$._value){return}this$._errors=ret.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)}},manager:function(arg$){var depth,ref$,ret;depth=(ref$=(arg$!=null?arg$:{}).depth)!=null?ref$:0;ret=this.mod&&this.mod.manager?this.mod.manager.apply(this,[{depth:depth}]):[];ret=(Array.isArray(ret)?ret:[ret]).filter(function(it){return it});if(this.disabled()){ret.map(function(it){return it.disable()})}return ret},ctrl: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.ctrl){return this.mod.ctrl.apply(this,args)}},resolve:function(path){if(this.mod&&this.mod.resolve){return this.mod.resolve.apply(this,[path])}else{return[]}},paths:function(path){if(this.mod&&this.mod.paths){return this.mod.paths.apply(this,[path])}else{return[]}},export:function(){var variant,header;if(this.mod&&this.mod["export"]){return this.mod["export"].apply(this)}variant=(this._meta.config||{}).variant;header=(this._meta.title||"")+(variant?"("+variant+")":"");return[{uid:"",header:header,sortKey:[],value:form.value.toString(this.value())}]},exportable:function(){var ms;if(!(ms=this.manager({depth:1})).length){return true}if(!(this.mod&&this.mod["export"])){return false}return ms.every(function(it){return it.exportable&&it.exportable()})},settle:function(){return Promise.resolve(this.mod&&this.mod.settle?this.mod.settle.apply(this):null)}});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}function in$(x,xs){var i=-1,l=xs.length>>>0;while(++i<l)if(x===xs[i])return true;return false}}).call(this);
1
+ (function(){var form,ref$,wordLen,countOps,slice$=[].slice;form={};form.value={toString:function(v){var list2str,parts;list2str=function(list){if(list[0]&&list[0].key!=null&&list[0].value!=null){return list.map(function(it){return form.value.toString(it.value)}).join(" / ")}else{return list.map(function(it){return form.value.toString(it)}).join(" / ")}};if(typeof v==="string"){return v}else if(v==null){return""}else if(Array.isArray(v)){return list2str(v)}else if(v.filename){return v.filename}else if(v.key!=null&&v.value!=null){return form.value.toString(v.value)}else if(v.list&&v.other){parts=(v.list||[]).map(function(it){return form.value.toString(it)});if(v.other.enabled&&v.other.text){parts=parts.concat([v.other.text])}return parts.join(" / ")}else if(v.list&&Array.isArray(v.list)){return list2str(v.list)}else if(v.v!=null){return form.value.toString(v.v)}else{return JSON.stringify(v)}}};form.utils={lexCompare:function(a,b){var i$,to$,i;for(i$=0,to$=Math.max(a.length,b.length);i$<to$;++i$){i=i$;if(a[i]==null&&b[i]!=null){return-1}if(a[i]!=null&&b[i]==null){return 1}if(a[i]==null&&b[i]==null){return 0}if(a[i]!==b[i]){return a[i]-b[i]}}return 0},mergeExports:function(records){var uidMap,headers,k,v,rows;uidMap={};records.map(function(cols){return cols.map(function(col){if(!uidMap[col.uid]){return uidMap[col.uid]={uid:col.uid,header:col.header,sortKey:col.sortKey}}})});headers=function(){var ref$,results$=[];for(k in ref$=uidMap){v=ref$[k];results$.push({k:k,v:v})}return results$}().sort(function(a,b){return form.utils.lexCompare(a.v.sortKey,b.v.sortKey)});rows=records.map(function(cols){var valMap;valMap={};cols.map(function(it){return valMap[it.uid]=it.value});return headers.map(function(it){return valMap[it.k]||""})});return{headers:headers.map(function(it){return it.v.header}),rows:rows}}};form.condctrl=function(opt){var ref$,this$=this;opt==null&&(opt={});ref$=this._||(this._={});ref$.hash={};ref$.enabled={};ref$.manager=opt.manager;ref$.list=opt.conditions||[];ref$.autorun=false;ref$.applyBaseRule=opt.baseRule||function(){};this._.manager.on("change",function(){if(this$._.autorun){return this$.run()}});return this};form.condctrl.prototype=(ref$=Object.create(Object.prototype),ref$.isEnabled=function(it){return this._.enabled[it]},ref$.get=function(it){return this._.hash[it]},ref$.reset=function(opt){var fields,ref$,i$,to$,i,cond,results$=[];opt==null&&(opt={});this._.autorun=opt.autorun!=null&&!opt.autorun?false:true;this._.list=opt.conditions||this._.list||[];fields=this._.manager.widgets();ref$=this._||(this._={});ref$.hash={};ref$.enabled={};for(i$=0,to$=this._.list.length;i$<to$;++i$){i=i$;cond=this._.list[i];cond.id=cond.id||"_"+(i+1);this._.hash[cond.id]=cond;if(!Array.isArray(cond.config)){cond.config=[cond.config]}cond.config=cond.config.map(fn$);results$.push(cond.config.forEach(fn1$))}return results$;function fn$(it){var ref$;return ref$=import$({},JSON.parse(JSON.stringify(it))),ref$.src=cond.src,ref$.func=cond.func,ref$}function fn1$(cfg){var k,v;if(!cfg.path&&cfg.targets){cfg.path=cfg.targets}if(!cfg.value&&cfg.values){cfg.value=cfg.values}if(!cfg.tag&&cfg.tags){cfg.tag=cfg.tags}return cfg.path=Array.from(new Set((cfg.prefix?cfg.prefix:[]).concat(cfg.path||[],function(){var ref$,results$=[];for(k in ref$=fields){v=ref$[k];results$.push({k:k,v:v})}return results$}().filter(function(arg$){var k,v,ref$;k=arg$.k,v=arg$.v;return(!cfg.prefix?0:cfg.prefix.filter(function(p){return k.startsWith(p)}).length)+((v!=null?(ref$=v.meta)!=null?ref$.tag:void 8:void 8)||[]).filter(function(t){return in$(t,cfg.tag||[])}).length>0}).map(function(it){return it.k}))))}},ref$.subcond=function(arg$){var path,active,config,itf,ms;path=arg$.path,active=arg$.active,config=arg$.config;if(!((itf=this._.manager.widget(path))&&!itf.disabled()&&(ms=itf.manager({depth:1})||[]).length)){return}return ms.forEach(function(m){var cond;cond=m.condition();cond.apply(import$({active:active},config));return cond.run()})},ref$.apply=function(opt){var path,active,enabled,isRequired,readonly,widget,curMeta,newMeta;opt==null&&(opt={});path=opt.path,active=opt.active,enabled=opt.enabled,isRequired=opt.isRequired,readonly=opt.readonly;if(Array.isArray(path)){if(path[1]){return this.subcond({path:path[0],active:active,config:{path:path.slice(1),enabled:enabled,isRequired:isRequired,readonly:readonly}})}path=path[0]}if(enabled!=null){this._.enabled[path]=!(!enabled!==!active&&(enabled||active))}if(widget=this._.manager.widget(path)){curMeta=widget.serialize();newMeta=JSON.parse(JSON.stringify(curMeta));if(enabled!=null){newMeta.disabled=!!(!enabled!==!active&&(enabled||active))}if(readonly!=null){newMeta.readonly=!(!readonly!==!active&&(readonly||active))}if(isRequired!=null){newMeta.isRequired=!(!isRequired!==!active&&(isRequired||active))}this._.applyBaseRule({path:path,widget:widget,meta:newMeta,opt:opt});if(!!curMeta.disabled===!!newMeta.disabled&&!!curMeta.isRequired===!!newMeta.isRequired&&!!curMeta.readonly===!!newMeta.readonly){return}return widget.deserialize(newMeta,{init:true})}},ref$._run=function(cfg,precond){var src,values,paths,isRequired,enabled,readonly,func,result,i$,len$,path,active,ref$,itf,content;cfg==null&&(cfg={});src=cfg.src,values=cfg.value,paths=cfg.path,isRequired=cfg.isRequired,enabled=cfg.enabled,readonly=cfg.readonly,func=cfg.func;if(func){result=true;for(i$=0,len$=paths.length;i$<len$;++i$){path=paths[i$];active=!!func.apply(this,[(ref$=import$({},cfg),ref$.path=path,ref$)])&&!(precond!=null&&!precond);result=result&&active;if(Array.isArray(path)&&path[1]){this.subcond({path:path[0],config:(ref$=import$({},cfg),ref$.path=path.slice(1),ref$),active:active});continue}this.apply({path:path,enabled:enabled,active:active,isRequired:isRequired,readonly:readonly})}}else{values=Array.isArray(values)?values:[values];if(!(itf=this._.manager.widget(src))){return console.error("[@plotdb/form] condctrl: field '"+src+"' not found")}content=itf.content();content=Array.isArray(content)?content:[content];active=!!content.filter(function(it){return in$(it,values)}).length;if(precond!=null&&!precond){active=false}for(i$=0,len$=paths.length;i$<len$;++i$){path=paths[i$];if(Array.isArray(path)&&path[1]){this.subcond({path:path[0],config:(ref$=import$({},cfg),ref$.path=path.slice(1),ref$),active:active});continue}this.apply({path:path,enabled:enabled,active:active,isRequired:isRequired,readonly:readonly})}result=active}return result},ref$.run=function(){var result,_,this$=this;result={};_=function(list,ref){var i$,to$,i,lresult$,cond,j$,ref$,len$,cfg,r,results$=[];ref==null&&(ref=[]);for(i$=0,to$=list.length;i$<to$;++i$){i=i$;lresult$=[];cond=list[i];if(result[cond.id]!=null){continue}if(cond.precond&&this$._.hash[cond.precond]){if(in$(cond.id,ref)){console.error("[@plotdb/form] condctrl: circular ref id "+cond.id)}else{_([this$._.hash[cond.precond]],ref.concat([cond.id]))}}for(j$=0,len$=(ref$=cond.config).length;j$<len$;++j$){cfg=ref$[j$];r=this$._run(cfg,result[cond.precond]);lresult$.push(result[cond.id]=result[cond.id]!=null?result[cond.id]&&r:r)}results$.push(lresult$)}return results$};return _(this._.list||[])},ref$);form.conditor=function(opt){var this$=this;opt==null&&(opt={});this._={id:"c"+Math.random().toString(36).substring(2),manager:opt.manager,rules:[],hash:{},result:{},dep:{},tgt:{},touched:new Set,pending:null};this._.manager.on("change",function(arg$){var path;path=(arg$!=null?arg$:{}).path;return this$._schedule(path)});return this};form.conditor.prototype=(ref$=Object.create(Object.prototype),ref$.init=function(rules){var ref$,i$,len$,r,j$,ref1$,len1$,k,ref2$,p;rules==null&&(rules=[]);this.clear();this._.rules=rules.map(function(r,i){var ref$;return ref$=import$({},r),ref$.id=r.id||"_r"+(i+1),ref$});ref$=this._;ref$.hash={};ref$.result={};ref$.dep={};ref$.tgt={};for(i$=0,len$=(ref$=this._.rules).length;i$<len$;++i$){r=ref$[i$];this._.hash[r.id]=r;for(j$=0,len1$=(ref1$=form.conditor.deps(r)).length;j$<len1$;++j$){k=ref1$[j$];((ref2$=this._.dep)[k]||(ref2$[k]=[])).push(r.id)}for(j$=0,len1$=(ref1$=form.conditor.targets(r)).length;j$<len1$;++j$){p=ref1$[j$];k=typeof p[0]==="string"&&!form.conditor.isSelector(p[0])?p[0]:"*";((ref2$=this._.tgt)[k]||(ref2$[k]=[])).push(r.id)}}return this.run()},ref$.rules=function(){return this._.rules},ref$.result=function(id){if(arguments.length){return this._.result[id]}else{return import$({},this._.result)}},ref$.clear=function(){var this$=this;this._.touched.forEach(function(w){var k;if(!(w._conditor&&w._conditor.fx)){return}for(k in w._conditor.fx){if(k.startsWith(this$._.id+"/")){delete w._conditor.fx[k]}}return form.conditor.merge(w)});return this._.touched=new Set},ref$._schedule=function(key){var this$=this;this._.pending=this._.pending||new Set;this._.pending.add(key!=null?key:"*");if(this._.pending.size>1){return}return Promise.resolve().then(function(){var keys,ids,i$,ref$,len$,k,j$,ref1$,len1$,id;keys=Array.from(this$._.pending);this$._.pending=null;if(in$("*",keys)){return this$.run()}ids=new Set;for(i$=0,len$=(ref$=keys.concat(["*"])).length;i$<len$;++i$){k=ref$[i$];for(j$=0,len1$=(ref1$=(this$._.dep[k]||[]).concat(this$._.tgt[k]||[])).length;j$<len1$;++j$){id=ref1$[j$];ids.add(id)}}return this$.run(Array.from(ids))})},ref$.run=function(ids){var list,queue,seen,r,i$,ref$,len$,o,result,stack,evalRule,this$=this;list=ids?ids.map(function(it){return this$._.hash[it]}).filter(function(it){return it}):this._.rules.slice();if(ids){queue=list.slice();seen=new Set(list.map(function(it){return it.id}));while(queue.length){r=queue.shift();for(i$=0,len$=(ref$=this._.rules).length;i$<len$;++i$){o=ref$[i$];if(!seen.has(o.id)&&(o.precond===r.id||in$(r.id,form.conditor.refs(o)))){seen.add(o.id);list.push(o);queue.push(o)}}}}result={};stack=[];evalRule=function(r){var ret,e;if(result[r.id]!=null){return result[r.id]}if(in$(r.id,stack)){console.error("[@plotdb/form] conditor: circular reference in rule `"+r.id+"`");return false}stack.push(r.id);ret=r.precond&&this$._.hash[r.precond]&&!evalRule(this$._.hash[r.precond])?false:function(){try{return form.conditor.truthy(this.eval(r.when,{rule:evalRule}))}catch(e$){e=e$;console.error("[@plotdb/form] conditor: rule `"+r.id+"` failed:",e);return false}}.call(this$);stack.pop();return this$._.result[r.id]=result[r.id]=ret};for(i$=0,len$=list.length;i$<len$;++i$){r=list[i$];this._apply(r,evalRule(r))}return result},ref$.eval=function(x,ctx){var op,a,v,r,i$,to$,i,ret,b,c,list,test,this$=this;ctx==null&&(ctx={});if(typeof x==="function"){return x.call(this,ctx)}if(Array.isArray(x)){return x.map(function(it){return this$.eval(it,ctx)})}if(!(x&&typeof x==="object")){return x}op=Object.keys(x)[0];a=x[op];a=Array.isArray(a)?a:[a];v=function(i){return this$.eval(a[i],ctx)};switch(op){case"var":if(ctx.data!=null){return form.conditor.dig(ctx.data,a.length===1?a[0]:a)}return this.value(a);case"rule":if(!(r=this._.hash[v(0)])){return false}else if(ctx.rule){return ctx.rule(r)}else{return!!this._.result[r.id]}case"and":for(i$=0,to$=a.length;i$<to$;++i$){i=i$;if(!form.conditor.truthy(ret=v(i))){return ret}}return ret;case"or":for(i$=0,to$=a.length;i$<to$;++i$){i=i$;if(form.conditor.truthy(ret=v(i))){return ret}}return ret;case"!":return!form.conditor.truthy(v(0));case"!!":return form.conditor.truthy(v(0));case"if":for(i$=0,to$=a.length-1;i$<to$;i$+=2){i=i$;if(form.conditor.truthy(v(i))){return v(i+1)}}if(a.length%2){return v(a.length-1)}else{return null}case"==":return v(0)==v(1);case"!=":return v(0)!=v(1);case"===":return v(0)===v(1);case"!==":return v(0)!==v(1);case"<":return v(0)<v(1)&&(a.length<3||v(1)<v(2));case"<=":return v(0)<=v(1)&&(a.length<3||v(1)<=v(2));case">":return v(0)>v(1);case">=":return v(0)>=v(1);case"in":b=v(1);if(Array.isArray(b)||typeof b==="string"){return b.indexOf(v(0))>=0}else{return false}case"empty":c=v(0);return c==null||c===""||Array.isArray(c)&&!c.length;case"some":case"all":case"none":list=a[0]&&typeof a[0]==="object"&&a[0]["var"]!=null?ctx.data!=null?form.conditor.dig(ctx.data,a[0]["var"]):this.values(a[0]["var"]):v(0);list=Array.isArray(list)?list:list!=null?[list]:[];test=function(item){var ref$;return form.conditor.truthy(this$.eval(a[1],(ref$=import$({},ctx),ref$.data=item,ref$)))};if(op==="some"){return list.some(test)}else if(op==="none"){return!list.some(test)}else{return!!list.length&&list.every(test)}default:throw new Error("unsupported operator `"+op+"`")}},ref$.value=function(path){var ws;path=Array.isArray(path)?path:[path];if(form.conditor.fan(path)){return this.values(path)}ws=this._.manager.resolve(path);if(ws.length===1){return ws[0].content()}else if(ws.length){return this.values(path)}else{return null}},ref$.values=function(path){var ret,i$,ref$,len$,w,c;ret=[];for(i$=0,len$=(ref$=this._.manager.resolve(Array.isArray(path)?path:[path])).length;i$<len$;++i$){w=ref$[i$];c=w.content();if(Array.isArray(c)){ret=ret.concat(c)}else if(c!=null){ret.push(c)}}return ret},ref$._apply=function(rule,active){var fx,state,i$,ref$,len$,k,key,p,lresult$,j$,ref1$,len1$,w,ref2$,results$=[];fx=rule.effect||{};state={};if(fx.enabled!=null){state.disabled=!!(!fx.enabled!==!active&&(fx.enabled||active))}else if(fx.disabled!=null){state.disabled=!(!fx.disabled!==!active&&(fx.disabled||active))}for(i$=0,len$=(ref$=["isRequired","readonly"]).length;i$<len$;++i$){k=ref$[i$];if(fx[k]!=null){state[k]=!(!fx[k]!==!active&&(fx[k]||active))}}key=this._.id+"/"+rule.id;for(i$=0,len$=(ref$=form.conditor.targets(rule)).length;i$<len$;++i$){p=ref$[i$];lresult$=[];for(j$=0,len1$=(ref1$=this._.manager.resolve(p)).length;j$<len1$;++j$){w=ref1$[j$];((ref2$=w._conditor||(w._conditor={})).fx||(ref2$.fx={}))[key]=state;this._.touched.add(w);lresult$.push(form.conditor.merge(w))}results$.push(lresult$)}return results$},ref$);form.conditor.merge=function(w){var lc,cur,next,i$,ref$,len$,k,vs,res$,id,ref1$,s;lc=w._conditor;lc.base=lc.base||{};cur=w.serialize();next=JSON.parse(JSON.stringify(cur));for(i$=0,len$=(ref$=["disabled","isRequired","readonly"]).length;i$<len$;++i$){k=ref$[i$];res$=[];for(id in ref1$=lc.fx){s=ref1$[id];if(s[k]!=null){res$.push(s[k])}}vs=res$;if(vs.length){if(!(k in lc.base)){lc.base[k]=!!cur[k]}next[k]=vs.some(fn$)}else if(k in lc.base){next[k]=lc.base[k];delete lc.base[k]}}if(["disabled","isRequired","readonly"].every(function(k){return!!cur[k]===!!next[k]})){return}return w.deserialize(next,{init:true});function fn$(it){return it}};form.conditor.truthy=function(v){if(Array.isArray(v)){return!!v.length}else{return!!v}};form.conditor.dig=function(o,p){var i$,ref$,len$,k;if(p===""||p==null){return o}for(i$=0,len$=(ref$=Array.isArray(p)?p:(p+"").split(".")).length;i$<len$;++i$){k=ref$[i$];if(!(o!=null&&typeof o==="object")){return null}o=o[k]}return o};form.conditor.isSelector=function(s){return typeof s==="number"||typeof s==="string"&&!!/^(\*$|#|:|\[)/.exec(s)};form.conditor.fan=function(p){return p.some(function(s){return form.conditor.isSelector(s)&&typeof s!=="number"&&!/^#/.exec(s)})};form.conditor.targets=function(r){return((r.effect||{}).targets||[]).map(function(it){if(Array.isArray(it)){return it}else{return[it]}})};form.conditor.deps=function(r){var ret,walk;ret=new Set;walk=function(x,inner){var op,a,p;if(typeof x==="function"){return ret.add("*")}if(Array.isArray(x)){return x.map(function(it){return walk(it,inner)})}if(!(x&&typeof x==="object")){return}op=Object.keys(x)[0];a=x[op];if(op==="var"){if(inner){return}p=Array.isArray(a)?a:[a];return ret.add(typeof p[0]==="string"&&!form.conditor.isSelector(p[0])?p[0]:"*")}a=Array.isArray(a)?a:[a];if(op==="some"||op==="all"||op==="none"){walk(a[0],inner);return walk(a.slice(1),true)}else{return walk(a,inner)}};walk(r.when,false);return Array.from(ret)};form.conditor.refs=function(r){var ret,walk;ret=[];walk=function(x){var k,v,results$=[];if(Array.isArray(x)){return x.map(walk)}if(!(x&&typeof x==="object")){return}for(k in x){v=x[k];if(k==="rule"&&typeof v==="string"){results$.push(ret.push(v))}else{results$.push(walk(v))}}return results$};walk(r.when);return ret};form.manager=function(o){var this$=this;o==null&&(o={});this._ws={w:{},p:new WeakMap,s:{},l:{}};this._disabled=false;this._evthdr={};this._status=1;this._mode="edit";this._mod={};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)});this._restatusDebounced=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$._restatus.apply(this$,args)});return this};form.manager.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$._evthdr)[n]||(ref$[n]=[])).push(cb)})},condition:function(){return this._cond||(this._cond=new form.condctrl({manager:this}))},conditor:function(){return this._conditor||(this._conditor=new form.conditor({manager:this}))},disabled:function(){return!!this._disabled},disable:function(it){return this._disabled=it!=null?!!it:true},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.mode(this._mode);w.on("meta",this._ws.l[p].m=function(v){return this$.fire("meta",{widget:w,path:p,meta:v})});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;if(this$._ws.w[p]._meta.disabled){return}this$.fire("status",{widget:w,path:p,status:s});return this$._restatusDebounced()})},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}ws.off("meta",this._ws.l[o.path].m);ws.off("change",this._ws.l[o.path].c);ws.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$},widgets:function(){return import$({},this._ws.w||{})},widget:function(p){return this._ws.w[p]},content:function(p){var that;if(that=this._ws.w[p]){return that.content()}else{return null}},status:function(){return this._status},progress:function(arg$){var _recurred,ref$,ret,list,k,s,this$=this;_recurred=(ref$=(arg$!=null?arg$:{})._recurred)!=null?ref$:false;ret={total:0,invalid:0,done:0};list=function(){var ref$,results$=[];for(k in ref$=this._ws.s){s=ref$[k];results$.push({k:k,s:s})}return results$}.call(this).filter(function(it){return this$._ws.w[it.k]&&this$._ws.w[it.k]._meta&&!this$._ws.w[it.k]._meta.disabled});list.forEach(function(o){var w,ms,e;w=this$._ws.w[o.k];if(!_recurred&&(ms=w.manager()).length){ms=ms.filter(function(it){return!it.disabled()}).filter(function(m){var p;p=m.progress({_recurred:true});ret.total+=p.total;ret.invalid+=p.invalid;ret.done+=p.done;return!!p.invalid});if(ms.length){return}}ret.total+=1;e=w.errors();if(e.length===1&&e[0]==="nested"){return}if(o.s!=null&&o.s===2){ret.invalid+=1}if(o.s!=null&&o.s===0){return ret.done+=1}});ret.percent=!ret.total?1:ret.done/(ret.total||1);return ret},invalidWidgets:function(){var ret,list,k,s,this$=this;ret=[];list=function(){var ref$,results$=[];for(k in ref$=this._ws.s){s=ref$[k];results$.push({k:k,s:s})}return results$}.call(this).filter(function(it){return this$._ws.w[it.k]&&this$._ws.w[it.k]._meta&&!this$._ws.w[it.k]._meta.disabled});list.forEach(function(o){var w,ms,e;w=this$._ws.w[o.k];if((ms=w.manager()).length){ms.filter(function(it){return!it.disabled()}).forEach(function(m){return ret=ret.concat(m.invalidWidgets())});e=w.errors();if(o.s!=null&&o.s===2&&!(e.length===1&&e[0]==="nested")){ret.push({widget:w,path:o.k,status:2})}return}e=w.errors();if(e.length===1&&e[0]==="nested"){return}if(o.s!=null&&o.s===2){return ret.push({widget:w,path:o.k,status:2})}});return ret},_restatus:function(){var os,ret,k,v,this$=this;os=this._status;delete this._status;ret=function(){var ref$,results$=[];for(k in ref$=this._ws.w){v=ref$[k];results$.push({k:k,v:v})}return results$}.call(this).filter(function(arg$){var k,v;k=arg$.k,v=arg$.v;return!v._meta.disabled}).map(function(arg$){var k,v,s;k=arg$.k,v=arg$.v;s=this$._ws.s[k];if(s==null){return true}if(s===0){return false}if(s===1&&!v._meta.isRequired){return false}return true}).filter(function(it){return it}).length;this._status=ret?1:0;if(os!==this._status){return this.fire("readystatechange",this._status===0)}},check:function(o,opt){var this$=this;if(typeof opt!=="object"){opt={now:opt}}return Promise.resolve().then(function(){var p,w;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$).filter(function(arg$){var widget;widget=arg$.widget;if(widget._meta.disabled){return false}return!opt.skipEmpty?true:!widget.isEmpty()||widget.status()===2}),opt)}this$.checkList=(this$.checkList||(this$.checkList=[])).concat(Array.isArray(o)?o:[o]);return opt.now?this$._check(null,opt):this$._checkDebounced(null,opt)}).then(function(it){this$._restatus();return it})},_check:function(o,opt){var list,this$=this;opt==null&&(opt={});if(!o){list=this.checkList||[];this.checkList=[];return this._check(list,opt)}if(Array.isArray(o)){return Promise.all(o.map(function(it){return this$._check(it,opt)})).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({force:opt.force,init:opt.init,skipEmpty:opt.skipEmpty}).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]})}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];if(w._meta.disabled){continue}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,ps,res$,e;opt==null&&(opt={});if(!arguments.length){ret={};for(p in ref$=this._ws.w){w=ref$[p];if(!(opt.partial&&w._meta.disabled)){ret[p]=w.value()}}return ret}v=v!=null?JSON.parse(JSON.stringify(v)):{};try{res$=[];for(p in ref$=this._ws.w){w=ref$[p];if(!v.hasOwnProperty(p)&&opt.partial){continue}res$.push(w.value(v[p],opt))}ps=res$;return Promise.all(ps)}catch(e$){e=e$;return Promise.reject(e)}},mode:function(m){var this$=this;if(!(m!=null)){return this._mode}return Promise.resolve().then(function(){var ref$,p,w;if(!(m==="edit"||m==="view"||m==="config")){return Promise.reject((ref$=new Error,ref$.name="lderror",ref$.id=1015,ref$))}if(this$._mode===m){return}this$._mode=m;this$.fire("mode",m);return Promise.all(function(){var ref$,results$=[];for(p in ref$=this._ws.w){w=ref$[p];results$.push(w)}return results$}.call(this$).map(function(w){return w.mode(m)}))})},order:function(){var nodeIdx,_,obj;nodeIdx=function(r,n,opt){var i$,to$,i,v;opt.idx++;if(r===n){return true}if(!(r&&r.childNodes)){return}for(i$=0,to$=r.childNodes.length;i$<to$;++i$){i=i$;v=nodeIdx(r.childNodes[i],n,opt);if(v){return true}}};_=function(m,obj){var ws,k,v,ms,child,opt,results$=[];ws=m._ws.w;for(k in ws){v=ws[k];ms=v.manager()||[];if(ms.length){child={};ms.map(fn$)}else{child=null}opt={idx:0};nodeIdx(document.body,v._root,opt);results$.push(obj[k]={title:v._meta.title,idx:opt.idx,child:child,meta:v.serialize()})}return results$;function fn$(m){return _(m,child)}};_(this,obj={});return obj},export:function(){var attached,ws,res$,k,ref$,v,cols,i$,len$,rank;attached=function(w){return w._root&&w._root.isConnected};res$=[];for(k in ref$=this._ws.w){v=ref$[k];res$.push({k:k,v:v})}ws=res$;ws.sort(function(a,b){var ref$,ra,rb;ref$=[attached(a.v),attached(b.v)],ra=ref$[0],rb=ref$[1];if(!(ra&&rb)){return ra?-1:rb?1:0}if(a.v._root===b.v._root){return 0}if(a.v._root.compareDocumentPosition(b.v._root)&Node.DOCUMENT_POSITION_FOLLOWING){return-1}else{return 1}});cols=[];for(i$=0,len$=ws.length;i$<len$;++i$){rank=i$;ref$=ws[i$],k=ref$.k,v=ref$.v;cols=cols.concat(v["export"]().map(fn$))}return cols.sort(function(a,b){return form.utils.lexCompare(a.sortKey,b.sortKey)});function fn$(col){col.sortKey=[rank].concat(col.sortKey);col.uid=col.uid?k+"|"+col.uid:k;return col}},exportable:function(){var k,v;return function(){var ref$,results$=[];for(k in ref$=this._ws.w){v=ref$[k];results$.push(v)}return results$}.call(this).every(function(it){return it.exportable()})},settle:function(){var k,v;return Promise.all(function(){var ref$,results$=[];for(k in ref$=this._ws.w){v=ref$[k];results$.push(v.settle())}return results$}.call(this))},manager:function(opt){var ret,k,ref$,s,w,mgrs;ret=[];for(k in ref$=this._ws.s){s=ref$[k];if(!(this._ws.w[k]&&this._ws.w[k]._meta&&!this._ws.w[k]._meta.disabled)){continue}if(!(w=this.widget(k))){continue}if(!(mgrs=w.manager(opt)||[]).length){continue}ret=ret.concat(mgrs)}if(this.disabled()){ret.map(function(it){return it.disable()})}return ret},resolve:function(path){var head,tail,ws,that;if(!path||!path.length){return[]}head=path[0],tail=slice$.call(path,1);ws=form.conditor.isSelector(head)?this._select(head):(that=this._ws.w[head])?[that]:[];if(!tail.length){return ws}return ws.map(function(it){return it.resolve(tail)}).reduce(function(a,b){return a.concat(b)},[])},_select:function(s){var k,v,m,that;if(s==="*"){return function(){var ref$,results$=[];for(k in ref$=this._ws.w){v=ref$[k];results$.push(v)}return results$}.call(this)}if(typeof s!=="string"){return[]}if(m=/^#(.+)$/.exec(s)){return(that=this._ws.w[m[1]])?[that]:[]}if(m=/^\[tag~=["']?([^"'\]]+)["']?\]$/.exec(s)){return function(){var ref$,results$=[];for(k in ref$=this._ws.w){v=ref$[k];results$.push(v)}return results$}.call(this).filter(function(it){return in$(m[1],it._meta.tag||[])})}return[]},paths:function(path){var k,v,head,tail,w;if(!path||!path.length){return function(){var ref$,results$=[];for(k in ref$=this._ws.w){v=ref$[k];results$.push({id:k,name:v._meta.title||k})}return results$}.call(this)}head=path[0],tail=slice$.call(path,1);w=this._ws.w[head];if(!w){return[]}return w.paths(tail)}});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),{getConfig:function(valspec){if(typeof this.config==="function"){return this.config(valspec)}else{return this.config}},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(valspec){var cfg,k,ref$,v;cfg={};for(k in ref$=this.getConfig(valspec)){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.valdef=opt.valdef||null;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.list=function(opt){var list,vd;opt==null&&(opt={});list=(this._list||[]).map(function(it){return it});if(!opt.valdef){return list}vd=Array.isArray(opt.valdef)?opt.valdef:[opt.valdef];return list.filter(function(os){if(!os.valdef){return!vd.length||vd.some(function(it){return!it||it==="@plotdb/form:valdef/generic"})}return vd.some(function(v){return in$(v,os.valdef)})})};wordLen=function(v,method){v==null&&(v="");return method==="simple-word"?v.split(/\s|[,.;:!?,。;:︰!?、. "]/).filter(function(it){return it}).map(function(it){return it.split(/[\u1000-\uffff]/).map(function(it){if(it.length){return 2}else{return 1}}).reduce(function(a,b){return a+b},0)-1}).reduce(function(a,b){return a+b},0):v.length};countOps={"count-max":{func:function(v,c){c==null&&(c={});return v.length<=c.val},config:{val:{type:"number",name:"count-max",hint:"maximal amount"}}},"count-min":{func:function(v,c){c==null&&(c={});return v.length>=c.val},config:{val:{type:"number",name:"count-min",hint:"minimal amount"}}},"count-range":{func:function(v,c){c==null&&(c={});return v.length>=c.min&&v.length<=c.max},config:{min:{type:"number",name:"count-min",hint:"minimal amount"},max:{type:"number",name:"count-max",hint:"maximal amount"}}},count:{func:function(v,c){c==null&&(c={});return v.length===c.val},config:{val:{type:"number",name:"count",hint:"number of entries"}}}};form.opset["default"]=[{id:"list",i18n:{"zh-TW":{list:"列表","count-max":"數量上限","count-min":"數量下限","count-range":"數量範圍",count:"數量要求"},en:{list:"List","count-max":"Maximal Count","count-min":"Minimal Count","count-range":"Count Range",count:"Required Count"}},convert:function(v){var ret;ret=Array.isArray(v)?v:v&&Array.isArray(v.list)?v.list.concat(v.other&&v.other.enabled?[v.other.text]:[]):[v];return ret.filter(function(it){return it})},ops:import$({},countOps)},{id:"file",i18n:{"zh-TW":{file:"檔案","max-size":"檔案大小上限(位元數)","min-size":"檔案大小下限(位元數)","count-limit":"檔案數量上限",extension:"副檔名限制","count-max":"檔案數量上限","count-min":"檔案數量下限","count-range":"檔案數量範圍"},en:{file:"File","max-size":"Max File Size (bytes)","min-size":"Min File Size (bytes)","count-limit":"Max File Count",extension:"File Extension","count-max":"Max File Count","count-min":"Min File Count","count-range":"File Count Range"}},convert:function(v){return(Array.isArray(v)?v:[v]).filter(function(it){return it})},ops:import$({"size-limit":{func:function(v,c){var max;c==null&&(c={});max=c.max!=null?c.max:c.size!=null?c.size:c.val;return!v.filter(function(it){return max!=null&&it.size>max||c.min!=null&&it.size<c.min}).length},config:{max:{type:"number",name:"max-size"},min:{type:"number",name:"min-size"}}},extension:{func:function(v,c){var exts;c==null&&(c={});exts=(c.str.split(",")||[]).map(function(it){return(it||"").trim().toLowerCase()}).filter(function(it){return it});return!v.filter(function(it){var ext,ref$;return!in$(((ext=(ref$=(it.filename||"").split("."))[ref$.length-1])||"").trim().toLowerCase(),exts)}).length},config:{str:{type:"text",name:"extension",hint:"extension, comma separated, without dot"}}},"count-limit":{func:function(v,c){c==null&&(c={});return v.length<c.val},config:{val:{type:"number",name:"count-limit",hint:"maximal file amount"}}}},countOps)},{id:"image",i18n:{"zh-TW":{image:"圖像","long-side":"長邊尺寸限制","short-side":"短邊尺寸限制",width:"寬度限制",height:"高度限制","pixel-count":"像素量限制","min-size":"最小尺寸","max-size":"最大尺寸"},en:{image:"Image","long-side":"Long Side","short-side":"Short Side",width:"Width",height:"Height","pixel-count":"Max Pixel Count","min-size":"Min Size","max-size":"Max Size"}},convert:function(v){return(Array.isArray(v)?v:[v]).filter(function(it){return it})},ops:Object.fromEntries([["long-side","long"],["short-side","short"],["width","width"],["height","height"],["pixel-count","pixels"]].map(function(n){return[n[0],function(k){return{func:function(v,c){v==null&&(v=[]);c==null&&(c={});return!v.filter(function(it){return!((c.min==null||it[k]>=(c.min||0))&&(c.max==null||it[k]<=c.max))}).length},config:{min:{type:"number",name:"min-size",hint:"minimal size"},max:{type:"number",name:"max-size",hint:"maximal size"}}}}(n[1])]}))},{id:"string",i18n:{"zh-TW":{string:"文字",include:"包含",exclude:"排除",email:"電子郵件",url:"網址",regex:"正規表達式"},en:{string:"Text",include:"Include",exclude:"Exclude",email:"Email",url:"URL",regex:"Regular Expression"}},ops:{include:{func:function(v,c){c==null&&(c={});return~(""+(v||"")).indexOf(c.str||"")},config:{str:{type:"text",name:"include"}}},exclude:{func:function(v,c){c==null&&(c={});return!~(""+(v||"")).indexOf(c.str||"")},config:{str:{type:"text",name:"exclude"}}},email:{func:function(v){return/^[^@]+@[^@]+$/.exec(v)},config:{}},url:{func:function(v){return/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,15}\b([-a-zA-Z0-9@:%_\+.~#()?&//=]*)$/.exec(v)},config:{}},regex:{func:function(v,c){c==null&&(c={});if(!c.rule){return true}if(typeof c.rule==="object"&&c.rule.exec){return c.rule.exec(v)}return new RegExp(c.rule).exec(v)},config:{rule:{type:"text",name:"regex"}}}}},{id:"length",i18n:{"zh-TW":{length:"字串長度",lte:"≦ 小於或等於",range:"範圍",number:"數字",method:"計算方式",char:"字元","simple-word":"簡易計字/詞","minimal length":"長度下限","maximal length":"長度上限"},en:{length:"String Length",lte:"≦",range:"Range",number:"Number",method:"Method",char:"By Character","simple-word":"Count Word","minimal length":"Min Length","maximal length":"Max Length"}},ops:{range:{func:function(v,c){var len;c==null&&(c={});len=wordLen(v+"",c.method);return len>=+c.min&&len<=+c.max},config:{min:{type:"number",name:"minimal length"},max:{type:"number",name:"maximal length"},method:{type:"choice",default:"char",values:["char","simple-word"]}}},lte:{func:function(v,c){c==null&&(c={});return wordLen(v+"",c.method)<=+c.val},config:{val:{type:"number",name:"maximal length"},method:{type:"choice",default:"char",values:["char","simple-word"]}}},gte:{func:function(v,c){c==null&&(c={});return wordLen(v+"",c.method)>=+c.val},config:{val:{type:"number",name:"minimal length"},method:{type:"choice",default:"char",values:["char","simple-word"]}}},eq:{func:function(v,c){c==null&&(c={});return wordLen(v+"",c.method)===+c.val},config:{val:{type:"number",name:"length"},method:{type:"choice",default:"char",values:["char","simple-word"]}}}}},{id:"number",i18n:{"zh-TW":{number:"數字",lte:"≦ 小於或等於",gte:"≧ 大於或等於",ne:"≠ 不等於",eq:"= 等於",is:"任何數字","ref-val":"參考值"},en:{number:"Number",lte:"≦ Equal/Less Than",gte:"≧ Equal/Greater Than",ne:"≠ Not Equals",eq:"= Equals",is:"Is Number","ref-val":"Reference Value"}},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",name:"ref-val"}}},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",name:"ref-val"}}},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",name:"ref-val"}}},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",name:"ref-val"}}},is:{func:function(v){return!isNaN(v)},config:{}}}},{id:"date",i18n:{"zh-TW":{date:"日期","min age":"最小年齡","max age":"最大年齡",age:"年齡"},en:{date:"Date","min age":"Min Age","max age":"Max Age",age:"Age"}},convert:function(v){return new Date(v)},ops:{age:{config:{min:{type:"number",name:"min age"},max:{type:"number",name:"max age"}},func:function(v,c){var dmin,dmax,dnow;c==null&&(c={});dmin=new Date(v.getYear()+1900+(c.min||0),v.getMonth(),v.getDate()).getTime();dmax=new Date(v.getYear()+1900+(c.max||0),v.getMonth(),v.getDate()).getTime();dnow=Date.now();if(c.min!=null&&dmin>dnow){return false}if(c.max!=null&&dmax<dnow){return false}return true}}}},{id:"choice",valdef:["@plotdb/form:valdef/choice"],i18n:{"zh-TW":{choice:"選擇",is:"選了","is-not":"沒選","is-any":"選了其中一個",val:"選項值",vals:"選項值(任一)"},en:{choice:"Choice",is:"Is","is-not":"Is Not","is-any":"Is Any Of",val:"Option value",vals:"Option values (any)"}},convert:function(v){var toKey,list;toKey=function(item){if(typeof item==="string"){return item}else{return item.key||item.value||""}};if(!v){return{list:[],other:{}}}if(typeof v==="string"){return{list:v?[v]:[],other:{}}}list=(v.list||v||[]).filter(function(it){return it}).map(toKey);return{list:list,other:v.other||{}}},ops:{is:{func:function(v,c){c==null&&(c={});return!!(c.val&&in$(c.val,v.list))},config:function(valspec){var values;values=((valspec!=null?valspec.values:void 8)||[]).map(function(v){return{value:v.key||v.value||v,name:v.label||v.value||v.key||v}});return{val:values.length?{type:"choice",name:"val",values:values}:{type:"text",name:"val"}}}},"is-not":{func:function(v,c){c==null&&(c={});return!(c.val&&in$(c.val,v.list))},config:function(valspec){var values;values=((valspec!=null?valspec.values:void 8)||[]).map(function(v){return{value:v.key||v.value||v,name:v.label||v.value||v.key||v}});return{val:values.length?{type:"choice",name:"val",values:values}:{type:"text",name:"val"}}}},"is-any":{func:function(v,c){var vals;c==null&&(c={});vals=Array.isArray(c.vals)?c.vals:typeof c.vals==="string"?c.vals.split(",").map(function(it){return it.trim()}).filter(function(it){return it}):[];return vals.some(function(it){return in$(it,v.list)})},config:function(valspec){var values;values=((valspec!=null?valspec.values:void 8)||[]).map(function(v){return{value:v.key||v.value||v,name:v.label||v.value||v.key||v}});return{vals:values.length?{type:"choice",name:"vals",values:values}:{type:"text",name:"vals",hint:"comma separated"}}}}}}];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);if(!this.id){this.id=Math.random().toString(36).substring(2)}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{id:this.id,enabled:this.enabled,opset:this.opset.id,op:this.op.id,config:this.config,msg:this.msg}},deserialize:function(v){this.toggle(v.enabled);return this.setOpset(v.opset,v.op,v.config)}});form.widget=function(opt){var o,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._meta_dig="";this._value=undefined;this._empty=true;this._mode=opt.mode||"edit";this._validate=opt.validate||null;this._opsets=(opt.opsets||[]).concat((opt.mod||{}).opsets||[]).map(function(opset){if(typeof opset==="string"){return form.opset.get(opset)}else if(opset instanceof 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._opsets=Object.fromEntries(function(){var i$,ref$,len$,results$=[];for(i$=0,len$=(ref$=this._opsets).length;i$<len$;++i$){o=ref$[i$];results$.push([o.id||o.name||"",o])}return results$}.call(this));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(){var this$=this;return Promise.resolve(this.mod&&this.mod.init?this.mod.init.apply(this):"").then(function(){return this$.fire("init")})},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$,i$,len$,k;ret=(ref1$={},ref1$.key=(ref$=this._meta).key,ref1$.title=ref$.title,ref1$.desc=ref$.desc,ref1$.isRequired=ref$.isRequired,ref1$.disabled=ref$.disabled,ref1$.readonly=ref$.readonly,ref1$.defaultValue=ref$.defaultValue,ref1$);ret.config=JSON.parse(JSON.stringify(this._meta.config||{}));ret.term=(this._meta.term||[]).map(function(it){return it.serialize()});for(i$=0,len$=(ref1$=["condition","tag"]).length;i$<len$;++i$){k=ref1$[i$];if(this._meta[k]!=null){ret[k]=JSON.parse(JSON.stringify(this._meta[k]))}}return ret},deserialize:function(v,o){var ref$,i$,len$,k,dig,this$=this;o==null&&(o={});ref$=this._meta;ref$.key=v.key;ref$.title=v.title;ref$.desc=v.desc;ref$.isRequired=v.isRequired;ref$.disabled=v.disabled;ref$.readonly=v.readonly;ref$.defaultValue=v.defaultValue;this._meta.config=JSON.parse(JSON.stringify(v.config||{}));for(i$=0,len$=(ref$=["condition","tag"]).length;i$<len$;++i$){k=ref$[i$];this._meta[k]=v[k]!=null?JSON.parse(JSON.stringify(v[k])):undefined}this._meta.term=(v.term||[]).map(function(t){var that;return new form.term(import$(import$({},t),(that=this$._opsets[t.opset||""])?{opset:that}:{}))});dig=JSON.stringify(v);if(this._meta_dig!==dig){this.fire("meta",this.serialize(),{init:o.init})}this.manager();this._meta_dig=dig;return Promise.resolve().then(function(){if(!o.init){return}if(!(this$._meta.defaultValue!=null&&this$.isEmpty())){return}return this$.value(this$._meta.defaultValue,{init:true,fromSource:true})}).then(function(){return this$.validate({init:o.init})}).then(function(){return this$.render()})},mode:function(m){var this$=this;if(!(m!=null)){return this._mode}return Promise.resolve().then(function(){var ref$;if(!(m==="edit"||m==="view"||m==="config")){return Promise.reject((ref$=new Error,ref$.name="lderror",ref$.id=1015,ref$))}if(this$._mode===m){return}this$._mode=m;this$.fire("mode",m);return this$.validate({init:true}).then(function(){return this$.render()})})},errors:function(){return this._errors},value:function(v,opt){var _v,_vs,p,this$=this;opt==null&&(opt={});if(arguments.length===0){return this._value!=null?JSON.parse(JSON.stringify(this._value)):this._value}if(this.isEqual(v,this._value)){return Promise.resolve()}_v=v!=null?JSON.parse(_vs=JSON.stringify(v)):v;this._value=_v;this._empty=this.isEmpty(_v);p=this.mod&&this.mod.value?Promise.resolve(this.mod.value.call(this,_vs!=null?JSON.parse(_vs):_v)):Promise.resolve();return p.then(function(){return this$.validate({init:opt.init}).then(function(){if(opt.fromSource){return}return this$.fire("change",this$._value!=null?JSON.parse(JSON.stringify(this$._value)):undefined)})})},disabled:function(){return!!this._meta.disabled},readonly:function(){return!!this._meta.readonly},isRequired:function(){return!!this._meta.isRequired},isEmpty:function(v){if(!arguments.length){v=this._value}if(this.mod&&this.mod.isEmpty){return this.mod.isEmpty.call(this,v)}else{return v===null||typeof v==="undefined"||v===""}},isEqual:function(u,v){var eu,ev;if(arguments.length===1){v=this._value}if(this.mod&&this.mod.isEqual){return this.mod.isEqual.call(this,u,v)}eu=this.isEmpty(u);ev=this.isEmpty(v);if(!eu!==!ev&&(eu||ev)){return false}if(eu&&ev){return true}return JSON.stringify(u)===JSON.stringify(v)},content:function(v){if(!arguments.length){v=this._value}if(this.mod&&this.mod.content){return this.mod.content.call(this,v)}else if(typeof v==="object"&&v&&v.hasOwnProperty(v)){return v.v}else{return v}},valdef:function(){if(this.mod&&this.mod.valdef){if(typeof this.mod.valdef==="function"){return this.mod.valdef.call(this)}return this.mod.valdef}return null},valspec:function(){if(this.mod&&this.mod.valspec){if(typeof this.mod.valspec==="function"){return this.mod.valspec.call(this)}return this.mod.valspec}return null},validate:function(opt){var lc,getVal,this$=this;opt==null&&(opt={});lc={v:null,_value:this._value};getVal=function(){if(lc.v){return lc.v}else{return lc.v=this$.content()}};return Promise.resolve().then(function(){if(this$.mod&&this$.mod.validate){return this$.mod.validate.call(this$,opt)}if(this$._validate){return this$._validate(getVal())}}).then(function(ret){var ref$;ret==null&&(ret=[]);if(ret.status===3){this$._errors=ret.errors||[];this$.status(3);this$.render();return this$._errors}if(ret&&ret.length){this$._errors=ret;this$.status(opt.init?1:2);this$.render();return this$._errors}if(this$._empty){if(this$._meta.isRequired){this$._errors=["required"];this$.status(opt.init?1:2);this$.render();return this$._errors}else{this$.status(0);this$.render();return this$._errors=[]}}return Promise.all((((ref$=this$._meta)!=null?ref$.term:void 8)||[]).filter(function(t){return t.enabled}).map(function(t){return t.validate(getVal()).then(function(v){return[t,v]})})).then(function(ret){var ref$,ref1$;if((ref$=this$._meta)!=null&&((ref1$=ref$.term)!=null&&ref1$.length)&&lc._value!==this$._value){return}this$._errors=ret.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)}},manager:function(arg$){var depth,ref$,ret;depth=(ref$=(arg$!=null?arg$:{}).depth)!=null?ref$:0;ret=this.mod&&this.mod.manager?this.mod.manager.apply(this,[{depth:depth}]):[];ret=(Array.isArray(ret)?ret:[ret]).filter(function(it){return it});if(this.disabled()){ret.map(function(it){return it.disable()})}return ret},ctrl: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.ctrl){return this.mod.ctrl.apply(this,args)}},resolve:function(path){if(this.mod&&this.mod.resolve){return this.mod.resolve.apply(this,[path])}else{return[]}},paths:function(path){if(this.mod&&this.mod.paths){return this.mod.paths.apply(this,[path])}else{return[]}},export:function(){var variant,header;if(this.mod&&this.mod["export"]){return this.mod["export"].apply(this)}variant=(this._meta.config||{}).variant;header=(this._meta.title||"")+(variant?"("+variant+")":"");return[{uid:"",header:header,sortKey:[],value:form.value.toString(this.value())}]},exportable:function(){var ms;if(!(ms=this.manager({depth:1})).length){return true}if(!(this.mod&&this.mod["export"])){return false}return ms.every(function(it){return it.exportable&&it.exportable()})},settle:function(){return Promise.resolve(this.mod&&this.mod.settle?this.mod.settle.apply(this):null)}});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}function in$(x,xs){var i=-1,l=xs.length>>>0;while(++i<l)if(x===xs[i])return true;return false}}).call(this);
package/package.json CHANGED
@@ -1 +1 @@
1
- {"author":"zbryikt","name":"@plotdb/form","license":"MIT","description":"form blocks","version":"0.2.5","browser":"index.min.js","main":"index.min.js","homepage":"https://github.com/plotdb/form","repository":{"type":"git","url":"git+https://github.com/plotdb/form.git"},"engines":{"node":">=10.14.1","npm":">=6.4.1"},"scripts":{"start":"npx server -r web -o true"},"devDependencies":{"@grantdash/composer":"^0.0.3","@loadingio/bootstrap.ext":"^0.0.12","@loadingio/debounce.js":"^1.0.1","@loadingio/ldquery":"^3.0.6","@loadingio/vscroll":"^0.0.11","@makeform/nest":"^0.3.11","@plotdb/datadom":"^0.2.0","@plotdb/datahub":"^0.6.0","@plotdb/json0":"^0.0.5","@plotdb/semver":"^0.0.3","@zbryikt/deploy":"^0.0.4","@zbryikt/template":"^2.5.0","bootstrap":"^4.6.1","bootstrap.native":"^3.0.13","dayjs":"^1.11.20","fedep":"^1.9.0","i18next":"^19.9.2","imgtype":"^0.0.3","ldcover":"^3.5.6","lddatetimepicker":"^0.0.13","ldfile":"^2.0.1","ldiconfont":"^0.3.0","ldloader":"^3.0.3","ldview":"^1.7.0","livescript":"^1.6.0","marked":"^15.0.12","moment":"^2.30.1","moment-timezone":"^0.6.2","@plotdb/quill":"^1.0.0","stylus":"^0.55.0","uglify-js":"^3.13.1","uglifycss":"^0.0.29","zmgr":"^2.2.3"},"frontendDependencies":{"root":"web/static/assets/lib","modules":["@grantdash/composer","@loadingio/bootstrap.ext","@loadingio/ldquery","@loadingio/debounce.js","@loadingio/ldcolorpicker","@loadingio/vscroll","@makeform/nest","@plotdb/sheet","@plotdb/block","@plotdb/json0","@plotdb/datadom","@plotdb/datahub","@plotdb/semver","@plotdb/rescope","@plotdb/suuid","@plotdb/konfig","@plotdb/csscope","bootstrap","bootstrap.native","curegex","dompurify","i18next","ldcolor","ldcover","ldiconfont","ldslider","ldpalettepicker","ldview","marked","proxise","moment-timezone","moment","imgtype","lddatetimepicker","dayjs","@plotdb/quill","ldloader","ldfile","zmgr"]},"dependencies":{"@loadingio/ldcolorpicker":"^3.0.6","@plotdb/block":"^5.7.2","@plotdb/csscope":"^5.0.4","@plotdb/datadom":"^0.2.0","@plotdb/konfig":"^4.11.1","@plotdb/rescope":"^5.0.17","@plotdb/sheet":"^0.7.0","@plotdb/suuid":"^0.6.0","curegex":"^0.0.4","dompurify":"^3.2.4","ldcolor":"^1.1.3","ldpalettepicker":"^4.1.2","ldslider":"^3.1.2","proxise":"^1.0.1"}}
1
+ {"author":"zbryikt","name":"@plotdb/form","license":"MIT","description":"form blocks","version":"0.3.1","browser":"index.min.js","main":"index.min.js","homepage":"https://github.com/plotdb/form","repository":{"type":"git","url":"git+https://github.com/plotdb/form.git"},"engines":{"node":">=10.14.1","npm":">=6.4.1"},"scripts":{"start":"npx server -r web -o true"},"devDependencies":{"@grantdash/composer":"^0.0.3","@loadingio/bootstrap.ext":"^0.0.12","@loadingio/debounce.js":"^1.0.1","@loadingio/ldquery":"^3.0.6","@loadingio/vscroll":"^0.0.11","@makeform/nest":"^0.3.11","@plotdb/datadom":"^0.2.0","@plotdb/datahub":"^0.6.0","@plotdb/json0":"^0.0.5","@plotdb/semver":"^0.0.3","@zbryikt/deploy":"^0.0.4","@zbryikt/template":"^2.5.0","bootstrap":"^4.6.1","bootstrap.native":"^3.0.13","dayjs":"^1.11.20","fedep":"^1.9.0","i18next":"^19.9.2","imgtype":"^0.0.3","ldcover":"^3.5.6","lddatetimepicker":"^0.0.13","ldfile":"^2.0.1","ldiconfont":"^0.3.0","ldloader":"^3.0.3","ldview":"^1.7.0","livescript":"^1.6.0","marked":"^15.0.12","moment":"^2.30.1","moment-timezone":"^0.6.2","@plotdb/quill":"^1.0.0","stylus":"^0.55.0","uglify-js":"^3.13.1","uglifycss":"^0.0.29","zmgr":"^2.2.3"},"frontendDependencies":{"root":"web/static/assets/lib","modules":["@grantdash/composer","@loadingio/bootstrap.ext","@loadingio/ldquery","@loadingio/debounce.js","@loadingio/ldcolorpicker","@loadingio/vscroll","@makeform/nest","@plotdb/sheet","@plotdb/block","@plotdb/json0","@plotdb/datadom","@plotdb/datahub","@plotdb/semver","@plotdb/rescope","@plotdb/suuid","@plotdb/konfig","@plotdb/csscope","bootstrap","bootstrap.native","curegex","dompurify","i18next","ldcolor","ldcover","ldiconfont","ldslider","ldpalettepicker","ldview","marked","proxise","moment-timezone","moment","imgtype","lddatetimepicker","dayjs","@plotdb/quill","ldloader","ldfile","zmgr"]},"dependencies":{"@loadingio/ldcolorpicker":"^3.0.6","@plotdb/block":"^5.7.2","@plotdb/csscope":"^5.0.4","@plotdb/datadom":"^0.2.0","@plotdb/konfig":"^4.11.1","@plotdb/rescope":"^5.0.17","@plotdb/sheet":"^0.7.0","@plotdb/suuid":"^0.6.0","curegex":"^0.0.4","dompurify":"^3.2.4","ldcolor":"^1.1.3","ldpalettepicker":"^4.1.2","ldslider":"^3.1.2","proxise":"^1.0.1"}}