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

IgniteUI / igniteui-angular / 14170364351

31 Mar 2025 11:21AM UTC coverage: 91.625% (-0.005%) from 91.63%
14170364351

Pull #15628

github

web-flow
Merge 6a98144a2 into 82885eff3
Pull Request #15628: [tree] - fix igx-circular-bar in context of tree component

13318 of 15578 branches covered (85.49%)

26859 of 29314 relevant lines covered (91.63%)

34136.44 hits per line

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

91.7
/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';
2✔
60
const DEFAULT_PIPE_TIME_FORMAT = 'mediumTime';
2✔
61
const DEFAULT_PIPE_DATE_TIME_FORMAT = 'medium';
2✔
62
const DEFAULT_PIPE_DIGITS_INFO = '1.0-3';
2✔
63
const DEFAULT_CHIP_FOCUS_DELAY = 50;
2✔
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 {
2✔
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,057✔
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;
275✔
142

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

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

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

166
        return this._fields;
57,387✔
167
    }
168

169
    /**
170
     * Sets the fields.
171
     */
172
    @Input()
173
    public set fields(fields: FieldType[]) {
174
        this._fields = fields;
772✔
175

176
        if (!this._fields && this.isAdvancedFiltering()) {
772✔
177
            this._fields = this.entities[0].fields;
27✔
178
        }
179

180
        if (this._fields) {
772✔
181
            this._fields.forEach(field => {
772✔
182
                this.setFilters(field);
3,163✔
183
                this.setFormat(field);
3,163✔
184
            });
185
        }
186
    }
187

188
    /**
189
    * Returns the expression tree.
190
    */
191
    public get expressionTree(): IExpressionTree {
192
        return this._expressionTree;
5,835✔
193
    }
194

195
    /**
196
     * Sets the expression tree.
197
     */
198
    @Input()
199
    public set expressionTree(expressionTree: IExpressionTree) {
200
        this._expressionTree = expressionTree;
484✔
201
        if (!expressionTree) {
484✔
202
            this._selectedEntity = null;
144✔
203
            this._selectedReturnFields = [];
144✔
204
        }
205

206
        if (!this._preventInit) {
484✔
207
            this.init();
475✔
208
        }
209
    }
210

211
    /**
212
     * Gets the `locale` of the query builder.
213
     * If not set, defaults to application's locale.
214
     */
215
    @Input()
216
    public get locale(): string {
217
        return this._locale;
950✔
218
    }
219

220
    /**
221
     * Sets the `locale` of the query builder.
222
     * Expects a valid BCP 47 language tag.
223
     */
224
    public set locale(value: string) {
225
        this._locale = value;
417✔
226
        // if value is invalid, set it back to _localeId
227
        try {
417✔
228
            getLocaleFirstDayOfWeek(this._locale);
417✔
229
        } catch {
230
            this._locale = this._localeId;
98✔
231
        }
232
    }
233

234
    /**
235
     * Sets the resource strings.
236
     * By default it uses EN resources.
237
     */
238
    @Input()
239
    public set resourceStrings(value: IQueryBuilderResourceStrings) {
240
        this._resourceStrings = Object.assign({}, this._resourceStrings, value);
143✔
241
    }
242

243
    /**
244
     * Returns the resource strings.
245
     */
246
    public get resourceStrings(): IQueryBuilderResourceStrings {
247
        return this._resourceStrings;
235,763✔
248
    }
249

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

256
    /**
257
     * Event fired if a nested query builder tree is being edited.
258
     */
259
    @Output()
260
    public inEditModeChange = new EventEmitter<ExpressionOperandItem>();
275✔
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) ||
755✔
310
            (value && this._editingInputsContainer && this._editingInputsContainer.nativeElement !== value.nativeElement)) {
311
            requestAnimationFrame(() => {
269✔
312
                this.scrollElementIntoView(value.nativeElement);
269✔
313
            });
314
        }
315

316
        this._editingInputsContainer = value;
755✔
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) ||
755✔
327
            (value && this._currentGroupButtonsContainer && this._currentGroupButtonsContainer.nativeElement !== value.nativeElement)) {
328
            requestAnimationFrame(() => {
346✔
329
                this.scrollElementIntoView(value.nativeElement);
346✔
330
            });
331
        }
332

333
        this._currentGroupButtonsContainer = value;
755✔
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[] = [];
275✔
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 };
275✔
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;
275✔
404

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

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

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

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

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

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

459
    private destroy$ = new Subject<any>();
275✔
460
    private _timeoutId: any;
461
    private _lastFocusedChipIndex: number;
462
    private _focusDelay = DEFAULT_CHIP_FOCUS_DELAY;
275✔
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;
275✔
472
    private _prevFocusedContainer: ElementRef;
473
    private _expandedExpressions: IFilteringExpression[] = [];
275✔
474
    private _fields: FieldType[];
475
    private _locale;
476
    private _entityNewValue: EntityType;
477
    private _resourceStrings = getCurrentResourceStrings(QueryBuilderResourceStringsEN);
275✔
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;
11,782✔
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,583✔
493
    }
494

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

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

515
    private _overlaySettings: OverlaySettings = {
275✔
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;
18,321✔
525
    }
526

527
    /** @hidden */
528
    protected isSearchValueInputDisabled(): boolean {
529
        return !this.selectedField ||
1,477✔
530
            !this.selectedCondition ||
531
            (this.selectedField &&
532
                (this.selectedField.filters.condition(this.selectedCondition).isUnary ||
533
                    this.selectedField.filters.condition(this.selectedCondition).isNestedQuery));
534
    }
535

536
    constructor(public cdr: ChangeDetectorRef,
275✔
537
        public dragService: IgxQueryBuilderDragService,
275✔
538
        protected platform: PlatformUtil,
275✔
539
        private elRef: ElementRef,
275✔
540
        @Inject(LOCALE_ID) protected _localeId: string) {
275✔
541
        this.locale = this.locale || this._localeId;
275✔
542
        this.dragService.register(this, elRef);
275✔
543
    }
544

545
    /**
546
     * @hidden @internal
547
     */
548
    public ngAfterViewInit(): void {
549
        this._overlaySettings.outlet = this.overlayOutlet;
275✔
550
        this.entitySelectOverlaySettings.outlet = this.overlayOutlet;
275✔
551
        this.fieldSelectOverlaySettings.outlet = this.overlayOutlet;
275✔
552
        this.conditionSelectOverlaySettings.outlet = this.overlayOutlet;
275✔
553
        this.returnFieldSelectOverlaySettings.outlet = this.overlayOutlet;
275✔
554
        this.addExpressionDropDownOverlaySettings.outlet = this.overlayOutlet;
275✔
555
        this.groupContextMenuDropDownOverlaySettings.outlet = this.overlayOutlet;
275✔
556
        // Trigger additional change detection cycle
557
        this.cdr.detectChanges();
275✔
558
    }
559

