• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IgniteUI / igniteui-angular / 14446782580

14 Apr 2025 01:23PM UTC coverage: 91.602% (-0.01%) from 91.612%
14446782580

Pull #15562

github

web-flow
Merge c6fddccbe into c749f1fc8
Pull Request #15562: feat(h-grid): add support for advanced filtering

13406 of 15682 branches covered (85.49%)

84 of 90 new or added lines in 6 files covered. (93.33%)

4 existing lines in 3 files now uncovered.

26995 of 29470 relevant lines covered (91.6%)

34179.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.38
/projects/igniteui-angular/src/lib/query-builder/query-builder-tree.component.ts
1
import {
2
    AfterViewInit,
3
    EventEmitter,
4
    LOCALE_ID,
5
    Output,
6
    TemplateRef
7
} from '@angular/core';
8
import { getLocaleFirstDayOfWeek, NgTemplateOutlet, NgClass, DatePipe } from '@angular/common';
9
import { Inject } from '@angular/core';
10
import {
11
    Component, Input, ViewChild, ChangeDetectorRef, ViewChildren, QueryList, ElementRef, OnDestroy, HostBinding
12
} from '@angular/core';
13
import { FormsModule } from '@angular/forms';
14
import { Subject } from 'rxjs';
15
import { IgxChipComponent } from '../chips/chip.component';
16
import { IQueryBuilderResourceStrings, QueryBuilderResourceStringsEN } from '../core/i18n/query-builder-resources';
17
import { PlatformUtil, trackByIdentity } from '../core/utils';
18
import { DataType, DataUtil } from '../data-operations/data-util';
19
import { IgxBooleanFilteringOperand, IgxDateFilteringOperand, IgxDateTimeFilteringOperand, IgxNumberFilteringOperand, IgxStringFilteringOperand, IgxTimeFilteringOperand } from '../data-operations/filtering-condition';
20
import { FilteringLogic, IFilteringExpression } from '../data-operations/filtering-expression.interface';
21
import { FilteringExpressionsTree, IExpressionTree, IFilteringExpressionsTree } from '../data-operations/filtering-expressions-tree';
22
import { IgxDatePickerComponent } from '../date-picker/date-picker.component';
23

24
import { IgxButtonDirective } from '../directives/button/button.directive';
25
import { IgxDateTimeEditorDirective } from '../directives/date-time-editor/date-time-editor.directive';
26

27
import { IgxOverlayOutletDirective } from '../directives/toggle/toggle.directive';
28
import { FieldType, EntityType } from '../grids/common/grid.interface';
29
import { IgxSelectComponent } from '../select/select.component';
30
import { HorizontalAlignment, OverlaySettings, VerticalAlignment } from '../services/overlay/utilities';
31
import { AbsoluteScrollStrategy, AutoPositionStrategy, CloseScrollStrategy, ConnectedPositioningStrategy } from '../services/public_api';
32
import { IgxTimePickerComponent } from '../time-picker/time-picker.component';
33
import { IgxPickerToggleComponent, IgxPickerClearComponent } from '../date-common/picker-icons.common';
34
import { IgxInputDirective } from '../directives/input/input.directive';
35
import { IgxInputGroupComponent } from '../input-group/input-group.component';
36
import { IgxSelectItemComponent } from '../select/select-item.component';
37
import { IgxPrefixDirective } from '../directives/prefix/prefix.directive';
38
import { IgxIconComponent } from '../icon/icon.component';
39
import { getCurrentResourceStrings } from '../core/i18n/resources';
40
import { IgxIconButtonDirective } from '../directives/button/icon-button.directive';
41
import { IComboSelectionChangingEventArgs, IgxComboComponent } from "../combo/combo.component";
42
import { IgxComboHeaderDirective } from '../combo/public_api';
43
import { IgxCheckboxComponent } from "../checkbox/checkbox.component";
44
import { IChangeCheckboxEventArgs } from '../checkbox/checkbox-base.directive';
45
import { IgxDialogComponent } from "../dialog/dialog.component";
46
import { ISelectionEventArgs } from '../drop-down/drop-down.common';
47
import { IgxTooltipDirective } from '../directives/tooltip/tooltip.directive';
48
import { IgxTooltipTargetDirective } from '../directives/tooltip/tooltip-target.directive';
49
import { IgxQueryBuilderSearchValueTemplateDirective } from './query-builder.directives';
50
import { IgxQueryBuilderComponent } from './query-builder.component';
51
import { IgxDragIgnoreDirective, IgxDropDirective } from '../directives/drag-drop/drag-drop.directive';
52
import { IgxDropDownComponent } from '../drop-down/drop-down.component';
53
import { IgxDropDownItemComponent } from '../drop-down/drop-down-item.component';
54
import { IgxDropDownItemNavigationDirective } from '../drop-down/drop-down-navigation.directive';
55
import { IgxQueryBuilderDragService } from './query-builder-drag.service';
56
import { isTree } from '../data-operations/expressions-tree-util';
57
import { ExpressionGroupItem, ExpressionItem, ExpressionOperandItem, IgxFieldFormatterPipe } from './query-builder.common';
58

59
const DEFAULT_PIPE_DATE_FORMAT = 'mediumDate';
3✔
60
const DEFAULT_PIPE_TIME_FORMAT = 'mediumTime';
3✔
61
const DEFAULT_PIPE_DATE_TIME_FORMAT = 'medium';
3✔
62
const DEFAULT_PIPE_DIGITS_INFO = '1.0-3';
3✔
63
const DEFAULT_CHIP_FOCUS_DELAY = 50;
3✔
64

65
/** @hidden */
66
@Component({
67
    selector: 'igx-query-builder-tree',
68
    templateUrl: './query-builder-tree.component.html',
69
    host: { 'class': 'igx-query-builder-tree' },
70
    imports: [
71
        DatePipe,
72
        FormsModule,
73
        IgxButtonDirective,
74
        IgxCheckboxComponent,
75
        IgxChipComponent,
76
        IgxComboComponent,
77
        IgxComboHeaderDirective,
78
        IgxDatePickerComponent,
79
        IgxDateTimeEditorDirective,
80
        IgxDialogComponent,
81
        IgxDragIgnoreDirective,
82
        IgxDropDirective,
83
        IgxDropDownComponent,
84
        IgxDropDownItemComponent,
85
        IgxDropDownItemNavigationDirective,
86
        IgxFieldFormatterPipe,
87
        IgxIconButtonDirective,
88
        IgxIconComponent,
89
        IgxInputDirective,
90
        IgxInputGroupComponent,
91
        IgxOverlayOutletDirective,
92
        IgxPickerClearComponent,
93
        IgxPickerToggleComponent,
94
        IgxPrefixDirective,
95
        IgxSelectComponent,
96
        IgxSelectItemComponent,
97
        IgxTimePickerComponent,
98
        IgxTooltipDirective,
99
        IgxTooltipTargetDirective,
100
        NgClass,
101
        NgTemplateOutlet
102
    ],
103
    providers: [
104
        IgxQueryBuilderDragService
105
    ],
106
})
107
export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
3✔
108
    /**
109
     * @hidden @internal
110
     */
111
    public _expressionTree: IExpressionTree;
112

113
    /**
114
     * @hidden @internal
115
     */
116
    public _expressionTreeCopy: IExpressionTree;
117

118
    /**
119
     * @hidden @internal
120
     */
121
    @HostBinding('class') public get getClass() {
122
        return `igx-query-builder-tree--level-${this.level}`;
11,377✔
123
    }
124

125
    /**
126
     * Sets/gets the entities.
127
     */
128
    @Input()
129
    public entities: EntityType[];
130

131
    /**
132
     * Sets/gets the parent query builder component.
133
     */
134
    @Input()
135
    public queryBuilder: IgxQueryBuilderComponent;
136

137
    /**
138
     * Sets/gets the search value template.
139
     */
140
    @Input()
141
    public searchValueTemplate: TemplateRef<IgxQueryBuilderSearchValueTemplateDirective> = null;
293✔
142

143
    /**
144
    * Returns the parent expression operand.
145
    */
146
    @Input()
147
    public get parentExpression(): ExpressionOperandItem {
148
        return this._parentExpression;
153,368✔
149
    }
150

151
    /**
152
     * Sets the parent expression operand.
153
     */
154
    public set parentExpression(value: ExpressionOperandItem) {
155
        this._parentExpression = value;
141✔
156
    }
157

158
    /**
159
    * Returns the fields.
160
    */
161
    public get fields(): FieldType[] {
162
        if (!this._fields && this.isAdvancedFiltering()) {
58,528✔
163
            this._fields = this.entities[0].fields;
59✔
164
        }
165

166
        return this._fields;
58,528✔
167
    }
168

169
    /**
170
     * Sets the fields.
171
     */
172
    @Input()
173
    public set fields(fields: FieldType[]) {
174
        this._fields = fields;
802✔
175
        
176
        this._fields = this._fields?.map(f => ({...f, filters: this.getFilters(f), pipeArgs: this.getPipeArgs(f) }));
3,280✔
177
        
178
        if (!this._fields && this.isAdvancedFiltering()) {
802✔
179
            this._fields = this.entities[0].fields;
1✔
180
        }
181
    }
