@saltcorn/data 0.6.1 → 0.6.2-beta.0

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.
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Embedded Types definition.
3
3
  *
4
- * More types can be added by plugin store mechanism https://store.saltcorn.com/
4
+ * More types can be added by plugin store mechanism https://store.saltcorn.com/
5
5
  * @category saltcorn-data
6
- * @module base-plugin/types
6
+ * @module base-plugin/types
7
7
  * @subcategory base-plugin
8
8
  */
9
9
 
@@ -24,16 +24,102 @@ const {
24
24
  text_attr,
25
25
  } = require("@saltcorn/markup/tags");
26
26
  const { contract, is } = require("contractis");
27
- const { radio_group } = require("@saltcorn/markup/helpers");
27
+ const { radio_group, checkbox_group } = require("@saltcorn/markup/helpers");
28
28
  const { getState } = require("../db/state");
29
29
 
30
30
  const isdef = (x) => (typeof x === "undefined" || x === null ? false : true);
31
31
 
32
32
  const eqStr = (x, y) => `${x}` === `${y}`;
33
33
 
34
+ const number_slider = (type) => ({
35
+ configFields: (field) => [
36
+ ...(!isdef(field.attributes.max) && !isdef(field.attributes.min)
37
+ ? [
38
+ { name: "min", type, required: false },
39
+ { name: "max", type, required: false },
40
+ ]
41
+ : []),
42
+ //{ name: "also_entry", type: "Bool", label: "Also entry" },
43
+ ],
44
+ isEdit: true,
45
+ run: (nm, v, attrs = {}, cls, required, field) =>
46
+ input({
47
+ type: "range",
48
+ class: ["form-control", cls],
49
+ name: text_attr(nm),
50
+ "data-fieldname": text_attr(field.name),
51
+ disabled: attrs.disabled,
52
+ onChange: attrs.onChange,
53
+ step:
54
+ type === "Integer"
55
+ ? 1
56
+ : attrs.decimal_places
57
+ ? Math.pow(10, -attrs.decimal_places)
58
+ : "0.01",
59
+ id: `input${text_attr(nm)}`,
60
+ ...(attrs.max && { max: attrs.max }),
61
+ ...(attrs.min && { min: attrs.min }),
62
+ ...(isdef(v) && { value: text_attr(v) }),
63
+ }),
64
+ });
65
+ const progress_bar = (type) => ({
66
+ configFields: (field) => [
67
+ ...(!isdef(field.attributes.min)
68
+ ? [{ name: "min", type, required: true }]
69
+ : []),
70
+ ...(!isdef(field.attributes.max)
71
+ ? [{ name: "max", type, required: true }]
72
+ : []),
73
+ { name: "bar_color", type: "Color", label: "Bar color" },
74
+ { name: "bg_color", type: "Color", label: "Background color" },
75
+ { name: "px_height", type: "Integer", label: "Height in px" },
76
+ ],
77
+ isEdit: false,
78
+ run: (v, req, attrs = {}) =>
79
+ div(
80
+ {
81
+ style: {
82
+ width: "100%",
83
+ height: `${attrs.px_height || 8}px`,
84
+ backgroundColor: attrs.bg_color,
85
+ },
86
+ },
87
+ div({
88
+ style: {
89
+ width: `${(100 * (v - attrs.min)) / (attrs.max - attrs.min)}%`,
90
+ height: `${attrs.px_height || 8}px`,
91
+ backgroundColor: attrs.bar_color,
92
+ },
93
+ })
94
+ ),
95
+ });
96
+
97
+ const number_limit = (type, direction) => ({
98
+ isEdit: false,
99
+ isFilter: true,
100
+ run: (nm, v, attrs = {}, cls, required, field, state = {}) =>
101
+ input({
102
+ type: "number",
103
+ class: ["form-control", cls],
104
+ disabled: attrs.disabled,
105
+ onChange: `set_state_field('_${direction}_${nm}', this.value)`,
106
+ step:
107
+ type === "Integer"
108
+ ? "1"
109
+ : attrs.decimal_places
110
+ ? Math.pow(10, -attrs.decimal_places)
111
+ : "0.01",
112
+ ...(attrs.max && { max: attrs.max }),
113
+ ...(attrs.min && { min: attrs.min }),
114
+ ...(isdef(state[`_${direction}_${nm}`]) && {
115
+ value: text_attr(state[`_${direction}_${nm}`]),
116
+ }),
117
+ }),
118
+ });
119
+
34
120
  /**
35
- * @param {string} v
36
- * @param {string} optsStr
121
+ * @param {string} v
122
+ * @param {string} optsStr
37
123
  * @returns {string[]}
38
124
  */