560
    /**
561
     * @hidden @internal
562
     */
563
    public ngOnDestroy(): void {
564
        this.destroy$.next(true);
275✔
565
        this.destroy$.complete();
275✔
566
    }
567

568
    /**
569
     * @hidden @internal
570
     */
571
    public set selectedEntity(value: string) {
572
        this._selectedEntity = this.entities?.find(el => el.name === value);
×
573
    }
574

575
    /**
576
     * @hidden @internal
577
     */
578
    public get selectedEntity(): EntityType {
579
        return this._selectedEntity;
66,761✔
580
    }
581

582
    /**
583
     * @hidden @internal
584
     */
585
    public onEntitySelectChanging(event: ISelectionEventArgs) {
586
        event.cancel = true;
41✔
587
        this._entityNewValue = event.newSelection.value;
41✔
588
        if (event.oldSelection.value && this.queryBuilder.showEntityChangeDialog) {
41✔
589
            this.entityChangeDialog.open();
6✔
590
        } else {
591
            this.onEntityChangeConfirm();
35✔
592
        }
593
    }
594

595
    /**
596
     * @hidden
597
     */
598
    public onShowEntityChangeDialogChange(eventArgs: IChangeCheckboxEventArgs) {
599
        this.queryBuilder.showEntityChangeDialog = !eventArgs.checked;
1✔
600
    }
601

602
    /**
603
     * @hidden
604
     */
605
    public onEntityChangeCancel() {
606
        this.entityChangeDialog.close();
3✔
607
        this.entitySelect.close();
3✔
608
        this._entityNewValue = null;
3✔
609
    }
610

611
    /**
612
     * @hidden
613
     */
614
    public onEntityChangeConfirm() {
615
        if (this._parentExpression) {
37✔
616
            this._expressionTree = this.createExpressionTreeFromGroupItem(this.createExpressionGroupItem(this._expressionTree));
5✔
617
        }
618

619
        this._selectedEntity = this._entityNewValue;
37✔
620
        if (!this._selectedEntity.fields) {
37!
621
            this._selectedEntity.fields = [];
×
622
        }
623
        this.fields = this._entityNewValue ? this._entityNewValue.fields : [];
37!
624

625
        this._selectedReturnFields = this.parentExpression ? [] : this._entityNewValue.fields?.map(f => f.field);
128✔
626

627
        if (this._expressionTree) {
37✔
628
            this._expressionTree.entity = this._entityNewValue.name;
4✔
629
            this._expressionTree.returnFields = [];
4✔
630
            this._expressionTree.filteringOperands = [];
4✔
631

632
            this._editedExpression = null;
4✔
633
            if (!this.parentExpression) {
4✔
634
                this.expressionTreeChange.emit(this._expressionTree);
3✔
635
            }
636

637
            this.rootGroup = null;
4✔
638
            this.currentGroup = this.rootGroup;
4✔
639
        }
640

641
        this._selectedField = null;
37✔
642
        this.selectedCondition = null;
37✔
643
        this.searchValue.value = null;
37✔
644

645
        this.entityChangeDialog.close();
37✔
646
        this.entitySelect.close();
37✔
647

648
        this._entityNewValue = null;
37✔
649
        this.innerQueryNewExpressionTree = null;
37✔
650

651
        this.initExpressionTree(this._selectedEntity.name, this.selectedReturnFields);
37✔
652
    }
653

654
    /**
655
     * @hidden @internal
656
     */
657
    public set selectedReturnFields(value: string[]) {
658
        if (this._selectedReturnFields !== value) {
2✔
659
            this._selectedReturnFields = value;
2✔
660

661
            if (this._expressionTree && !this.parentExpression) {
2✔
662
                this._expressionTree.returnFields = value;
2✔
663
                this.expressionTreeChange.emit(this._expressionTree);
2✔
664
            }
665
        }
666
    }
667

668
    /**
669
     * @hidden @internal
670
     */
671
    public get selectedReturnFields(): string[] {
672
        if (typeof this._selectedReturnFields == 'string') {
44,441!
673
            return [this._selectedReturnFields];
×
674
        }
675
        return this._selectedReturnFields;
44,441✔
676
    }
677

678
    /**
679
     * @hidden @internal
680
     */
681
    public set selectedField(value: FieldType) {
682
        const oldValue = this._selectedField;
184✔
683

684
        if (this._selectedField !== value) {
184✔
685
            this._selectedField = value;
142✔
686
            this.selectDefaultCondition();
142✔
687
            if (oldValue && this._selectedField && this._selectedField.dataType !== oldValue.dataType) {
142✔
688
                this.searchValue.value = null;
17✔
689
                this.cdr.detectChanges();
17✔
690
            }
691
        }
692
    }
693

694
    /**
695
     * @hidden @internal
696
     */
697
    public get selectedField(): FieldType {
698
        return this._selectedField;
43,414✔
699
    }
700

701
    /**
702
     * @hidden @internal
703
     *
704
     * used by the grid
705
     */
706
    public setPickerOutlet(outlet?: IgxOverlayOutletDirective | ElementRef) {
707
        this.pickerOutlet = outlet;
44✔
708
    }
709

710
    /**
711
     * @hidden @internal
712
     *
713
     * used by the grid
714
     */
715
    public get isContextMenuVisible(): boolean {
716
        return !this.groupContextMenuDropDown.collapsed;
×
717
    }
718

719
    /**
720
     * @hidden @internal
721
     */
722
    public get hasEditedExpression(): boolean {
723
        return this._editedExpression !== undefined && this._editedExpression !== null;
30,599✔
724
    }
725

726
    /**
727
     * @hidden @internal
728
     */
729
    public addCondition(parent: ExpressionGroupItem, afterExpression?: ExpressionOperandItem, isUIInteraction?: boolean) {
730
        this.cancelOperandAdd();
67✔
731

732
        const operandItem = new ExpressionOperandItem({
67✔
733
            fieldName: null,
734
            condition: null,
735
            conditionName: null,
736
            ignoreCase: true,
737
            searchVal: null
738
        }, parent);
739

740
        const groupItem = new ExpressionGroupItem(this.getOperator(null) ?? FilteringLogic.And, parent);
67!
741
        this.contextualGroup = groupItem;
67✔
742
        this.initialOperator = null;
67✔
743

744
        this._lastFocusedChipIndex = this._lastFocusedChipIndex === undefined ? -1 : this._lastFocusedChipIndex;
67✔
745

746
        if (parent) {
67✔
747
            if (afterExpression) {
11✔
748
                const index = parent.children.indexOf(afterExpression);
1✔
749
                parent.children.splice(index + 1, 0, operandItem);
1✔
750
            } else {
751
                parent.children.push(operandItem);
10✔
752
            }
753
            this._lastFocusedChipIndex++;
11✔
754
        } else {
755
            this.rootGroup = groupItem;
56✔
756
            operandItem.parent = groupItem;
56✔
757
            this.rootGroup.children.push(operandItem);
56✔
758
            this._lastFocusedChipIndex = 0;
56✔
759
        }
760

761
        this._focusDelay = 250;
67✔
762

763
        if (isUIInteraction && !afterExpression) {
67✔
764
            this._lastFocusedChipIndex = this.expressionsChips.length;
63✔
765
            this._focusDelay = DEFAULT_CHIP_FOCUS_DELAY;
63✔
766
        }
767

768
        this.enterExpressionEdit(operandItem);
67✔
769
    }
