-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
custom-inputFilter.ts
134 lines (117 loc) · 4.21 KB
/
custom-inputFilter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import {
type Column,
type ColumnFilter,
type Filter,
type FilterArguments,
type FilterCallback,
type GridOption,
OperatorType,
type OperatorString,
type SearchTerm,
type SlickGrid,
} from './../modules/angular-slickgrid';
export class CustomInputFilter implements Filter {
private _clearFilterTriggered = false;
private _shouldTriggerQuery = true;
private filterElm!: HTMLInputElement;
grid!: SlickGrid;
searchTerms: SearchTerm[] = [];
columnDef!: Column;
callback!: FilterCallback;
operator: OperatorType | OperatorString = OperatorType.equal;
/** Getter for the Column Filter */
get columnFilter(): ColumnFilter {
return this.columnDef?.filter ?? {};
}
/** Getter for the Grid Options pulled through the Grid Object */
get gridOptions(): GridOption {
return (this.grid?.getOptions() ?? {}) as GridOption;
}
/**
* Initialize the Filter
*/
init(args: FilterArguments) {
this.grid = args.grid as SlickGrid;
this.callback = args.callback;
this.columnDef = args.columnDef;
this.searchTerms = ('searchTerms' in args ? args.searchTerms : []) || [];
// filter input can only have 1 search term, so we will use the 1st array index if it exist
const searchTerm = Array.isArray(this.searchTerms) && this.searchTerms.length >= 0 ? this.searchTerms[0] : '';
// step 2, create the DOM Element of the filter & initialize it if searchTerm is filled
this.filterElm = this.createFilterElement(searchTerm);
// step 3, subscribe to the keyup event and run the callback when that happens
this.filterElm?.addEventListener('keyup', this.onKeyup.bind(this));
}
/**
* Clear the filter value
*/
clear(shouldTriggerQuery = true) {
if (this.filterElm) {
this._clearFilterTriggered = true;
this._shouldTriggerQuery = shouldTriggerQuery;
this.filterElm.value = '';
this.filterElm.dispatchEvent(new Event('keyup'));
}
}
/**
* destroy the filter
*/
destroy() {
this.filterElm?.removeEventListener('keyup', this.onKeyup.bind(this));
this.filterElm?.remove();
}
/** Set value(s) on the DOM element */
setValues(values: SearchTerm | SearchTerm[]) {
if (values && this.filterElm) {
this.filterElm.value = values as string;
}
}
//
// private functions
// ------------------
private onKeyup(e: KeyboardEvent) {
let value = (e as KeyboardEvent & { target: HTMLInputElement })?.target?.value ?? '';
const enableWhiteSpaceTrim = this.gridOptions.enableFilterTrimWhiteSpace || this.columnFilter.enableTrimWhiteSpace;
if (typeof value === 'string' && enableWhiteSpaceTrim) {
value = value.trim();
}
if (this._clearFilterTriggered) {
this.callback(e, {
columnDef: this.columnDef,
clearFilterTriggered: this._clearFilterTriggered,
shouldTriggerQuery: this._shouldTriggerQuery,
});
this.filterElm?.classList.remove('filled');
} else {
value === '' ? this.filterElm?.classList.remove('filled') : this.filterElm?.classList.add('filled');
this.callback(e, { columnDef: this.columnDef, searchTerms: [value], shouldTriggerQuery: this._shouldTriggerQuery });
}
// reset both flags for next use
this._clearFilterTriggered = false;
this._shouldTriggerQuery = true;
}
/**
* From the html template string, create a DOM element
* @param filterTemplate
*/
private createFilterElement(searchTerm?: SearchTerm) {
const headerElm = this.grid.getHeaderRowColumn(this.columnDef.id);
if (headerElm) {
headerElm.innerHTML = '';
let placeholder = this.gridOptions?.defaultFilterPlaceholder ?? '';
if (this.columnFilter?.placeholder) {
placeholder = this.columnFilter.placeholder;
}
// create the DOM element & add an ID and filter class
this.filterElm = document.createElement('input');
this.filterElm.type = 'text';
this.filterElm.className = 'form-control search-filter';
this.filterElm.placeholder = placeholder;
this.filterElm.value = `${searchTerm || ''}`;
this.filterElm.dataset.columnid = `${this.columnDef.id}`;
// append the new DOM element to the header row
headerElm.appendChild(this.filterElm);
}
return this.filterElm;
}
}