182

183
    /**
184
    * Returns the expression tree.
185
    */
186
    public get expressionTree(): IExpressionTree {
187
        return this._expressionTree;
6,474✔
188
    }
189

190
    /**
191
     * Sets the expression tree.
192
     */
193
    @Input()
194
    public set expressionTree(expressionTree: IExpressionTree) {
195
        this._expressionTree = expressionTree;
507✔
196
        if (!expressionTree) {
507✔
197
            this._selectedEntity = null;
159✔
198
            this._selectedReturnFields = [];
159✔
199
        }
200

201
        if (!this._preventInit) {
507✔
202
            this.init();
498✔
203
        }
204
    }
205

206
    /**
207
     * Gets the `locale` of the query builder.
208
     * If not set, defaults to application's locale.
209
     */
210
    @Input()
211
    public get locale(): string {
212
        return this._locale;
1,000✔
213
    }
214

215
    /**
216
     * Sets the `locale` of the query builder.
217
     * Expects a valid BCP 47 language tag.
218
     */
219
    public set locale(value: string) {
220
        this._locale = value;
445✔
221
        // if value is invalid, set it back to _localeId
222
        try {
445✔
223
            getLocaleFirstDayOfWeek(this._locale);
445✔
224
        } catch {
225
            this._locale = this._localeId;
101✔
226
        }
227
    }
228

229
    /**
230
     * Sets the resource strings.
231
     * By default it uses EN resources.
232
     */
233
    @Input()
234
    public set resourceStrings(value: IQueryBuilderResourceStrings) {
235
        this._resourceStrings = Object.assign({}, this._resourceStrings, value);
153✔
236
    }
237

238
    /**
239
     * Returns the resource strings.
240
     */
241
    public get resourceStrings(): IQueryBuilderResourceStrings {
242
        return this._resourceStrings;
244,650✔
243
    }
244

245
    /**
246
     * Gets/sets the expected return field.
247
     */
248
    @Input() public expectedReturnField: string = null;
293✔
249

250
    /**
251
     * Event fired as the expression tree is changed.
252
     */
253
    @Output()
254
    public expressionTreeChange = new EventEmitter<IExpressionTree>();
293✔
255

256
    /**
257
     * Event fired if a nested query builder tree is being edited.
258
     */
259
    @Output()
260
    public inEditModeChange = new EventEmitter<ExpressionOperandItem>();
293✔
261

262
    @ViewChild('entitySelect', { read: IgxSelectComponent })
263
    protected entitySelect: IgxSelectComponent;
264

265
    @ViewChild('editingInputs', { read: ElementRef })
266
    private editingInputs: ElementRef;
267

268
    @ViewChild('returnFieldsCombo', { read: IgxComboComponent })
269
    private returnFieldsCombo: IgxComboComponent;
270

271
    @ViewChild('returnFieldSelect', { read: IgxSelectComponent })
272
    protected returnFieldSelect: IgxSelectComponent;
273

274
    @ViewChild('fieldSelect', { read: IgxSelectComponent })
275
    private fieldSelect: IgxSelectComponent;
276

277
    @ViewChild('conditionSelect', { read: IgxSelectComponent })
278
    private conditionSelect: IgxSelectComponent;
279

280
    @ViewChild('searchValueInput', { read: ElementRef })
281
    private searchValueInput: ElementRef;
282

283
    @ViewChild('picker')
284
    private picker: IgxDatePickerComponent | IgxTimePickerComponent;
285

286
    @ViewChild('addRootAndGroupButton', { read: ElementRef })
287
    private addRootAndGroupButton: ElementRef;
288

289
    @ViewChild('addConditionButton', { read: ElementRef })
290
    private addConditionButton: ElementRef;
291

292
    @ViewChild('entityChangeDialog', { read: IgxDialogComponent })
293
    private entityChangeDialog: IgxDialogComponent;
294

295
    @ViewChild('addOptionsDropDown', { read: IgxDropDownComponent })
296
    private addExpressionItemDropDown: IgxDropDownComponent;
297

298
    @ViewChild('groupContextMenuDropDown', { read: IgxDropDownComponent })
299
    private groupContextMenuDropDown: IgxDropDownComponent;
300

301
    /**
302
     * @hidden @internal
303
     */
304
    @ViewChildren(IgxChipComponent, { read: IgxChipComponent })
305
    public expressionsChips: QueryList<IgxChipComponent>;
306

307
    @ViewChild('editingInputsContainer', { read: ElementRef })
308
    protected set editingInputsContainer(value: ElementRef) {
309
        if ((value && !this._editingInputsContainer) ||
806✔
310
            (value && this._editingInputsContainer && this._editingInputsContainer.nativeElement !== value.nativeElement)) {
311
            requestAnimationFrame(() => {
286✔
312
                this.scrollElementIntoView(value.nativeElement);
286✔
313
            });
314
        }
315

316
        this._editingInputsContainer = value;
806✔
317
    }
318

319
    /** @hidden */
320
    protected get editingInputsContainer(): ElementRef {
321
        return this._editingInputsContainer;
×
322
    }
323

324
    @ViewChild('currentGroupButtonsContainer', { read: ElementRef })
325
    protected set currentGroupButtonsContainer(value: ElementRef) {
326
        if ((value && !this._currentGroupButtonsContainer) ||
806✔
327
            (value && this._currentGroupButtonsContainer && this._currentGroupButtonsContainer.nativeElement !== value.nativeElement)) {
328
            requestAnimationFrame(() => {
366✔
329
                this.scrollElementIntoView(value.nativeElement);
366✔
330
            });
331
        }
332

333
        this._currentGroupButtonsContainer = value;
806✔
334
    }
335

336
    /** @hidden */
337
    protected get currentGroupButtonsContainer(): ElementRef {
338
        return this._currentGroupButtonsContainer;
×
339
    }
340

341
    @ViewChild('expressionsContainer')
342
    private expressionsContainer: ElementRef;
343

344
    @ViewChild('overlayOutlet', { read: IgxOverlayOutletDirective, static: true })
345
    private overlayOutlet: IgxOverlayOutletDirective;
346

347
    @ViewChildren(IgxQueryBuilderTreeComponent)
348
    private innerQueries: QueryList<IgxQueryBuilderTreeComponent>;
349

350
    /**
351
     * @hidden @internal
352
     */
353
    public innerQueryNewExpressionTree: IExpressionTree;
354

355
    /**
356
     * @hidden @internal
357
     */
358
    public rootGroup: ExpressionGroupItem;
359

360
    /**
361
     * @hidden @internal
362
     */
363
    public selectedExpressions: ExpressionOperandItem[] = [];
293✔
364

365
    /**
366
     * @hidden @internal
367
     */
368
    public currentGroup: ExpressionGroupItem;
369

370
    /**
371
     * @hidden @internal
372
     */
373
    public contextualGroup: ExpressionGroupItem;
374

375
    /**
376
     * @hidden @internal
377
     */
378
    public filteringLogics;
379

380
    /**
381
     * @hidden @internal
382
     */
383
    public selectedCondition: string;
384

385
    /**
386
     * @hidden @internal
387
     */
388
    public searchValue: { value: any } = { value: null };
293✔
389

390
    /**
391
     * @hidden @internal
392
     */
393
    public pickerOutlet: IgxOverlayOutletDirective | ElementRef;
394

395
    /**
396
     * @hidden @internal
397
     */
398
    public prevFocusedExpression: ExpressionOperandItem;
399

400
    /**
401
     * @hidden @internal
402
     */
403
    public initialOperator = 0;
293✔
404

405
    /**
406
     * @hidden @internal
407
     */
408
    public returnFieldSelectOverlaySettings: OverlaySettings = {
293✔
409
        scrollStrategy: new AbsoluteScrollStrategy(),
410
        modal: false,
411
        closeOnOutsideClick: true
412
    };
413

414
    /**
415
     * @hidden @internal
416
     */
417
    public entitySelectOverlaySettings: OverlaySettings = {
293✔
418
        scrollStrategy: new AbsoluteScrollStrategy(),
419
        modal: false,
420
        closeOnOutsideClick: true
421
    };
422

423
    /**
424
     * @hidden @internal
425
     */
426
    public fieldSelectOverlaySettings: OverlaySettings = {
293✔
427
        scrollStrategy: new AbsoluteScrollStrategy(),
428
        modal: false,
429
        closeOnOutsideClick: true
430
    };
431

432
    /**
433
     * @hidden @internal
434
     */
435
    public conditionSelectOverlaySettings: OverlaySettings = {
293✔
436
        scrollStrategy: new AbsoluteScrollStrategy(),
437
        modal: false,
438
        closeOnOutsideClick: true
439
    };
440

441
    /**
442
     * @hidden @internal
443
     */
444
    public addExpressionDropDownOverlaySettings: OverlaySettings = {
293✔
445
        scrollStrategy: new AbsoluteScrollStrategy(),
446
        modal: false,
447
        closeOnOutsideClick: true
448
    };
449

450
    /**
451
     * @hidden @internal
452
     */