770

771
    /**
772
     * @hidden @internal
773
     */
774
    public addReverseGroup(parent?: ExpressionGroupItem, afterExpression?: ExpressionItem) {
775
        parent = parent ?? this.rootGroup;
3!
776

777
        if (parent.operator === FilteringLogic.And) {
3✔
778
            this.addGroup(FilteringLogic.Or, parent, afterExpression);
2✔
779
        } else {
780
            this.addGroup(FilteringLogic.And, parent, afterExpression);
1✔
781
        }
782
    }
783

784
    /**
785
     * @hidden @internal
786
     */
787
    public endGroup(groupItem: ExpressionGroupItem) {
788
        this.currentGroup = groupItem.parent;
×
789
    }
790

791
    /**
792
     * @hidden @internal
793
     */
794
    public commitExpression() {
795
        this.commitOperandEdit();
44✔
796
        this.focusEditedExpressionChip();
44✔
797
    }
798

799
    /**
800
     * @hidden @internal
801
     */
802
    public discardExpression(expressionItem?: ExpressionOperandItem) {
803
        this.cancelOperandEdit();
11✔
804
        if (expressionItem && expressionItem.expression.fieldName) {
11✔
805
            this.focusEditedExpressionChip();
4✔
806
        }
807
    }
808

809
    /**
810
     * @hidden @internal
811
     */
812
    public commitOperandEdit() {
813
        const actualSearchValue = this.searchValue.value;
59✔
814
        if (this._editedExpression) {
59✔
815
            this._editedExpression.expression.fieldName = this.selectedField.field;
56✔
816
            this._editedExpression.expression.condition = this.selectedField.filters.condition(this.selectedCondition);
56✔
817
            this._editedExpression.expression.conditionName = this.selectedCondition;
56✔
818
            this._editedExpression.expression.searchVal = DataUtil.parseValue(this.selectedField.dataType, actualSearchValue) || actualSearchValue;
56✔
819
            this._editedExpression.fieldLabel = this.selectedField.label
56!
820
                ? this.selectedField.label
821
                : this.selectedField.header
56✔
822
                    ? this.selectedField.header
823
                    : this.selectedField.field;
824

825
            const innerQuery = this.innerQueries.filter(q => q.isInEditMode())[0]
56✔
826
            if (innerQuery && this.selectedField?.filters?.condition(this.selectedCondition)?.isNestedQuery) {
56✔
827
                innerQuery.exitEditAddMode();
7✔
828
                this._editedExpression.expression.searchTree = this.getExpressionTreeCopy(innerQuery.expressionTree);
7✔
829
                this._editedExpression.expression.searchTree.returnFields = innerQuery.selectedReturnFields;
7✔
830
            } else {
831
                this._editedExpression.expression.searchTree = null;
49✔
832
            }
833
            this.innerQueryNewExpressionTree = null;
56✔
834

835
            if (this.selectedField.filters.condition(this.selectedCondition)?.isUnary || this.selectedField.filters.condition(this.selectedCondition)?.isNestedQuery) {
56✔
836
                this._editedExpression.expression.searchVal = null;
12✔
837
            }
838

839
            this._editedExpression.inEditMode = false;
56✔
840
            this._editedExpression = null;
56✔
841
        }
842

843
        this._expressionTree = this.createExpressionTreeFromGroupItem(this.rootGroup, this.selectedEntity?.name, this.selectedReturnFields);
59✔
844
        if (!this.parentExpression) {
59✔
845
            this.expressionTreeChange.emit(this._expressionTree);
50✔
846
        }
847
    }
848

849
    /**
850
     * @hidden @internal
851
     */
852
    public cancelOperandAdd() {
853
        if (this._addModeExpression) {
560!
854
            this._addModeExpression.inAddMode = false;
×
855
            this._addModeExpression = null;
×
856
        }
857
    }
858

859
    /**
860
     * @hidden @internal
861
     */
862
    public deleteItem = (expressionItem: ExpressionItem, skipEmit: boolean = false) => {
275✔
863
        if (!expressionItem.parent) {
48✔
864
            this.rootGroup = null;
15✔
865
            this.currentGroup = null;
15✔
866
            //this._expressionTree = null;
867
            return;
15✔
868
        }
869

870
        if (expressionItem === this.currentGroup) {
33!
871
            this.currentGroup = this.currentGroup.parent;
×
872
        }
873

874
        const children = expressionItem.parent.children;
33✔
875
        const index = children.indexOf(expressionItem);
33✔
876
        children.splice(index, 1);
33✔
877
        const entity = this.expressionTree ? this.expressionTree.entity : null;
33✔
878
        const returnFields = this.expressionTree ? this.expressionTree.returnFields : null;
33✔
879
        this._expressionTree = this.createExpressionTreeFromGroupItem(this.rootGroup, entity, returnFields); // TODO: don't recreate if not necessary
33✔
880

881
        if (!children.length) {
33✔
882
            this.deleteItem(expressionItem.parent, true);
16✔
883
        }
884

885
        if (!this.parentExpression && !skipEmit) {
33✔
886
            this.expressionTreeChange.emit(this._expressionTree);
31✔
887
        }
888
    }
889

890
    /**
891
     * @hidden @internal
892
     */
893
    public cancelOperandEdit() {
894
        if (this.innerQueries) {
502✔
895
            const innerQuery = this.innerQueries.filter(q => q.isInEditMode())[0];
227✔
896
            if (innerQuery) {
227✔
897
                if (innerQuery._editedExpression) {
9✔
898
                    innerQuery.cancelOperandEdit();
1✔
899
                }
900

901
                innerQuery.expressionTree = this.getExpressionTreeCopy(this._editedExpression.expression.searchTree);
9✔
902
                this.innerQueryNewExpressionTree = null;
9✔
903
            }
904
        }
905

906
        if (this._editedExpression) {
502✔
907
            this._editedExpression.inEditMode = false;
30✔
908

909
            if (!this._editedExpression.expression.fieldName) {
30✔
910
                this.deleteItem(this._editedExpression);
16✔
911
            }
912

913
            this._editedExpression = null;
30✔
914
        }
915

916
        if (!this.expressionTree && this.contextualGroup) {
502✔
917
            this.initialOperator = this.contextualGroup.operator;
1✔
918
        }
919
    }
920

921
    /**
922
     * @hidden @internal
923
     */
