Skip to content

Commit

Permalink
feat: filter strategy abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
markuczy committed Jan 10, 2025
1 parent f7263fd commit fbf0a2b
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ export class DataSortBase {
)?.toString()
switch (filter.filterType) {
case undefined:
case FilterType.EQUAL:
case FilterType.EQUALS:
return value === String(filter.value)
case FilterType.TRUTHY: {
case FilterType.IS_NOT_EMPTY: {
return filter.value ? !!value : !value
}
default:
return true
}
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
<ng-container *ngIf="columnFilterTemplates$ | async as columnFilterTemplates">
<p-multiSelect
class="filterMultiSelect"
*ngIf="column.filterable && (!column.filterType || column.filterType === FilterType.EQUAL)"
*ngIf="column.filterable && (!column.filterType || column.filterType === FilterType.EQUALS)"
[options]="equalFilterOptions.column?.id === column.id ? equalFilterOptions.options : []"
[ngModel]="(currentEqualSelectedFilters$ | async) || []"
[showToggleAll]="true"
Expand Down Expand Up @@ -202,7 +202,7 @@
</ng-container>
<p-multiSelect
class="filterMultiSelect"
*ngIf="column.filterable && column.filterType === FilterType.TRUTHY"
*ngIf="column.filterable && column.filterType === FilterType.IS_NOT_EMPTY"
[options]="truthyFilterOptions"
[ngModel]="(currentTruthySelectedFilters$ | async) || []"
[showToggleAll]="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ export class DataTableComponent extends DataSortBase implements OnInit, AfterCon
return filters
.filter(
(filter) =>
filter.columnId === currentFilterColumn?.id && currentFilterColumn.filterType === FilterType.TRUTHY
filter.columnId === currentFilterColumn?.id && currentFilterColumn.filterType === FilterType.IS_NOT_EMPTY
)
.map((filter) => filter.value)
})
Expand All @@ -465,15 +465,15 @@ export class DataTableComponent extends DataSortBase implements OnInit, AfterCon
.filter(
(filter) =>
filter.columnId === currentFilterColumn?.id &&
(!currentFilterColumn.filterType || currentFilterColumn.filterType === FilterType.EQUAL)
(!currentFilterColumn.filterType || currentFilterColumn.filterType === FilterType.EQUALS)
)
.map((filter) => filter.value)
})
)
this.currentEqualFilterOptions$ = combineLatest([this._rows$, this.currentFilterColumn$, this._filters$]).pipe(
filter(
([_, currentFilterColumn, __]) =>
!currentFilterColumn?.filterType || currentFilterColumn.filterType === FilterType.EQUAL
!currentFilterColumn?.filterType || currentFilterColumn.filterType === FilterType.EQUALS
),
mergeMap(([rows, currentFilterColumn, filters]) => {
if (!currentFilterColumn?.id) {
Expand All @@ -484,7 +484,7 @@ export class DataTableComponent extends DataSortBase implements OnInit, AfterCon
.filter(
(filter) =>
filter.columnId === currentFilterColumn?.id &&
(!currentFilterColumn.filterType || currentFilterColumn.filterType === FilterType.EQUAL)
(!currentFilterColumn.filterType || currentFilterColumn.filterType === FilterType.EQUALS)
)
.map((filter) => filter.value)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@
style="white-space: nowrap"
class="p-chip-text flex flex-nowrap"
>{{column?.nameKey ?? '' | translate }}:<ng-container
*ngIf="filter.filterType === FilterType.EQUAL || !filter.filterType"
*ngIf="filter.filterType === FilterType.EQUALS || !filter.filterType"
[ngTemplateOutlet]="chipTemplate"
[ngTemplateOutletContext]="{
templates: templates,
filter: filter,
column: column
}"
></ng-container>
<ng-container *ngIf="filter.filterType === FilterType.TRUTHY">
<ng-container *ngIf="filter.filterType === FilterType.IS_NOT_EMPTY">
<ng-container
[ngTemplateOutlet]="truthyTemplate"
[ngTemplateOutletContext]="{
Expand Down Expand Up @@ -152,7 +152,7 @@
<ng-template pTemplate="valueIdCell" let-rowObject="rowObject" let-column="column">
<ng-container *ngIf="getColumn(rowObject['valueColumnId'], columns) as valueColumn">
<ng-container
*ngIf="!valueColumn.filterType || valueColumn.filterType === FilterType.EQUAL"
*ngIf="!valueColumn.filterType || valueColumn.filterType === FilterType.EQUALS"
[ngTemplateOutlet]="templates[valueColumn.id]"
[ngTemplateOutletContext]="{
rowObject: getRowForValueColumn(rowObject),
Expand All @@ -161,7 +161,7 @@
>
</ng-container>
<ng-container
*ngIf="valueColumn.filterType === FilterType.TRUTHY"
*ngIf="valueColumn.filterType === FilterType.IS_NOT_EMPTY"
[ngTemplateOutlet]="truthyTemplate"
[ngTemplateOutletContext]="{
value: resolveFieldData(rowObject, column.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ describe('InteractiveDataViewComponent', () => {
nameKey: 'COLUMN_HEADER_NAME.TEST_TRUTHY',
filterable: true,
sortable: true,
filterType: FilterType.TRUTHY,
filterType: FilterType.IS_NOT_EMPTY,
predefinedGroupKeys: ['PREDEFINED_GROUP.EXTENDED', 'PREDEFINED_GROUP.FULL'],
},
]
Expand Down Expand Up @@ -308,7 +308,7 @@ describe('InteractiveDataViewComponent', () => {
editItemEvent = undefined
deleteItemEvent = undefined

console.log("Global IntersectionObserver", global.IntersectionObserver)
console.log('Global IntersectionObserver', global.IntersectionObserver)
})

it('should create', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const defaultInteractiveDataViewArgs = {
nameKey: 'Available',
sortable: false,
filterable: true,
filterType: FilterType.TRUTHY,
filterType: FilterType.IS_NOT_EMPTY,
predefinedGroupKeys: ['test2'],
},
{
Expand Down
20 changes: 18 additions & 2 deletions libs/angular-accelerator/src/lib/model/filter.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,23 @@ export interface ColumnFilterDataSelectOptions {

export type Filter = { columnId: string; value: unknown; filterType?: FilterType }

export interface FilterObject {
type: FilterType
value1: unknown
value2: unknown
}

export enum FilterType {
EQUAL = 'EQUAL',
TRUTHY = 'TRUTHY',
ENDS_WITH = 'endsWith',
STARTS_WITH = 'startsWith',
CONTAINS = 'contains',
NOT_CONTAINS = 'notContains',
EQUALS = 'equals',
NOT_EQUALS = 'notEquals',
LESS_THAN = 'lessThan',
GREATER_THAN = 'greaterThan',
LESS_THAN_OR_EQUAL = 'lessThanOrEqual',
GREATER_THAN_OR_EQUAL = 'greaterThanOrEqual',
IS_EMPTY = 'isEmpty',
IS_NOT_EMPTY = 'isNotEmpty',
}
74 changes: 74 additions & 0 deletions libs/angular-accelerator/src/lib/utils/filter-strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { FilterObject } from '../model/filter.model'

/* eslint-disable @typescript-eslint/no-unused-vars */
export abstract class FilterStrategy {
endsWith(value: unknown, target: unknown): boolean {
console.error('endsWith method not implemented')
return true
}

startsWith(value: unknown, target: unknown): boolean {
console.error('startsWith method not implemented')
return true
}

contains(value: unknown, target: unknown): boolean {
console.error('contains method not implemented')
return true
}

notContains(value: unknown, target: unknown): boolean {
console.error('notContains method not implemented')
return true
}

equals(value: unknown, target: unknown): boolean {
console.error('equals method not implemented')
return true
}

notEquals(value: unknown, target: unknown): boolean {
console.error('notEquals method not implemented')
return true
}

lessThan(value: unknown, target: unknown): boolean {
console.error('lessThan method not implemented')
return true
}

greaterThan(value: unknown, target: unknown): boolean {
console.error('greaterThan method not implemented')
return true
}

lessThanOrEqual(value: unknown, target: unknown): boolean {
console.error('lessThanOrEqual method not implemented')
return true
}

greaterThanOrEqual(value: unknown, target: unknown): boolean {
console.error('greaterThanOrEqual method not implemented')
return true
}

isEmpty(value: unknown): boolean {
console.error('isEmpty method not implemented')
return true
}

isNotEmpty(value: unknown): boolean {
console.error('isNotEmpty method not implemented')
return true
}

compare(a: unknown, b: unknown): number {
console.error('compare method not implemented')
return 0
}

filter(hayStack: unknown[], filterObject: FilterObject): unknown[] {
const { type, ...rest } = filterObject
return hayStack.filter((item) => this[type](item, rest))
}
}

0 comments on commit fbf0a2b

Please sign in to comment.