453
    public groupContextMenuDropDownOverlaySettings: OverlaySettings = {
293✔
454
        scrollStrategy: new AbsoluteScrollStrategy(),
455
        modal: false,
456
        closeOnOutsideClick: true
457
    };
458

459
    private destroy$ = new Subject<any>();
293✔
460
    private _timeoutId: any;
461
    private _lastFocusedChipIndex: number;
462
    private _focusDelay = DEFAULT_CHIP_FOCUS_DELAY;
293✔
463
    private _parentExpression: ExpressionOperandItem;
464
    private _selectedEntity: EntityType;
465
    private _selectedReturnFields: string | string[];
466
    private _selectedField: FieldType;
467
    private _editingInputsContainer: ElementRef;
468
    private _currentGroupButtonsContainer: ElementRef;
469
    private _addModeExpression: ExpressionOperandItem;
470
    private _editedExpression: ExpressionOperandItem;
471
    private _preventInit = false;
293✔
472
    private _prevFocusedContainer: ElementRef;
473
    private _expandedExpressions: IFilteringExpression[] = [];
293✔
474
    private _fields: FieldType[];
475
    private _locale;
476
    private _entityNewValue: EntityType;
477
    private _resourceStrings = getCurrentResourceStrings(QueryBuilderResourceStringsEN);
293✔
478

479
    /**
480
     * Returns if the select entity dropdown at the root level is disabled after the initial selection.
481
     */
482
    public get disableEntityChange(): boolean {
483

484
        return !this.parentExpression && this.selectedEntity ? this.queryBuilder.disableEntityChange : false;
12,159✔
485
    }
486

487
    /**
488
     * Returns if the fields combo at the root level is disabled.
489
     */
490
    public get disableReturnFieldsChange(): boolean {
491

492
        return !this.selectedEntity || this.queryBuilder.disableReturnFieldsChange;
5,808✔
493
    }
494

495
    /**
496
     * Returns the current level.
497
     */
498
    public get level(): number {
499
        let parent = this.elRef.nativeElement.parentElement;
11,377✔
500
        let _level = 0;
11,377✔
501
        while (parent) {
11,377✔
502
            if (parent.localName === 'igx-query-builder-tree') {
95,700✔
503
                _level++;
6,166✔
504
            }
505
            parent = parent.parentElement;
95,700✔
506
        }
507
        return _level;
11,377✔
508
    }
509

510
    private _positionSettings = {
293✔
511
        horizontalStartPoint: HorizontalAlignment.Right,
512
        verticalStartPoint: VerticalAlignment.Top
513
    };
514

515
    private _overlaySettings: OverlaySettings = {
293✔
516
        closeOnOutsideClick: false,
517
        modal: false,
518
        positionStrategy: new ConnectedPositioningStrategy(this._positionSettings),
519
        scrollStrategy: new CloseScrollStrategy()
520
    };
521

522
    /** @hidden */
523
    protected isAdvancedFiltering(): boolean {
524
        return (this.entities?.length === 1 && !this.entities[0]?.name) ||
20,562✔
525
            this.entities?.find(e => e.childEntities?.length > 0) !== undefined ||
36,756✔
526
            this.entities !== this.queryBuilder?.entities;
527
    }
528

529
    /** @hidden */
530
    protected isHierarchicalGridNestedQuery(): boolean {
531
        return this.queryBuilder.entities !== this.entities
14,117✔
532
    }
533

534
    /** @hidden */
535
    protected isSearchValueInputDisabled(): boolean {
536
        return !this.selectedField ||
1,675✔
537
            !this.selectedCondition ||
538
            (this.selectedField &&
539
                (this.selectedField.filters.condition(this.selectedCondition).isUnary ||
540
                    this.selectedField.filters.condition(this.selectedCondition).isNestedQuery));
541
    }
542

543
    constructor(public cdr: ChangeDetectorRef,
293✔
544
        public dragService: IgxQueryBuilderDragService,
293✔
545
        protected platform: PlatformUtil,
293✔
546
        private elRef: ElementRef,
293✔
547
        @Inject(LOCALE_ID) protected _localeId: string) {
293✔
548
        this.locale = this.locale || this._localeId;
293✔
549
        this.dragService.register(this, elRef);
293✔
550
    }
551

552
    /**
553
     * @hidden @internal
554
     */
555
    public ngAfterViewInit(): void {
556
        this._overlaySettings.outlet = this.overlayOutlet;
293✔
557
        this.entitySelectOverlaySettings.outlet = this.overlayOutlet;
293✔
558
        this.fieldSelectOverlaySettings.outlet = this.overlayOutlet;
293✔
559
        this.conditionSelectOverlaySettings.outlet = this.overlayOutlet;
293✔
560
        this.returnFieldSelectOverlaySettings.outlet = this.overlayOutlet;
293✔
561
        this.addExpressionDropDownOverlaySettings.outlet = this.overlayOutlet;
293✔
562
        this.groupContextMenuDropDownOverlaySettings.outlet = this.overlayOutlet;
293✔
563
        
564
        if (this.isAdvancedFiltering() && this.entities?.length === 1) {
293✔
565
            this.selectedEntity = this.entities[0].name;
55✔
566
            if (this._selectedEntity.fields.find(f => f.field === this.expectedReturnField)) {
316✔
567
                this._selectedReturnFields = [this.expectedReturnField];
4✔
568
            }
569
        }
570

571
        // Trigger additional change detection cycle
572
        this.cdr.detectChanges();
293✔
573
    }
574

575
    /**
576
     * @hidden @internal
577
     */
578
    public ngOnDestroy(): void {
579
        this.destroy$.next(true);
293✔
580
        this.destroy$.complete();
293✔
581
    }
582

583
    /**
584
     * @hidden @internal
585
     */
586
    public set selectedEntity(value: string) {
587
        this._selectedEntity = this.entities?.find(el => el.name === value);
55✔
588
    }
589

590
    /**
591
     * @hidden @internal
592
     */
593
    public get selectedEntity(): EntityType {
594
        return this._selectedEntity;
82,761✔
595
    }
596

597
    /**
598
     * @hidden @internal
599
     */
600
    public onEntitySelectChanging(event: ISelectionEventArgs) {
601
        event.cancel = true;
43✔
602
        this._entityNewValue = event.newSelection.value;
43✔
603
        if (event.oldSelection.value && this.queryBuilder.showEntityChangeDialog) {
43✔
604
            this.entityChangeDialog.open();
6✔
605
        } else {
606
            this.onEntityChangeConfirm();
37✔
607
        }
608
    }
609

610
    /**
611
     * @hidden
612
     */
613
    public onShowEntityChangeDialogChange(eventArgs: IChangeCheckboxEventArgs) {
614
        this.queryBuilder.showEntityChangeDialog = !eventArgs.checked;
1✔
615
    }
616

617
    /**
618
     * @hidden
619
     */
620
    public onEntityChangeCancel() {
621
        this.entityChangeDialog.close();
3✔
622
        this.entitySelect.close();
3✔
623
        this._entityNewValue = null;
3✔
624
    }
625

626
    /**
627
     * @hidden
628
     */
629
    public onEntityChangeConfirm() {
630
        if (this._parentExpression) {
39✔
631
            this._expressionTree = this.createExpressionTreeFromGroupItem(this.createExpressionGroupItem(this._expressionTree));
6✔
632
        }
633

634
        this._selectedEntity = this._entityNewValue;
39✔
635
        if (!this._selectedEntity.fields) {
39!
636
            this._selectedEntity.fields = [];
×
637
        }
638
        this.fields = this._entityNewValue ? this._entityNewValue.fields : [];
39!
639

640
        if (this._selectedEntity.fields.find(f => f.field === this.expectedReturnField)) {
150✔
641
            this._selectedReturnFields = [this.expectedReturnField];
6✔
642
        } else {
643
            this._selectedReturnFields = this.parentExpression ? [] : this._entityNewValue.fields?.map(f => f.field);
132!
644
        }
645

646
        if (this._expressionTree) {
39✔
647
            this._expressionTree.entity = this._entityNewValue.name;
5✔
648

649
            this._expressionTree.returnFields = this.fields.length === this._selectedReturnFields.length ? ['*'] : this._selectedReturnFields;
5✔
650

651
            this._expressionTree.filteringOperands = [];
5✔
652

653
            this._editedExpression = null;
5✔
654
            if (!this.parentExpression) {
5✔
655
                this.expressionTreeChange.emit(this._expressionTree);
4✔
656
            }
657

658
            this.rootGroup = null;
5✔
659
            this.currentGroup = this.rootGroup;
5✔
660
        }
661

662
        this._selectedField = null;
39✔
663
        this.selectedCondition = null;
39✔
664
        this.searchValue.value = null;
39✔
665

666
        this.entityChangeDialog.close();
39✔
667
        this.entitySelect.close();
39✔
668

669
        this._entityNewValue = null;
39✔
670
        this.innerQueryNewExpressionTree = null;
39✔
671

672
        this.initExpressionTree(this._selectedEntity.name, this.selectedReturnFields);
39✔
673
    }
674