924
    public operandCanBeCommitted(): boolean {
925
        const innerQuery = this.innerQueries.filter(q => q.isInEditMode())[0];
1,544✔
926

927
        return this.selectedField && this.selectedCondition &&
1,544✔
928
            (
929
                (
930
                    ((!Array.isArray(this.searchValue.value) && !!this.searchValue.value) || (Array.isArray(this.searchValue.value) && this.searchValue.value.length !== 0)) &&
931
                    !(this.selectedField?.filters?.condition(this.selectedCondition)?.isNestedQuery)
932
                ) ||
933
                (
934
                    this.selectedField?.filters?.condition(this.selectedCondition)?.isNestedQuery && innerQuery && !!innerQuery.expressionTree && innerQuery.selectedReturnFields?.length > 0
935
                ) ||
936
                this.selectedField.filters.condition(this.selectedCondition)?.isUnary
937
            );
938
    }
939

940
    /**
941
     * @hidden @internal
942
     */
943
    public canCommitCurrentState(): boolean {
944
        const innerQuery = this.innerQueries.filter(q => q.isInEditMode())[0];
36✔
945
        if (innerQuery) {
36✔
946
            return this.selectedReturnFields?.length > 0 && innerQuery.canCommitCurrentState();
8✔
947
        } else {
948
            return this.selectedReturnFields?.length > 0 &&
28✔
949
                (
950
                    (!this._editedExpression) || // no edited expr
951
                    (this._editedExpression && !this.selectedField) || // empty edited expr
952
                    (this._editedExpression && this.operandCanBeCommitted() === true) // valid edited expr
953
                );
954
        }
955
    }
956

957
    /**
958
     * @hidden @internal
959
     */
960
    public commitCurrentState(): void {
961
        const innerQuery = this.innerQueries.filter(q => q.isInEditMode())[0];
3✔
962
        if (innerQuery) {
3✔
963
            innerQuery.commitCurrentState();
1✔
964
        }
965

966
        if (this._editedExpression) {
3✔
967
            if (this.selectedField) {
3!
968
                this.commitOperandEdit();
3✔
969
            } else {
970
                this.deleteItem(this._editedExpression);
×
971
                this._editedExpression = null;
×
972
            }
973
        }
974
    }
975

976
    /**
977
     * @hidden @internal
978
     */
979
    public exitEditAddMode(shouldPreventInit = false) {
39✔
980
        if (!this._editedExpression) {
211✔
981
            return;
196✔
982
        }
983

984
        this.exitOperandEdit();
15✔
985
        this.cancelOperandAdd();
15✔
986

987
        if (shouldPreventInit) {
15✔
988
            this._preventInit = true;
7✔
989
        }
990
    }
991

992
    /**
993
     * @hidden @internal
994
     *
995
     * used by the grid
996
     */
997
    public exitOperandEdit() {
998
        if (!this._editedExpression) {
34✔
999
            return;
15✔
1000
        }
1001

1002
        if (this.operandCanBeCommitted()) {
19✔
1003
            this.commitOperandEdit();
9✔
1004
        } else {
1005
            this.cancelOperandEdit();
10✔
1006
        }
1007
    }
1008

1009
    /**
1010
     * @hidden @internal
1011
     */
1012
    public isExpressionGroup(expression: ExpressionItem): boolean {
1013
        return expression instanceof ExpressionGroupItem;
34,144✔
1014
    }
1015

1016
    /**
1017
     * @hidden @internal
1018
     */
1019
    public onExpressionFocus(expressionItem: ExpressionOperandItem) {
1020
        if (this.prevFocusedExpression) {
41✔
1021
            this.prevFocusedExpression.focused = false;
4✔
1022
        }
1023
        expressionItem.focused = true;
41✔
1024
        this.prevFocusedExpression = expressionItem;
41✔
1025
    }
1026

1027
    /**
1028
     * @hidden @internal
1029
     */
1030
    public onExpressionBlur(event, expressionItem: ExpressionOperandItem) {
1031
        if (this._prevFocusedContainer && this._prevFocusedContainer !== event.target.closest('.igx-filter-tree__expression-item')) {
2!
1032
            expressionItem.focused = false;
×
1033
        }
1034
        this._prevFocusedContainer = event.target.closest('.igx-filter-tree__expression-item');
2✔
1035
    }
1036

1037
    /**
1038
     * @hidden @internal
1039
     */
1040
    public onChipRemove(expressionItem: ExpressionItem) {
1041
        this.exitEditAddMode();
5✔
1042
        this.deleteItem(expressionItem);
5✔
1043
    }
1044

1045
    /**
1046
     * @hidden @internal
1047
     */
1048
    public focusChipAfterDrag = (index: number) => {
275✔
1049
        this._lastFocusedChipIndex = index;
11✔
1050
        this.focusEditedExpressionChip();
11✔
1051
    }
1052
    /**
1053
     * @hidden @internal
1054
     */
1055
    public addExpressionBlur() {
1056
        if (this.prevFocusedExpression) {
×
1057
            this.prevFocusedExpression.focused = false;
×
1058
        }
1059
        if (this.addExpressionItemDropDown && !this.addExpressionItemDropDown.collapsed) {
×
1060
            this.addExpressionItemDropDown.close();
×
1061
        }
1062
    }
1063

1064
    /**
1065
     * @hidden @internal
1066
     */
1067
    public onChipClick(expressionItem: ExpressionOperandItem, chip: IgxChipComponent) {
1068
        this.enterExpressionEdit(expressionItem, chip);
55✔
1069
    }
1070

1071
    /**
1072
     * @hidden @internal
1073
     */
1074
    public enterExpressionEdit(expressionItem: ExpressionOperandItem, chip?: IgxChipComponent) {
1075
        this.exitEditAddMode(true);
122✔
1076
        this.cdr.detectChanges();
122✔
1077
        this._lastFocusedChipIndex = chip ? this.expressionsChips.toArray().findIndex(expr => expr === chip) : this._lastFocusedChipIndex;
122✔
1078
        this.enterEditMode(expressionItem);
122✔
1079
    }
1080

1081

1082
    /**
1083
     * @hidden @internal
1084
     */
1085
    public clickExpressionAdd(targetButton: HTMLElement, chip: IgxChipComponent) {
1086
        this.exitEditAddMode(true);
4✔
1087
        this.cdr.detectChanges();
4✔
1088
        this._lastFocusedChipIndex = this.expressionsChips.toArray().findIndex(expr => expr === chip);
22✔
1089
        this.openExpressionAddDialog(targetButton);
4✔
1090
    }
1091

1092
    /**
1093
     * @hidden @internal
1094
     */
