This repository was archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathPaginator.js
161 lines (148 loc) · 4.85 KB
/
Paginator.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
/**
* Paginator - Simple paginator utility example that abstracts logic in a controllable pattern
*
* @param paginate {function} Query function that takes paginationOptions
*
* @example
* resolve: {
* // Prepares the paginator
* paginator: function(Paginator, Project) {
* // Calls `Project.list(paginationOptions)`
* return new Paginator(Project.list, { limit: 50 });
* }
* },
* controller: function($scope, paginator) {
* $scope.paginator = paginator; // ng-repeat="item in paginator.items"
* paginator.next(); // asynchronously load the first dataset
* }
*
* @example
* resolve: {
* taskPaginator: function(Paginator, Task, $stateParams) {
* return new Paginator( (paginationOptions) => Task.list($stateParams.projectId, paginationOptions) );
* // or
* return new Paginator( Task.list, { projectId: $stateParams.projectId } );
* },
* controller: function($scope, taskPaginator) {
* $scope.paginator = taskPaginator; // ng-repeat="item in paginator.items"
* taskPaginator.next(); // asynchronously load the first dataset
* }
*/
angular.module('App').factory('Paginator', function($http, $q){
class Paginator {
/**
* @param {string|function} paginate URL or callback function that returns
* a promise
* @param {object} options Default paginate query options
* @param {object} relatedHelpers Map of callback functions that take array
* of items and returns an indexed hash
*/
constructor(paginate, options = {}, relatedHelpers = {}) {
this.resetOptions = options;
this.paginate = paginate;
this.relatedHelpers = relatedHelpers;
this.related = _.mapValues(this.relatedHelpers, () => {
return {};
});
}
/**
* paginator.paginate - paginator function
*
* @param {url|function} paginate
* If a url is provided, a wrapper for $http.get() is created
* If a callback is provided, use that instead
*/
set paginate(paginate) {
this.reset();
if (angular.isString(paginate))
this._paginate = (paginateOptions) => $http.get(paginate, { params: paginateOptions });
else
this._paginate = paginate;
}
get paginate() {
return this._paginate;
}
/**
* reset()
*
* Clear items collection. Useful for preserving related data.
*
* @note If you want a hard reset of all related data, create a new Paginator
*
* @param {object} [resetOptions]
* Optional hash of options to reset with,
* otherwise last reset options will be used
*/
reset(resetOptions = this.resetOptions) {
this.resetOptions = resetOptions;
this.options = _.extend({
limit: 20,
offset: 0
}, resetOptions);
this.hasMore = true;
return this.items = [];
}
next() {
if (!this.hasMore) return $q.when();
if (this.loading) return this.loading;
return this.loading = this.paginate(this.options).then( items => {
if (items.length < this.options.limit)
this.hasMore = false;
this.items.push.apply(this.items, items);
this.options.offset = this.items.length;
return this.getRelated(items)
.then( related => this.items )
.finally( () => this.loading = null );
});
}
/**
* add()
*
* Add item to this.items and populate related
*
* @param {index|object} item Reference to an object or the index
*/
add(item) {
this.items.unshift(item);
return this.getRelated([item]);
}
/**
* remove()
*
* Remove item from this.items
*
* @param {index|object} item Reference to an object or the index
*/
remove(item) {
if (!this.items[item])
item = this.items.indexOf(item);
if (~item)
this.items.splice(item, 1);
}
/**
* getRelated(newItems)
*
* Iterates over related data retrieval helpers
* When each helper resolves with a hash of relatedItems, they are merged onto
* the paginator's existing cache of related items.
*
* @example
* paginator = new Paginator(Project.list(), {}, { owners: Project.relatedOwners });
* paginator.next();
*
* <li ng-repeat="project in paginator.projects">
* {{paginator.related.owners[project.owner_id].name}}
* </li>
*
* @param {array} [items] an array of objects to pass to the related helper
* @return {Promise} resolved when all helpers are done
*/
getRelated(items = this.items) {
return $q.all(_.mapValues(this.relatedHelpers, (helper, name) =>
helper(items)
.then( relatedItems => _.extend(this.related[name], relatedItems) )
));
}
}
return Paginator;
});