-
Notifications
You must be signed in to change notification settings - Fork 775
/
Copy pathTableDataStore.js
717 lines (650 loc) · 19.9 KB
/
TableDataStore.js
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/* eslint no-nested-ternary: 0 */
/* eslint guard-for-in: 0 */
/* eslint no-console: 0 */
/* eslint eqeqeq: 0 */
/* eslint one-var: 0 */
import Const from '../Const';
export class TableDataStore {
constructor(data) {
this.data = data;
this.filteredData = null;
this.isOnFilter = false;
this.filterObj = null;
this.searchText = null;
this.sortList = [];
this.pageObj = {};
this.selected = [];
this.showOnlySelected = false;
}
setProps(props) {
this.keyField = props.keyField;
this.enablePagination = props.isPagination;
this.colInfos = props.colInfos;
this.remote = props.remote;
this.multiColumnSearch = props.multiColumnSearch;
// default behaviour if strictSearch prop is not provided: !multiColumnSearch
this.strictSearch = typeof props.strictSearch === 'undefined' ?
!props.multiColumnSearch : props.strictSearch;
this.multiColumnSort = props.multiColumnSort;
}
clean() {
this.filteredData = null;
this.isOnFilter = false;
this.filterObj = null;
this.searchText = null;
this.sortList = [];
this.pageObj = {};
this.selected = [];
}
isSearching() {
return this.searchText !== null;
}
isFiltering() {
return this.filterObj !== null;
}
setData(data) {
this.data = data;
if (this.remote) {
return;
}
this._refresh(true);
}
getColInfos() {
return this.colInfos;
}
getSortInfo() {
return this.sortList;
}
setSortInfo(order, sortField) {
if (typeof order !== typeof sortField) {
throw new Error('The type of sort field and order should be both with String or Array');
}
if (Array.isArray(order) && Array.isArray(sortField)) {
if (order.length !== sortField.length) {
throw new Error('The length of sort fields and orders should be equivalent');
}
order = order.slice().reverse();
this.sortList = sortField.slice().reverse().map((field, i) => {
return {
order: order[i],
sortField: field
};
});
this.sortList = this.sortList.slice(0, this.multiColumnSort);
} else {
const sortObj = {
order: order,
sortField: sortField
};
if (this.multiColumnSort > 1) {
let i = this.sortList.length - 1;
let sortFieldInHistory = false;
for (; i >= 0; i--) {
if (this.sortList[i].sortField === sortField) {
sortFieldInHistory = true;
break;
}
}
if (sortFieldInHistory) {
if (i > 0) {
this.sortList = this.sortList.slice(0, i);
} else {
this.sortList = this.sortList.slice(1);
}
}
this.sortList.unshift(sortObj);
this.sortList = this.sortList.slice(0, this.multiColumnSort);
} else {
this.sortList = [ sortObj ];
}
}
}
cleanSortInfo() {
this.sortList = [];
}
setSelectedRowKey(selectedRowKeys) {
this.selected = selectedRowKeys;
}
getRowByKey(keys) {
// Bad Performance #1164
// return keys.map(key => {
// const result = this.data.filter(d => d[this.keyField] === key);
// if (result.length !== 0) return result[0];
// });
const result = [];
if (!keys || keys.length === 0) {
return result;
}
for (let i = 0; i < this.data.length; i++) {
const d = this.data[i];
if (keys.indexOf(d[this.keyField]) > -1) {
keys = keys.filter(k => k !== d[this.keyField]);
result.push(d);
}
}
return result;
}
getSelectedRowKeys() {
return this.selected;
}
getCurrentDisplayData() {
if (this.isOnFilter) return this.filteredData;
else return this.data;
}
_refresh(skipSorting) {
if (this.isOnFilter) {
if (this.filterObj !== null) this.filter(this.filterObj);
if (this.searchText !== null) this.search(this.searchText);
}
if (!skipSorting && this.sortList.length > 0) {
this.sort();
}
}
ignoreNonSelected() {
this.showOnlySelected = !this.showOnlySelected;
if (this.showOnlySelected) {
this.isOnFilter = true;
this.filteredData = this.data.filter( row => {
const result = this.selected.find(x => row[this.keyField] === x);
return typeof result !== 'undefined' ? true : false;
});
} else {
this.isOnFilter = false;
}
}
sort() {
let currentDisplayData = this.getCurrentDisplayData();
currentDisplayData = this._sort(currentDisplayData);
return this;
}
page(page, sizePerPage) {
this.pageObj.end = page * sizePerPage - 1;
this.pageObj.start = this.pageObj.end - (sizePerPage - 1);
return this;
}
edit(newVal, rowIndex, fieldName) {
const currentDisplayData = this.getCurrentDisplayData();
let rowKeyCache;
if (!this.enablePagination) {
currentDisplayData[rowIndex][fieldName] = newVal;
rowKeyCache = currentDisplayData[rowIndex][this.keyField];
} else {
currentDisplayData[this.pageObj.start + rowIndex][fieldName] = newVal;
rowKeyCache = currentDisplayData[this.pageObj.start + rowIndex][this.keyField];
}
if (this.isOnFilter) {
this.data.forEach(function(row) {
if (row[this.keyField] === rowKeyCache) {
row[fieldName] = newVal;
}
}, this);
if (this.filterObj !== null) this.filter(this.filterObj);
if (this.searchText !== null) this.search(this.searchText);
}
return this;
}
addAtBegin(newObj) {
if (!newObj[this.keyField] || newObj[this.keyField].toString() === '') {
throw new Error(`${this.keyField} can't be empty value.`);
}
const currentDisplayData = this.getCurrentDisplayData();
currentDisplayData.forEach(function(row) {
if (row[this.keyField].toString() === newObj[this.keyField].toString()) {
throw new Error(`${this.keyField} ${newObj[this.keyField]} already exists`);
}
}, this);
currentDisplayData.unshift(newObj);
if (this.isOnFilter) {
this.data.unshift(newObj);
}
this._refresh(false);
}
add(newObj) {
const e = this.isValidKey(newObj[this.keyField]);
if (e) throw new Error(e);
const currentDisplayData = this.getCurrentDisplayData();
currentDisplayData.push(newObj);
if (this.isOnFilter) {
this.data.push(newObj);
}
this._refresh(false);
}
isValidKey = key => {
if (key === null || key === undefined || key.toString() === '') {
return `${this.keyField} can't be empty value.`;
}
const currentDisplayData = this.getCurrentDisplayData();
const exist = currentDisplayData.find(row => row[this.keyField].toString() === key.toString());
if (exist) return `${this.keyField} ${key} already exists`;
}
remove(rowKey) {
const currentDisplayData = this.getCurrentDisplayData();
const result = currentDisplayData.filter(row => {
return rowKey.indexOf(row[this.keyField]) === -1;
});
if (this.isOnFilter) {
this.data = this.data.filter(row => {
return rowKey.indexOf(row[this.keyField]) === -1;
});
this.filteredData = result;
} else {
this.data = result;
}
}
filter(filterObj) {
if (Object.keys(filterObj).length === 0) {
this.filteredData = null;
this.isOnFilter = false;
this.filterObj = null;
if (this.searchText) this._search(this.data);
} else {
let source = this.data;
this.filterObj = filterObj;
if (this.searchText) {
this._search(source);
source = this.filteredData;
}
this._filter(source);
}
}
filterNumber(targetVal, filterVal, comparator) {
let valid = true;
switch (comparator) {
case '=': {
if (targetVal != filterVal) {
valid = false;
}
break;
}
case '>': {
if (targetVal <= filterVal) {
valid = false;
}
break;
}
case '>=': {
if (targetVal < filterVal) {
valid = false;
}
break;
}
case '<': {
if (targetVal >= filterVal) {
valid = false;
}
break;
}
case '<=': {
if (targetVal > filterVal) {
valid = false;
}
break;
}
case '!=': {
if (targetVal == filterVal) {
valid = false;
}
break;
}
default: {
console.error('Number comparator provided is not supported');
break;
}
}
return valid;
}
filterDate(targetVal, filterVal, comparator) {
if (!targetVal) return false;
const filterDate = filterVal.getDate();
const filterMonth = filterVal.getMonth();
const filterYear = filterVal.getFullYear();
if (typeof targetVal !== 'object') {
targetVal = new Date(targetVal);
}
const targetDate = targetVal.getDate();
const targetMonth = targetVal.getMonth();
const targetYear = targetVal.getFullYear();
let valid = true;
switch (comparator) {
case '=': {
if (filterDate !== targetDate ||
filterMonth !== targetMonth ||
filterYear !== targetYear) {
valid = false;
}
break;
}
case '>': {
if (targetVal <= filterVal) {
valid = false;
}
break;
}
case '>=': {
if (targetYear < filterYear) {
valid = false;
} else if (targetYear === filterYear &&
targetMonth < filterMonth) {
valid = false;
} else if (targetYear === filterYear &&
targetMonth === filterMonth &&
targetDate < filterDate) {
valid = false;
}
break;
}
case '<': {
if (targetVal >= filterVal) {
valid = false;
}
break;
}
case '<=': {
if (targetYear > filterYear) {
valid = false;
} else if (targetYear === filterYear &&
targetMonth > filterMonth) {
valid = false;
} else if (targetYear === filterYear &&
targetMonth === filterMonth &&
targetDate > filterDate) {
valid = false;
}
break;
}
case '!=': {
if (filterDate === targetDate &&
filterMonth === targetMonth &&
filterYear === targetYear) {
valid = false;
}
break;
}
default: {
console.error('Date comparator provided is not supported');
break;
}
}
return valid;
}
filterRegex(targetVal, filterVal) {
try {
return new RegExp(filterVal, 'i').test(targetVal);
} catch (e) {
return true;
}
}
filterCustom(targetVal, filterVal, callbackInfo, cond) {
if (callbackInfo !== null && typeof callbackInfo === 'object') {
return callbackInfo.callback(targetVal, callbackInfo.callbackParameters);
}
return this.filterText(targetVal, filterVal, cond);
}
filterText(targetVal, filterVal, cond = Const.FILTER_COND_LIKE) {
targetVal = targetVal.toString();
filterVal = filterVal.toString();
if (cond === Const.FILTER_COND_EQ) {
return targetVal === filterVal;
} else {
targetVal = targetVal.toLowerCase();
filterVal = filterVal.toLowerCase();
return !(targetVal.indexOf(filterVal) === -1);
}
}
/* General search function
* It will search for the text if the input includes that text;
*/
search(searchText) {
if (searchText.trim() === '') {
this.filteredData = null;
this.isOnFilter = false;
this.searchText = null;
if (this.filterObj) this._filter(this.data);
} else {
let source = this.data;
this.searchText = searchText;
if (this.filterObj) {
this._filter(source);
source = this.filteredData;
}
this._search(source);
}
}
_filter(source) {
const filterObj = this.filterObj;
this.filteredData = source.filter((row, r) => {
let valid = true;
let filterVal;
for (const key in filterObj) {
let targetVal = row[key];
if (targetVal === null || targetVal === undefined) {
targetVal = '';
}
switch (filterObj[key].type) {
case Const.FILTER_TYPE.NUMBER: {
filterVal = filterObj[key].value.number;
break;
}
case Const.FILTER_TYPE.CUSTOM: {
filterVal = (typeof filterObj[key].value === 'object') ?
undefined :
(typeof filterObj[key].value === 'string') ?
filterObj[key].value.toLowerCase() :
filterObj[key].value;
break;
}
case Const.FILTER_TYPE.DATE: {
filterVal = filterObj[key].value.date;
break;
}
case Const.FILTER_TYPE.REGEX: {
filterVal = filterObj[key].value;
break;
}
default: {
filterVal = filterObj[key].value;
if (filterVal === undefined) {
// Support old filter
filterVal = filterObj[key];
}
break;
}
}
let format, filterFormatted, formatExtraData, filterValue;
if (this.colInfos[key]) {
format = this.colInfos[key].format;
filterFormatted = this.colInfos[key].filterFormatted;
formatExtraData = this.colInfos[key].formatExtraData;
filterValue = this.colInfos[key].filterValue;
if (filterFormatted && format) {
targetVal = format(row[key], row, formatExtraData, r);
} else if (filterValue) {
targetVal = filterValue(row[key], row);
}
}
switch (filterObj[key].type) {
case Const.FILTER_TYPE.NUMBER: {
valid = this.filterNumber(targetVal, filterVal, filterObj[key].value.comparator);
break;
}
case Const.FILTER_TYPE.DATE: {
valid = this.filterDate(targetVal, filterVal, filterObj[key].value.comparator);
break;
}
case Const.FILTER_TYPE.REGEX: {
valid = this.filterRegex(targetVal, filterVal);
break;
}
case Const.FILTER_TYPE.CUSTOM: {
const cond = filterObj[key].props ? filterObj[key].props.cond : Const.FILTER_COND_LIKE;
valid = this.filterCustom(targetVal, filterVal, filterObj[key].value, cond);
break;
}
default: {
if (filterObj[key].type === Const.FILTER_TYPE.SELECT &&
filterFormatted && filterFormatted && format) {
filterVal = format(filterVal, row, formatExtraData, r);
}
const cond = filterObj[key].props ? filterObj[key].props.cond : Const.FILTER_COND_LIKE;
valid = this.filterText(targetVal, filterVal, cond);
break;
}
}
if (!valid) {
break;
}
}
return valid;
});
this.isOnFilter = true;
}
/*
* Four different sort modes, all case insensitive:
* (1) strictSearch && !multiColumnSearch
* search text must be contained as provided in a single column
* (2) strictSearch && multiColumnSearch
* conjunction (AND combination) of whitespace separated terms over multiple columns
* (3) !strictSearch && !multiColumnSearch
* conjunction (AND combination) of whitespace separated terms in a single column
* (4) !strictSearch && multiColumnSearch
* any of the whitespace separated terms must be contained in any column
*/
_search(source) {
let searchTextArray;
if (this.multiColumnSearch || !this.strictSearch) {
// ignore leading and trailing whitespaces
searchTextArray = this.searchText.trim().toLowerCase().split(/\s+/);
} else {
searchTextArray = [ this.searchText.toLowerCase() ];
}
const searchTermCount = searchTextArray.length;
const multipleTerms = searchTermCount > 1;
const nonStrictMultiCol = multipleTerms && !this.strictSearch && this.multiColumnSearch;
const nonStrictSingleCol = multipleTerms && !this.strictSearch && !this.multiColumnSearch;
this.filteredData = source.filter((row, r) => {
const keys = Object.keys(row);
// only clone array if necessary
let searchTerms = multipleTerms ? searchTextArray.slice() : searchTextArray;
// for loops are ugly, but performance matters here.
// And you cant break from a forEach.
// http://jsperf.com/for-vs-foreach/66
for (let i = 0, keysLength = keys.length; i < keysLength; i++) {
const key = keys[i];
const colInfo = this.colInfos[key];
if (colInfo && colInfo.searchable) {
const {
format,
filterFormatted,
filterValue,
formatExtraData
} = colInfo;
let targetVal;
if (filterFormatted && format) {
targetVal = format(row[key], row, formatExtraData, r);
} else if (filterValue) {
targetVal = filterValue(row[key], row);
} else {
targetVal = row[key];
}
if (targetVal !== null && typeof targetVal !== 'undefined') {
targetVal = targetVal.toString().toLowerCase();
if (nonStrictSingleCol && searchTermCount > searchTerms.length) {
// reset search terms for single column search
searchTerms = searchTextArray.slice();
}
for (let j = searchTerms.length - 1; j > -1; j--) {
if (targetVal.indexOf(searchTerms[j]) !== -1) {
if (nonStrictMultiCol || searchTerms.length === 1) {
// match found: the last or only one
return true;
}
// match found: but there are more search terms to check for
searchTerms.splice(j, 1);
} else if (!this.multiColumnSearch) {
// one of the search terms was not found in this column
break;
}
}
}
}
}
return false;
});
this.isOnFilter = true;
}
_sort(arr) {
if (this.sortList.length === 0 || typeof(this.sortList[0]) === 'undefined') {
return arr;
}
arr.sort((a, b) => {
let result = 0;
for (let i = 0; i < this.sortList.length; i++) {
const sortDetails = this.sortList[i];
const isDesc = sortDetails.order.toLowerCase() === Const.SORT_DESC;
const { sortFunc, sortFuncExtraData } = this.colInfos[sortDetails.sortField];
if (sortFunc) {
result = sortFunc(a, b, sortDetails.order, sortDetails.sortField, sortFuncExtraData);
} else {
const valueA = a[sortDetails.sortField] === null ? '' : a[sortDetails.sortField];
const valueB = b[sortDetails.sortField] === null ? '' : b[sortDetails.sortField];
if (isDesc) {
if (typeof valueB === 'string') {
result = valueB.localeCompare(valueA);
} else {
result = valueA > valueB ? -1 : ((valueA < valueB) ? 1 : 0);
}
} else {
if (typeof valueA === 'string') {
result = valueA.localeCompare(valueB);
} else {
result = valueA < valueB ? -1 : ((valueA > valueB) ? 1 : 0);
}
}
}
if (result !== 0) {
return result;
}
}
return result;
});
return arr;
}
getDataIgnoringPagination() {
return this.getCurrentDisplayData();
}
get() {
const _data = this.getCurrentDisplayData();
if (_data.length === 0) return _data;
const remote = typeof this.remote === 'function' ?
(this.remote(Const.REMOTE))[Const.REMOTE_PAGE] : this.remote;
if (remote || !this.enablePagination) {
return _data;
} else {
const result = [];
for (let i = this.pageObj.start; i <= this.pageObj.end; i++) {
if (!!_data[i]) result.push(_data[i]);
if (i + 1 === _data.length) break;
}
return result;
}
}
getKeyField() {
return this.keyField;
}
getDataNum() {
return this.getCurrentDisplayData().length;
}
isChangedPage() {
return this.pageObj.start && this.pageObj.end ? true : false;
}
isEmpty() {
return (this.data.length === 0 ||
this.data === null ||
this.data === undefined);
}
getAllRowkey() {
return this.data.map(row => {
return row[this.keyField];
});
}
}