675
    /**
676
     * @hidden @internal
677
     */
678
    public set selectedReturnFields(value: string[]) {
679
        if (this._selectedReturnFields !== value) {
5✔
680
            this._selectedReturnFields = value;
5✔
681

682
            if (this._expressionTree && !this.parentExpression) {
5✔
683
                this._expressionTree.returnFields = value.length === this.fields.length ? ['*'] : value;
5✔
684
                this.expressionTreeChange.emit(this._expressionTree);
5✔
685
            }
686
        }
687
    }
688

689
    /**
690
     * @hidden @internal
691
     */
692
    public get selectedReturnFields(): string[] {
693
        if (typeof this._selectedReturnFields == 'string') {
49,323!
694
            return [this._selectedReturnFields];
×
695
        }
696
        return this._selectedReturnFields;
49,323✔
697
    }
698

699
    /**
700
     * @hidden @internal
701
     */
702
    public set selectedField(value: FieldType) {
703
        const oldValue = this._selectedField;
203✔
704

705
        if (this._selectedField !== value) {
203✔
706
            this._selectedField = value;
171✔
707
            if (this._selectedField && !this._selectedField.dataType) {
171!
NEW
708
                this._selectedField.filters = this.getFilters(this._selectedField);
×
709
            }
710

711
            this.selectDefaultCondition();
171✔
712
            if (oldValue && this._selectedField && this._selectedField.dataType !== oldValue.dataType) {
171✔
713
                this.searchValue.value = null;
17✔
714
                this.cdr.detectChanges();
17✔
715
            }
716
        }
717
    }
718

719
    /**
720
     * @hidden @internal
721
     */
722
    public get selectedField(): FieldType {
723
        return this._selectedField;
57,097✔
724
    }
725

726
    /**
727
     * @hidden @internal
728
     *
729
     * used by the grid
730
     */
731
    public setPickerOutlet(outlet?: IgxOverlayOutletDirective | ElementRef) {
732
        this.pickerOutlet = outlet;
51✔
733
    }
734

735
    /**
736
     * @hidden @internal
737
     *
738
     * used by the grid
739
     */
740
    public get isContextMenuVisible(): boolean {
741
        return !this.groupContextMenuDropDown.collapsed;
×
742
    }
743

744
    /**
745
     * @hidden @internal
746
     */
747
    public get hasEditedExpression(): boolean {
748
        return this._editedExpression !== undefined && this._editedExpression !== null;
31,193✔
749
    }
750

751
    /**
752
     * @hidden @internal
753
     */
754
    public addCondition(parent: ExpressionGroupItem, afterExpression?: ExpressionOperandItem, isUIInteraction?: boolean) {
755
        this.cancelOperandAdd();
78✔
756

757
        const operandItem = new ExpressionOperandItem({
78✔
758
            fieldName: null,
759
            condition: null,
760
            conditionName: null,
761
            ignoreCase: true,
762
            searchVal: null
763
        }, parent);
764

765
        const groupItem = new ExpressionGroupItem(this.getOperator(null) ?? FilteringLogic.And, parent);
78!
766
        this.contextualGroup = groupItem;
78✔
767
        this.initialOperator = null;
78✔
768

769
        this._lastFocusedChipIndex = this._lastFocusedChipIndex === undefined ? -1 : this._lastFocusedChipIndex;
78✔
770

771
        if (parent) {
78✔
772
            if (afterExpression) {
11✔
773
                const index = parent.children.indexOf(afterExpression);
1✔
774
                parent.children.splice(index + 1, 0, operandItem);
1✔
775
            } else {
776
                parent.children.push(operandItem);
10✔
777
            }
778
            this._lastFocusedChipIndex++;
11✔
779
        } else {
780
            this.rootGroup = groupItem;
67✔
781
            operandItem.parent = groupItem;
67✔
782
            this.rootGroup.children.push(operandItem);
67✔
783
            this._lastFocusedChipIndex = 0;
67✔
784
        }
785

786
        this._focusDelay = 250;
78✔
787

788
        if (isUIInteraction && !afterExpression) {
78✔
789
            this._lastFocusedChipIndex = this.expressionsChips.length;
74✔
790
            this._focusDelay = DEFAULT_CHIP_FOCUS_DELAY;
74✔
791
        }
792

793
        this.enterExpressionEdit(operandItem);
78✔
794
    }
795

796
    /**
797
     * @hidden @internal
798
     */
799
    public addReverseGroup(parent?: ExpressionGroupItem, afterExpression?: ExpressionItem) {
800
        parent = parent ?? this.rootGroup;
3!
801

802
        if (parent.operator === FilteringLogic.And) {
3✔
803
            this.addGroup(FilteringLogic.Or, parent, afterExpression);
2✔
804
        } else {
805
            this.addGroup(FilteringLogic.And, parent, afterExpression);
1✔
806
        }
807
    }
808

809
    /**
810
     * @hidden @internal
811
     */
812
    public endGroup(groupItem: ExpressionGroupItem) {
813
        this.currentGroup = groupItem.parent;
×
814
    }
815

816
    /**
817
     * @hidden @internal
818
     */
819
    public commitExpression() {
820
        this.commitOperandEdit();
46✔
821
        this.focusEditedExpressionChip();
46✔
822
    }
823

824
    /**
825
     * @hidden @internal
826
     */
827
    public discardExpression(expressionItem?: ExpressionOperandItem) {
828
        this.cancelOperandEdit();
11✔
829
        if (expressionItem && expressionItem.expression.fieldName) {
11✔
830
            this.focusEditedExpressionChip();
4✔
831
        }
832
    }
833

834
    /**
835
     * @hidden @internal
836
     */
837
    public commitOperandEdit() {
838
        const actualSearchValue = this.searchValue.value;
61✔
839
        if (this._editedExpression) {
61✔
840
            this._editedExpression.expression.fieldName = this.selectedField.field;
58✔
841
            this._editedExpression.expression.condition = this.selectedField.filters.condition(this.selectedCondition);
58✔
842
            this._editedExpression.expression.conditionName = this.selectedCondition;
58✔
843
            this._editedExpression.expression.searchVal = DataUtil.parseValue(this.selectedField.dataType, actualSearchValue) || actualSearchValue;
58✔
844
            this._editedExpression.fieldLabel = this.selectedField.label
58!
845
                ? this.selectedField.label
846
                : this.selectedField.header
58!
847
                    ? this.selectedField.header
848
                    : this.selectedField.field;
849

850
            const innerQuery = this.innerQueries.filter(q => q.isInEditMode())[0]
58✔
851
            if (innerQuery && this.selectedField?.filters?.condition(this.selectedCondition)?.isNestedQuery) {
58✔
852
                innerQuery.exitEditAddMode();
8✔
853
                this._editedExpression.expression.searchTree = this.getExpressionTreeCopy(innerQuery.expressionTree);
8✔
854
                const returnFields = innerQuery.selectedReturnFields.length > 0 ?
8!
855
                                        innerQuery.selectedReturnFields :
856
                                        [innerQuery.fields[0].field];
857
                this._editedExpression.expression.searchTree.returnFields = returnFields;
8✔
858
            } else {
859
                this._editedExpression.expression.searchTree = null;
50✔
860
            }
861
            this.innerQueryNewExpressionTree = null;
58✔
862

863
            if (this.selectedField.filters.condition(this.selectedCondition)?.isUnary || this.selectedField.filters.condition(this.selectedCondition)?.isNestedQuery) {
58✔
864
                this._editedExpression.expression.searchVal = null;
13✔
865
            }
866

867
            this._editedExpression.inEditMode = false;
58✔
868
            this._editedExpression = null;
58✔
869
        }
870

871
        this._expressionTree = this.createExpressionTreeFromGroupItem(this.rootGroup, this.selectedEntity?.name, this.selectedReturnFields);
61✔
872
        if (!this.parentExpression) {
61✔
873
            this.expressionTreeChange.emit(this._expressionTree);
51✔
874
        }
875
    }
876

877
    /**
878
     * @hidden @internal
879
     */
880
    public cancelOperandAdd() {
881
        if (this._addModeExpression) {
594!
882
            this._addModeExpression.inAddMode = false;
×
883
            this._addModeExpression = null;
×
884
        }
885
    }
886

887
    /**
888
     * @hidden @internal
889
     */
890
    public deleteItem = (expressionItem: ExpressionItem, skipEmit: boolean = false) => {
293✔
891
        if (!expressionItem.parent) {
48✔
892
            this.rootGroup = null;
15✔
893
            this.currentGroup = null;
15✔
894
            //this._expressionTree = null;
895
            return;
15✔
896
        }
897

898
        if (expressionItem === this.currentGroup) {
33!
899
            this.currentGroup = this.currentGroup.parent;
×
900
        }
901

902
        const children = expressionItem.parent.children;
33✔
903
        const index = children.indexOf(expressionItem);
33✔
904
        children.splice(index, 1);
33✔
905
        const entity = this.expressionTree ? this.expressionTree.entity : null;
33✔
906
        const returnFields = this.expressionTree ? this.expressionTree.returnFields : null;
33✔
907
        this._expressionTree = this.createExpressionTreeFromGroupItem(this.rootGroup, entity, returnFields); // TODO: don't recreate if not necessary
33✔
908

909
        if (!children.length) {
33✔
910
            this.deleteItem(expressionItem.parent, true);
16✔
911
        }
912

913
        if (!this.parentExpression && !skipEmit) {
33✔
914
            this.expressionTreeChange.emit(this._expressionTree);
31✔
915
        }
916
    }
