-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsortable-table.service.ts
217 lines (200 loc) · 6.55 KB
/
sortable-table.service.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
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { database } from 'firebase';
import { FieldToQueryBy } from '../models/';
/**
* Events that could happen in NGFBSortableTable process
*/
export enum SortableEvents {
InfiniteScroll = 1,
SortByField,
FilterBySearch,
FilterBySelect
}
/**
* Service that provides querying under the hood of SortableTableComponent
*/
@Injectable()
export class SortableTableService {
public DB: database.Database = database();
public query : Object = {
orderByKey: true
};
public lastKeyGot: string | undefined;
public lastItemGot: Object;
public lastEventHappened: number;
public lastSort: FieldToQueryBy | null;
public pagination: number;
constructor() { }
/**
* Changes the direction of limit
* @param first
* @param number
*/
public setLimit(first, number) {
this.query[first ? 'limitToFirst' : 'limitToLast'] = Number(number);
}
/**
* Changes pagination number
* @param pagination
*/
public setPagination(pagination: number): void {
this.pagination = pagination;
}
/**
* Add ability to transform native firebase querying that uses chaining to object passing like in
* [Angularfire](https://github.com/angular/angularfire2)
* @param ref
* @returns {database.Reference}
*/
public querify(ref: database.Reference): any {
const keys = Object.keys(this.query);
return keys.reduce((ref, key: string) =>
key === 'orderByKey' || key === 'orderByPriority' ?
ref[key]() :
ref[key](this.query[key]),
ref
);
}
/**
* Control quering before sending request. Handle all possible cases of querying.
* @param event
* @param fieldToQueryBy
*/
public preGet(event?: number, fieldToQueryBy?: FieldToQueryBy) {
const se = SortableEvents;
switch (event) {
case se.InfiniteScroll:
//if there was no events, user just scrolled down;
if (this.lastEventHappened === undefined) {
if (this.lastKeyGot) {
this.query['startAt'] = this.lastKeyGot
}
//if prev event was sorting by field
} else if (this.lastEventHappened === se.SortByField) {
const query = Object.assign({}, this.query);
this.query = {
orderByChild: this.query['orderByChild'],
};
if (this.pagination) {
this.setLimit(query.hasOwnProperty('limitToFirst'), this.pagination += this.pagination)
}
//else if last was an other events
} else if (this.lastEventHappened === se.FilterBySearch || this.lastEventHappened === se.FilterBySelect) {
if (this.pagination) {
this.setLimit(true, this.pagination += this.pagination);
}
}
break;
case se.SortByField:
this.lastSort = Object.assign({}, fieldToQueryBy);
this.query = {
orderByChild: fieldToQueryBy.field,
};
if (this.pagination) {
this.setLimit(fieldToQueryBy.order === 'desc', this.pagination)
}
break;
case se.FilterBySearch:
this.lastSort = Object.assign({}, fieldToQueryBy);
this.query = {
startAt: fieldToQueryBy.value,
endAt: fieldToQueryBy.value + "\uf8ff",
orderByChild: fieldToQueryBy.field
};
if (this.pagination) {
this.setLimit(true, this.pagination);
}
break;
case se.FilterBySelect:
if (fieldToQueryBy.value === null) {
this.query = {
orderByKey: true
}
} else {
this.query = {
orderByChild: fieldToQueryBy.field,
equalTo: fieldToQueryBy.value
};
}
if (this.pagination) {
this.setLimit(true, this.pagination);
}
break;
default:
this.query = {
orderByKey: true
};
if (this.pagination) {
this.setLimit(true, this.pagination);
}
break;
}
}
/**
* Function that actually makes a request to database.
* @param path
* @param event
* @param fieldToQueryBy
*/
public get(path: string, event?: number, fieldToQueryBy?: FieldToQueryBy) : Observable<any> {
if (event !== this.lastEventHappened && event !== SortableEvents.InfiniteScroll) {
this.lastItemGot = null;
this.lastKeyGot = null;
}
const prevLastKey = this.lastKeyGot;
this.preGet(event, fieldToQueryBy);
return Observable.fromPromise(this.querify(this.DB['ref'](path)).once('value') as Promise<any>)
.map((snapshot : database.DataSnapshot) => {
let data = [];
snapshot['forEach']((elem : database.DataSnapshot) => {
const val = elem['val']();
if (val) {
Object.defineProperty(val, '$key', {
value: elem['key']
});
data.push(val);
}
return false;
});
const lastObj = data.slice(-1)[0];
if (lastObj) {
this.lastItemGot = lastObj;
this.lastKeyGot = lastObj['$key'];
}
if (prevLastKey === this.lastKeyGot &&
event === SortableEvents.InfiniteScroll &&
this.lastEventHappened === undefined
) {
data = [];
}
return ({key : snapshot['key'], value : data})
})
.map(({key, value} : {key : string, value: Array<any>}): any => {
if (
(event === SortableEvents.SortByField && fieldToQueryBy.order === 'asc') ||
(event === SortableEvents.InfiniteScroll &&
this.lastSort &&
this.lastSort.order &&
this.lastSort.order === 'asc')
) {
value = value.reverse();
//filter by input string
} else if (event === SortableEvents.FilterBySearch) {
value = value.filter(
elem => typeof elem[fieldToQueryBy.field] === 'string' ?
elem[fieldToQueryBy.field]
.toLowerCase()
.startsWith((fieldToQueryBy.value as string)
.toLowerCase()) : false
)
}
return ({key: key, value: value});
})
.do(() => {
if (event !== SortableEvents.InfiniteScroll) {
this.lastEventHappened = event;
}
})
}
}