forked from bencripps/react-redux-grid-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirebasePaginator.js
116 lines (102 loc) · 2.99 KB
/
FirebasePaginator.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
/* eslint-disable */
import FinitePagingStrategy from './FirebasePaginatorFiniteStrategy';
import InfinitePagingStrategy from './FirebasePaginatorInfiniteStrategy';
class FirebasePaginator {
constructor(ref, defaults) {
this.defaults = defaults || {};
this.pages = {};
this.pageSize = defaults.pageSize ? parseInt(defaults.pageSize, 10) : 10;
this.isFinite = defaults.finite ? defaults.finite : false;
this.retainLastPage = defaults.retainLastPage || false;
this.auth = defaults.auth;
this.ref = ref;
this.isBrowser = defaults.isBrowser;
this.events = {};
this.pageCount;
// Events
this.listen = callback => {
this.allEventHandler = callback;
};
this.fire = this.fire.bind(this);
this.on = this.on.bind(this);
this.off = this.off.bind(this);
this.once = this.once.bind(this);
// Pagination can be finite or infinite. Infinite pagination is the default.
const paginator = this;
if (this.isFinite) {
//this.setupFinite();
this.strategy = new FinitePagingStrategy(paginator);
} else {
this.strategy = new InfinitePagingStrategy(paginator);
}
this.next = this.next.bind(this);
this.previous = this.previous.bind(this);
this.goToPage = this.goToPage.bind(this);
console.log('FirebasePaginator constructor this: ', this);
}
fire(eventName, payload) {
if (typeof this.allEventHandler === 'function') {
this.allEventHandler.call(this, eventName, payload);
}
if (this.events[eventName] && this.events[eventName].queue) {
const queue = events[eventName].queue.reverse();
let i = queue.length;
while (i--) {
if (typeof queue[i] === 'function') {
queue[i].call(this, payload);
}
}
}
}
on(eventName, callback) {
if (!this.events[eventName]) {
this.events[eventName] = {
queue: []
};
}
this.events[eventName].queue.push(callback);
}
off(eventName, callback) {
if (this.events[eventName] && this.events[eventName].queue) {
const queue = this.events[eventName].queue;
let i = queue.length;
while (i--) {
if (queue[i] === callback) {
queue.splice(i, 1);
}
}
}
}
once(eventName, callback) {
return new Promise((resolve, reject) => {
const handler = payload => {
this.off(eventName, handler);
if (typeof callback === 'function') {
try {
resolve(callback.call(this, payload));
} catch (e) {
reject(e);
}
} else {
resolve(payload);
}
};
this.on(eventName, handler);
});
}
// strategies based on finite or infinite
next() {
return this.strategy.next();
}
previous() {
return this.strategy.previous();
}
reset() {
return this.strategy.reset();
}
goToPage(pageNumber) {
console.log('access this.strategy', this.strategy);
return this.strategy.goToPage(pageNumber);
}
}
export default FirebasePaginator;