-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistController.ts
418 lines (330 loc) · 10.5 KB
/
listController.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
export type PropertySearch = { name: string, value: string | number | Array<string> | Array<number> | Function, cacheName?: string }
export interface OnInitEntity {
onInit(): void;
}
export class ListController<M extends object> {
private static readonly QUANTITY_PAGE_PER_LOT = 5;
private modelClass: new () => M;
private _total = 0;
private _totalPage: number;
private _currentPage: number;
private _visiblePages: number[];
private _list: M[];
private _currentList: M[];
private _originalList: M[];
private _filterCacheByKeyword = {};
private _onChangePage: (page: number) => void;
private _onFilter: (props: PropertySearch[]) => void;
private _rowPerPage: number;
static normalize = s => s.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '');
static isEqual = (s1, s2) => ListController.normalize(s1) === ListController.normalize(s2);
static contain = (s1, s2) => ListController.normalize(s1).indexOf(ListController.normalize(s2)) > -1;
private data = {};
constructor(model: new () => M, rowPerPage: number = 10) {
this.modelClass = model;
this.onChangePage = null;
this.onFilter = null;
this._rowPerPage = rowPerPage;
return this;
}
get currentList(): Array<M> {
return this._currentList;
}
get currentPage(): number {
return this._currentPage;
}
get totalPage(): number {
return this._totalPage;
}
get total(): number {
return this._total;
}
get visiblePages(): Array<number> {
return this._visiblePages;
}
get firstVisiblePage(): number {
return this.visiblePages ? this.visiblePages[0] : 0;
}
get lastVisiblePage(): number {
return this.visiblePages ? this.visiblePages[this.visiblePages.length - 1] : 0;
}
get isFiltered(): boolean {
return this._originalList !== this.list;
}
get onChangePage(): (page: number) => void {
return this._onChangePage;
}
set onChangePage(callback: (page: number) => void) {
this._onChangePage = (page: number) => {
this._currentPage = page;
if (callback) {
callback.call(this, page);
} else {
this.setPage(page);
}
};
}
get onFilter(): (props: PropertySearch[]) => void {
return this._onFilter;
}
set onFilter(callback: (props: PropertySearch[]) => void) {
this._onFilter = (props: PropertySearch[]) => {
if (callback) {
callback.call(this, props);
} else {
this.search(props);
}
};
}
get list(): Array<M> {
return this._list;
}
set list(list: Array<M>) {
this._originalList = list;
this._filterCacheByKeyword = {};
this._currentPage = 1;
if (list) {
this._total = list.length;
} else {
this._total = 0;
}
this.updateList(list);
}
getData<T>(name: string): T {
return this.data[name];
}
setData(name: string, value: any): void {
this.data[name] = value;
}
setPage(page: number): ListController<M> {
page = isNaN(page) ? 1 : page;
const inicio = page * this._rowPerPage - this._rowPerPage;
const fim = this._rowPerPage ? inicio + this._rowPerPage : this._list.length;
this._currentList = this._list.slice(inicio, fim);
for (let i = -1, s = this._currentList.length; ++i < s;) {
const o = this._currentList[i];
if (!(o instanceof this.modelClass)) {
this._currentList[i] = this.get(this._originalList.indexOf(o));
}
}
if (page > 1 && this._currentList.length === 0) {
this.previous();
} else {
this._onChangePage(page);
}
this._currentPage = page;
return this;
}
search(props: PropertySearch[], identicalSearch = true): ListController<M> {
let list: M[];
list = this._originalList
if (props && props.length > 0) {
let refCache = '';
for (let i = -1, s = props.length; ++i < s;) {
const prop = props[i];
const valueIsFnc = typeof prop.value === 'function';
if (prop.value === undefined || prop.value === null || prop.value === '' ||
Array.isArray(prop.value) && prop.value.length === 0 ||
valueIsFnc && typeof prop.cacheName === 'string' && prop.cacheName.trim() === '') {
props.splice(i, 1);
--i;
--s
continue;
}
let refName = prop.value;
if (typeof prop.value === 'string') {
prop.value = ListController.normalize(prop.value);
} else if (Array.isArray(prop.value)) {
for (let b = -1, ss = prop.value.length; ++b < ss;) {
const value = prop.value[b];
if (typeof value === 'string')
prop.value[b] = ListController.normalize(value);
}
} else if (valueIsFnc) {
if (prop.cacheName === undefined || prop.cacheName === null) {
throw new Error(`The value of ${prop.name} is of type function, so it defines a cacheName to reference in the ${this.modelClass.prototype.constructor.name} list.`);
}
refName = prop.cacheName;
}
refCache += refName + '#|#'
}
if (refCache) {
const filterCached = this._filterCacheByKeyword[refCache];
if (filterCached) {
list = filterCached;
} else {
list = this._filterCacheByKeyword[refCache] = [];
const normalizedPropName = '$normalized.';
for (let i = -1, s = this._originalList.length; ++i < s;) {
const entity = this.get(i);
let cnt = 0;
for (const prop of props) {
let originalValue;
if (prop.name.indexOf('.') > -1) {
originalValue = entity;
for (const name of prop.name.split('.')) {
if (!originalValue) break;
originalValue = originalValue[name];
}
} else originalValue = entity[prop.name];
if (!originalValue) continue;
let v = entity[normalizedPropName + prop.name];
if (v === undefined) {
v = originalValue;
if (typeof originalValue === 'string') {
v = v.normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase();
}
entity[normalizedPropName + prop.name] = v;
}
if (typeof prop.value === 'function') {
if (prop.value(entity)) ++cnt;
} else {
let values: any[];
if (Array.isArray(prop.value)) {
values = prop.value;
} else values = [prop.value];
for (const value of values) {
if (typeof v === 'string' && v.indexOf(value) > -1 || v == value) {
++cnt;
break;
}
}
}
}
if (!identicalSearch && cnt > 0 || props.length === cnt) list.push(entity);
}
}
}
}
this._total = list.length;
this._visiblePages = undefined;
this._currentPage = 1;
this.updateList(list);
return this;
}
remove(o: M): boolean {
const index = this._originalList.indexOf(o);
if (index === -1) return false;
const filterReference = o['$filterReference'];
if (filterReference) {
for (const key in filterReference) {
const filterList = this._filterCacheByKeyword[key];
filterList?.splice(filterList?.indexOf(filterReference[key]), 1);
}
delete o['$filterReference'];
}
this._originalList.splice(index, 1);
this._total = this.list.length;
this.updateList(this._originalList);
return true;
}
previous(): void {
if (this._currentPage > 1) {
if (this._currentPage === this.firstVisiblePage) {
this.previousPages();
} else {
this._onChangePage(this._currentPage - 1);
}
}
}
previousPages(): void {
if (this.firstVisiblePage > 1) {
this.generateVisiblePages(false);
this.onChangePage(this.lastVisiblePage);
}
}
next(): void {
if (this._currentPage < this._totalPage) {
if (this._currentPage === this.lastVisiblePage) {
this.nextPages();
} else {
this._onChangePage(this._currentPage + 1);
}
}
}
nextPages(): void {
this._nextPages(true);
}
clean(): ListController<M> {
this.list = undefined;
this._filterCacheByKeyword = {};
this._total = 0;
return this;
}
private get(i: number): M {
let o = this._originalList[i];
if (!(o instanceof this.modelClass)) {
o = Object.assign(new this.modelClass, o);
if ((o as any).onInit) (o as any).onInit();
this._originalList[i] = o;
}
return o;
}
private _nextPages(setCurrentPage: boolean): void {
if (this.lastVisiblePage < this._totalPage) {
this.generateVisiblePages(true);
if (setCurrentPage) {
this.onChangePage(this.firstVisiblePage);
}
}
}
private async updateList(list: Array<M>): Promise<void> {
this._list = list;
this._visiblePages = undefined;
if (!list || list.length === 0) {
this._currentList = null;
this._totalPage = 0;
return;
}
if (this._rowPerPage) {
this._totalPage = Math.ceil(list.length / this._rowPerPage);
if (this.currentPage < 1) {
this._currentPage = 1;
} else if (this.currentPage > this._totalPage) {
this._currentPage = this._totalPage;
}
} else {
this._totalPage = 1;
this._currentPage = 1;
}
this.setPage(this.currentPage);
this.generateVisiblePages();
if (this.visiblePages.length > 0) {
while (this.visiblePages.indexOf(this.currentPage) === -1) {
this._nextPages(false);
}
}
}
private generateVisiblePages(next?: boolean): void {
if (this._totalPage < 1) return null;
const pageListLength = new Array(this._totalPage).length;
let min, max;
if (this._visiblePages === undefined) {
min = 1;
max = ListController.QUANTITY_PAGE_PER_LOT;
if (max > pageListLength) {
max = pageListLength;
}
} else {
const listLength = this._visiblePages.length,
n = this._visiblePages[listLength - 1];
if (next) {
min = n + 1;
max = n + ListController.QUANTITY_PAGE_PER_LOT;
if (max > pageListLength) {
max = pageListLength;
}
} else {
max = n - listLength;
min = max - ListController.QUANTITY_PAGE_PER_LOT + 1;
if (min < 1) {
min = 1;
}
}
}
this._visiblePages = [];
for (let i = min - 1; ++i <= max;) {
this._visiblePages.push(i);
}
}
}