917

918
    /**
919
     * @hidden @internal
920
     */
921
    public cancelOperandEdit() {
922
        if (this.innerQueries) {
525✔
923
            const innerQuery = this.innerQueries.filter(q => q.isInEditMode())[0];
232✔
924
            if (innerQuery) {
232✔
925
                if (innerQuery._editedExpression) {
9✔
926
                    innerQuery.cancelOperandEdit();
1✔
927
                }
928

929
                innerQuery.expressionTree = this.getExpressionTreeCopy(this._editedExpression.expression.searchTree);
9✔
930
                this.innerQueryNewExpressionTree = null;
9✔
931
            }
932
        }
933

934
        if (this._editedExpression) {
525✔
935
            this._editedExpression.inEditMode = false;
30✔
936

937
            if (!this._editedExpression.expression.fieldName) {
30✔
938
                this.deleteItem(this._editedExpression);
16✔
939
            }
940

941
            this._editedExpression = null;
30✔
942
        }
943

944
        if (!this.expressionTree && this.contextualGroup) {
525✔
945
            this.initialOperator = this.contextualGroup.operator;
1✔
946
        }
947
    }
948

949
    /**
950
     * @hidden @internal
951
     */
952
    public operandCanBeCommitted(): boolean {
953
        const innerQuery = this.innerQueries.filter(q => q.isInEditMode())[0];
1,742✔
954
        return this.selectedField && this.selectedCondition &&
1,742✔
955
            (
956
                (
957
                    ((!Array.isArray(this.searchValue.value) && !!this.searchValue.value) || (Array.isArray(this.searchValue.value) && this.searchValue.value.length !== 0)) &&
958
                    !(this.selectedField?.filters?.condition(this.selectedCondition)?.isNestedQuery)
959
                ) ||
960
                (
961
                    this.selectedField?.filters?.condition(this.selectedCondition)?.isNestedQuery && innerQuery && !!innerQuery.expressionTree && innerQuery.selectedReturnFields?.length > 0
962
                ) ||
963
                this.selectedField.filters.condition(this.selectedCondition)?.isUnary
964
            );
965
    }
966
    
967
    /**
968
     * @hidden @internal
969
     */
970
    public canCommitCurrentState(): boolean {
971
        const innerQuery = this.innerQueries.filter(q => q.isInEditMode())[0];
36✔
972
        if (innerQuery) {
36✔
973
            return this.selectedReturnFields?.length > 0 && innerQuery.canCommitCurrentState();
8✔
974
        } else {
975
            return this.selectedReturnFields?.length > 0 &&
28✔
976
                (
977
                    (!this._editedExpression) || // no edited expr
978
                    (this._editedExpression && !this.selectedField) || // empty edited expr
979
                    (this._editedExpression && this.operandCanBeCommitted() === true) // valid edited expr
980
                );
981
        }
982
    }
983

984
    /**
985
     * @hidden @internal
986
     */
987
    public commitCurrentState(): void {
988
        const innerQuery = this.innerQueries.filter(q => q.isInEditMode())[0];
3✔
989
        if (innerQuery) {
3✔
990
            innerQuery.commitCurrentState();
1✔
991
        }
992

993
        if (this._editedExpression) {
3✔
994
            if (this.selectedField) {
3!
995
                this.commitOperandEdit();
3✔
996
            } else {
997
                this.deleteItem(this._editedExpression);
×
998
                this._editedExpression = null;
×
999
            }
1000
        }
1001
    }
1002

1003
    /**
1004
     * @hidden @internal
1005
     */
1006
    public exitEditAddMode(shouldPreventInit = false) {
46✔
1007
        if (!this._editedExpression) {
234✔
1008
            return;
219✔
1009
        }
1010

1011
        this.exitOperandEdit();
15✔
1012
        this.cancelOperandAdd();
15✔
1013

1014
        if (shouldPreventInit) {
15✔
1015
            this._preventInit = true;
7✔
1016
        }
1017
    }
1018

1019
    /**
1020
     * @hidden @internal
1021
     *
1022
     * used by the grid
1023
     */
1024
    public exitOperandEdit() {
1025
        if (!this._editedExpression) {
35✔
1026
            return;
16✔
1027
        }
1028

1029
        if (this.operandCanBeCommitted()) {
19✔
1030
            this.commitOperandEdit();
9✔
1031
        } else {
1032
            this.cancelOperandEdit();
10✔
1033
        }
1034
    }
1035

1036
    /**
1037
     * @hidden @internal
1038
     */
1039
    public isExpressionGroup(expression: ExpressionItem): boolean {
1040
        return expression instanceof ExpressionGroupItem;
34,525✔
1041
    }
1042

1043
    /**
1044
     * @hidden @internal
1045
     */
1046
    public onExpressionFocus(expressionItem: ExpressionOperandItem) {
1047
        if (this.prevFocusedExpression) {
41✔
1048
            this.prevFocusedExpression.focused = false;
4✔
1049
        }
1050
        expressionItem.focused = true;
41✔
1051
        this.prevFocusedExpression = expressionItem;
41✔
1052
    }
1053

1054
    /**
1055
     * @hidden @internal
1056
     */
1057
    public onExpressionBlur(event, expressionItem: ExpressionOperandItem) {
1058
        if (this._prevFocusedContainer && this._prevFocusedContainer !== event.target.closest('.igx-filter-tree__expression-item')) {
2!
1059
            expressionItem.focused = false;
×
1060
        }
1061
        this._prevFocusedContainer = event.target.closest('.igx-filter-tree__expression-item');
2✔
1062
    }
1063

1064
    /**
1065
     * @hidden @internal
1066
     */
1067
    public onChipRemove(expressionItem: ExpressionItem) {
1068
        this.exitEditAddMode();
5✔
1069
        this.deleteItem(expressionItem);
5✔
1070
    }
1071

1072
    /**
1073
     * @hidden @internal
1074
     */
1075
    public focusChipAfterDrag = (index: number) => {
293✔
1076
        this._lastFocusedChipIndex = index;
11✔
1077
        this.focusEditedExpressionChip();
11✔
1078
    }
1079
    /**
1080
     * @hidden @internal
1081
     */
1082
    public addExpressionBlur() {
1083
        if (this.prevFocusedExpression) {
×
1084
            this.prevFocusedExpression.focused = false;
×
1085
        }
1086
        if (this.addExpressionItemDropDown && !this.addExpressionItemDropDown.collapsed) {
×
1087
            this.addExpressionItemDropDown.close();
×
1088
        }
1089
    }
1090

1091
    /**
1092
     * @hidden @internal
1093
     */
1094
    public onChipClick(expressionItem: ExpressionOperandItem, chip: IgxChipComponent) {
1095
        this.enterExpressionEdit(expressionItem, chip);
55✔
1096
    }
1097

1098
    /**
1099
     * @hidden @internal
1100
     */
1101
    public enterExpressionEdit(expressionItem: ExpressionOperandItem, chip?: IgxChipComponent) {
1102
        this.exitEditAddMode(true);
133✔
1103
        this.cdr.detectChanges();
133✔
1104
        this._lastFocusedChipIndex = chip ? this.expressionsChips.toArray().findIndex(expr => expr === chip) : this._lastFocusedChipIndex;
133✔
1105
        this.enterEditMode(expressionItem);
133✔
1106
    }
1107

1108

1109
    /**
1110
     * @hidden @internal
1111
     */
1112
    public clickExpressionAdd(targetButton: HTMLElement, chip: IgxChipComponent) {
1113
        this.exitEditAddMode(true);
4✔
1114
        this.cdr.detectChanges();
4✔
1115
        this._lastFocusedChipIndex = this.expressionsChips.toArray().findIndex(expr => expr === chip);
22✔
1116
        this.openExpressionAddDialog(targetButton);
4✔
1117
    }
1118

1119
    /**
1120
     * @hidden @internal
1121
     */
1122
    public openExpressionAddDialog(targetButton: HTMLElement) {
1123
        this.addExpressionDropDownOverlaySettings.target = targetButton;
4✔
1124
        this.addExpressionDropDownOverlaySettings.positionStrategy = new ConnectedPositioningStrategy({
4✔
1125
            horizontalDirection: HorizontalAlignment.Right,
1126
            horizontalStartPoint: HorizontalAlignment.Left,
1127
            verticalStartPoint: VerticalAlignment.Bottom
1128
        });
1129

1130
        this.addExpressionItemDropDown.open(this.addExpressionDropDownOverlaySettings);
4✔
1131
    }
1132

1133
    /**
1134
     * @hidden @internal
1135
     */