39
125
  const getStrOptions = (v, optsStr) =>
@@ -64,11 +150,11 @@ const getStrOptions = (v, optsStr) =>
64
150
  );
65
151
 
66
152
  /**
67
- * string type
68
- * @namespace
69
- * @category saltcorn-data
70
- * @subcategory types / string
71
- */
153
+ * string type
154
+ * @namespace
155
+ * @category saltcorn-data
156
+ * @subcategory types / string
157
+ */
72
158
  const string = {
73
159
  /** @type {string} */
74
160
  name: "String",
@@ -242,6 +328,7 @@ const string = {
242
328
  "data-fieldname": text_attr(field.name),
243
329
  id: `input${text_attr(nm)}`,
244
330
  disabled: attrs.disabled,
331
+ onChange: attrs.onChange,
245
332
  },
246
333
  required || attrs.force_required
247
334
  ? getStrOptions(v, attrs.options)
@@ -260,6 +347,7 @@ const string = {
260
347
  disabled: attrs.disabled,
261
348
  "data-fieldname": text_attr(field.name),
262
349
  id: `input${text_attr(nm)}`,
350
+ onChange: attrs.onChange,
263
351
  "data-selected": v,
264
352
  "data-calc-options": encodeURIComponent(
265
353
  JSON.stringify(attrs.calcOptions)
@@ -272,6 +360,7 @@ const string = {
272
360
  disabled: attrs.disabled,
273
361
  class: ["form-control", cls],
274
362
  placeholder: attrs.placeholder,
363
+ onChange: attrs.onChange,
275
364
  "data-fieldname": text_attr(field.name),
276
365
  name: text_attr(nm),
277
366
  id: `input${text_attr(nm)}`,
@@ -292,6 +381,7 @@ const string = {
292
381
  name: text_attr(nm),
293
382
  "data-fieldname": text_attr(field.name),
294
383
  disabled: attrs.disabled,
384
+ onChange: attrs.onChange,
295
385
  id: `input${text_attr(nm)}`,
296
386
  rows: 5,
297
387
  },
@@ -315,6 +405,31 @@ const string = {
315
405
  run: (nm, v, attrs, cls, required, field) =>
316
406
  attrs.options
317
407
  ? radio_group({
408
+ class: cls,
409
+ name: text_attr(nm),
410
+ disabled: attrs.disabled,
411
+ inline: attrs.inline,
412
+ onChange: attrs.onChange,
413
+ options: Array.isArray(attrs.options)
414
+ ? attrs.options
415
+ : attrs.options.split(",").map((o) => o.trim()),
416
+ value: v,
417
+ })
418
+ : i("None available"),
419
+ },
420
+ checkbox_group: {
421
+ isEdit: false,
422
+ isFilter: true,
423
+ configFields: [
424
+ {
425
+ type: "Bool",
426
+ name: "inline",
427
+ label: "Inline",
428
+ },
429
+ ],
430
+ run: (nm, v, attrs, cls, required, field) =>
431
+ attrs && attrs.options
432
+ ? checkbox_group({
318
433
  class: cls,
319
434
  name: text_attr(nm),
320
435
  disabled: attrs.disabled,
@@ -339,7 +454,7 @@ const string = {
339
454
  disabled: attrs.disabled,
340
455
  class: ["form-control", cls],
341
456
  "data-fieldname": text_attr(field.name),
342
-
457
+ onChange: attrs.onChange,
343
458
  name: text_attr(nm),
344
459
  id: `input${text_attr(nm)}`,
345
460
  ...(isdef(v) && { value: text_attr(v) }),
@@ -347,7 +462,7 @@ const string = {
347
462
  },
348
463
  },
349
464
  /**
350
- * @param {*} v
465
+ * @param {*} v
351
466
  * @returns {string|undefined}
352
467
  */
353
468
  read: (v) => {
@@ -403,7 +518,7 @@ const string = {
403
518
  };
404
519
 
405
520
  /**
406
- * @param {string} s
521
+ * @param {string} s
407
522
  * @returns {boolean}
408
523
  */
409
524
  const is_valid_regexp = (s) => {
@@ -434,23 +549,23 @@ const int = {
434
549
  */
435
550
  contract: ({ min, max }) => is.integer({ lte: max, gte: min }),
436
551
  primaryKey: { sql_type: "serial" },
437
- /**
552
+ /**
438
553
  * @namespace
439
554
  * @category saltcorn-data
440
555
  * @subcategory types / int
441
556
  */
442
557
  fieldviews: {
443
- /**
558
+ /**
444
559
  * @namespace
445
560
  * @category saltcorn-data
446
561
  * @subcategory types / int
447
562
  */
448
563
  show: { isEdit: false, run: (s) => text(s) },
449
- /**
450
- * @namespace
451
- * @category saltcorn-data
452
- * @subcategory types / int
453
- */
564
+ /**
565
+ * @namespace
566
+ * @category saltcorn-data
567
+ * @subcategory types / int
568
+ */
454
569
  edit: {
455
570
  isEdit: true,
456
571
  run: (nm, v, attrs, cls, required, field) =>
@@ -460,6 +575,7 @@ const int = {
460
575
  disabled: attrs.disabled,
461
576
  "data-fieldname": text_attr(field.name),
462
577
  name: text_attr(nm),
578
+ onChange: attrs.onChange,
463
579
  id: `input${text_attr(nm)}`,
464
580
  step: "1",
465
581
  ...(attrs.max && { max: attrs.max }),
@@ -467,11 +583,15 @@ const int = {
467
583
  ...(isdef(v) && { value: text_attr(v) }),
468
584
  }),
469
585
  },
586
+ number_slider: number_slider("Integer"),
587
+ progress_bar: progress_bar("Integer"),
588
+ above_input: number_limit("Integer", "gte"),
589
+ below_input: number_limit("Integer", "lte"),
470
590
  },
471
591
  /** @type {object[]} */
472
592
  attributes: [
473
- { name: "max", type: "Integer", required: false },
474
593
  { name: "min", type: "Integer", required: false },
594
+ { name: "max", type: "Integer", required: false },
475
595
  ],
476
596
  /**
477
597
  * @param {object} param
@@ -480,7 +600,7 @@ const int = {
480
600
  validate_attributes: ({ min, max }) =>
481
601
  !isdef(min) || !isdef(max) || max > min,
482
602
  /**
483
- * @param {object} v
603
+ * @param {object} v
484
604
  * @returns {object}
485
605
  */
486
606
  read: (v) => {
@@ -496,7 +616,7 @@ const int = {
496
616
  }
497
617
  },
498
618
  /**
499
- * @param {object} param
619
+ * @param {object} param
500
620
  * @returns {boolean}
501
621
  */
502
622
  validate: ({ min, max }) => (x) => {
@@ -521,17 +641,17 @@ const color = {
521
641
  * @returns {function}
522
642
  */
523
643
  contract: () => is.str,
524
- /**
644
+ /**
525
645
  * @namespace
526
646
  * @category saltcorn-data
527
647
  * @subcategory types / color
528
648
  */
529
649
  fieldviews: {
530
- /**
531
- * @namespace
532
- * @category saltcorn-data
533
- * @subcategory types / color
534
- */
650
+ /**
651
+ * @namespace
652
+ * @category saltcorn-data
653
+ * @subcategory types / color
654
+ */
535
655
  show: {
536
656
  isEdit: false,
537
657
  run: (s) =>
@@ -542,11 +662,11 @@ const color = {
542
662
  })
543
663
  : "",
544
664
  },
545
- /**
546
- * @namespace
547
- * @category saltcorn-data
548
- * @subcategory types / color
549
- */
665
+ /**
666
+ * @namespace
667
+ * @category saltcorn-data
668
+ * @subcategory types / color
669
+ */
550
670
  edit: {
551
671
  isEdit: true,
552
672
  run: (nm, v, attrs, cls, required, field) =>
@@ -554,6 +674,7 @@ const color = {
554
674
  type: "color",
555
675
  class: ["form-control", cls],
556
676
  disabled: attrs.disabled,
677
+ onChange: attrs.onChange,
557
678
  "data-fieldname": text_attr(field.name),
558
679
  name: text_attr(nm),
559
680
  id: `input${text_attr(nm)}`,
@@ -564,7 +685,7 @@ const color = {
564
685
  /** @type {object[]} */
565
686
  attributes: [],
566
687
  /**
567
- * @param {object} v
688
+ * @param {object} v
568
689
  * @returns {object}
569
690
  */
570
691
  read: (v) => {
@@ -585,7 +706,7 @@ const color = {
585
706
 
586
707
  /**
587
708
  * Float type
588
- * @namespace
709
+ * @namespace
589
710
  * @category saltcorn-data
590
711
  * @subcategory types / float
591
712
  */
@@ -601,23 +722,23 @@ const float = {
601
722
  * @returns {function}
602
723
  */
603
724
  contract: ({ min, max }) => is.number({ lte: max, gte: min }),
604
- /**
605
- * @namespace
725
+ /**
726
+ * @namespace
606
727
  * @category saltcorn-data
607
728
  * @subcategory types / float
608
729
  */
609
730
  fieldviews: {
610
- /**
611
- * @namespace
612
- * @category saltcorn-data
613
- * @subcategory types / float
614
- */
731
+ /**
732
+ * @namespace
733
+ * @category saltcorn-data
734
+ * @subcategory types / float
735
+ */
615
736
  show: { isEdit: false, run: (s) => text(s) },
616
- /**
617
- * @namespace
618
- * @category saltcorn-data
619
- * @subcategory types / float
620
- */
737
+ /**
738
+ * @namespace
739
+ * @category saltcorn-data
740
+ * @subcategory types / float
741
+ */
621
742
  edit: {
622
743
  isEdit: true,
623
744
  run: (nm, v, attrs, cls, required, field) =>
@@ -627,6 +748,7 @@ const float = {
627
748
  name: text_attr(nm),
628
749
  "data-fieldname": text_attr(field.name),
629
750
  disabled: attrs.disabled,
751
+ onChange: attrs.onChange,
630
752
  step: attrs.decimal_places
631
753
  ? Math.pow(10, -attrs.decimal_places)
632
754
  : "0.01",
@@ -636,16 +758,20 @@ const float = {
636
758
  ...(isdef(v) && { value: text_attr(v) }),
637
759
  }),
638
760
  },
761
+ number_slider: number_slider("Float"),
762
+ progress_bar: progress_bar("Float"),
763
+ above_input: number_limit("Float", "gte"),
764
+ below_input: number_limit("Float", "lte"),
639
765
  },
640
766
  /** @type {object[]} */
641
767
  attributes: [
642
- { name: "max", type: "Float", required: false },
643
768
  { name: "min", type: "Float", required: false },
769
+ { name: "max", type: "Float", required: false },
644
770
  { name: "units", type: "String", required: false },
645
771
  { name: "decimal_places", type: "Integer", required: false },
646
772
  ],
647
773
  /**
648
- * @param {object} v
774
+ * @param {object} v
649
775
  * @returns {number|string|undefined}
650
776
  */
651
777
  read: (v) => {
@@ -671,7 +797,7 @@ const float = {
671
797
  };
672
798
 
673
799
  /**
674
- * @param {object} req
800
+ * @param {object} req
675
801
  * @returns {string|undefined}
676
802
  */
677
803
  const locale = (req) => {
@@ -680,7 +806,7 @@ const locale = (req) => {
680
806
  };
681
807
 
682
808
  /**
683
- * @param {*} x
809
+ * @param {*} x
684
810
  * @returns {*}
685
811
  */
686
812
  const logit = (x) => {
@@ -690,7 +816,7 @@ const logit = (x) => {
690
816
 
691
817
  /**
692
818
  * Date type
693
- * @namespace
819
+ * @namespace
694
820
  * @category saltcorn-data
695
821
  * @subcategory types / date
696
822
  */
@@ -705,17 +831,17 @@ const date = {
705
831
  contract: () => is.date,
706
832
  /** @type {object[]} */
707
833
  attributes: [],
708
- /**
709
- * @namespace
834
+ /**
835
+ * @namespace
710
836
  * @category saltcorn-data
711
- * @subcategory types / date
837
+ * @subcategory types / date
712
838
  */
713
839
  fieldviews: {
714
- /**
715
- * @namespace
716
- * @category saltcorn-data
717
- * @subcategory types / date
718
- */
840
+ /**
841
+ * @namespace
842
+ * @category saltcorn-data
843
+ * @subcategory types / date
844
+ */
719
845
  show: {
720
846
  isEdit: false,
721
847
  run: (d, req) =>
@@ -727,11 +853,11 @@ const date = {
727
853
  : ""
728
854
  ),
729
855
  },
730
- /**
731
- * @namespace
732
- * @category saltcorn-data
733
- * @subcategory types / date
734
- */
856
+ /**
857
+ * @namespace
858
+ * @category saltcorn-data
859
+ * @subcategory types / date
860
+ */
735
861
  showDay: {
736
862
  isEdit: false,
737
863
  run: (d, req) =>
@@ -743,7 +869,7 @@ const date = {
743
869
  : ""
744
870
  ),
745
871
  },
746
- /**
872
+ /**
747
873
  * @namespace
748
874
  * @category saltcorn-data
749
875
  * @subcategory types / date
@@ -764,7 +890,7 @@ const date = {
764
890
  return text(moment(d).format(options.format));
765
891
  },
766
892
  },
767
- /**
893
+ /**
768
894
  * @namespace
769
895
  * @category saltcorn-data
770
896
  * @subcategory types / date
@@ -778,7 +904,7 @@ const date = {
778
904
  else return text(moment(d).fromNow());
779
905
  },
780
906
  },
781
- /**
907
+ /**
782
908
  * @namespace
783
909
  * @category saltcorn-data
784
910
  * @subcategory types / date
@@ -790,7 +916,7 @@ const date = {
790
916
  return text(moment.duration(new Date() - d).years());
791
917
  },
792
918
  },
793
- /**
919
+ /**
794
920
  * @namespace
795
921
  * @category saltcorn-data
796
922
  * @subcategory types / date
@@ -803,6 +929,7 @@ const date = {
803
929
  class: ["form-control", cls],
804
930
  "data-fieldname": text_attr(field.name),
805
931
  name: text_attr(nm),
932
+ onChange: attrs.onChange,
806
933
  disabled: attrs.disabled,
807
934
  id: `input${text_attr(nm)}`,
808
935
  ...(isdef(v) && {
@@ -812,7 +939,7 @@ const date = {
812
939
  }),
813
940
  }),
814
941
  },
815
- /**
942
+ /**
816
943
  * @namespace
817
944
  * @category saltcorn-data
818
945
  * @subcategory types / date
@@ -825,6 +952,7 @@ const date = {
825
952
  class: ["form-control", cls],
826
953
  "data-fieldname": text_attr(field.name),
827
954
  name: text_attr(nm),
955
+ onChange: attrs.onChange,
828
956
  disabled: attrs.disabled,
829
957
  id: `input${text_attr(nm)}`,
830
958
  ...(isdef(v) && {
@@ -835,17 +963,17 @@ const date = {
835
963
  }),
836
964
  },
837
965
  },
838
- /**
839
- * @namespace
966
+ /**
967
+ * @namespace
840
968
  * @category saltcorn-data
841
- * @subcategory types / date
969
+ * @subcategory types / date
842
970
  */
843
971
  presets: {
844
972
  Now: () => new Date(),
845
973
  },
846
974
  /**
847
- * @param {object} v
848
- * @param {object} attrs
975
+ * @param {object} v
976
+ * @param {object} attrs
849
977
  * @returns {object}
850
978
  */
851
979
  read: (v, attrs) => {
@@ -869,7 +997,7 @@ const date = {
869
997
 
870
998
  /**
871
999
  * Boolean Type
872
- * @namespace
1000
+ * @namespace
873
1001
  * @category saltcorn-data
874
1002
  * @subcategory types / bool
875
1003
  */
@@ -882,13 +1010,13 @@ const bool = {
882
1010
  * @returns {function}
883
1011
  */
884
1012
  contract: () => is.bool,
885
- /**
886
- * @namespace
1013
+ /**
1014
+ * @namespace
887
1015
  * @category saltcorn-data
888
1016
  * @subcategory types / bool
889
1017
  */
890
1018
  fieldviews: {
891
- /**
1019
+ /**
892
1020
  * @namespace
893
1021
  * @category saltcorn-data
894
1022
  * @subcategory types / bool
@@ -906,8 +1034,8 @@ const bool = {
906
1034
  })
907
1035
  : "",
908
1036
  },
909
- /**
910
- * @namespace
1037
+ /**
1038
+ * @namespace
911
1039
  * @category saltcorn-data
912
1040
  * @subcategory types / bool
913
1041
  */
@@ -920,8 +1048,8 @@ const bool = {
920
1048
  ? input({ type: "checkbox", disabled: true })
921
1049
  : "",
922
1050
  },
923
- /**
924
- * @namespace
1051
+ /**
1052
+ * @namespace
925
1053
  * @category saltcorn-data
926
1054
  * @subcategory types / bool
927
1055
  */
@@ -929,8 +1057,8 @@ const bool = {
929
1057
  isEdit: false,
930
1058
  run: (v) => (v === true ? "True" : v === false ? "False" : ""),
931
1059
  },
932
- /**
933
- * @namespace
1060
+ /**
1061
+ * @namespace
934
1062
  * @category saltcorn-data
935
1063
  * @subcategory types / bool
936
1064
  */
@@ -941,14 +1069,15 @@ const bool = {
941
1069
  class: ["mr-2 mt-1", cls],
942
1070
  "data-fieldname": text_attr(field.name),
943
1071
  type: "checkbox",
1072
+ onChange: attrs.onChange,
944
1073
  name: text_attr(nm),
945
1074
  id: `input${text_attr(nm)}`,
946
1075
  ...(v && { checked: true }),
947
1076
  ...(attrs.disabled && { onclick: "return false;" }),
948
1077
  }),
949
1078
  },
950
- /**
951
- * @namespace
1079
+ /**
1080
+ * @namespace
952
1081
  * @category saltcorn-data
953
1082
  * @subcategory types / bool
954
1083
  */
@@ -981,8 +1110,8 @@ const bool = {
981
1110
  /** @type {object[]} */
982
1111
  attributes: [],
983
1112
  /**
984
- * @param {*} rec
985
- * @param {string} name
1113
+ * @param {*} rec
1114
+ * @param {string} name
986
1115
  * @returns {boolean|null}
987
1116
  */
988
1117
  readFromFormRecord: (rec, name) => {
@@ -992,7 +1121,7 @@ const bool = {
992
1121
  return rec[name] ? true : false;
993
1122
  },
994
1123
  /**
995
- * @param {object} v
1124
+ * @param {object} v
996
1125
  * @returns {boolean|null}
997
1126
  */
998
1127
  read: (v) => {
@@ -1007,12 +1136,12 @@ const bool = {
1007
1136
  }
1008
1137
  },
1009
1138
  /**
1010
- * @param {object} v
1139
+ * @param {object} v
1011
1140
  * @returns {object}
1012
1141
  */
1013
1142
  readFromDB: (v) => !!v,
1014
- /**
1015
- * @param {object} v
1143
+ /**
1144
+ * @param {object} v
1016
1145
  * @returns {object}
1017
1146
  */
1018
1147
  listAs: (v) => JSON.stringify(v),