1095
    public openExpressionAddDialog(targetButton: HTMLElement) {
1096
        this.addExpressionDropDownOverlaySettings.target = targetButton;
4✔
1097
        this.addExpressionDropDownOverlaySettings.positionStrategy = new ConnectedPositioningStrategy({
4✔
1098
            horizontalDirection: HorizontalAlignment.Right,
1099
            horizontalStartPoint: HorizontalAlignment.Left,
1100
            verticalStartPoint: VerticalAlignment.Bottom
1101
        });
1102

1103
        this.addExpressionItemDropDown.open(this.addExpressionDropDownOverlaySettings);
4✔
1104
    }
1105

1106
    /**
1107
     * @hidden @internal
1108
     */
1109
    public enterExpressionAdd(event: ISelectionEventArgs, expressionItem: ExpressionOperandItem) {
1110
        if (this._addModeExpression) {
1!
1111
            this._addModeExpression.inAddMode = false;
×
1112
        }
1113

1114
        if (this.parentExpression) {
1!
1115
            this.inEditModeChange.emit(this.parentExpression);
×
1116
        }
1117

1118
        const parent = expressionItem.parent ?? this.rootGroup;
1!
1119
        requestAnimationFrame(() => {
1✔
1120
            if (event.newSelection.value === 'addCondition') {
1!
1121
                this.addCondition(parent, expressionItem);
1✔
1122
            } else if (event.newSelection.value === 'addGroup') {
×
1123
                this.addReverseGroup(parent, expressionItem);
×
1124
            }
1125
            expressionItem.inAddMode = true;
1✔
1126
            this._addModeExpression = expressionItem;
1✔
1127
        })
1128
    }
1129

1130
    /**
1131
     * @hidden @internal
1132
     */
1133
    public enterEditMode(expressionItem: ExpressionOperandItem) {
1134
        if (this._editedExpression) {
122!
1135
            this._editedExpression.inEditMode = false;
×
1136
        }
1137

1138
        if (this.parentExpression) {
122✔
1139
            this.inEditModeChange.emit(this.parentExpression);
13✔
1140
        }
1141

1142
        expressionItem.hovered = false;
122✔
1143
        this.fields = this.selectedEntity ? this.selectedEntity.fields : null;
122✔
1144
        this.selectedField =
122✔
1145
            expressionItem.expression.fieldName ?
122✔
1146
                this.fields?.find(field => field.field === expressionItem.expression.fieldName)
89✔
1147
                : null;
1148
        this.selectedCondition =
122✔
1149
            expressionItem.expression.condition ?
122✔
1150
                expressionItem.expression.condition.name :
1151
                null;
1152
        this.searchValue.value = expressionItem.expression.searchVal instanceof Set ?
122✔
1153
            Array.from(expressionItem.expression.searchVal) :
1154
            expressionItem.expression.searchVal;
1155

1156
        expressionItem.inEditMode = true;
122✔
1157
        this._editedExpression = expressionItem;
122✔
1158
        this.cdr.detectChanges();
122✔
1159

1160
        this.entitySelectOverlaySettings.target = this.entitySelect.element;
122✔
1161
        this.entitySelectOverlaySettings.excludeFromOutsideClick = [this.entitySelect.element as HTMLElement];
122✔
1162
        this.entitySelectOverlaySettings.positionStrategy = new AutoPositionStrategy();
122✔
1163

1164
        if (this.returnFieldSelect) {
122✔
1165
            this.returnFieldSelectOverlaySettings.target = this.returnFieldSelect.element;
13✔
1166
            this.returnFieldSelectOverlaySettings.excludeFromOutsideClick = [this.returnFieldSelect.element as HTMLElement];
13✔
1167
            this.returnFieldSelectOverlaySettings.positionStrategy = new AutoPositionStrategy();
13✔
1168
        }
1169
        if (this.fieldSelect) {
122✔
1170
            this.fieldSelectOverlaySettings.target = this.fieldSelect.element;
119✔
1171
            this.fieldSelectOverlaySettings.excludeFromOutsideClick = [this.fieldSelect.element as HTMLElement];
119✔
1172
            this.fieldSelectOverlaySettings.positionStrategy = new AutoPositionStrategy();
119✔
1173
        }
1174
        if (this.conditionSelect) {
122✔
1175
            this.conditionSelectOverlaySettings.target = this.conditionSelect.element;
119✔
1176
            this.conditionSelectOverlaySettings.excludeFromOutsideClick = [this.conditionSelect.element as HTMLElement];
119✔
1177
            this.conditionSelectOverlaySettings.positionStrategy = new AutoPositionStrategy();
119✔
1178
        }
1179

1180
        if (!this.selectedField) {
122✔
1181
            this.fieldSelect.input.nativeElement.focus();
67✔
1182
        } else if (this.selectedField.filters.condition(this.selectedCondition)?.isUnary) {
55✔
1183
            this.conditionSelect.input.nativeElement.focus();
5✔
1184
        } else {
1185
            const input = this.searchValueInput?.nativeElement || this.picker?.getEditElement();
50✔
1186
            input?.focus();
50✔
1187
        }
1188

1189
        (this.editingInputs?.nativeElement.parentElement as HTMLElement)?.scrollIntoView({ block: "nearest", inline: "nearest" });
122✔
1190
    }
1191

1192
    /**
1193
     * @hidden @internal
1194
     */
1195
    public onConditionSelectChanging(event: ISelectionEventArgs) {
1196
        event.cancel = true;
61✔
1197
        this.selectedCondition = event.newSelection.value;
61✔
1198
        this.conditionSelect.close();
61✔
1199
        this.cdr.detectChanges();
61✔
1200
    }
1201

1202
    /**
1203
     * @hidden @internal
1204
     */
1205
    public onKeyDown(eventArgs: KeyboardEvent) {
1206
        eventArgs.stopPropagation();
×
1207
    }
1208

1209
    /**
1210
     * @hidden @internal
1211
     */
1212
    public onGroupClick(groupContextMenuDropDown: any, targetButton: HTMLButtonElement, groupItem: ExpressionGroupItem) {
1213
        this.exitEditAddMode();
7✔
1214
        this.cdr.detectChanges();
7✔
1215

1216
        this.groupContextMenuDropDown = groupContextMenuDropDown;
7✔
1217
        this.groupContextMenuDropDownOverlaySettings.target = targetButton;
7✔
1218
        this.groupContextMenuDropDownOverlaySettings.positionStrategy = new ConnectedPositioningStrategy({
7✔
1219
            horizontalDirection: HorizontalAlignment.Right,
1220
            horizontalStartPoint: HorizontalAlignment.Left,
1221
            verticalStartPoint: VerticalAlignment.Bottom
1222
        });
1223

1224
        if (groupContextMenuDropDown.collapsed) {
7!
1225
            this.contextualGroup = groupItem;
7✔
1226
            groupContextMenuDropDown.open(this.groupContextMenuDropDownOverlaySettings);
7✔
1227
        } else {
1228
            groupContextMenuDropDown.close();
×
1229
        }
1230
    }
1231

1232
    /**
1233
     * @hidden @internal
1234
     */