1136
    public enterExpressionAdd(event: ISelectionEventArgs, expressionItem: ExpressionOperandItem) {
1137
        if (this._addModeExpression) {
1!
1138
            this._addModeExpression.inAddMode = false;
×
1139
        }
1140

1141
        if (this.parentExpression) {
1!
1142
            this.inEditModeChange.emit(this.parentExpression);
×
1143
        }
1144

1145
        const parent = expressionItem.parent ?? this.rootGroup;
1!
1146
        requestAnimationFrame(() => {
1✔
1147
            if (event.newSelection.value === 'addCondition') {
1!
1148
                this.addCondition(parent, expressionItem);
1✔
1149
            } else if (event.newSelection.value === 'addGroup') {
×
1150
                this.addReverseGroup(parent, expressionItem);
×
1151
            }
1152
            expressionItem.inAddMode = true;
1✔
1153
            this._addModeExpression = expressionItem;
1✔
1154
        })
1155
    }
1156

1157
    /**
1158
     * @hidden @internal
1159
     */
1160
    public enterEditMode(expressionItem: ExpressionOperandItem) {
1161
        if (this._editedExpression) {
133!
1162
            this._editedExpression.inEditMode = false;
×
1163
        }
1164

1165
        if (this.parentExpression) {
133✔
1166
            this.inEditModeChange.emit(this.parentExpression);
16✔
1167
        }
1168

1169
        expressionItem.hovered = false;
133✔
1170
        this.fields = this.selectedEntity ? this.selectedEntity.fields : null;
133✔
1171
        this.selectedField =
133✔
1172
            expressionItem.expression.fieldName ?
133✔
1173
                this.fields?.find(field => field.field === expressionItem.expression.fieldName)
89✔
1174
                : null;
1175
        this.selectedCondition =
133✔
1176
            expressionItem.expression.condition ?
133✔
1177
                expressionItem.expression.condition.name :
1178
                null;
1179
        this.searchValue.value = expressionItem.expression.searchVal instanceof Set ?
133✔
1180
            Array.from(expressionItem.expression.searchVal) :
1181
            expressionItem.expression.searchVal;
1182

1183
        expressionItem.inEditMode = true;
133✔
1184
        this._editedExpression = expressionItem;
133✔
1185
        this.cdr.detectChanges();
133✔
1186

1187
        this.entitySelectOverlaySettings.target = this.entitySelect.element;
133✔
1188
        this.entitySelectOverlaySettings.excludeFromOutsideClick = [this.entitySelect.element as HTMLElement];
133✔
1189
        this.entitySelectOverlaySettings.positionStrategy = new AutoPositionStrategy();
133✔
1190

1191
        if (this.returnFieldSelect) {
133✔
1192
            this.returnFieldSelectOverlaySettings.target = this.returnFieldSelect.element;
16✔
1193
            this.returnFieldSelectOverlaySettings.excludeFromOutsideClick = [this.returnFieldSelect.element as HTMLElement];
16✔
1194
            this.returnFieldSelectOverlaySettings.positionStrategy = new AutoPositionStrategy();
16✔
1195
        }
1196
        if (this.fieldSelect) {
133✔
1197
            this.fieldSelectOverlaySettings.target = this.fieldSelect.element;
130✔
1198
            this.fieldSelectOverlaySettings.excludeFromOutsideClick = [this.fieldSelect.element as HTMLElement];
130✔
1199
            this.fieldSelectOverlaySettings.positionStrategy = new AutoPositionStrategy();
130✔
1200
        }
1201
        if (this.conditionSelect) {
133✔
1202
            this.conditionSelectOverlaySettings.target = this.conditionSelect.element;
130✔
1203
            this.conditionSelectOverlaySettings.excludeFromOutsideClick = [this.conditionSelect.element as HTMLElement];
130✔
1204
            this.conditionSelectOverlaySettings.positionStrategy = new AutoPositionStrategy();
130✔
1205
        }
1206

1207
        if (!this.selectedField) {
133✔
1208
            this.fieldSelect.input.nativeElement.focus();
78✔
1209
        } else if (this.selectedField.filters.condition(this.selectedCondition)?.isUnary) {
55✔
1210
            this.conditionSelect?.input.nativeElement.focus();
5✔
1211
        } else {
1212
            const input = this.searchValueInput?.nativeElement || this.picker?.getEditElement();
50✔
1213
            input?.focus();
50✔
1214
        }
1215

1216
        (this.editingInputs?.nativeElement.parentElement as HTMLElement)?.scrollIntoView({ block: "nearest", inline: "nearest" });
133✔
1217
    }
1218

1219
    /**
1220
     * @hidden @internal
1221
     */
1222
    public onConditionSelectChanging(event: ISelectionEventArgs) {
1223
        event.cancel = true;
67✔
1224
        this.selectedCondition = event.newSelection.value;
67✔
1225
        this.conditionSelect.close();
67✔
1226
        this.cdr.detectChanges();
67✔
1227
    }
1228

1229
    /**
1230
     * @hidden @internal
1231
     */
1232
    public onKeyDown(eventArgs: KeyboardEvent) {
1233
        eventArgs.stopPropagation();
×
1234
    }
1235

1236
    /**
1237
     * @hidden @internal
1238
     */
1239
    public onGroupClick(groupContextMenuDropDown: any, targetButton: HTMLButtonElement, groupItem: ExpressionGroupItem) {
1240
        this.exitEditAddMode();
7✔
1241
        this.cdr.detectChanges();
7✔
1242

1243
        this.groupContextMenuDropDown = groupContextMenuDropDown;
7✔
1244
        this.groupContextMenuDropDownOverlaySettings.target = targetButton;
7✔
1245
        this.groupContextMenuDropDownOverlaySettings.positionStrategy = new ConnectedPositioningStrategy({
7✔
1246
            horizontalDirection: HorizontalAlignment.Right,
1247
            horizontalStartPoint: HorizontalAlignment.Left,
1248
            verticalStartPoint: VerticalAlignment.Bottom
1249
        });
1250

1251
        if (groupContextMenuDropDown.collapsed) {
7!
1252
            this.contextualGroup = groupItem;
7✔
1253
            groupContextMenuDropDown.open(this.groupContextMenuDropDownOverlaySettings);
7✔
1254
        } else {
1255
            groupContextMenuDropDown.close();
×
1256
        }
1257
    }
1258

1259
    /**
1260
     * @hidden @internal
1261
     */
1262
    public getOperator(expressionItem: any) {
1263
        // if (!expressionItem && !this.expressionTree && !this.initialOperator) {
1264
        //     this.initialOperator = 0;
1265
        // }
1266

1267
        const operator = expressionItem ?
89,574✔
1268
            expressionItem.operator :
1269
            this.expressionTree ?
2,964✔
1270
                this.expressionTree.operator :
1271
                this.initialOperator;
1272
        return operator;
89,574✔
1273
    }
1274

1275
    /**
1276
     * @hidden @internal
1277
     */
1278
    public getSwitchGroupText(expressionItem: any) {
1279
        const operator = this.getOperator(expressionItem);
14,916✔
1280
        const condition = operator === FilteringLogic.Or ? this.resourceStrings.igx_query_builder_and_label : this.resourceStrings.igx_query_builder_or_label
14,916✔
1281
        return this.resourceStrings.igx_query_builder_switch_group.replace('{0}', condition.toUpperCase());
14,916✔
1282
    }
1283

1284
    /**
1285
     * @hidden @internal
1286
     */
1287
    public onGroupContextMenuDropDownSelectionChanging(event: ISelectionEventArgs) {
1288
        event.cancel = true;
4✔
1289

1290
        if (event.newSelection.value === 'switchCondition') {
4✔
1291
            const newOperator = (!this.expressionTree ? this.initialOperator : (this.contextualGroup ?? this._expressionTree).operator) === 0 ? 1 : 0;
3!
1292
            this.selectFilteringLogic(newOperator);
3✔
1293
        } else if (event.newSelection.value === 'ungroup') {
1✔
1294
            this.ungroup();
1✔
1295
        }
1296

1297
        this.groupContextMenuDropDown.close();
4✔
1298
    }
1299

1300
    /**
1301
     * @hidden @internal
1302
     */
1303
    public ungroup() {
1304
        const selectedGroup = this.contextualGroup;
1✔
1305
        const parent = selectedGroup.parent;
1✔
1306
        if (parent) {
1✔
1307
            const index = parent.children.indexOf(selectedGroup);
1✔
1308
            parent.children.splice(index, 1, ...selectedGroup.children);
1✔
1309

1310
            for (const expr of selectedGroup.children) {
1✔
1311
                expr.parent = parent;
2✔
1312
            }
1313
        }
1314
        this.commitOperandEdit();
1✔
1315
    }
1316

1317
    /**
1318
     * @hidden @internal
1319
     */
1320
    public selectFilteringLogic(index: number) {
1321
        if (!this.expressionTree) {
3!
1322
            this.initialOperator = index;
×
1323
            return;
×
1324
        }
1325

1326
        if (this.contextualGroup) {
3✔
1327
            this.contextualGroup.operator = index as FilteringLogic;
2✔
1328
            this.commitOperandEdit();
2✔
1329
        } else if (this.expressionTree) {
1✔
1330
            this._expressionTree.operator = index as FilteringLogic;
1✔
1331
        }
1332

1333
        this.initialOperator = null;
3✔
1334
    }
