Skip to content

Commit deec117

Browse files
committed
refactor(combo): move sorting in groupPipe, remove sorting pipe, cleanup
1 parent b946c93 commit deec117

File tree

4 files changed

+27
-138
lines changed

4 files changed

+27
-138
lines changed

projects/igniteui-angular/src/lib/combo/combo.common.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ElementRef, EventEmitter, InjectionToken } from '@angular/core';
22
import { CancelableEventArgs, CancelableBrowserEventArgs } from '../core/utils';
3-
import { IFilteringExpression } from '../data-operations/filtering-expression.interface';
43

54
export const IGX_COMBO_COMPONENT = new InjectionToken<IgxComboBase>('IgxComboComponentToken');
65

@@ -12,7 +11,6 @@ export interface IgxComboBase {
1211
groupKey: string;
1312
isRemote: boolean;
1413
filteredData: any[];
15-
filteringExpressions: IFilteringExpression[];
1614
totalItemCount: number;
1715
itemsMaxHeight: number;
1816
itemHeight: number;

projects/igniteui-angular/src/lib/combo/combo.component.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,10 @@
4242
<div #dropdownItemContainer class="igx-combo__content" [style.overflow]="'hidden'"
4343
[style.maxHeight.px]="itemsMaxHeight" [igxDropDownItemNavigation]="dropdown" (focus)="dropdown.onFocus()"
4444
[tabindex]="dropdown.collapsed ? -1 : 0" role="listbox" [attr.id]="dropdown.id">
45-
<igx-combo-item *igxFor="let item of data | comboFiltering:searchValue:displayKey
46-
| comboSorting:sortingExpressions
45+
<igx-combo-item *igxFor="let item of data | comboFiltering:searchValue:displayKey:filterable
4746
| comboGrouping:groupKey:valueKey;
4847
index as rowIndex; containerSize: itemsMaxHeight; scrollOrientation: 'vertical'; itemSize: itemHeight"
49-
[itemHeight]='itemHeight' [value]="item" [isHeader]="item.isHeader" role="option" [index]="rowIndex"
50-
(onChunkPreload)="dataLoading($event)">
48+
[itemHeight]='itemHeight' [value]="item" [isHeader]="item.isHeader" role="option" [index]="rowIndex">
5149
<ng-container *ngIf="item.isHeader">
5250
<ng-container
5351
*ngTemplateOutlet="headerItemTemplate ? headerItemTemplate : headerItemBase;
@@ -90,4 +88,4 @@
9088
</ng-template>
9189
<ng-template #headerItemBase let-item let-key="valueKey" let-groupKey="groupKey">
9290
{{ item[key] }}
93-
</ng-template>
91+
</ng-template>

projects/igniteui-angular/src/lib/combo/combo.component.ts

Lines changed: 9 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,15 @@ import { IgxDropDownModule } from '../drop-down/index';
3030
import { IgxInputGroupModule, IgxInputGroupComponent } from '../input-group/input-group.component';
3131
import { IgxComboItemComponent } from './combo-item.component';
3232
import { IgxComboDropDownComponent } from './combo-dropdown.component';
33-
import { IgxComboFilteringPipe, IgxComboGroupingPipe, IgxComboSortingPipe } from './combo.pipes';
33+
import { IgxComboFilteringPipe, IgxComboGroupingPipe } from './combo.pipes';
3434
import { OverlaySettings, AbsoluteScrollStrategy } from '../services';
3535
import { Subject } from 'rxjs';
3636
import { takeUntil } from 'rxjs/operators';
37-
import { DefaultSortingStrategy, ISortingStrategy } from '../data-operations/sorting-strategy';
3837
import { DisplayDensityBase, DisplayDensityToken, IDisplayDensityOptions } from '../core/density';
3938
import { IGX_COMBO_COMPONENT, IgxComboBase } from './combo.common';
4039
import { IgxComboAddItemComponent } from './combo-add-item.component';
4140
import { IgxComboAPIService } from './combo.api';
4241
import { EditorProvider } from '../core/edit-provider';
43-
import { take } from 'rxjs/operators';
4442
import { IgxInputState, IgxInputDirective } from '../directives/input/input.directive';
4543

4644
/**
@@ -142,9 +140,6 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
142140
public defaultFallbackGroup = 'Other';
143141
protected stringFilters = IgxStringFilteringOperand;
144142
protected booleanFilters = IgxBooleanFilteringOperand;
145-
protected _filteringLogic = FilteringLogic.Or;
146-
protected _filteringExpressions: IFilteringExpression[] = [];
147-
protected _sortingExpressions: ISortingExpression[] = [];
148143
protected _groupKey = '';
149144
protected _displayKey: string;
150145
protected _prevInputValue = '';
@@ -763,9 +758,7 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
763758
*/
764759
@Input()
765760
public set groupKey(val: string) {
766-
this.clearSorting(this._groupKey);
767761
this._groupKey = val;
768-
this.sort(this._groupKey);
769762
}
770763

771764
/**
@@ -783,7 +776,7 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
783776
/**
784777
* An @Input property that enabled/disables filtering in the list. The default is `true`.
785778
* ```html
786-
*<igx-combo [filterable]="'false'">
779+
*<igx-combo [filterable]="false">
787780
* ```
788781
*/
789782
@Input()
@@ -912,50 +905,6 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
912905
*/
913906
set totalItemCount(count: number) {
914907
this.virtDir.totalItemCount = count;
915-
this.cdr.detectChanges();
916-
}
917-
918-
/**
919-
* @hidden @internal
920-
*/
921-
public get filteringExpressions(): IFilteringExpression[] {
922-
return this.filterable ? this._filteringExpressions : [];
923-
}
924-
925-
/**
926-
* @hidden @internal
927-
*/
928-
public set filteringExpressions(value: IFilteringExpression[]) {
929-
this._filteringExpressions = value;
930-
this.cdr.markForCheck();
931-
}
932-
933-
/**
934-
* @hidden @internal
935-
*/
936-
public get sortingExpressions(): ISortingExpression[] {
937-
return this._sortingExpressions;
938-
}
939-
940-
/**
941-
* @hidden @internal
942-
*/
943-
public set sortingExpressions(value: ISortingExpression[]) {
944-
this._sortingExpressions = value;
945-
this.cdr.markForCheck();
946-
}
947-
948-
protected clearSorting(field?: string | number) {
949-
if (field === undefined || field === null) {
950-
this.sortingExpressions = [];
951-
return;
952-
}
953-
const currentState = cloneArray(this.sortingExpressions);
954-
const index = currentState.findIndex((expr) => expr.fieldName === field);
955-
if (index > -1) {
956-
currentState.splice(index, 1);
957-
this.sortingExpressions = currentState;
958-
}
959908
}
960909

961910
/**
@@ -1022,41 +971,10 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
1022971
* @hidden @internal
1023972
*/
1024973
public handleInputChange(event?: string) {
1025-
this.onSearchInput.emit(event);
1026-
if (this.filterable) {
1027-
this.checkMatch();
1028-
}
1029-
}
1030-
1031-
/**
1032-
* @hidden @internal
1033-
*/
1034-
public sort(fieldName: string, dir: SortingDirection = SortingDirection.Asc, ignoreCase: boolean = true,
1035-
strategy: ISortingStrategy = DefaultSortingStrategy.instance()): void {
1036-
if (!fieldName) {
1037-
return;
1038-
}
1039-
const sortingState = cloneArray(this.sortingExpressions, true);
1040-
1041-
this.prepare_sorting_expression(sortingState, fieldName, dir, ignoreCase, strategy);
1042-
this.sortingExpressions = sortingState;
1043-
}
1044-
1045-
protected prepare_sorting_expression(state: ISortingExpression[], fieldName: string, dir: SortingDirection, ignoreCase: boolean,
1046-
strategy: ISortingStrategy) {
1047-
1048-
if (dir === SortingDirection.None) {
1049-
state.splice(state.findIndex((expr) => expr.fieldName === fieldName), 1);
1050-
return;
1051-
}
1052-
1053-
const expression = state.find((expr) => expr.fieldName === fieldName);
1054-
1055-
if (!expression) {
1056-
state.push({ fieldName, dir, ignoreCase, strategy });
1057-
} else {
1058-
Object.assign(expression, { fieldName, dir, ignoreCase });
974+
if (event !== undefined) {
975+
this.onSearchInput.emit(event);
1059976
}
977+
this.checkMatch();
1060978
}
1061979

1062980
/**
@@ -1235,6 +1153,9 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
12351153
this.manageRequiredAsterisk();
12361154
this.cdr.detectChanges();
12371155
}
1156+
this.virtDir.onChunkPreload.pipe(takeUntil(this.destroy$)).subscribe((e) => {
1157+
this.onDataPreLoad.emit(e);
1158+
});
12381159
}
12391160

12401161
/**
@@ -1247,13 +1168,6 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
12471168
this.selection.clear(this.id);
12481169
}
12491170

1250-
/**
1251-
* @hidden @internal
1252-
*/
1253-
public dataLoading(event) {
1254-
this.onDataPreLoad.emit(event);
1255-
}
1256-
12571171
/**
12581172
* @hidden @internal
12591173
*/
@@ -1556,7 +1470,7 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
15561470
*/
15571471
@NgModule({
15581472
declarations: [IgxComboComponent, IgxComboItemComponent, IgxComboGroupingPipe,
1559-
IgxComboFilteringPipe, IgxComboSortingPipe, IgxComboDropDownComponent, IgxComboAddItemComponent,
1473+
IgxComboFilteringPipe, IgxComboDropDownComponent, IgxComboAddItemComponent,
15601474
IgxComboItemDirective,
15611475
IgxComboEmptyDirective,
15621476
IgxComboHeaderItemDirective,

projects/igniteui-angular/src/lib/combo/combo.pipes.ts

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { Pipe, PipeTransform, Inject } from '@angular/core';
22
import { cloneArray } from '../core/utils';
33
import { DataUtil } from '../data-operations/data-util';
4-
import { IFilteringExpression } from '../data-operations/filtering-expression.interface';
5-
import { ISortingExpression } from '../data-operations/sorting-expression.interface';
6-
import { FilteringStrategy } from '../data-operations/filtering-strategy';
4+
import { SortingDirection } from '../data-operations/sorting-expression.interface';
75
import { IGX_COMBO_COMPONENT, IgxComboBase } from './combo.common';
6+
import { DefaultSortingStrategy } from '../data-operations/sorting-strategy';
87

98

109
/**
@@ -14,49 +13,23 @@ import { IGX_COMBO_COMPONENT, IgxComboBase } from './combo.common';
1413
name: 'comboFiltering'
1514
})
1615
export class IgxComboFilteringPipe implements PipeTransform {
17-
public transform(collection: any[], searchValue: any, displayKey: any) {
16+
public transform(collection: any[], searchValue: any, displayKey: any, shouldFilter: boolean) {
1817
if (!collection) {
1918
return [];
2019
}
21-
if (!searchValue) {
20+
if (!searchValue || !shouldFilter) {
2221
return collection;
2322
} else {
2423
const searchTerm = searchValue.toLowerCase().trim();
2524
if (displayKey != null) {
2625
return collection.filter(e => e[displayKey].toLowerCase().includes(searchTerm));
2726
} else {
28-
return collection.filter(e => e.includes(searchTerm));
27+
return collection.filter(e => e.toLowerCase().includes(searchTerm));
2928
}
3029
}
3130
}
3231
}
3332

34-
/** @hidden */
35-
export class SimpleFilteringStrategy extends FilteringStrategy {
36-
public findMatchByExpression(rec: object, expr: IFilteringExpression): boolean {
37-
const cond = expr.condition;
38-
const val = expr.fieldName === undefined ? rec : rec[expr.fieldName];
39-
return cond.logic(val, expr.searchVal, expr.ignoreCase);
40-
}
41-
}
42-
43-
/**
44-
* @hidden
45-
*/
46-
@Pipe({
47-
name: 'comboSorting',
48-
pure: true
49-
})
50-
export class IgxComboSortingPipe implements PipeTransform {
51-
public transform(collection: any[], expressions: ISortingExpression []) {
52-
if (!expressions.length) {
53-
return collection;
54-
}
55-
const result = DataUtil.sort(cloneArray(collection), expressions);
56-
return result;
57-
}
58-
}
59-
6033
/**
6134
* @hidden
6235
*/
@@ -72,13 +45,19 @@ export class IgxComboGroupingPipe implements PipeTransform {
7245
if ((!groupKey && groupKey !== 0) || !collection.length) {
7346
return collection;
7447
}
75-
const data = cloneArray(collection);
48+
const sorted = DataUtil.sort(cloneArray(collection), [{
49+
fieldName: groupKey,
50+
dir: SortingDirection.Asc,
51+
ignoreCase: true,
52+
strategy: DefaultSortingStrategy.instance()
53+
}]);
54+
const data = cloneArray(sorted);
7655
let inserts = 0;
7756
let currentHeader = null;
78-
for (let i = 0; i < collection.length; i++) {
57+
for (let i = 0; i < sorted.length; i++) {
7958
let insertFlag = 0;
80-
if (currentHeader !== collection[i][groupKey]) {
81-
currentHeader = collection[i][groupKey];
59+
if (currentHeader !== sorted[i][groupKey]) {
60+
currentHeader = sorted[i][groupKey];
8261
insertFlag = 1;
8362
}
8463
if (insertFlag) {

0 commit comments

Comments
 (0)