1235
    public getOperator(expressionItem: any) {
1236
        // if (!expressionItem && !this.expressionTree && !this.initialOperator) {
1237
        //     this.initialOperator = 0;
1238
        // }
1239

1240
        const operator = expressionItem ?
87,439✔
1241
            expressionItem.operator :
1242
            this.expressionTree ?
2,521✔
1243
                this.expressionTree.operator :
1244
                this.initialOperator;
1245
        return operator;
87,439✔
1246
    }
1247

1248
    /**
1249
     * @hidden @internal
1250
     */
1251
    public getSwitchGroupText(expressionItem: any) {
1252
        const operator = this.getOperator(expressionItem);
14,562✔
1253
        const condition = operator === FilteringLogic.Or ? this.resourceStrings.igx_query_builder_and_label : this.resourceStrings.igx_query_builder_or_label
14,562✔
1254
        return this.resourceStrings.igx_query_builder_switch_group.replace('{0}', condition.toUpperCase());
14,562✔
1255
    }
1256

1257
    /**
1258
     * @hidden @internal
1259
     */
1260
    public onGroupContextMenuDropDownSelectionChanging(event: ISelectionEventArgs) {
1261
        event.cancel = true;
4✔
1262

1263
        if (event.newSelection.value === 'switchCondition') {
4✔
1264
            const newOperator = (!this.expressionTree ? this.initialOperator : (this.contextualGroup ?? this._expressionTree).operator) === 0 ? 1 : 0;
3!
1265
            this.selectFilteringLogic(newOperator);
3✔
1266
        } else if (event.newSelection.value === 'ungroup') {
1✔
1267
            this.ungroup();
1✔
1268
        }
1269

1270
        this.groupContextMenuDropDown.close();
4✔
1271
    }
1272

1273
    /**
1274
     * @hidden @internal
1275
     */
1276
    public ungroup() {
1277
        const selectedGroup = this.contextualGroup;
1✔
1278
        const parent = selectedGroup.parent;
1✔
1279
        if (parent) {
1✔
1280
            const index = parent.children.indexOf(selectedGroup);
1✔
1281
            parent.children.splice(index, 1, ...selectedGroup.children);
1✔
1282

1283
            for (const expr of selectedGroup.children) {
1✔
1284
                expr.parent = parent;
2✔
1285
            }
1286
        }
1287
        this.commitOperandEdit();
1✔
1288
    }
1289

1290
    /**
1291
     * @hidden @internal
1292
     */
1293
    public selectFilteringLogic(index: number) {
1294
        if (!this.expressionTree) {
3!
1295
            this.initialOperator = index;
×
1296
            return;
×
1297
        }
1298

1299
        if (this.contextualGroup) {
3✔
1300
            this.contextualGroup.operator = index as FilteringLogic;
2✔
1301
            this.commitOperandEdit();
2✔
1302
        } else if (this.expressionTree) {
1✔
1303
            this._expressionTree.operator = index as FilteringLogic;
1✔
1304
        }
1305

1306
        this.initialOperator = null;
3✔
1307
    }
1308

1309
    /**
1310
     * @hidden @internal
1311
     */
1312
    public getConditionFriendlyName(name: string): string {
1313
        // 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.
1314
        // So instead of in/notIn we end up with 'inQuery'/'notInQuery', hence removing the suffix from the friendly name.
1315
        return this.resourceStrings[`igx_query_builder_filter_${name?.replace('Query', '')}`] || name;
57,092✔
1316
    }
1317

1318
    /**
1319
     * @hidden @internal
1320
     */
1321
    public isDate(value: any) {
1322
        return value instanceof Date;
19,942✔
1323
    }
1324

1325
    /**
1326
     * @hidden @internal
1327
     */
1328
    public invokeClick(eventArgs: KeyboardEvent) {
1329
        if (!this.dragService.dropGhostExpression && this.platform.isActivationKey(eventArgs)) {
2✔
1330
            eventArgs.preventDefault();
2✔
1331
            (eventArgs.currentTarget as HTMLElement).click();
2✔
1332
        }
1333
    }
1334

1335
    /**
1336
     * @hidden @internal
1337
     */
1338
    public openPicker(args: KeyboardEvent) {
1339
        if (this.platform.isActivationKey(args)) {
×
1340
            args.preventDefault();
×
1341
            this.picker.open();
×
1342
        }
1343
    }
1344

1345
    /**
1346
     * @hidden @internal
1347
     */
1348
    public onOutletPointerDown(event) {
1349
        // This prevents closing the select's dropdown when clicking the scroll
1350
        event.preventDefault();
53✔
1351
    }
1352

1353
    /**
1354
     * @hidden @internal
1355
     */
1356
    public getConditionList(): string[] {
1357
        if (!this.selectedField) return [];
1,498✔
1358

1359
        if (this.entities?.length === 1 && !this.entities[0].name) {
1,106✔
1360
            return this.selectedField.filters.conditionList();
214✔
1361
        }
1362

1363
        return this.selectedField.filters.extendedConditionList();
892✔
1364
    }
1365

1366
    /**
1367
     * @hidden @internal
1368
     */
1369
    public getFormatter(field: string) {
1370
        return this.fields?.find(el => el.field === field)?.formatter;
71,808✔
1371
    }
1372

1373
    /**
1374
     * @hidden @internal
1375
     */
1376
    public getFormat(field: string) {
1377
        return this.fields?.find(el => el.field === field).pipeArgs.format;
1,749✔
1378
    }
1379

1380
    /**
1381
     * @hidden @internal
1382
     *
1383
     * used by the grid
1384
     */
1385
    public setAddButtonFocus() {
1386
        if (this.addRootAndGroupButton) {
39!
1387
            this.addRootAndGroupButton.nativeElement.focus();
×
1388
        } else if (this.addConditionButton) {
39✔
1389
            this.addConditionButton.nativeElement.focus();
18✔
1390
        }
1391
    }
1392

1393
    /**
1394
     * @hidden @internal
1395
     */
1396
    public context(expression: ExpressionItem, afterExpression?: ExpressionItem) {
1397
        return {
55,313✔
1398
            $implicit: expression,
1399
            afterExpression
1400
        };
1401
    }
1402

1403
    public formatReturnFields(innerTree: IFilteringExpressionsTree) {
1404
        const returnFields = innerTree.returnFields;
3,608✔
1405
        let text = returnFields.join(', ');
3,608✔
1406
        const innerTreeEntity = this.entities?.find(el => el.name === innerTree.entity);
3,608✔
1407
        if (returnFields.length === innerTreeEntity?.fields.length) {
3,608!
1408
            text = this.resourceStrings.igx_query_builder_all_fields;
×
1409
        } else {
1410
            text = returnFields.join(', ');
3,608✔
1411
            text = text.length > 25 ? text.substring(0, 25) + ' ...' : text;
3,608!
1412
        }
1413
        return text;
3,608✔
1414
    }
1415