1335

1336
    /**
1337
     * @hidden @internal
1338
     */
1339
    public getConditionFriendlyName(name: string): string {
1340
        // As we have an 'In' condition already used in ESF to search in a Set, we add the 'Query' suffix to the newly introduced nested query condition names.
1341
        // So instead of in/notIn we end up with 'inQuery'/'notInQuery', hence removing the suffix from the friendly name.
1342
        return this.resourceStrings[`igx_query_builder_filter_${name?.replace('Query', '')}`] || name;
60,606✔
1343
    }
1344

1345
    /**
1346
     * @hidden @internal
1347
     */
1348
    public isDate(value: any) {
1349
        return value instanceof Date;
20,056✔
1350
    }
1351

1352
    /**
1353
     * @hidden @internal
1354
     */
1355
    public invokeClick(eventArgs: KeyboardEvent) {
1356
        if (!this.dragService.dropGhostExpression && this.platform.isActivationKey(eventArgs)) {
2✔
1357
            eventArgs.preventDefault();
2✔
1358
            (eventArgs.currentTarget as HTMLElement).click();
2✔
1359
        }
1360
    }
1361

1362
    /**
1363
     * @hidden @internal
1364
     */
1365
    public openPicker(args: KeyboardEvent) {
1366
        if (this.platform.isActivationKey(args)) {
×
1367
            args.preventDefault();
×
1368
            this.picker.open();
×
1369
        }
1370
    }
1371

1372
    /**
1373
     * @hidden @internal
1374
     */
1375
    public onOutletPointerDown(event) {
1376
        // This prevents closing the select's dropdown when clicking the scroll
1377
        event.preventDefault();
60✔
1378
    }
1379

1380
    /**
1381
     * @hidden @internal
1382
     */
1383
    public getConditionList(): string[] {
1384
        if (!this.selectedField) return [];
1,696✔
1385

1386
        if (!this.selectedField.filters) {
1,244!
NEW
1387
            this.selectedField.filters = this.getFilters(this.selectedField);
×
1388
        }
1389

1390
        if ((this.isAdvancedFiltering() && !this.entities[0].childEntities) ||
1,244✔
1391
            (this.isHierarchicalGridNestedQuery() && this.selectedEntity.name && !this.selectedEntity.childEntities)) {
1392
            return this.selectedField.filters.conditionList();
218✔
1393
        }
1394

1395
        return this.selectedField.filters.extendedConditionList();
1,026✔
1396
    }
1397

1398
    /**
1399
     * @hidden @internal
1400
     */
1401
    public getFormatter(field: string) {
1402
        return this.fields?.find(el => el.field === field)?.formatter;
72,322✔
1403
    }
1404

1405
    /**
1406
     * @hidden @internal
1407
     */
1408
    public getFormat(field: string) {
1409
        return this.fields?.find(el => el.field === field).pipeArgs.format;
1,845✔
1410
    }
1411

1412
    /**
1413
     * @hidden @internal
1414
     *
1415
     * used by the grid
1416
     */
1417
    public setAddButtonFocus() {
1418
        if (this.addRootAndGroupButton) {
46!
1419
            this.addRootAndGroupButton.nativeElement.focus();
×
1420
        } else if (this.addConditionButton) {
46✔
1421
            this.addConditionButton.nativeElement.focus();
18✔
1422
        }
1423
    }
1424

1425
    /**
1426
     * @hidden @internal
1427
     */
1428
    public context(expression: ExpressionItem, afterExpression?: ExpressionItem) {
1429
        return {
56,204✔
1430
            $implicit: expression,
1431
            afterExpression
1432
        };
1433
    }
1434

1435
    public formatReturnFields(innerTree: IFilteringExpressionsTree) {
1436
        const returnFields = innerTree.returnFields;
3,642✔
1437
        let text = returnFields.join(', ');
3,642✔
1438
        const innerTreeEntity = this.entities?.find(el => el.name === innerTree.entity);
3,642✔
1439
        if (returnFields.length === innerTreeEntity?.fields.length) {
3,642!
1440
            text = this.resourceStrings.igx_query_builder_all_fields;
×
1441
        } else {
1442
            text = returnFields.join(', ');
3,642✔
1443
            text = text.length > 25 ? text.substring(0, 25) + ' ...' : text;
3,642!
1444
        }
1445
        return text;
3,642✔
1446
    }
1447

1448
    public isInEditMode(): boolean {
1449
        return !this.parentExpression || (this.parentExpression && this.parentExpression.inEditMode);
64,781✔
1450
    }
1451

1452
    public onInEditModeChanged(expressionItem: ExpressionOperandItem) {
1453
        if (!expressionItem.inEditMode) {
16!
1454
            this.enterExpressionEdit(expressionItem);
×
1455
        }
1456
    }
1457

1458
    public getExpressionTreeCopy(expressionTree: IExpressionTree, shouldAssignInnerQueryExprTree?: boolean): IExpressionTree {
1459
        if (!expressionTree) {
310✔
1460
            return null;
279✔
1461
        }
1462

1463
        const exprTreeCopy = new FilteringExpressionsTree(expressionTree.operator, expressionTree.fieldName, expressionTree.entity, expressionTree.returnFields);
31✔
1464
        exprTreeCopy.filteringOperands = [];
31✔
1465

1466
        expressionTree.filteringOperands.forEach(o => isTree(o) ? exprTreeCopy.filteringOperands.push(this.getExpressionTreeCopy(o)) : exprTreeCopy.filteringOperands.push(o));
56!
1467

1468
        if (!this.innerQueryNewExpressionTree && shouldAssignInnerQueryExprTree) {
31✔
1469
            this.innerQueryNewExpressionTree = exprTreeCopy;
18✔
1470
        }
1471

1472
        return exprTreeCopy;
31✔
1473
    }
1474

1475
    public onSelectAllClicked() {
1476
        if (
2✔
1477
            (this._selectedReturnFields.length > 0 && this._selectedReturnFields.length < this._selectedEntity.fields.length) ||
5✔
1478
            this._selectedReturnFields.length == this._selectedEntity.fields.length
1479
        ) {
1480
            this.returnFieldsCombo.deselectAllItems();
1✔
1481
        } else {
1482
            this.returnFieldsCombo.selectAllItems();
1✔
1483
        }
1484
    }
1485

1486
    public onReturnFieldSelectChanging(event: IComboSelectionChangingEventArgs | ISelectionEventArgs) {
1487
        let newSelection = [];
11✔
1488
        if (Array.isArray(event.newSelection)) {
11✔
1489
            newSelection = event.newSelection.map(item => item.field)
11✔
1490
        } else {
1491
            newSelection.push(event.newSelection.value);
6✔
1492
            this._selectedReturnFields = newSelection;
6✔
1493
        }
1494

1495
        this.initExpressionTree(this.selectedEntity.name, newSelection);
11✔
1496
    }
1497

1498
    public initExpressionTree(selectedEntityName: string, selectedReturnFields: string[]) {
1499
        if (!this._expressionTree) {
50✔
1500
            this._expressionTree = this.createExpressionTreeFromGroupItem(new ExpressionGroupItem(FilteringLogic.And, this.rootGroup), selectedEntityName, selectedReturnFields);
34✔
1501
        }
1502

1503
        if (!this.parentExpression) {
50✔
1504
            this.expressionTreeChange.emit(this._expressionTree);
38✔
1505
        }
1506
    }
1507

1508
    public getSearchValueTemplateContext(defaultSearchValueTemplate): any {
1509
        const ctx = {
1,696✔
1510
            $implicit: this.searchValue,
1511
            selectedField: this.selectedField,
1512
            selectedCondition: this.selectedCondition,
1513
            defaultSearchValueTemplate: defaultSearchValueTemplate
1514
        };
1515
        return ctx;
1,696✔
1516
    }
1517

1518
    private getPipeArgs(field: FieldType) {
1519
        let pipeArgs = {...field.pipeArgs};
3,280✔
1520
        if (!pipeArgs) {
3,280!
1521
            pipeArgs = { digitsInfo: DEFAULT_PIPE_DIGITS_INFO };
×
1522
        }
1523

1524
        if (!pipeArgs.format) {
3,280✔
1525
            pipeArgs.format = field.dataType === DataType.Time ?
3,053!
1526
                DEFAULT_PIPE_TIME_FORMAT : field.dataType === DataType.DateTime ?
3,053!
1527
                    DEFAULT_PIPE_DATE_TIME_FORMAT : DEFAULT_PIPE_DATE_FORMAT;
1528
        }
1529
        
1530
        return pipeArgs;
3,280✔
1531
    }
1532

1533
    private selectDefaultCondition() {
1534
        if (this.selectedField && this.selectedField.filters) {
171✔
1535
            this.selectedCondition = this.selectedField.filters.conditionList().indexOf('equals') >= 0 ? 'equals' : this.selectedField.filters.conditionList()[0];
125✔
1536
        }
1537
    }
1538

