-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathquery-builder.ts
7281 lines (7181 loc) · 370 KB
/
query-builder.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
/**
* Query Builder Source
*/
/* eslint-disable max-len */
import { Component, INotifyPropertyChanged, NotifyPropertyChanges, getComponent, MouseEventArgs, Browser, compile, append, ModuleDeclaration, Draggable, remove } from '@syncfusion/ej2-base';
import { Property, ChildProperty, Complex, L10n, closest, extend, isNullOrUndefined, Collection, cldrData } from '@syncfusion/ej2-base';
import { getInstance, addClass, removeClass, rippleEffect, detach, classList } from '@syncfusion/ej2-base';
import { Internationalization, DateFormatOptions, KeyboardEventArgs, getUniqueID, select } from '@syncfusion/ej2-base';
import { QueryBuilderModel, ShowButtonsModel, ColumnsModel, RuleModel, ValueModel } from './query-builder-model';
import { Button, CheckBox, RadioButton, ChangeEventArgs as ButtonChangeEventArgs, RadioButtonModel } from '@syncfusion/ej2-buttons';
import { DropDownList, ChangeEventArgs as DropDownChangeEventArgs, FieldSettingsModel, CheckBoxSelection, DropDownTreeModel, DropDownTree, DdtFilteringEventArgs } from '@syncfusion/ej2-dropdowns';
import { MultiSelect, MultiSelectChangeEventArgs, PopupEventArgs, MultiSelectModel, DropDownListModel } from '@syncfusion/ej2-dropdowns';
import { EmitType, Event, EventHandler, getValue, Animation, BaseEventArgs } from '@syncfusion/ej2-base';
import { Query, Predicate, DataManager, Deferred } from '@syncfusion/ej2-data';
import { TextBox, NumericTextBox, InputEventArgs, ChangeEventArgs as InputChangeEventArgs } from '@syncfusion/ej2-inputs';
import { TextBoxModel, NumericTextBoxModel } from '@syncfusion/ej2-inputs';
import { DatePicker, ChangeEventArgs as CalendarChangeEventArgs, DatePickerModel } from '@syncfusion/ej2-calendars';
import { DropDownButton, ItemModel, MenuEventArgs } from '@syncfusion/ej2-splitbuttons';
import { Tooltip, createSpinner, showSpinner, hideSpinner, TooltipEventArgs } from '@syncfusion/ej2-popups';
import { compile as templateCompiler, getNumericObject } from '@syncfusion/ej2-base';
type ReturnType = { result: Object[], count: number, aggregates?: Object };
type ruleObj = { condition: string, not: boolean, isLocked?: boolean };
/**
* Defines the Columns of Query Builder
*/
export class Columns extends ChildProperty<Columns> {
/**
* Specifies the fields in columns.
*
* @default null
*/
@Property(null)
public field: string;
/**
* Specifies the labels name in columns.
*
* @default null
*/
@Property(null)
public label: string;
/**
* Specifies the types in columns field.
*
* @default null
*/
@Property(null)
public type: string;
/**
* Specifies the values in columns or bind the values from sub controls.
*
* @default null
*/
@Property(null)
public values: string[] | number[] | boolean[];
/**
* Specifies the operators in columns.
*
* @default null
*/
@Property(null)
public operators: { [key: string]: Object }[];
/**
* Specifies the rule template for the field with any other widgets.
*
* @default null
* @aspType string
*/
@Property()
public ruleTemplate: string | Function;
/**
* Specifies the template for value field such as slider or any other widgets.
*
* @default null
*/
@Property(null)
public template: TemplateColumn | string | Function;
/**
* Specifies the validation for columns (text, number and date).
*
* @default { isRequired: true , min: 0, max: Number.MAX_VALUE }
*/
@Property({ isRequired: true , min: 0, max: Number.MAX_VALUE })
public validation: Validation;
/**
* Specifies the date format for columns.
*
* @aspType string
* @blazorType string
* @default null
*/
@Property(null)
public format: string | FormatObject;
/**
* Specifies the step value(numeric textbox) for columns.
*
* @default null
*/
@Property(null)
public step: number;
/**
* Specifies the default value for columns.
*
* @default null
*/
@Property(null)
public value: string[] | number[] | string | number | boolean | Date;
/**
* Specifies the category for columns.
*
* @default null
*/
@Property(null)
public category: string;
/**
* Specifies the sub fields in columns.
*
* @default null
*/
@Property(null)
public columns: ColumnsModel[];
}
/**
* Defines the rule of Query Builder
*/
export class Rule extends ChildProperty<Rule> {
/**
* Specifies the condition value in group.
*
* @default null
*/
@Property(null)
public condition: string;
/**
* Specifies the rules in group.
*
* @default []
*/
@Collection<RuleModel>([], Rule)
public rules: RuleModel[];
/**
* Specifies the field value in group.
*
* @default null
*/
@Property(null)
public field: string;
/**
* Specifies the label value in group.
*
* @default null
*/
@Property(null)
public label: string;
/**
* Specifies the type value in group.
*
* @default null
*/
@Property(null)
public type: string;
/**
* Specifies the operator value in group.
*
* @default null
*/
@Property(null)
public operator: string;
/**
* Specifies the sub controls value in group.
*
* @default null
*/
@Property(null)
public value: string[] | number[] | string | number | boolean;
/**
* Specifies whether not condition is true/false.
*
* @default false
*/
@Property(false)
public not: boolean;
/**
* Specifies whether rule is locked or not.
*
* @default null
*/
@Property(null)
public isLocked: boolean;
}
/**
* Defines the property for value.
*/
export class Value extends ChildProperty <Value> {
/**
* Specifies the property for NumericTextBox value.
*
* @default null
*/
@Property(null)
public numericTextBoxModel: NumericTextBoxModel;
/**
* Specifies the property for MultiSelect value.
*
* @default null
*/
@Property(null)
public multiSelectModel: MultiSelectModel;
/**
* Specifies the property for DatePicker value.
*
* @default null
*/
@Property(null)
public datePickerModel: DatePickerModel;
/**
* Specifies the TextBox value.
*
* @default null
*/
@Property(null)
public textBoxModel: TextBoxModel;
/**
* Specifies the RadioButton value.
*
* @default null
*/
@Property(null)
public radioButtonModel: RadioButtonModel;
}
/**
* Defines the ruleDelete, groupInsert, and groupDelete options of Query Builder.
*/
export class ShowButtons extends ChildProperty<ShowButtons> {
/**
* Specifies the boolean value in ruleDelete that the enable/disable the buttons in rule.
*
* @default false
*/
@Property(false)
public cloneRule: boolean;
/**
* Specifies the boolean value in ruleDelete that the enable/disable the buttons in rule.
*
* @default false
*/
@Property(false)
public cloneGroup: boolean;
/**
* Specifies the boolean value in ruleDelete that the enable/disable the buttons in rule.
*
* @default false
*/
@Property(false)
public lockRule: boolean;
/**
* Specifies the boolean value in ruleDelete that the enable/disable the buttons in rule.
*
* @default false
*/
@Property(false)
public lockGroup: boolean;
/**
* Specifies the boolean value in ruleDelete that the enable/disable the buttons in rule.
*
* @default true
*/
@Property(true)
public ruleDelete: boolean;
/**
* Specifies the boolean value in groupInsert that the enable/disable the buttons in group.
*
* @default true
*/
@Property(true)
public groupInsert: boolean;
/**
* Specifies the boolean value in groupDelete that the enable/disable the buttons in group.
*
* @default true
*/
@Property(true)
public groupDelete: boolean;
}
export interface FormatObject {
/**
* Specifies the format in which the date format will process
*/
skeleton?: string;
}
/**
* Defines the fieldMode of Dropdown control.
* ```props
* Default :- To Specifies the fieldMode as DropDownList.
* DropdownTree :- To Specifies the fieldMode as DropdownTree.
* ```
*/
export type FieldMode =
/** Display the DropdownList */
'Default' |
/** Display the DropdownTree */
'DropdownTree';
/**
* Defines the display mode of the control.
* ```props
* Horizontal :- To display the control in a horizontal UI.
* Vertical :- To display the control in a vertical UI.
* ```
*/
export type DisplayMode =
/** Display the Horizontal UI */
'Horizontal' |
/** Display the Vertical UI */
'Vertical';
/**
* Defines the sorting direction of the field names in a control.
* ```props
* Default :- Specifies the field names in default sorting order.
* Ascending :- Specifies the field names in ascending order.
* Descending :- Specifies the field names in descending order.
* ```
*/
export type SortDirection =
/** Show the field names in default */
'Default' |
/** Show the field names in Ascending */
'Ascending' |
/** Show the field names in Descending */
'Descending';
@NotifyPropertyChanges
export class QueryBuilder extends Component<HTMLDivElement> implements INotifyPropertyChanged {
private groupIdCounter: number;
private ruleIdCounter: number;
private subFilterCounter: number;
private btnGroupId: number;
private levelColl: Level;
private isImportRules: boolean;
private isPublic: boolean;
private parser: string[][];
private defaultLocale: Object;
private l10n: L10n;
private intl: Internationalization;
private items: ItemModel[];
private customOperators: Object;
private operators: Object;
private sqlOperators: Object;
private ruleElem: Element;
private groupElem: Element;
private dataColl: object[];
private dataManager: DataManager;
private selectedColumn: ColumnsModel;
private previousColumn: ColumnsModel;
private actionButton: Element;
private isInitialLoad: boolean;
private timer: number;
private isReadonly: boolean = true;
private fields: Object = { text: 'label', value: 'field' };
private columnTemplateFn: Function;
private target: Element;
private updatedRule: ruleObj = { not: false, condition: 'and', isLocked: false };
private ruleTemplateFn: Function;
private isLocale: boolean = false;
private isRefreshed: boolean = false;
private headerFn: Function;
private subFieldElem: HTMLElement;
private selectedRule: RuleModel;
private isNotified: boolean = false;
private isAddSuccess: boolean = false;
private isNotValueChange: boolean = false;
private isRoot: boolean;
private prevItemData: FieldSettingsModel;
private isFieldChange: boolean = false;
private isFieldClose: boolean = false;
private isDestroy: boolean = false;
private isGetNestedData: boolean = false;
private isCustomOprCols: string[] = [];
private dummyDropdownTreeDs: Object;
private groupCounter: number = 0;
private lockItems: string[] = [];
private groupIndex: number = -1;
private ruleIndex: number = -1;
private isLastGroup: boolean = false;
private cloneGrpBtnClick: boolean = false;
private isMiddleGroup: boolean = false;
private cloneRuleBtnClick: boolean = false;
private isNumInput: boolean;
private draggable: Draggable;
private draggedRule: HTMLElement;
private dragElement: HTMLElement;
private prvtEvtTgrDaD: boolean;
private isDragEventPrevent: boolean;
private isValueEmpty: boolean = false;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private ddTree: any;
/**
* Triggers when the component is created.
*
* @event created
* @blazorProperty 'Created'
*/
@Event()
public created: EmitType<Event>;
/**
* Triggers when field, operator, value is change.
*
* @event actionBegin
* @blazorProperty 'OnActionBegin'
*/
@Event()
public actionBegin: EmitType<ActionEventArgs>;
/**
* Triggers before the condition (And/Or), field, operator, value is changed.
*
* @event beforeChange
* @blazorProperty 'OnValueChange'
*/
@Event()
public beforeChange: EmitType<ChangeEventArgs>;
/**
* Triggers when changing the condition(AND/OR), field, value, operator is changed.
*
* @event change
* @blazorProperty 'Changed'
*/
@Event()
public change: EmitType<ChangeEventArgs>;
/**
* Triggers when dataBound to the Query Builder.
*
* @event dataBound
* @blazorProperty 'dataBound'
*/
@Event()
public dataBound: EmitType<Object>;
/**
* Triggers when changing the condition(AND/OR), field, value, operator is changed
*
* @event ruleChange
* @blazorProperty 'RuleChanged'
*/
@Event()
public ruleChange: EmitType<RuleChangeEventArgs>;
/**
* Triggers when rule/ group dragging starts.
*
*
*/
@Event()
public dragStart: EmitType<DragEventArgs>;
/**
* Triggers when rule/ group are dragged (moved) continuously.
*
*
*/
@Event()
public drag: EmitType<DragEventArgs>;
/**
* Triggers when rule/ group are dropped on to the target rule/ group.
*
*
*/
@Event()
public drop: EmitType<DropEventArgs>;
/**
* Specifies the showButtons settings of the query builder component.
* The showButtons can be enable Enables or disables the ruleDelete, groupInsert, and groupDelete buttons.
*
* @default { ruleDelete: true , groupInsert: true, groupDelete: true }
*/
@Complex<ShowButtonsModel>({}, ShowButtons)
public showButtons: ShowButtonsModel;
/**
* Shows or hides the filtered query.
*
* @default false
*/
@Property(false)
public summaryView: boolean;
/**
* Enables or disables the validation.
*
* @default false
*/
@Property(false)
public allowValidation: boolean;
/**
* Specifies the fieldMode as DropDownList or DropDownTree.
*
* @default 'Default'
*/
@Property('Default')
public fieldMode: FieldMode;
/**
* Specifies columns to create filters.
*
* @default {}
*/
@Property([])
public columns: ColumnsModel[];
/**
* Specifies the property for field.
*
* @default null
*/
@Property(null)
public fieldModel: DropDownListModel | DropDownTreeModel;
/**
* Specifies the property for operator.
*
* @default null
*/
@Property(null)
public operatorModel: DropDownListModel;
/**
* Specifies the property for value.
*
* @default null
*/
@Property(null)
public valueModel: ValueModel;
/**
* Specifies the template for the header with any other widgets.
*
* @default null
* @aspType string
*/
@Property()
public headerTemplate: string | Function;
/**
* Defines class or multiple classes, which are separated by a space in the QueryBuilder element.
* You can add custom styles to the QueryBuilder using the cssClass property.
*
* @default ''
*/
@Property('')
public cssClass: string;
/**
* Binds the column name from data source in query-builder.
* The `dataSource` is an array of JavaScript objects.
*
* @default []
*/
@Property([])
public dataSource: Object[] | Object | DataManager;
/**
* Specifies the displayMode as Horizontal or Vertical.
*
* @default 'Horizontal'
*/
@Property('Horizontal')
public displayMode: DisplayMode;
/**
* Enable or disable persisting component's state between page reloads.
* If enabled, filter states will be persisted.
*
* @default false.
*/
@Property(false)
public enablePersistence: boolean;
/**
* Specifies the sort direction of the field names.
*
* @default 'Default'
*/
@Property('Default')
public sortDirection: SortDirection;
/**
* Specifies the maximum group count or restricts the group count.
*
* @default 5
*/
@Property(5)
public maxGroupCount: number;
/**
* Specifies the height of the query builder.
*
* @default 'auto'
*/
@Property('auto')
public height: string;
/**
* Specifies the width of the query builder.
*
* @default 'auto'
*/
@Property('auto')
public width: string;
/**
* If match case is set to true, the grid filters the records with exact match.
* if false, it filters case insensitive records (uppercase and lowercase letters treated the same).
*
* @default false
*/
@Property(false)
public matchCase: boolean;
/**
* If immediateModeDelay is set by particular number, the rule Change event is triggered after that period.
*
* @default 0
*/
@Property(0)
public immediateModeDelay: number;
/**
* Enables/Disables the not group condition in query builder.
*
* @default false
*/
@Property(false)
public enableNotCondition: boolean;
/**
* When set to true, the user interactions on the component are disabled.
*
* @default false
*/
@Property(false)
public readonly: boolean;
/**
* Specifies a boolean value whether enable / disable the new rule adding while adding new groups.
*
* @remarks
* If this property is true, the empty rule is inserted while inserting new group.
* If set to false, the group is inserted without any rule.
* @default true
*/
@Property(true)
public addRuleToNewGroups: boolean;
/**
* Specifies a boolean value whether enable / disable the auto selection with the first value for the field.
*
* @remarks
* If this property is true, the field dropdown list will render with the first value of the dropdown list.
* If set to false, the dropdown list render with placeholder.
* @default false
*/
@Property(false)
public autoSelectField: boolean;
/**
* Specifies a boolean value whether enable / disable the auto selection with the first value for the operator.
*
* @remarks
* If this property is true, the operator dropdown list will render with the first value of the dropdown list.
* If set to false, the dropdown list render with placeholder.
* @default true
*/
@Property(true)
public autoSelectOperator: boolean;
/**
* Specifies the separator string for column.
*
* @default ''
*/
@Property('')
public separator: string;
/**
* Specifies whether to enable separate connectors between rules/groups.
*
* @remarks
* When this property is set to true, each rule/group will have its own connector, allowing them to be connected independently with different connectors.
* When set to false, will result in connectors being shared between rules/groups, possibly connecting them with the same connector.
*
* @default false
*
*/
@Property(false)
public enableSeparateConnector: boolean;
/**
* Defines rules in the QueryBuilder.
* Specifies the initial rule, which is JSON data.
*
* @default {}
*/
@Complex<RuleModel>({ condition: 'and', rules: [] }, Rule)
public rule: RuleModel;
/**
* Specifies a boolean value whether to enable / disable the drag and drop support to move the rules/ groups.
*
* @remarks
* If this property is true, the drag handle will be rendered in front of the rule/ group element to perform, drag and drop.
* If set to false, the drag handle element is not rendered.
* @default false
*/
@Property(false)
public allowDragAndDrop: boolean;
constructor(options?: QueryBuilderModel, element?: string | HTMLDivElement) {
super(options, <string | HTMLDivElement>element);
MultiSelect.Inject(CheckBoxSelection);
}
protected getPersistData(): string {
return this.addOnPersist(['rule']);
}
/**
* Clears the rules without root rule.
*
* @returns {void}.
*/
public reset(): void {
this.isImportRules = false;
let bodyElem: Element = this.element.querySelector('.e-group-body');
const inputElement: NodeListOf<HTMLElement> = this.element.querySelectorAll('input.e-control') as NodeListOf<HTMLElement>;
for (let i: number = 0, len: number = inputElement.length; i < len; i++) {
if (inputElement[i as number].className.indexOf('e-tooltip') > -1) {
(getComponent(inputElement[i as number], 'tooltip') as Tooltip).destroy();
} else if (inputElement[i as number].parentElement.className.indexOf('e-tooltip') > -1) {
(getComponent(inputElement[i as number].parentElement, 'tooltip') as Tooltip).destroy();
}
}
if (bodyElem) {
bodyElem.innerHTML = '';
} else {
const grpContainer: HTMLElement = this.createElement('div', { attrs: { class: 'e-group-container' } });
const grpHeader: HTMLElement = this.createElement('div', { attrs: { class: 'e-group-header' } });
const grpBody: HTMLElement = this.createElement('div', { attrs: { class: 'e-group-body' } });
grpContainer.appendChild(grpHeader).appendChild(grpBody);
this.element.appendChild(grpContainer);
bodyElem = this.element.querySelector('.e-group-body');
}
if (this.headerTemplate && this.isRoot) {
this.element.innerHTML = '';
this.isRoot = false;
}
if (this.enableNotCondition) {
removeClass(this.element.querySelectorAll('.e-qb-toggle'), 'e-active-toggle');
}
bodyElem.appendChild(this.createElement('div', { attrs: { class: 'e-rule-list' } }));
this.levelColl[this.element.id + '_group0'] = [0];
this.setProperties({ rule: { condition: 'and', not: false, rules: [] } }, true);
this.disableRuleCondition(bodyElem.parentElement);
}
private getWrapper(): Element {
return this.element;
}
protected getModuleName(): string {
return 'query-builder';
}
public requiredModules(): ModuleDeclaration[] {
const modules: ModuleDeclaration[] = [];
modules.push({
member: 'query-library',
args: [this]
});
return modules;
}
private GetRootColumnName(field: string): string {
return this.separator ? field.split(this.separator)[0] : field;
}
private initialize(): void {
if (this.dataColl.length) {
const columnKeys: string[] = Object.keys(this.dataColl[0]);
const cols: ColumnsModel[] = []; const categories: string[] = [];
let type: string; let groupBy: boolean = false;
let isDate: boolean = false; let value: string | number | boolean | Object;
const validateObj: Validation = {isRequired: true, min: 0, max: Number.MAX_VALUE};
if (this.columns.length) {
this.columnSort();
const columns: ColumnsModel[] = this.columns;
for (let i: number = 0, len: number = columns.length; i < len; i++) {
this.updateCustomOperator(columns[i as number], 'initial');
if (!columns[i as number].type) {
if (columnKeys.indexOf(columns[i as number].field) > -1) {
value = this.dataColl[0][columns[i as number].field];
type = typeof value;
if (type === 'string') {
isDate = !isNaN(Date.parse(value as string));
} else if (type === 'object') {
isDate = value instanceof Date && !isNaN(value.getTime());
type = 'string';
}
columns[i as number].type = type;
isDate = false;
}
type = 'string';
}
if (!columns[i as number].validation) {
columns[i as number].validation = validateObj;
}
if (columns[i as number].category) {
groupBy = true;
} else {
columns[i as number].category = this.l10n.getConstant('OtherFields');
}
if (categories.indexOf(columns[i as number].category) < 0) {
categories.push(columns[i as number].category);
}
if (!columns[i as number].operators ||
(this.isLocale && this.isCustomOprCols.indexOf(columns[i as number].field) === -1)) {
columns[i as number].operators = this.customOperators[columns[i as number].type + 'Operator'];
}
}
if (groupBy && (categories.length > 1 || categories[0] !== this.l10n.getConstant('OtherFields'))) {
this.fields = { text: 'label', value: 'field', groupBy: 'category' };
}
this.updateSubFieldsFromColumns(this.columns);
} else {
for (let i: number = 0, len: number = columnKeys.length; i < len; i++) {
value = this.dataColl[0][columnKeys[i as number]];
type = typeof value;
if (type === 'string') {
isDate = !isNaN(Date.parse(value as string));
} else if (type === 'object' && !Object.keys(value as Object).length) {
isDate = value instanceof Date && !isNaN(value.getTime());
type = 'string';
}
cols[i as number] = { 'field': columnKeys[i as number], 'label': columnKeys[i as number], 'type': isDate ? 'date' : type,
'validation': validateObj } as Columns;
isDate = false;
cols[i as number].operators = this.customOperators[cols[i as number].type + 'Operator'];
if (type === 'object') {
this.updateSubFields(value, cols[i as number]);
}
}
this.columns = cols as Columns[];
}
} else if (this.columns && this.columns.length) {
const columns: ColumnsModel[] = this.columns;
for (let i: number = 0, len: number = columns.length; i < len; i++) {
if (columns[i as number].category) {
this.fields = { text: 'label', value: 'field', groupBy: 'category' };
} else {
columns[i as number].category = this.l10n.getConstant('OtherFields');
}
this.updateCustomOperator(columns[i as number], 'initial');
if (!columns[i as number].operators ||
(this.isLocale && this.isCustomOprCols.indexOf(columns[i as number].field) === -1)) {
columns[i as number].operators = this.customOperators[columns[i as number].type + 'Operator'];
}
}
this.updateSubFieldsFromColumns(this.columns);
}
this.trigger('dataBound', {type: 'dataBound'});
}
private updateSubFieldsFromColumns(col: ColumnsModel[], field?: string): void {
for (let i: number = 0; i < col.length; i++) {
if (this.separator !== '' && col[i as number].field.indexOf(this.separator) < 0) {
col[i as number].field = field ? field + this.separator + col[i as number].field : col[i as number].field;
}
if (col[i as number].operators) {
this.updateCustomOperator(col[i as number]);
} else if (col[i as number].type && col[i as number].type !== 'object') {
col[i as number].operators = this.customOperators[col[i as number].type + 'Operator'];
}
if (col[i as number].columns) {
col[i as number].type = 'object';
this.updateSubFieldsFromColumns(col[i as number].columns, col[i as number].field);
}
}
}
private updateSubFields(value: string | number | boolean | Object, col: ColumnsModel, data?: Object): void {
let sampCol: ColumnsModel; col.columns = []; const columnKeys: string[] = Object.keys(value as Object);
let field: string; let label: string; let type: string; let result: Object;
data = data ? data : this.dataColl[0];
for (let i: number = 0, len: number = columnKeys.length; i < len; i++) {
const compField: string[] = col.field.split('.');
if (data) {
result = data[compField[compField.length - 1]][columnKeys[i as number]];
} else {
result = this.dataColl[0][col.field][columnKeys[i as number]];
}
const resData: Object = data[col.field.split(this.separator)[col.field.split(this.separator).length - 1]];
type = typeof result; field = col.field + this.separator + columnKeys[i as number]; label = columnKeys[i as number];
type = (type === 'object' && !isNaN(Date.parse(result as string))) ? 'date' : type;
sampCol = { field: field, label: label, type: type };
if (type !== 'object') {
sampCol.operators = this.customOperators[type + 'Operator'];
}
col.columns.push(sampCol);
if (type === 'object') {
this.updateSubFields(result, sampCol, resData);
}
}
}
private updateCustomOperator(column: ColumnsModel, from?: string): void {
if (column.operators) {
if (!this.isLocale && from === 'initial' && !isNullOrUndefined(this.isCustomOprCols)) {
this.isCustomOprCols.push(column.field);
}
for (let j: number = 0; j < column.operators.length; j++) {
const sqlIdx: number = Object.keys(column.operators[j as number]).indexOf('sqlOperator');
if (sqlIdx > -1) {
const operator: { [key: string]: object } = column.operators[j as number];
const operColl: string[] = Object.keys(operator);
const values: string[] = operColl.map((key: string) => operator[`${key}`]).join(',').split(',');
const valueIdx: number = operColl.indexOf('value');
this.operators[values[valueIdx as number]] = values[sqlIdx as number];
}
}
}
}
private focusEventHandler(event: MouseEventArgs): void {
this.target = event.target as Element;
}
private clickEventHandler(event: MouseEventArgs): void {
let target: Element = event.target as Element; let args: ChangeEventArgs;
this.isImportRules = false; let groupID: string;
if (target.tagName === 'SPAN') {
target = target.parentElement;
}
if (typeof target.className === 'string' && target.className.indexOf('e-collapse-rule') > -1) {
const animation: Animation = new Animation({ duration: 1000, delay: 0 });
if (this.element.querySelectorAll('.e-summary-content').length < 1) {
this.renderSummary();
}
const summaryElem: HTMLElement = document.getElementById(this.element.id + '_summary_content');
const txtareaElem: HTMLElement = summaryElem.querySelector('.e-summary-text');
animation.animate('.e-query-builder', { name: 'SlideLeftIn' });
const groupElem: HTMLElement = this.element.querySelector('.e-group-container') as HTMLElement;
groupElem.style.display = 'none';
txtareaElem.textContent = this.getSqlFromRules(this.rule);
summaryElem.style.display = 'block';
txtareaElem.style.height = txtareaElem.scrollHeight + 'px';
}
if (target.tagName === 'BUTTON' && typeof target.className === 'string' && target.className.indexOf('e-qb-toggle') < 0) {
const animation: Animation = new Animation({ duration: 1000, delay: 0 });
switch (true) {
case target.className.indexOf('e-removerule') > -1:
this.actionButton = target;
this.deleteRule(target);
break;
case target.className.indexOf('e-clone-rule-btn') > -1:
this.actionButton = target;
this.cloneRuleBtnClick = true;
this.ruleClone(target);
break;
case target.className.indexOf('e-lock-rule-btn') > -1:
this.actionButton = target;
this.ruleLock(target);
break;
case target.className.indexOf('e-lock-grp-btn') > -1:
this.actionButton = target;
this.groupLock(target);
break;
case target.className.indexOf('e-clone-grp-btn') > -1:
this.actionButton = target;
this.cloneGrpBtnClick = true;
this.groupClone(closest(target, '.e-group-container'));
break;
case target.className.indexOf('e-deletegroup') > -1:
this.actionButton = target;
this.deleteGroup(closest(target, '.e-group-container'));
break;
case target.className.indexOf('e-edit-rule') > -1:
animation.animate('.e-query-builder', { name: 'SlideLeftIn' });
document.getElementById(this.element.id + '_summary_content').style.display = 'none';
if (this.element.querySelectorAll('.e-group-container').length < 1) {
this.addGroupElement(false, this.element, this.rule.condition, false, this.rule.not);
const mRules: RuleModel = extend({}, this.rule, {}, true);
this.setGroupRules(mRules);
this.renderSummaryCollapse();
} else {
const groupElem: HTMLElement = this.element.querySelector('.e-group-container') as HTMLElement;
if (groupElem.querySelectorAll('.e-collapse-rule').length < 1) {
this.renderSummaryCollapse();
}
groupElem.style.display = 'block';
}
break;
}
} else if ((target.tagName === 'LABEL' && target.parentElement.className.indexOf('e-btn-group') > -1) ||
(typeof target.className === 'string' && target.className.indexOf('e-qb-toggle') > -1)) {
const element: Element = closest(target, '.e-group-container');
if (!this.headerTemplate) {
const forIdValue: string = target.getAttribute('for');
let targetValue: string;
if (forIdValue) {
targetValue = document.getElementById(forIdValue).getAttribute('value');
} else if (this.enableSeparateConnector) {
targetValue = target.textContent;
}
groupID = element.id.replace(this.element.id + '_', '');
const group: RuleModel = this.getGroup(groupID);
let ariaChecked: boolean;
if (this.enableNotCondition) {
if (target.className.indexOf('e-qb-toggle') > -1) {
const toggleElem: Element = element.getElementsByClassName('e-qb-toggle')[0];
if (toggleElem.className.indexOf('e-active-toggle') > -1) {
removeClass([toggleElem], 'e-active-toggle');
ariaChecked = false;
} else {
addClass([toggleElem], 'e-active-toggle');
ariaChecked = true;
}
targetValue = group.condition;
} else {
ariaChecked = group.not;
}
}
args = { groupID: groupID, cancel: false, type: 'condition', value: targetValue.toLowerCase() };
if (this.enableNotCondition) {
args = { groupID: groupID, cancel: false, type: 'condition', value: targetValue.toLowerCase(),