1416
    public isInEditMode(): boolean {
1417
        return !this.parentExpression || (this.parentExpression && this.parentExpression.inEditMode);
63,371✔
1418
    }
1419

1420
    public onInEditModeChanged(expressionItem: ExpressionOperandItem) {
1421
        if (!expressionItem.inEditMode) {
13!
1422
            this.enterExpressionEdit(expressionItem);
×
1423
        }
1424
    }
1425

1426
    public getExpressionTreeCopy(expressionTree: IExpressionTree, shouldAssignInnerQueryExprTree?: boolean): IExpressionTree {
1427
        if (!expressionTree) {
210✔
1428
            return null;
180✔
1429
        }
1430

1431
        const exprTreeCopy = new FilteringExpressionsTree(expressionTree.operator, expressionTree.fieldName, expressionTree.entity, expressionTree.returnFields);
30✔
1432
        exprTreeCopy.filteringOperands = [];
30✔
1433

1434
        expressionTree.filteringOperands.forEach(o => isTree(o) ? exprTreeCopy.filteringOperands.push(this.getExpressionTreeCopy(o)) : exprTreeCopy.filteringOperands.push(o));
55!
1435

1436
        if (!this.innerQueryNewExpressionTree && shouldAssignInnerQueryExprTree) {
30✔
1437
            this.innerQueryNewExpressionTree = exprTreeCopy;
18✔
1438
        }
1439

1440
        return exprTreeCopy;
30✔
1441
    }
1442

1443
    public onSelectAllClicked() {
1444
        if (
×
1445
            (this._selectedReturnFields.length > 0 && this._selectedReturnFields.length < this._selectedEntity.fields.length) ||
×
1446
            this._selectedReturnFields.length == this._selectedEntity.fields.length
1447
        ) {
1448
            this.returnFieldsCombo.deselectAllItems();
×
1449
        } else {
1450
            this.returnFieldsCombo.selectAllItems();
×
1451
        }
1452
    }
1453

1454
    public onReturnFieldSelectChanging(event: IComboSelectionChangingEventArgs | ISelectionEventArgs) {
1455
        let newSelection = [];
8✔
1456
        if (Array.isArray(event.newSelection)) {
8✔
1457
            newSelection = event.newSelection.map(item => item.field)
6✔
1458
        } else {
1459
            newSelection.push(event.newSelection.value);
6✔
1460
            this._selectedReturnFields = newSelection;
6✔
1461
        }
1462

1463
        this.initExpressionTree(this.selectedEntity.name, newSelection);
8✔
1464
    }
1465

1466
    public initExpressionTree(selectedEntityName: string, selectedReturnFields: string[]) {
1467
        if (!this._expressionTree) {
45✔
1468
            this._expressionTree = this.createExpressionTreeFromGroupItem(new ExpressionGroupItem(FilteringLogic.And, this.rootGroup), selectedEntityName, selectedReturnFields);
33✔
1469
        }
1470

1471
        if (!this.parentExpression) {
45✔
1472
            this.expressionTreeChange.emit(this._expressionTree);
34✔
1473
        }
1474
    }
1475

1476
    public getSearchValueTemplateContext(defaultSearchValueTemplate): any {
1477
        const ctx = {
1,498✔
1478
            $implicit: this.searchValue,
1479
            selectedField: this.selectedField,
1480
            selectedCondition: this.selectedCondition,
1481
            defaultSearchValueTemplate: defaultSearchValueTemplate
1482
        };
1483
        return ctx;
1,498✔
1484
    }
1485

1486
    private setFormat(field: FieldType) {
1487
        if (!field.pipeArgs) {
3,163✔
1488
            field.pipeArgs = { digitsInfo: DEFAULT_PIPE_DIGITS_INFO };
8✔
1489
        }
1490

1491
        if (!field.pipeArgs.format) {
3,163✔
1492
            field.pipeArgs.format = field.dataType === DataType.Time ?
8!
1493
                DEFAULT_PIPE_TIME_FORMAT : field.dataType === DataType.DateTime ?
8!
1494
                    DEFAULT_PIPE_DATE_TIME_FORMAT : DEFAULT_PIPE_DATE_FORMAT;
1495
        }
1496
    }
1497

1498
    private selectDefaultCondition() {
1499
        if (this.selectedField && this.selectedField.filters) {
142✔
1500
            this.selectedCondition = this.selectedField.filters.conditionList().indexOf('equals') >= 0 ? 'equals' : this.selectedField.filters.conditionList()[0];
106✔
1501
        }
1502
    }
1503

1504
    private setFilters(field: FieldType) {
1505
        if (!field.filters) {
3,163✔
1506
            switch (field.dataType) {
8!
1507
                case DataType.Boolean:
1508
                    field.filters = IgxBooleanFilteringOperand.instance();
2✔
1509
                    break;
2✔
1510
                case DataType.Number:
1511
                case DataType.Currency:
1512
                case DataType.Percent:
1513
                    field.filters = IgxNumberFilteringOperand.instance();
3✔
1514
                    break;
3✔
1515
                case DataType.Date:
1516
                    field.filters = IgxDateFilteringOperand.instance();
1✔
1517
                    break;
1✔
1518
                case DataType.Time:
1519
                    field.filters = IgxTimeFilteringOperand.instance();
×
1520
                    break;
×
1521
                case DataType.DateTime:
1522
                    field.filters = IgxDateTimeFilteringOperand.instance();
×
1523
                    break;
×
1524
                case DataType.String:
1525
                default:
1526
                    field.filters = IgxStringFilteringOperand.instance();
2✔
1527
                    break;
2✔
1528
            }
1529
        }
1530
    }
1531

1532

1533
    private addGroup(operator: FilteringLogic, parent?: ExpressionGroupItem, afterExpression?: ExpressionItem) {
1534
        this.cancelOperandAdd();
3✔
1535

1536
        const groupItem = new ExpressionGroupItem(operator, parent);
3✔
1537

1538
        if (parent) {
3!
1539
            if (afterExpression) {
3!
1540
                const index = parent.children.indexOf(afterExpression);
×
1541
                parent.children.splice(index + 1, 0, groupItem);
×
1542
            } else {
1543
                parent.children.push(groupItem);
3✔
1544
            }
1545
        } else {
1546
            this.rootGroup = groupItem;
×
1547
        }
1548

1549
        this.addCondition(groupItem);
3✔
1550
        this.currentGroup = groupItem;
3✔
1551
    }
1552