1539
    private getFilters(field: FieldType) {
1540
        if (!field.filters) {
3,280✔
1541
            switch (field.dataType) {
3,053!
1542
                case DataType.Boolean:
1543
                    return IgxBooleanFilteringOperand.instance();
759✔
1544
                case DataType.Number:
1545
                case DataType.Currency:
1546
                case DataType.Percent:
1547
                    return IgxNumberFilteringOperand.instance();
1,135✔
1548
                case DataType.Date:
1549
                    return IgxDateFilteringOperand.instance();
384✔
1550
                case DataType.Time:
1551
                    return IgxTimeFilteringOperand.instance();
×
1552
                case DataType.DateTime:
1553
                    return IgxDateTimeFilteringOperand.instance();
×
1554
                case DataType.String:
1555
                default:
1556
                    return IgxStringFilteringOperand.instance();
775✔
1557
            }
1558
        } else {
1559
            return field.filters;
227✔
1560
        }
1561
    }
1562

1563

1564
    private addGroup(operator: FilteringLogic, parent?: ExpressionGroupItem, afterExpression?: ExpressionItem) {
1565
        this.cancelOperandAdd();
3✔
1566

1567
        const groupItem = new ExpressionGroupItem(operator, parent);
3✔
1568

1569
        if (parent) {
3!
1570
            if (afterExpression) {
3!
1571
                const index = parent.children.indexOf(afterExpression);
×
1572
                parent.children.splice(index + 1, 0, groupItem);
×
1573
            } else {
1574
                parent.children.push(groupItem);
3✔
1575
            }
1576
        } else {
1577
            this.rootGroup = groupItem;
×
1578
        }
1579

1580
        this.addCondition(groupItem);
3✔
1581
        this.currentGroup = groupItem;
3✔
1582
    }
1583

1584
    private createExpressionGroupItem(expressionTree: IExpressionTree, parent?: ExpressionGroupItem, entityName?: string): ExpressionGroupItem {
1585
        let groupItem: ExpressionGroupItem;
1586
        if (expressionTree) {
851✔
1587
            groupItem = new ExpressionGroupItem(expressionTree.operator, parent);
684✔
1588
            if (!expressionTree.filteringOperands) {
684!
1589
                return groupItem;
×
1590
            }
1591

1592
            for (let i = 0; i < expressionTree.filteringOperands.length; i++) {
684✔
1593
                const expr = expressionTree.filteringOperands[i];
1,493✔
1594

1595
                if (isTree(expr)) {
1,493✔
1596
                    groupItem.children.push(this.createExpressionGroupItem(expr, groupItem, expressionTree.entity));
90✔
1597
                } else {
1598
                    const filteringExpr = expr as IFilteringExpression;
1,403✔
1599
                    const exprCopy: IFilteringExpression = {
1,403✔
1600
                        fieldName: filteringExpr.fieldName,
1601
                        condition: filteringExpr.condition,
1602
                        conditionName: filteringExpr.condition?.name || filteringExpr.conditionName,
1,403!
1603
                        searchVal: filteringExpr.searchVal,
1604
                        searchTree: filteringExpr.searchTree,
1605
                        ignoreCase: filteringExpr.ignoreCase
1606
                    };
1607
                    const operandItem = new ExpressionOperandItem(exprCopy, groupItem);
1,403✔
1608
                    const field = this.fields?.find(el => el.field === filteringExpr.fieldName);
2,025✔
1609
                    operandItem.fieldLabel = field?.label || field?.header || field?.field;
1,403✔
1610
                    if (this._expandedExpressions.filter(e => e.searchTree == operandItem.expression.searchTree).length > 0) {
1,403!
1611
                        operandItem.expanded = true;
×
1612
                    }
1613
                    groupItem.children.push(operandItem);
1,403✔
1614
                }
1615
            }
1616

1617

1618
            if (expressionTree.entity) {
684✔
1619
                entityName = expressionTree.entity;
626✔
1620
            }
1621
            const entity = this.entities?.find(el => el.name === entityName);
981✔
1622
            if (entity) {
684✔
1623
                this.fields = entity.fields;
630✔
1624
            }
1625

1626
            this._selectedEntity = this.entities?.find(el => el.name === entityName);
981✔
1627
            this._selectedReturnFields =
684✔
1628
                !expressionTree.returnFields || expressionTree.returnFields.includes('*') || expressionTree.returnFields.includes('All') || expressionTree.returnFields.length === 0
2,874✔
1629
                    ? this.fields?.map(f => f.field)
1,240✔
1630
                    : this.fields?.filter(f => expressionTree.returnFields.indexOf(f.field) >= 0).map(f => f.field);
1,660✔
1631
        }
1632
        return groupItem;
851✔
1633
    }
1634

1635
    private createExpressionTreeFromGroupItem(groupItem: ExpressionGroupItem, entity?: string, returnFields?: string[]): FilteringExpressionsTree {
1636
        if (!groupItem) {
153✔
1637
            return null;
5✔
1638
        }
1639

1640
        const expressionTree = new FilteringExpressionsTree(groupItem.operator, undefined, entity, returnFields);
148✔
1641

1642
        for (let i = 0; i < groupItem.children.length; i++) {
148✔
1643
            const item = groupItem.children[i];
201✔
1644

1645
            if (item instanceof ExpressionGroupItem) {
201✔
1646
                const subTree = this.createExpressionTreeFromGroupItem((item as ExpressionGroupItem), entity, returnFields);
19✔
1647
                expressionTree.filteringOperands.push(subTree);
19✔
1648
            } else {
1649
                expressionTree.filteringOperands.push((item as ExpressionOperandItem).expression);
182✔
1650
            }
1651
        }
1652

1653
        return expressionTree;
148✔
1654
    }
1655

1656
    private scrollElementIntoView(target: HTMLElement) {
1657
        const container = this.expressionsContainer.nativeElement;
652✔
1658
        const targetOffset = target.offsetTop - container.offsetTop;
652✔
1659
        const delta = 10;
652✔
1660

1661
        if (container.scrollTop + delta > targetOffset) {
652✔
1662
            container.scrollTop = targetOffset - delta;
422✔
1663
        } else if (container.scrollTop + container.clientHeight < targetOffset + target.offsetHeight + delta) {
230✔
1664
            container.scrollTop = targetOffset + target.offsetHeight + delta - container.clientHeight;
225✔
1665
        }
1666
    }
1667

1668
    private focusEditedExpressionChip() {
1669
        if (this._timeoutId) {
61✔
1670
            clearTimeout(this._timeoutId);
5✔
1671
        }
1672

1673
        this._timeoutId = setTimeout(() => {
61✔
1674
            if (this._lastFocusedChipIndex != -1) {
61✔
1675
                //Sort the expression chip list.
1676
                //If there was a recent drag&drop and the tree hasn't rerendered(child query), they will be unordered
1677
                const sortedChips = this.expressionsChips.toArray().sort(function (a, b) {
59✔
1678
                    if (a === b) return 0;
73!
1679
                    if (a.chipArea.nativeElement.compareDocumentPosition(b.chipArea.nativeElement) & 2) {
73✔
1680
                        // b comes before a
1681
                        return 1;
60✔
1682
                    }
1683
                    return -1;
13✔
1684
                });
1685
                const chipElement = sortedChips[this._lastFocusedChipIndex]?.nativeElement;
59✔
1686
                if (chipElement) {
59✔
1687
                    chipElement.focus();
51✔
1688
                }
1689
                this._lastFocusedChipIndex = -1;
59✔
1690
                this._focusDelay = DEFAULT_CHIP_FOCUS_DELAY;
59✔
1691
            }
1692
        }, this._focusDelay);
1693
    }
1694

1695
    private init() {
1696
        this.cancelOperandAdd();
498✔
1697
        this.cancelOperandEdit();
498✔
1698

1699
        // Ignore values of certain properties for the comparison
1700
        const propsToIgnore = ['parent', 'hovered', 'ignoreCase', 'inEditMode', 'inAddMode'];
498✔
1701
        const propsReplacer = function replacer(key, value) {
498✔
1702
            if (propsToIgnore.indexOf(key) >= 0) {
19,533✔
1703
                return undefined;
2,606✔
1704
            } else {
1705
                return value;
16,927✔
1706
            }
1707
        };
1708

1709
        // Skip root being recreated if the same
1710
        const newRootGroup = this.createExpressionGroupItem(this.expressionTree);
498✔
1711
        if (JSON.stringify(this.rootGroup, propsReplacer) !== JSON.stringify(newRootGroup, propsReplacer)) {
498✔
1712
            this.rootGroup = this.createExpressionGroupItem(this.expressionTree);
257✔
1713
            this.currentGroup = this.rootGroup;
257✔
1714
        }
1715

1716
        if (this.rootGroup?.children?.length == 0) {
498✔
1717
            this.rootGroup = null;
40✔
1718
            this.currentGroup = null;
40✔
1719
        }
1720
    }
1721

1722
    /** rootGroup is recreated after clicking Apply, which sets new expressionTree and calls init()*/
1723
    protected trackExpressionItem = trackByIdentity;
293✔
1724
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc