-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsortable-table.component.ts
More file actions
260 lines (228 loc) · 8.06 KB
/
sortable-table.component.ts
File metadata and controls
260 lines (228 loc) · 8.06 KB
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
import {
Component, OnChanges, Input, ViewChild, ViewEncapsulation, SimpleChange, SimpleChanges
} from '@angular/core';
import { DefaultSort, SortableEvents } from '../../models/index';
import { SortableTableService } from "../../services/sortable-table.service";
import { HeaderItem, TableFilter, Pagination, FieldToQueryBy, SetHeadersFunction, SearchString, TableItem } from "../../models/";
import { Observable } from "rxjs";
declare const require: any;
const debounce = require('lodash.debounce');
/**
* Utility function for handling headers if there is no @Input setHeaders
* @param data
*/
const mapper = (data: Observable<{key: string, value : Array<any>}>) : Observable<Array<HeaderItem>> =>
data.map(({value}) => {
const keys = Object.keys(value);
if (keys.length) {
const data = value[Object.keys(value)[0]];
return Object.keys(data).map(key => ({name : key, sortable: false}))
}
});
/**
* Main component of the module.
*/
@Component({
selector: 'ngfb-sortable-table',
templateUrl: './sortable-table.component.html',
styleUrls: ['./sortable-table.component.scss'],
encapsulation: ViewEncapsulation.None,
})
export class SortableTableComponent implements OnChanges {
@Input() public title: string;
@Input() public databaseDataPath: string;
@Input() public setHeaders: SetHeadersFunction;
@Input() public pagination: Pagination;
@Input() public filterByInputValue: SearchString;
@Input() public filterBySelect: TableFilter;
@Input() public itemComponent: TableItem;
@Input() public onChange: Function;
@Input() public addNew: Component;
@Input() public defaultSort: DefaultSort | null;
@ViewChild('searchString') public searchString;
public isLoading: boolean = false;
public data: Array<any> = [];
public headers: Array<HeaderItem>;
public fieldToSortBy: string;
public isFirstTime: boolean = true;
constructor(public DB: SortableTableService) { }
/**
* Listen to input changes
* @param changes
*/
public ngOnChanges(changes: SimpleChanges): void {
const pathToFetchChangedTo = changes['databaseDataPath'] as SimpleChange;
if (pathToFetchChangedTo) {
this.DB.lastEventHappened = undefined;
if (!pathToFetchChangedTo.isFirstChange()) {
this.data = [];
this.fieldToSortBy = '';
}
if (this.pagination) {
this.DB.setPagination(this.pagination.defaultOption || 20);
}
this.isFirstTime = true;
if (this.filterBySelect) {
this.fetchData(SortableEvents.FilterBySelect, {
value: this.filterBySelect.defaultOption,
field: this.filterBySelect.field
}, true);
} else if (this.defaultSort) {
this.fieldToSortBy = this.defaultSort.config.field;
this.fetchData(this.defaultSort.event, this.defaultSort.config, true);
} else {
this.fetchData();
}
}
}
/**
* If there is an @Input filterByInputValue provided
* this method will be triggered on keyup event.
* debounce function is much more useful here in comparation with
* Observable.debounceTime(timer) because input could be created or deleted dinamically.
*/
public onSearchInputChanged = debounce(function ($event) {
Observable
.of($event['target']['value'])
.map(inputVal => inputVal.replace(/\?!.#$[]/g, ''))
.first()
.subscribe(inputVal => {
this.fetchData(SortableEvents.FilterBySearch, {
field: this.filterByInputValue.defaultField,
value: inputVal
}, true);
})
}, 500);
/**
* Resets view after other than previous event happened
* @param nextEvent
*/
public reset(nextEvent): void {
switch (nextEvent) {
case SortableEvents.FilterBySearch : {
this.fieldToSortBy = '';
if (this.filterBySelect) {
this.filterBySelect.defaultOption = this.filterBySelect.resetTo;
}
break;
}
case SortableEvents.FilterBySelect: {
this.fieldToSortBy = '';
if (this.filterByInputValue && this.searchString) {
this.searchString['nativeElement'].value = '';
}
break;
}
case SortableEvents.SortByField : {
if (this.filterBySelect) {
this.filterBySelect.defaultOption = this.filterBySelect.resetTo;
}
if (this.filterByInputValue && this.searchString) {
this.searchString['nativeElement'].value = '';
}
break;
}
}
}
/**
* Fetch data when user choose some value in filterBySelect
* @param $event
*/
public filterBySelectChanged($event) {
if (this.pagination) {
this.paginationChanged(this.pagination.defaultOption);
}
this.fetchData(SortableEvents.FilterBySelect, {
value: $event.value,
field: this.filterBySelect.field
}, true)
}
/**
* Change pagination number
* @param value
*/
public paginationChanged(value) {
this.pagination.defaultOption = value;
this.DB.setPagination(value);
}
/**
* Get data from database and parse it accrdingly.
* @param event
* @param fieldToQueryBy
* @param cleanUp
*/
public fetchData(event? : number, fieldToQueryBy?: FieldToQueryBy, cleanUp?: boolean): void {
this.isLoading = true;
this.reset(event);
/**
* This stream whaits for data from db.
* @type {"../..Observable".Observable<T>|Observable<R|T>}
*/
const requestStream = this.DB
.get(this.databaseDataPath, event, fieldToQueryBy)
.share()
.first();
/**
* This stream puts data to view
*/
requestStream
.map(({key, value}) => Object.keys(value || {}).map(key => value[key]))
.do(() => this.isLoading = false)
.first() //never lose the link to obj in order to save rendering process
.subscribe(arr => cleanUp ? this.data.splice(0, this.data.length, ...arr) : this.data.push(...arr));
/**
* Handle headers only once. When path to data in db changes.
*/
if (this.isFirstTime) {
const headers = this.setHeaders ? this.setHeaders(requestStream) : mapper(requestStream);
headers
.first()
.subscribe((data: Array<HeaderItem>) => this.headers = data);
this.isFirstTime = false;
}
}
/**
* InfiniteScroll event litener
*/
public onInfinite(): void {
if (this.pagination) {
this.fetchData(
SortableEvents.InfiniteScroll,
null,
this.DB.lastEventHappened !== undefined
);
}
}
/**
* Function for trackBy in *ngFor directive
* @param index
* @param item
*/
public trackByFn(index, item): string {
return item.$key;
}
/**
* Sort table by some field
* @param field
*/
public sortBy(field): void {
this.fieldToSortBy = this.fieldToSortBy === field ? `-${field}` : field;
if (this.pagination) {
this.paginationChanged(this.pagination.defaultOption);
}
this.fetchData(SortableEvents.SortByField, {
field: field,
order: this.fieldToSortBy.startsWith('-') ? 'desc' : 'asc'
}, true);
}
/**
* Emits when addNew component close
* or when itemChange event happen in TableItemComponent
* @param result
*/
public onItemChange(result): void {
if (this.onChange) {
this.onChange(result);
}
}
}