1553
    private createExpressionGroupItem(expressionTree: IExpressionTree, parent?: ExpressionGroupItem, entityName?: string): ExpressionGroupItem {
1554
        let groupItem: ExpressionGroupItem;
1555
        if (expressionTree) {
821✔
1556
            groupItem = new ExpressionGroupItem(expressionTree.operator, parent);
670✔
1557
            if (!expressionTree.filteringOperands) {
670!
1558
                return groupItem;
×
1559
            }
1560

1561
            for (let i = 0; i < expressionTree.filteringOperands.length; i++) {
670✔
1562
                const expr = expressionTree.filteringOperands[i];
1,461✔
1563

1564
                if (isTree(expr)) {
1,461✔
1565
                    groupItem.children.push(this.createExpressionGroupItem(expr, groupItem, expressionTree.entity));
90✔
1566
                } else {
1567
                    const filteringExpr = expr as IFilteringExpression;
1,371✔
1568
                    const exprCopy: IFilteringExpression = {
1,371✔
1569
                        fieldName: filteringExpr.fieldName,
1570
                        condition: filteringExpr.condition,
1571
                        conditionName: filteringExpr.condition?.name || filteringExpr.conditionName,
1,371!
1572
                        searchVal: filteringExpr.searchVal,
1573
                        searchTree: filteringExpr.searchTree,
1574
                        ignoreCase: filteringExpr.ignoreCase
1575
                    };
1576
                    const operandItem = new ExpressionOperandItem(exprCopy, groupItem);
1,371✔
1577
                    const field = this.fields?.find(el => el.field === filteringExpr.fieldName);
1,981✔
1578
                    operandItem.fieldLabel = field?.label || field?.header || field?.field;
1,371✔
1579
                    if (this._expandedExpressions.filter(e => e.searchTree == operandItem.expression.searchTree).length > 0) {
1,371!
1580
                        operandItem.expanded = true;
×
1581
                    }
1582
                    groupItem.children.push(operandItem);
1,371✔
1583
                }
1584
            }
1585

1586

1587
            if (expressionTree.entity) {
670✔
1588
                entityName = expressionTree.entity;
613✔
1589
            }
1590
            const entity = this.entities?.find(el => el.name === entityName);
961✔
1591
            if (entity) {
670✔
1592
                this.fields = entity.fields;
613✔
1593
            }
1594

1595
            this._selectedEntity = this.entities?.find(el => el.name === entityName);
961✔
1596
            this._selectedReturnFields =
670✔
1597
                !expressionTree.returnFields || expressionTree.returnFields.includes('*') || expressionTree.returnFields.includes('All') || expressionTree.returnFields.length === 0
2,816✔
1598
                    ? this.fields?.map(f => f.field)
1,213✔
1599
                    : this.fields?.filter(f => expressionTree.returnFields.indexOf(f.field) >= 0).map(f => f.field);
1,633✔
1600
        }
1601
        return groupItem;
821✔
1602
    }
1603

1604
    private createExpressionTreeFromGroupItem(groupItem: ExpressionGroupItem, entity?: string, returnFields?: string[]): FilteringExpressionsTree {
1605
        if (!groupItem) {
149✔
1606
            return null;
4✔
1607
        }
1608

1609
        const expressionTree = new FilteringExpressionsTree(groupItem.operator, undefined, entity, returnFields);
145✔
1610

1611
        for (let i = 0; i < groupItem.children.length; i++) {
145✔
1612
            const item = groupItem.children[i];
199✔
1613

1614
            if (item instanceof ExpressionGroupItem) {
199✔
1615
                const subTree = this.createExpressionTreeFromGroupItem((item as ExpressionGroupItem), entity, returnFields);
19✔
1616
                expressionTree.filteringOperands.push(subTree);
19✔
1617
            } else {
1618
                expressionTree.filteringOperands.push((item as ExpressionOperandItem).expression);
180✔
1619
            }
1620
        }
1621

1622
        return expressionTree;
145✔
1623
    }
1624

1625
    private scrollElementIntoView(target: HTMLElement) {
1626
        const container = this.expressionsContainer.nativeElement;
615✔
1627
        const targetOffset = target.offsetTop - container.offsetTop;
615✔
1628
        const delta = 10;
615✔
1629

1630
        if (container.scrollTop + delta > targetOffset) {
615✔
1631
            container.scrollTop = targetOffset - delta;
405✔
1632
        } else if (container.scrollTop + container.clientHeight < targetOffset + target.offsetHeight + delta) {
210✔
1633
            container.scrollTop = targetOffset + target.offsetHeight + delta - container.clientHeight;
205✔
1634
        }
1635
    }
1636

1637
    private focusEditedExpressionChip() {
1638
        if (this._timeoutId) {
59✔
1639
            clearTimeout(this._timeoutId);
5✔
1640
        }
1641

1642
        this._timeoutId = setTimeout(() => {
59✔
1643
            if (this._lastFocusedChipIndex != -1) {
59✔
1644
                //Sort the expression chip list.
1645
                //If there was a recent drag&drop and the tree hasn't rerendered(child query), they will be unordered
1646
                const sortedChips = this.expressionsChips.toArray().sort(function (a, b) {
57✔
1647
                    if (a === b) return 0;
73!
1648
                    if (a.chipArea.nativeElement.compareDocumentPosition(b.chipArea.nativeElement) & 2) {
73✔
1649
                        // b comes before a
1650
                        return 1;
60✔
1651
                    }
1652
                    return -1;
13✔
1653
                });
1654
                const chipElement = sortedChips[this._lastFocusedChipIndex]?.nativeElement;
57✔
1655
                if (chipElement) {
57✔
1656
                    chipElement.focus();
49✔
1657
                }
1658
                this._lastFocusedChipIndex = -1;
57✔
1659
                this._focusDelay = DEFAULT_CHIP_FOCUS_DELAY;
57✔
1660
            }
1661
        }, this._focusDelay);
1662
    }
1663

1664
    private init() {
1665
        this.cancelOperandAdd();
475✔
1666
        this.cancelOperandEdit();
475✔
1667

1668
        // Ignore values of certain properties for the comparison
1669
        const propsToIgnore = ['parent', 'hovered', 'ignoreCase', 'inEditMode', 'inAddMode'];
475✔
1670
        const propsReplacer = function replacer(key, value) {
475✔
1671
            if (propsToIgnore.indexOf(key) >= 0) {
19,076✔
1672
                return undefined;
2,552✔
1673
            } else {
1674
                return value;
16,524✔
1675
            }
1676
        };
1677

1678
        // Skip root being recreated if the same
1679
        const newRootGroup = this.createExpressionGroupItem(this.expressionTree);
475✔
1680
        if (JSON.stringify(this.rootGroup, propsReplacer) !== JSON.stringify(newRootGroup, propsReplacer)) {
475✔
1681
            this.rootGroup = this.createExpressionGroupItem(this.expressionTree);
251✔
1682
            this.currentGroup = this.rootGroup;
251✔
1683
        }
1684

1685
        if (this.rootGroup?.children?.length == 0) {
475✔
1686
            this.rootGroup = null;
40✔
1687
            this.currentGroup = null;
40✔
1688
        }
1689
    }
1690

1691
    /** rootGroup is recreated after clicking Apply, which sets new expressionTree and calls init()*/
1692
    protected trackExpressionItem = trackByIdentity;
275✔
1693
}
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

© 2026 Coveralls, Inc