-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
index.d.ts
1569 lines (1431 loc) · 56.9 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable no-extra-semi */
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */
import {List as ImmList, Map as ImmMap, OrderedMap as ImmOMap} from "immutable";
import {ElementType, ReactElement, Factory} from "react";
import moment from "moment";
import type { Moment as MomentType } from "moment";
import type { i18n } from "i18next";
export type Moment = MomentType;
export type ImmutableList<T> = ImmList<T>;
export type ImmutableMap<K, V> = ImmMap<K, V>;
export type ImmutableOMap<K, V> = ImmOMap<K, V>;
export type AnyImmutable = ImmutableList<any> | ImmutableMap<string, any> | ImmutableOMap<string, any>;
////////////////
// common
/////////////////
type ReactKey = string | number;
interface ReactAttributes {
key?: ReactKey | null | undefined;
}
export type FactoryWithContext<P> = (props: ReactAttributes & P, ctx?: ConfigContext) => ReactElement<P>;
export type RenderedReactElement = ReactElement | string;
export type SerializedFunction = JsonLogicFunction | string;
type AnyObject = Record<string, unknown>;
type Empty = null | undefined;
type ImmutablePath = ImmutableList<string>;
type IdPath = Array<string> | ImmutablePath; // should be used in actions only
type Optional<T> = {
[P in keyof T]?: T[P];
}
type OptionalBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
type TypedMap<T> = Record<string, T>;
// You can not use a union for types on a key, but can define overloaded accessors of different types.
// Key can be a string OR number
type TypedKeyMap<K extends string|number, T> = {
[key: string]: T;
[key: number]: T;
}
interface ObjectToImmOMap<P> extends ImmutableOMap<keyof P, any> {
get<K extends keyof P>(name: K): P[K];
get(name: string): any;
}
export type AsyncListValues = Array<any>;
// for export/import
type MongoValue = any;
type ElasticSearchQueryType = string;
type JsonLogicResult = {
logic?: JsonLogicTree;
data?: Object;
errors?: Array<string>;
}
type JsonLogicFunction = Object;
type JsonLogicTree = Object;
type JsonLogicValue = any;
type JsonLogicField = { "var": string };
interface SpelRawValue {
type: string;
children?: SpelRawValue[];
val?: RuleValue;
methodName?: string;
args?: SpelRawValue[];
obj?: SpelRawValue[];
isVar?: boolean;
cls: string[];
}
export type ConfigContext = {
utils: TypedMap<any>;
W: TypedMap<ElementType<any>>;
O: TypedMap<ElementType<any>>;
components?: TypedMap<ElementType<any>>;
[key: string]: any;
};
export type FlatItemPosition = {
caseNo: number | null;
globalNoByType: number;
indexPath: number[];
globalLeafNo?: number;
globalGroupNo?: number;
};
export type FlatItem = {
node: ImmutableItem;
index: number; // index in `flat`
id: string;
path: string[];
type: ItemType;
parent: string | null;
parentType: ItemType | null;
children: string[] | null;
caseId: string | null;
caseNo: number | null;
prev: string | null;
next: string | null;
lev: number; // depth level
isLeaf: boolean; // is atomic rule OR rule inside rule_group
isAtomicRule: boolean; // is atomic (rule or rule_group, but not rules inside rule_group)
isLocked: boolean;
// vertical
height: number; // visible height
_height: number; // real height (incl. collapsed)
top: number | null; // null if inside collapsed
bottom: number | null; // null if inside collapsed
// object with numbers indicating # of item in tree
position?: FlatItemPosition;
// for any group
depth?: number; // children of rule_group are not counted, collapsed are not counted
// for case only
isDefaultCase?: boolean;
atomicRulesCountInCase?: number;
// unused
_top: number;
collapsed: boolean;
/**
* @deprecated use isLeaf instead
*/
leaf: boolean;
};
export type FlatTree = {
items: TypedMap<FlatItem>;
flat: string[]; // ids of all items in top-to-bottom order
cases: string[]; // ids of cases
};
////////////////
// query value
/////////////////
// tip: Date is stored as string
export type SimpleValue = boolean | number | string | Array<string> | undefined | null;
type AnyValue = any;
export type RuleValue = SimpleValue | FuncValue | AnyValue;
// tip: multiselect value is stored as Array<string>, not ImmutableList<string>
export type RuleValueI = SimpleValue | FuncValueI | AnyValue;
export type FieldPath = string;
export interface FuncArgValue<V = unknown> {
value: V;
valueSrc?: ValueSource;
}
export type FuncArgValueI = ObjectToImmOMap<FuncArgValue<RuleValueI>>;
export interface FuncValue {
func: string;
args: TypedMap<FuncArgValue<RuleValue>>;
}
export type FuncValueI = ObjectToImmOMap<{
func: string;
args: ImmutableOMap<string, FuncArgValueI>;
}>;
export type FieldValue = FieldPath | FuncValue;
export type FieldValueI = FieldPath | FuncValueI;
export type AnyFieldValue = FieldValue | FieldValueI;
export type ValueSource = "value" | "field" | "func" | "const";
export type FieldSource = "field" | "func";
export type RuleGroupMode = "struct" | "some" | "array";
export type ItemType = "group" | "rule_group" | "rule" | "case_group" | "switch_group";
// note: these preperties types are used for actions
export type AnyRuleProperties = RuleProperties | RuleGroupExtProperties | RuleGroupProperties;
export type AnyGroupProperties = GroupProperties | SwitchGroupProperties | CaseGroupProperties;
export type ItemProperties = AnyRuleProperties | GroupProperties;
interface ExtraActionProperties {
// note: id can pre-generated for actions addRule, addGroup
id?: string;
}
interface BasicItemProperties {
isLocked?: boolean;
}
export type OperatorOptions = Record<string, SimpleValue>;
export type OperatorOptionsI = ImmMap<string, SimpleValue>;
export interface RuleProperties extends BasicItemProperties {
field: FieldValue | Empty;
fieldSrc?: FieldSource;
fieldType?: string;
fieldError?: string | Empty;
operator: string | Empty;
value: Array<RuleValue>;
valueSrc?: Array<ValueSource>;
valueType?: Array<string>;
valueError?: Array<string | Empty>;
operatorOptions?: OperatorOptions | Empty;
}
export interface RuleGroupExtProperties extends RuleProperties {
field: FieldPath | Empty; // tip: field can be only string, not func
mode: RuleGroupMode;
conjunction?: string;
not?: boolean;
}
export interface RuleGroupProperties extends BasicItemProperties {
field: FieldPath | Empty; // tip: field can be only string, not func
mode?: RuleGroupMode;
}
export interface GroupProperties extends BasicItemProperties {
conjunction: string;
not?: boolean;
}
export interface SwitchGroupProperties extends BasicItemProperties {
// todo: any properties here?
}
export interface CaseGroupProperties extends BasicItemProperties {
value?: Array<RuleValue>;
valueSrc?: Array<ValueSource>;
valueType?: Array<string>;
valueError?: Array<string | Empty>;
field?: string; // todo: only "!case_value" ?
}
//////
interface _RulePropertiesI extends Omit<RuleProperties, "field" | "value" | "valueSrc" | "valueType" | "valueError" | "operatorOptions"> {
field: FieldValueI | Empty;
value: ImmutableList<RuleValueI>;
valueSrc?: ImmutableList<ValueSource>;
valueType?: ImmutableList<string>;
valueError?: ImmutableList<string | Empty>;
operatorOptions?: OperatorOptionsI;
}
interface _CaseGroupPropertiesI extends Omit<CaseGroupProperties, "field" | "value" | "valueSrc" | "valueType" | "valueError"> {
field?: string | Empty; // todo: only "!case_value" ?
value?: ImmutableList<RuleValueI>;
valueSrc?: ImmutableList<ValueSource>;
valueType?: ImmutableList<string>;
valueError?: ImmutableList<string | Empty>;
}
// correct unions
interface _RuleGroupExtPropertiesI extends Pick<RuleGroupExtProperties, "field" | "mode" | "conjunction" | "not">, Omit<_RulePropertiesI, "field"> {}
interface _AnyRulePropertiesI extends Optional<_RulePropertiesI>, Optional<Pick<_RuleGroupExtPropertiesI, "mode" | "conjunction" | "not">> {}
interface _ItemPropertiesI extends _AnyRulePropertiesI, Optional<Pick<GroupProperties, "conjunction" | "not">> {}
interface _ItemOrCasePropertiesI extends Omit<_ItemPropertiesI, "field">, Optional<_CaseGroupPropertiesI> {}
interface _GroupOrSwitchPropertiesI extends Optional<GroupProperties>, Optional<SwitchGroupProperties> {}
export interface BasicItemPropertiesI<P = BasicItemProperties> extends ObjectToImmOMap<P> {}
export interface ImmutableRuleProperties<P = _RulePropertiesI> extends BasicItemPropertiesI<P> {}
export interface ImmutableRuleGroupProperties<P = RuleGroupProperties> extends BasicItemPropertiesI<P> {}
export interface ImmutableRuleGroupExtProperties<P = _RuleGroupExtPropertiesI> extends ImmutableRuleProperties<P> {}
export interface ImmutableGroupProperties<P = GroupProperties> extends BasicItemPropertiesI<P> {}
export interface ImmutableSwitchGroupProperties<P = SwitchGroupProperties> extends BasicItemPropertiesI<P> {}
export interface ImmutableCaseGroupProperties<P = CaseGroupProperties> extends BasicItemPropertiesI<P> {}
// correct unions
export interface ImmutableAnyRuleProperties<P = _AnyRulePropertiesI> extends BasicItemPropertiesI<P> {}
export interface ImmutableItemProperties<P = _ItemPropertiesI> extends BasicItemPropertiesI<P> {}
export interface ImmutableItemOrCaseProperties<P = _ItemOrCasePropertiesI> extends BasicItemPropertiesI<P> {}
export interface ImmutableGroupOrSwitchProperties<P = _GroupOrSwitchPropertiesI> extends BasicItemPropertiesI<P> {}
//////
export interface JsonBasicItem {
type: ItemType;
id?: string;
}
export interface JsonRule extends Omit<JsonBasicItem, "type"> {
type: "rule";
properties: RuleProperties;
}
// OldJson.. are types with `children1` as object instead of array (discouraged)
// `children1` as object can be used for import or export with children1AsArray = false
export interface OldJsonSwitchGroup extends JsonBasicItem {
type: "switch_group";
children1?: {[id: string]: OldJsonCaseGroup} | OldJsonCaseGroup[];
properties?: SwitchGroupProperties;
}
export interface OldJsonCaseGroup extends JsonBasicItem {
type: "case_group";
children1?: {[id: string]: OldJsonItem} | OldJsonItem[];
properties?: CaseGroupProperties;
}
export interface OldJsonGroup extends JsonBasicItem {
type: "group";
children1?: {[id: string]: OldJsonItem} | OldJsonItem[];
properties?: GroupProperties;
}
export interface OldJsonRuleGroup extends JsonBasicItem {
type: "rule_group";
children1?: {[id: string]: JsonRule} | JsonRule[];
properties?: RuleGroupProperties;
}
export interface OldJsonRuleGroupExt extends JsonBasicItem {
type: "rule_group";
children1?: {[id: string]: JsonRule} | JsonRule[];
properties?: RuleGroupExtProperties;
}
export type OldJsonAnyRule = JsonRule|OldJsonRuleGroup|OldJsonRuleGroupExt;
export type OldJsonItem = OldJsonGroup|OldJsonAnyRule;
export type OldJsonTree = OldJsonGroup|OldJsonSwitchGroup;
// Json.. (in comparison with OldJson..) are types with `children1` as array (default)
// Used as default for export with children1AsArray = true
export interface JsonSwitchGroup extends OldJsonSwitchGroup {
children1?: JsonCaseGroup[];
}
export interface JsonCaseGroup extends OldJsonCaseGroup {
children1?: JsonItem[];
}
export interface JsonGroup extends OldJsonGroup {
children1?: JsonItem[];
}
export interface JsonRuleGroup extends OldJsonRuleGroup {
children1?: JsonRule[];
}
export interface JsonRuleGroupExt extends OldJsonRuleGroupExt {
children1?: JsonRule[];
}
export type JsonAnyRule = JsonRule|JsonRuleGroup|JsonRuleGroupExt;
export type JsonItem = JsonGroup|JsonAnyRule;
export type JsonTree = JsonGroup|JsonSwitchGroup;
////
interface _BasicItemI {
type: ItemType;
id: string;
}
interface _RuleI extends _BasicItemI {
type: "rule";
properties: ImmutableRuleProperties;
}
interface _GroupI extends _BasicItemI {
type: "group";
children1?: ImmOMap<string, ImmutableItem>;
properties: ImmutableGroupProperties;
}
interface _RuleGroupI extends _BasicItemI {
type: "rule_group";
children1?: ImmOMap<string, ImmutableRule>;
properties: ImmutableRuleGroupProperties;
}
interface _RuleGroupExtI extends _BasicItemI {
type: "rule_group";
children1?: ImmOMap<string, ImmutableRule>;
properties: ImmutableRuleGroupExtProperties;
}
interface _SwitchGroupI extends _BasicItemI {
type: "switch_group";
children1?: ImmOMap<string, ImmutableCaseGroup>;
properties: ImmutableSwitchGroupProperties;
}
interface _CaseGroupI extends _BasicItemI {
type: "case_group";
children1?: ImmOMap<string, ImmutableGroup>;
properties: ImmutableCaseGroupProperties;
}
// Fix unions manially:
type _OmitI<T> = Omit<T, "type" | "properties" | "children1">;
// type _AnyRuleI = _RuleI | _RuleGroupI | _RuleGroupExtI;
interface _AnyRuleI extends _OmitI<_RuleI> {
type: "rule" | "rule_group";
properties: ImmutableAnyRuleProperties;
children1?: ImmOMap<string, ImmutableRule>;
}
// type _ItemI = _GroupI | _AnyRuleI;
interface _ItemI extends _OmitI<_GroupI>, _OmitI<_AnyRuleI> {
type: "rule" | "rule_group" | "group";
properties: ImmutableItemProperties;
children1?: ImmOMap<string, ImmutableItem>;
}
// type _ItemOrCaseI = _ItemI | _CaseGroupI;
interface _ItemOrCaseI extends _OmitI<_ItemI>, _OmitI<_CaseGroupI> {
type: "rule" | "rule_group" | "group" | "case_group";
properties: ImmutableItemOrCaseProperties;
children1?: ImmOMap<string, ImmutableItem>;
}
// type _TreeI = _GroupI | _SwitchGroupI;
interface _TreeI extends _OmitI<_GroupI>, _OmitI<_SwitchGroupI> {
type: "group" | "switch_group";
children1?: ImmOMap<string, ImmutableBasicItem<_ItemOrCaseI>>;
properties: ImmutableGroupOrSwitchProperties;
}
export interface ImmutableBasicItem<P = _BasicItemI> extends ObjectToImmOMap<P> {}
export interface ImmutableRule<P = _RuleI> extends ImmutableBasicItem<P> {}
export interface ImmutableGroup<P = _GroupI> extends ImmutableBasicItem<P> {}
export interface ImmutableRuleGroup<P = _RuleGroupI> extends ImmutableBasicItem<P> {}
export interface ImmutableRuleGroupExt<P = _RuleGroupExtI> extends ImmutableBasicItem<P> {}
export interface ImmutableSwitchGroup<P = _SwitchGroupI> extends ImmutableBasicItem<P> {}
export interface ImmutableCaseGroup<P = _CaseGroupI> extends ImmutableBasicItem<P> {}
export interface ImmutableAnyRule<P = _AnyRuleI> extends ImmutableBasicItem<P> {}
export interface ImmutableItem<P = _ItemI> extends ImmutableBasicItem<P> {}
export interface ImmutableTree<P = _TreeI> extends ImmutableBasicItem<P> {}
////////////////
// Utils
/////////////////
/**
* @deprecated
*/
export interface SpelConcatPart {
value: string;
type: "property" | "variable" | "const";
}
type SpelConcatParts = SpelConcatPart[];
interface SpelConcatCaseValue {
valueType: "case_value";
value: SpelConcatNormalValue[];
}
interface SpelConcatNormalValue {
value: string;
valueType: string;
valueSrc: "value" | "field";
isVariable?: boolean;
}
type SpelConcatValue = SpelConcatNormalValue | SpelConcatCaseValue;
export interface Translatable {
key: string;
args?: null | Record<string, any>;
}
export interface ValidationError extends Translatable {
// translated message
str?: string;
side?: "lhs" | "rhs" | "op";
delta?: number; // 0, 1, -1 for range, undefined for "lhs"
fixed?: boolean;
fixedFrom?: any;
fixedTo?: any;
}
export interface ValidationItemErrors {
path: Array<string>;
errors: ValidationError[];
itemStr?: string;
itemPosition?: FlatItemPosition & Pick<FlatItem, "type" | "index"> & {
isDeleted: boolean;
};
itemPositionStr?: string;
}
export type ValidationErrors = ValidationItemErrors[];
export interface SanitizeResult {
fixedTree: ImmutableTree;
fixedErrors: ValidationErrors;
nonFixedErrors: ValidationErrors;
allErrors: ValidationErrors;
}
export interface SanitizeOptions extends ValidationTranslateOptions {
removeEmptyGroups?: boolean; // default: true
removeEmptyRules?: boolean; // default: true
removeIncompleteRules?: boolean; // default: true
forceFix?: boolean; // default: false
}
export interface ValidationTranslateOptions {
translateErrors?: boolean; // default: true
includeItemsPositions?: boolean; // default: true
includeStringifiedItems?: boolean; // default: true
stringifyItemsUserFriendly?: boolean; // default: true
stringifyFixedItems?: boolean; // default: false (stringify item with error)
}
export interface ValidationOptions extends ValidationTranslateOptions {
}
interface Validation {
sanitizeTree(tree: ImmutableTree, config: Config, options?: SanitizeOptions): SanitizeResult;
validateTree(tree: ImmutableTree, config: Config, options?: ValidationOptions): ValidationErrors;
isValidTree(tree: ImmutableTree, config: Config): boolean;
getTreeBadFields(tree: ImmutableTree, config: Config): Array<FieldPath>;
translateValidation(tr: Translatable): string;
translateValidation(key: Translatable["key"], args?: Translatable["args"]): string;
/**
* @deprecated Use Utils.Validation.sanitizeTree() instead
*/
checkTree(tree: ImmutableTree, config: Config): ImmutableTree;
}
interface Import {
// tree
getTree(tree: ImmutableTree, light?: boolean, children1AsArray?: boolean): JsonTree;
getTree(tree: ImmutableTree, light: boolean, children1AsArray: false): OldJsonTree;
loadTree(jsonTree: JsonTree): ImmutableTree;
loadTree(jsonTree: OldJsonTree): ImmutableTree;
isImmutableTree(tree: any): boolean;
isTree(tree: any): boolean; // is JsonTree ?
isJsonLogic(value: any): boolean;
jsToImmutable(value: any): AnyImmutable;
// jsonlogic
loadFromJsonLogic(logicTree: JsonLogicTree | undefined, config: Config): ImmutableTree | undefined;
_loadFromJsonLogic(logicTree: JsonLogicTree | undefined, config: Config): [ImmutableTree | undefined, Array<string>];
// spel
loadFromSpel(spelStr: string, config: Config): [ImmutableTree | undefined, Array<string>];
}
interface Export {
jsonLogicFormat(tree: ImmutableTree, config: Config): JsonLogicResult;
/**
* @deprecated
*/
queryBuilderFormat(tree: ImmutableTree, config: Config): Object | undefined;
queryString(tree: ImmutableTree, config: Config, isForDisplay?: boolean, isDebugMode?: boolean): string | undefined;
sqlFormat(tree: ImmutableTree, config: Config): string | undefined;
_sqlFormat(tree: ImmutableTree, config: Config): [string | undefined, Array<string>];
spelFormat(tree: ImmutableTree, config: Config): string | undefined;
_spelFormat(tree: ImmutableTree, config: Config): [string | undefined, Array<string>];
mongodbFormat(tree: ImmutableTree, config: Config): Object | undefined;
_mongodbFormat(tree: ImmutableTree, config: Config): [Object | undefined, Array<string>];
elasticSearchFormat(tree: ImmutableTree, config: Config, syntax?: "ES_6_SYNTAX" | "ES_7_SYNTAX"): Object | undefined;
}
interface Autocomplete {
simulateAsyncFetch(all: ListValues, pageSize?: number, delay?: number): AsyncFetchListValuesFn;
getListValue(value: string | number, listValues: ListValues): ListItem; // get by value
// internal
mergeListValues(oldValues: ListItems, newValues: ListItems, toStart?: boolean): ListItems;
listValueToOption(listItem: ListItem): ListOptionUi;
}
interface ConfigUtils {
compressConfig(config: Config, baseConfig: Config): ZipConfig;
decompressConfig(zipConfig: ZipConfig, baseConfig: Config, ctx?: ConfigContext): Config;
compileConfig(config: Config): Config;
extendConfig(config: Config): Config;
getFieldConfig(config: Config, field: AnyFieldValue): FieldConfig;
getFuncConfig(config: Config, func: string): Func | null;
getFuncArgConfig(config: Config, func: string, arg: string): FuncArg | null;
getOperatorConfig(config: Config, operator: string, field?: AnyFieldValue): Operator | null;
getFieldWidgetConfig(config: Config, field: AnyFieldValue, operator: string, widget?: string, valueStr?: ValueSource): Widget | null;
isJSX(jsx: any): boolean;
isDirtyJSX(jsx: any): boolean;
cleanJSX(jsx: any): Object;
applyJsonLogic(logic: any, data?: any): any;
}
interface DefaultUtils {
getDefaultField(config: Config, canGetFirst?: boolean, parentRuleGroupField?: string): FieldValueI | null;
getDefaultSubField(config: Config, parentRuleGroupField?: string): FieldValueI | null;
getDefaultFieldSrc(config: Config, canGetFirst?: boolean): string;
getDefaultOperator(config: Config, field: Field, canGetFirst?: boolean): string;
defaultRule(id: string, config: Config): Record<string, ImmutableRule>;
defaultRoot(config: Config, canAddDefaultRule?: boolean): ImmutableGroup;
defaultItemProperties(config: Config, item: JsonItem): ImmutableItemProperties;
defaultGroupProperties(config: Config, groupFieldConfig?: FieldValueOrConfig): ImmutableGroupProperties;
defaultRuleProperties(config: Config, parentRuleGroupField?: string, item?: JsonItem, canUseDefaultFieldAndOp?: boolean, canGetFirst?: boolean): ImmutableRuleProperties;
/**
* @deprecated Use defaultGroupConjunction() instead
*/
defaultConjunction(config: Config): string;
defaultOperatorOptions(config: Config, operator: string, field: Field): OperatorOptionsI | null;
defaultGroupConjunction(config: Config, groupFieldConfig?: FieldValueOrConfig): string;
// createListWithOneElement<TItem>(el: TItem): ImmutableList<TItem>;
// createListFromArray<TItem>(array: TItem[]): ImmutableList<TItem>;
}
interface ExportUtils {
wrapWithBrackets(val: string): string;
spelEscape(val: any): string;
/**
* @deprecated
*/
spelFormatConcat(parts: SpelConcatParts): string;
/**
* @deprecated
*/
spelImportConcat(val: SpelConcatValue): [SpelConcatParts | undefined, Array<string>];
}
interface ListUtils {
getTitleInListValues(listValues: ListValues, value: string | number): string;
getListValue(value: string | number, listValues: ListValues): ListItem; // get by value
searchListValue(search: string, listValues: ListValues): ListItem; // search by value and title
listValuesToArray(listValues: ListValues): ListItems; // normalize
toListValue(value: string | number | ListItem, title?: string): ListItem | undefined; // create
makeCustomListValue(value: string | number): ListItem; // create
mapListValues<T>(listValues : ListValues, mapFn: (item: ListItem | undefined) => T | null) : T[];
}
interface TreeUtils {
jsToImmutable(value: any): AnyImmutable;
immutableToJs(imm: AnyImmutable): any;
isImmutable(value: any): boolean;
toImmutableList(path: string[]): ImmutablePath;
getItemByPath(tree: ImmutableTree, path: IdPath): ImmutableItem | undefined;
expandTreePath(path: ImmutablePath, ...suffix: string[]): ImmutablePath;
expandTreeSubpath(path: ImmutablePath, ...suffix: string[]): ImmutablePath;
fixEmptyGroupsInTree(tree: ImmutableTree): ImmutableTree;
fixPathsInTree(tree: ImmutableTree): ImmutableTree;
getFlatTree(tree: ImmutableTree, config?: Config): FlatTree;
getTotalReordableNodesCountInTree(tree: ImmutableTree): number;
getTotalRulesCountInTree(tree: ImmutableTree): number;
isEmptyTree(tree: ImmutableTree): boolean;
// case mode
getSwitchValues(tree: ImmutableTree): Array<any | null>;
}
interface OtherUtils {
uuid(): string;
deepFreeze(obj: any): any;
deepEqual(a: any, b: any): boolean;
shallowEqual(a: any, b: any, deep?: boolean): boolean;
mergeArraysSmart(a: string[], b: string[]): string[];
isJsonCompatible(tpl: object, target: object, bag: Record<string, any>): boolean; // mutates bag
isJsonLogic(value: any): boolean;
isJSX(jsx: any): boolean;
isDirtyJSX(jsx: any): boolean;
cleanJSX(jsx: any): Object;
escapeRegExp(str: string): string;
//applyToJS(imm: any): any; // same as immutableToJs
isImmutable(value: any): boolean;
toImmutableList(path: string[]): ImmutablePath;
}
export interface Utils extends Import, Export,
Pick<Validation, "sanitizeTree" | "validateTree" | "isValidTree" | "checkTree">,
Pick<ConfigUtils, "compressConfig" | "decompressConfig">,
Pick<OtherUtils, "uuid">,
Pick<TreeUtils, "getSwitchValues">
{
Import: Import;
Export: Export;
Autocomplete: Autocomplete;
Validation: Validation;
ConfigUtils: ConfigUtils;
DefaultUtils: DefaultUtils;
ExportUtils: ExportUtils;
ListUtils: ListUtils;
TreeUtils: TreeUtils;
OtherUtils: OtherUtils;
// libs
i18n: i18n;
moment: typeof moment;
}
/////////////////
// Config
/////////////////
export interface Config {
conjunctions: Conjunctions;
operators: Operators;
widgets: Widgets;
types: Types;
settings: Settings;
fields: Fields;
funcs?: Funcs;
ctx: ConfigContext;
}
export type ZipConfig = Omit<Config, "ctx">;
export interface ConfigMixin<C = Config, S = Settings> {
conjunctions?: Record<string, Partial<Conjunction>>;
operators?: Record<string, Partial<Operator<C>>>;
widgets?: Record<string, Partial<Widget<C>>>;
types?: Record<string, Partial<Type>>;
settings?: Partial<S>;
fields?: Record<string, Partial<FieldOrGroup>>;
funcs?: Record<string, Partial<FuncOrGroup>>;
ctx?: Partial<ConfigContext>;
}
/////////////////
// Actions
/////////////////
type Placement = "after" | "before" | "append" | "prepend";
type ActionType = string | "ADD_RULE" | "REMOVE_RULE" | "ADD_GROUP" | "ADD_CASE_GROUP" | "REMOVE_GROUP" | "SET_NOT" | "SET_LOCK" | "SET_CONJUNCTION" | "SET_FIELD" | "SET_FIELD_SRC" | "SET_OPERATOR" | "SET_VALUE" | "SET_VALUE_SRC" | "SET_OPERATOR_OPTION" | "MOVE_ITEM";
interface BaseAction {
type: ActionType;
id?: string; // for ADD_RULE, ADD_GROUP - id of new item
path?: string[]; // for all except MOVE_ITEM (for ADD_RULE/ADD_GROUP it's parent path)
conjunction?: string;
not?: boolean;
lock?: boolean;
field?: string;
operator?: string;
delta?: number; // for SET_VALUE
value?: SimpleValue;
valueType?: string;
srcKey?: ValueSource;
name?: string; // for SET_OPERATOR_OPTION
fromPath?: string[]; // for MOVE_ITEM
toPath?: string[]; // for MOVE_ITEM
placement?: Placement; // for MOVE_ITEM
properties?: TypedMap<any>; // for ADD_RULE, ADD_GROUP
}
export interface InputAction extends BaseAction {
config: Config;
}
export interface ActionMeta extends BaseAction {
affectedField?: string; // gets field name from `path` (or `field` for first SET_FIELD)
}
export interface Actions {
// tip: children will be converted to immutable ordered map in `_addChildren1`
addRule(path: IdPath, properties?: AnyRuleProperties & ExtraActionProperties, type?: ItemType, children?: Array<JsonAnyRule>): undefined;
removeRule(path: IdPath): undefined;
addGroup(path: IdPath, properties?: AnyGroupProperties & ExtraActionProperties, children?: Array<JsonItem>): undefined;
removeGroup(path: IdPath): undefined;
setNot(path: IdPath, not: boolean): undefined;
setLock(path: IdPath, lock: boolean): undefined;
setConjunction(path: IdPath, conjunction: string): undefined;
setField(path: IdPath, field: FieldValueI): undefined; // todo: try to support FieldValue - apply jsToImmutable() just like for `defaultField`
setFieldSrc(path: IdPath, fieldSrc: FieldSource): undefined;
setOperator(path: IdPath, operator: string): undefined;
setValue(path: IdPath, delta: number, value: RuleValueI, valueType: string): undefined; // todo: try to support RuleValue
setFuncValue(path: IdPath, delta: number, parentFuncs: Array<[string, string]>, argKey: string | null, value: SimpleValue, valueType: string | "!valueSrc"): undefined;
setValueSrc(path: IdPath, delta: number, valueSrc: ValueSource): undefined;
setOperatorOption(path: IdPath, name: string, value: SimpleValue): undefined;
moveItem(fromPath: IdPath, toPath: IdPath, placement: Placement): undefined;
setTree(tree: ImmutableTree): undefined;
}
interface TreeState {
tree: ImmutableTree;
__lastAction?: ActionMeta;
}
type TreeReducer = (state?: TreeState, action?: InputAction) => TreeState;
type TreeStore = (config: Config, tree?: ImmutableTree) => TreeReducer;
export interface TreeActions {
tree: {
setTree(config: Config, tree: ImmutableTree): InputAction;
addRule(config: Config, path: IdPath, properties?: AnyRuleProperties, type?: ItemType, children?: Array<JsonAnyRule>): InputAction;
removeRule(config: Config, path: IdPath): InputAction;
addDefaultCaseGroup(config: Config, path: IdPath, properties?: CaseGroupProperties, children?: Array<JsonAnyRule>): InputAction;
addCaseGroup(config: Config, path: IdPath, properties?: CaseGroupProperties, children?: Array<JsonAnyRule>): InputAction;
addGroup(config: Config, path: IdPath, properties?: GroupProperties, children?: Array<JsonItem>): InputAction;
removeGroup(config: Config, path: IdPath): InputAction;
moveItem(config: Config, fromPath: IdPath, toPath: IdPath, placement: Placement): InputAction;
};
group: {
setConjunction(config: Config, path: IdPath, conjunction: string): InputAction;
setNot(config: Config, path: IdPath, not: boolean): InputAction;
setLock(config: Config, path: IdPath, lock: boolean): InputAction;
};
rule: {
setField(config: Config, path: IdPath, field: FieldValueI): InputAction; // todo: try to support FieldValue
setFieldSrc(config: Config, path: IdPath, fieldSrc: FieldSource): InputAction;
setOperator(config: Config, path: IdPath, operator: string): InputAction;
setValue(config: Config, path: IdPath, delta: number, value: RuleValueI, valueType: string): InputAction; // todo: try to support RuleValue
setValueSrc(config: Config, path: IdPath, delta: number, valueSrc: ValueSource): InputAction;
setOperatorOption(config: Config, path: IdPath, name: string, value: SimpleValue): InputAction;
setFuncValue(config: Config, path: IdPath, delta: number, parentFuncs: Array<[string, string]>, argKey: string | null, value: SimpleValue, valueType: string | "!valueSrc"): InputAction;
};
}
/////////////////
// WidgetProps
// @ui
/////////////////
interface AbstractWidgetProps<C = Config> {
placeholder: string;
label?: string;
field: FieldValueI;
fieldDefinition: Field;
fieldSrc: FieldSource;
fieldType?: string;
fieldError?: string | Empty;
parentField?: string;
parentFuncs?: Array<[string, string]> | Empty; // array of [funcKey, argKey]
operator: string;
config: C;
delta?: number;
customProps?: AnyObject;
readonly?: boolean;
id?: string; // id of rule
groupId?: string; // id of parent group
widgetId?: string; // unique id of widget
isLHS?: boolean;
isSpecialRange?: boolean;
isFuncArg?: boolean;
// tip: setFuncValue prop exists, but is internal
}
interface BaseWidgetProps<C = Config, V = RuleValue> extends AbstractWidgetProps<C> {
value: V | Empty;
setValue(val: V | Empty, asyncListValues?: AsyncListValues): void;
valueError?: string | Empty;
errorMessage?: string | Empty; // fieldError or valueError
}
interface RangeWidgetProps<C = Config, V = RuleValue> extends AbstractWidgetProps<C> {
value: Array<V | Empty>;
setValue(val: Array<V | Empty>, asyncListValues?: AsyncListValues): void;
placeholders: Array<string>;
textSeparators: Array<string>;
valueError?: Array<string | Empty>;
errorMessage?: Array<string | Empty>; // same as valueError
}
// BaseWidgetProps | RangeWidgetProps
interface RangeableWidgetProps<C = Config, V = RuleValue> extends AbstractWidgetProps<C> {
value: V | Empty | Array<V | Empty>;
setValue(val: V | Empty | Array<V | Empty>, asyncListValues?: AsyncListValues): void;
placeholders?: Array<string>;
textSeparators?: Array<string>;
valueError?: string | Empty | Array<string | Empty>;
errorMessage?: string | Empty | Array<string | Empty>; // fieldError or valueError
}
export type WidgetProps<C = Config> = RangeableWidgetProps<C> & FieldSettings;
export type TextWidgetProps<C = Config> = BaseWidgetProps<C, string> & TextFieldSettings;
export type DateTimeWidgetProps<C = Config> = RangeableWidgetProps<C, string> & DateTimeFieldSettings;
export type BooleanWidgetProps<C = Config> = BaseWidgetProps<C, boolean> & BooleanFieldSettings;
export type NumberWidgetProps<C = Config> = RangeableWidgetProps<C, number> & NumberFieldSettings;
export type RangeSliderWidgetProps<C = Config> = RangeableWidgetProps<C, number> & NumberFieldSettings;
export type SelectWidgetProps<C = Config> = BaseWidgetProps<C, string | number> & SelectFieldSettings;
export type MultiSelectWidgetProps<C = Config> = BaseWidgetProps<C, string[] | number[]> & MultiSelectFieldSettings;
export type TreeSelectWidgetProps<C = Config> = BaseWidgetProps<C, string | number> & TreeSelectFieldSettings;
export type TreeMultiSelectWidgetProps<C = Config> = BaseWidgetProps<C, string[] | number[]> & TreeMultiSelectFieldSettings;
/**
* @deprecated
*/
export type CaseValueWidgetProps<C = Config> = BaseWidgetProps<C> & CaseValueFieldSettings;
/////////////////
// FieldProps
// @ui
/////////////////
type FieldItemSearchableKey = "key" | "path" | "label" | "altLabel" | "tooltip" | "grouplabel";
export type FieldItem = {
items?: FieldItems;
key: string;
path?: string; // field path with separator
label: string;
fullLabel?: string;
altLabel?: string; // label2
tooltip?: string;
disabled?: boolean;
grouplabel?: string;
matchesType?: boolean;
}
type FieldItems = FieldItem[];
export interface FieldProps<C = Config> {
items: FieldItems;
selectedFieldSrc?: FieldSource;
setField(field: FieldPath): void;
errorText?: string;
selectedKey: string | Empty;
selectedKeys?: Array<string> | Empty;
selectedPath?: Array<string> | Empty;
selectedLabel?: string | Empty;
selectedAltLabel?: string | Empty;
selectedFullLabel?: string | Empty;
config?: C;
customProps?: AnyObject;
placeholder?: string;
selectedOpts?: {tooltip?: string};
readonly?: boolean;
id?: string; // id of rule
groupId?: string; // id of parent group
}
/////////////////
// Widgets
/////////////////
type SpelImportValue = (val: any, wgtDef?: Widget, args?: TypedMap<any>) => [any, string[] | string | undefined];
type JsonLogicImportValue = (val: any, wgtDef?: Widget, args?: TypedMap<any>) => any | undefined; // can throw
type FormatValue = (val: RuleValue, fieldDef: Field, wgtDef: Widget, isForDisplay: boolean, op: string, opDef: Operator, rightFieldDef?: Field) => string;
type SqlFormatValue = (val: RuleValue, fieldDef: Field, wgtDef: Widget, op: string, opDef: Operator, rightFieldDef?: Field) => string;
type SpelFormatValue = (val: RuleValue, fieldDef: Field, wgtDef: Widget, op: string, opDef: Operator, rightFieldDef?: Field) => string;
type MongoFormatValue = (val: RuleValue, fieldDef: Field, wgtDef: Widget, op: string, opDef: Operator) => MongoValue;
type JsonLogicFormatValue = (val: RuleValue, fieldDef: Field, wgtDef: Widget, op: string, opDef: Operator) => JsonLogicValue;
export type ValidateValue<V = RuleValue> = (val: V, fieldSettings: FieldSettings, op: string, opDef: Operator, rightFieldDef?: Field) => boolean | string | { error: string | {key: string, args?: Object}, fixedValue?: V } | null;
type ElasticSearchFormatValue = (queryType: ElasticSearchQueryType, val: RuleValue, op: string, field: FieldPath, config: Config) => AnyObject | null;
export interface BaseWidget<C = Config, WP = WidgetProps<C>> {
type: string;
// Used for validation. Can be one of JS types (typeof) or "array"
jsType?: string;
valueSrc?: ValueSource;
valuePlaceholder?: string;
valueLabel?: string;
fullWidth?: boolean;
formatValue?: FormatValue | SerializedFunction;
sqlFormatValue?: SqlFormatValue | SerializedFunction;
spelFormatValue?: SpelFormatValue | SerializedFunction;
spelImportFuncs?: Array<string | object>;
spelImportValue?: SpelImportValue | SerializedFunction;
mongoFormatValue?: MongoFormatValue | SerializedFunction;
elasticSearchFormatValue?: ElasticSearchFormatValue | SerializedFunction;
hideOperator?: boolean;
operatorInlineLabel?: string;
jsonLogic?: JsonLogicFormatValue | SerializedFunction;
jsonLogicImport?: JsonLogicImportValue | SerializedFunction;
//obsolete:
validateValue?: ValidateValue | SerializedFunction;
//@ui
factory: FactoryWithContext<WP> | SerializedFunction;
customProps?: AnyObject;
}
export interface RangeableWidget<C = Config, WP = WidgetProps<C>> extends BaseWidget<C, WP> {
singleWidget?: string;
valueLabels?: Array<string | {label: string, placeholder: string}>;
}
interface BaseFieldWidget<C = Config, WP = WidgetProps<C>> {
valuePlaceholder?: string;
valueLabel?: string;
formatValue: FormatValue | SerializedFunction; // with rightFieldDef
sqlFormatValue?: SqlFormatValue | SerializedFunction; // with rightFieldDef
spelFormatValue?: SpelFormatValue | SerializedFunction; // with rightFieldDef
//obsolete:
validateValue?: ValidateValue | SerializedFunction;
//@ui
customProps?: AnyObject;
factory?: FactoryWithContext<WP>;
}
export interface FieldWidget<C = Config, WP = WidgetProps<C>> extends BaseFieldWidget<C, WP> {
valueSrc: "field";
}
export interface FuncWidget<C = Config, WP = WidgetProps<C>> extends BaseFieldWidget<C, WP> {
valueSrc: "func";
}
export type TextWidget<C = Config, WP = TextWidgetProps<C>> = BaseWidget<C, WP> & TextFieldSettings;
export type DateTimeWidget<C = Config, WP = DateTimeWidgetProps<C>> = RangeableWidget<C, WP> & DateTimeFieldSettings;
export type BooleanWidget<C = Config, WP = BooleanWidgetProps<C>> = BaseWidget<C, WP> & BooleanFieldSettings;
export type NumberWidget<C = Config, WP = NumberWidgetProps<C>> = RangeableWidget<C, WP> & NumberFieldSettings;
export type RangeSliderWidget<C = Config, WP = RangeSliderWidgetProps<C>> = RangeableWidget<C, WP> & NumberFieldSettings;
export type SelectWidget<C = Config, WP = SelectWidgetProps<C>> = BaseWidget<C, WP> & SelectFieldSettings;
export type MultiSelectWidget<C = Config, WP = MultiSelectWidgetProps<C>> = BaseWidget<C, WP> & MultiSelectFieldSettings;
export type TreeSelectWidget<C = Config, WP = TreeSelectWidgetProps<C>> = BaseWidget<C, WP> & TreeSelectFieldSettings;
export type TreeMultiSelectWidget<C = Config, WP = TreeMultiSelectWidgetProps<C>> = BaseWidget<C, WP> & TreeMultiSelectFieldSettings;
/**
* @deprecated
*/
export type CaseValueWidget<C = Config, WP = CaseValueWidgetProps<C>> = BaseWidget<C, WP> & CaseValueFieldSettings;
// tip: use generic WidgetProps here, TS can't determine correct factory
export type TypedWidget<C = Config> =
TextWidget<C, WidgetProps<C>>
| DateTimeWidget<C, WidgetProps<C>>
| BooleanWidget<C, WidgetProps<C>>
| NumberWidget<C, WidgetProps<C>>
| RangeSliderWidget<C, WidgetProps<C>>
| SelectWidget<C, WidgetProps<C>>
| MultiSelectWidget<C, WidgetProps<C>>
| TreeSelectWidget<C, WidgetProps<C>>
| TreeMultiSelectWidget<C, WidgetProps<C>>
| CaseValueWidget<C, WidgetProps<C>>;
export type Widget<C = Config> =
FieldWidget<C>
| FuncWidget<C>
| TypedWidget<C>
| RangeableWidget<C>
| BaseWidget<C>;
export type Widgets<C = Config> = TypedMap<Widget<C>>;
/////////////////
// Conjunctions
/////////////////
type FormatConj = (children: ImmutableList<string>, conj: string, not: boolean, isForDisplay?: boolean) => string;
type SqlFormatConj = (children: ImmutableList<string>, conj: string, not: boolean) => string;
type SpelFormatConj = (children: ImmutableList<string>, conj: string, not: boolean, omitBrackets?: boolean) => string;
export interface Conjunction {
label: string;
formatConj: FormatConj | SerializedFunction;
sqlFormatConj: SqlFormatConj | SerializedFunction;
spelFormatConj: SpelFormatConj | SerializedFunction;
mongoConj: string;
jsonLogicConj?: string;
sqlConj?: string;
spelConj?: string;
spelConjs?: string[];
reversedConj?: string;
}
export type Conjunctions = TypedMap<Conjunction>;
/////////////////
// ConjsProps
// @ui
/////////////////
export interface ConjunctionOption {
id: string;
key: string;
label: string;