forked from vaadin/vaadin-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaadin-grid-data-provider-mixin.html
390 lines (333 loc) · 10.3 KB
/
vaadin-grid-data-provider-mixin.html
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
<!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->
<script>
window.Vaadin = window.Vaadin || {};
window.Vaadin.Grid = window.Vaadin.Grid || {};
Vaadin.Grid.ItemCache = class ItemCache {
constructor(grid, parentCache, parentItem) {
this.grid = grid;
this.parentCache = parentCache;
this.parentItem = parentItem;
this.itemCaches = {};
this.items = {};
this.effectiveSize = 0;
this.size = 0;
this.pendingRequests = {};
}
isLoading() {
return Object.keys(this.pendingRequests).length || Object.keys(this.itemCaches).filter(index => {
return this.itemCaches[index].isLoading();
})[0];
}
getItemForIndex(index) {
const {cache, scaledIndex} = this.getCacheAndIndex(index);
return cache.items[scaledIndex];
}
updateSize() {
this.effectiveSize = (!this.parentItem || this.grid._isExpanded(this.parentItem)) ?
this.size + Object.keys(this.itemCaches).reduce((prev, curr) => {
const subCache = this.itemCaches[curr];
subCache.updateSize();
return prev + subCache.effectiveSize;
}, 0) :
0;
}
ensureSubCacheForScaledIndex(scaledIndex) {
if (!this.itemCaches[scaledIndex]) {
const subCache = new Vaadin.Grid.ItemCache(this.grid, this, this.items[scaledIndex]);
this.itemCaches[scaledIndex] = subCache;
this.grid._loadPage(0, subCache);
}
}
getCacheAndIndex(index) {
let thisLevelIndex = index;
const keys = Object.keys(this.itemCaches);
for (var i = 0; i < keys.length; i++) {
const expandedIndex = Number(keys[i]);
const subCache = this.itemCaches[expandedIndex];
if (thisLevelIndex <= expandedIndex) {
return {cache: this, scaledIndex: thisLevelIndex};
} else if (thisLevelIndex <= expandedIndex + subCache.effectiveSize) {
return subCache.getCacheAndIndex(thisLevelIndex - expandedIndex - 1);
}
thisLevelIndex -= subCache.effectiveSize;
}
return {cache: this, scaledIndex: thisLevelIndex};
}
};
/**
* @polymerMixin
*/
Vaadin.Grid.DataProviderMixin = superClass => class DataProviderMixin extends superClass {
static get properties() {
return {
/**
* Number of items fetched at a time from the dataprovider.
*/
pageSize: {
type: Number,
value: 50,
observer: '_pageSizeChanged'
},
/**
* Function that provides items lazily. Receives arguments `params`, `callback`
*
* `params.page` Requested page index
*
* `params.pageSize` Current page size
*
* `params.filters` Currently applied filters
*
* `params.sortOrders` Currently applied sorting orders
*
*/
dataProvider: {
type: Object,
notify: true,
observer: '_dataProviderChanged'
},
/**
* `true` while data is being requested from the data provider.
*/
_loading: {
type: Boolean,
observer: '_loadingChanged'
},
_cache: {
type: Object,
value: function() {
const cache = new Vaadin.Grid.ItemCache(this);
return cache;
}
},
/**
* Path to an item sub-property that identifies the item.
*/
itemIdPath: {
type: String,
value: null
},
/**
* An array that contains the expanded items.
*/
expandedItems: {
type: Object,
notify: true,
value: () => []
}
};
}
static get observers() {
return [
'_sizeChanged(size)',
'_expandedItemsChanged(expandedItems.*)'
];
}
_sizeChanged(size) {
const delta = size - this._cache.size;
this._cache.size += delta;
this._cache.effectiveSize += delta;
this._effectiveSize = this._cache.effectiveSize;
}
_loadingChanged(loading) {
this._toggleAttribute('loading', loading, this);
}
_updateRowItem(item, el) {
el.children.forEach(cell => {
cell._instance && (cell._instance.item = item);
});
}
_getItem(index, el) {
if (this._effectiveSize && index >= this._effectiveSize) {
return;
}
el.index = index;
const {cache, scaledIndex} = this._cache.getCacheAndIndex(index);
const item = cache.items[scaledIndex];
if (item) {
this._updateItem(el, item);
if (this._isExpanded(item)) {
cache.ensureSubCacheForScaledIndex(scaledIndex);
}
} else {
this._loadPage(this._getPageForIndex(scaledIndex), cache);
}
}
_pagesForPhysicalItems() {
// TODO: potentially heavy operation to run first visible index,
// reconsider if performance issues occur on data binding / scrolling.
// TODO: _vidxOffset shouldn't be read from here.
const firstVisiblePage = this._getPageForIndex(this._firstVisibleIndex + this._vidxOffset);
return [firstVisiblePage].concat(
this._physicalItems
.filter(row => row.index)
.items(row => this._getPageForIndex(row.index))
).reduce((prev, curr) => {
if (prev.indexOf(curr) === -1) {
prev.push(curr);
}
return prev;
}, []);
}
_expandedInstanceChangedCallback(inst, value) {
if (inst.item === undefined) {
return;
}
if (value) {
this.expandItem(inst.item);
} else {
this.collapseItem(inst.item);
}
}
/**
* Returns a value that identifies the item. Uses `itemIdPath` if available.
* Can be customized by overriding.
*/
getItemId(item) {
return this.itemIdPath ? this.get(this.itemIdPath, item) : item;
}
_isExpanded(item) {
return this.expandedItems && this._getItemIndexInArray(item, this.expandedItems) > -1;
}
_expandedItemsChanged(e) {
this._cache.updateSize();
this._effectiveSize = this._cache.effectiveSize;
this._assignModels();
}
/**
* Expands the given item hierarchy.
*/
expandItem(item) {
if (!this._isExpanded(item)) {
this.push('expandedItems', item);
}
}
/**
* Collapses the given item hierarchy.
*/
collapseItem(item) {
if (this._isExpanded(item)) {
this.splice('expandedItems', this._getItemIndexInArray(item, this.expandedItems), 1);
}
}
_getIndexLevel(index) {
let {cache} = this._cache.getCacheAndIndex(index);
let level = 0;
while (cache.parentCache) {
cache = cache.parentCache;
level++;
}
return level;
}
_canPopulate() {
return this._hasData;
}
_loadPage(page, cache) {
// make sure same page isn't requested multiple times.
if (!cache.pendingRequests[page] && this.dataProvider) {
cache.pendingRequests[page] = true;
const params = {
page,
pageSize: this.pageSize,
sortOrders: this._mapSorters(),
filters: this._mapFilters(),
parentItem: cache.parentItem
};
this.dataProvider(params, (items, size) => {
if (size !== undefined) {
cache.size = size;
} else {
if (params.parentItem) {
cache.size = items.length;
}
}
items.forEach((item, itemsIndex) => {
cache.items[page * this.pageSize + itemsIndex] = item;
});
this._cache.updateSize();
this._effectiveSize = this._cache.effectiveSize;
Array.from(this.$.items.children).forEach(row => {
const cachedItem = this._cache.getItemForIndex(row.index);
if (items.indexOf(cachedItem) > -1) {
this._updateItem(row, cachedItem);
}
});
this._hasData = true;
this._increasePoolIfNeeded(0);
delete cache.pendingRequests[page];
this._loading = this._cache.isLoading();
});
}
}
_getPageForIndex(index) {
return Math.floor(index / this.pageSize);
}
/**
* Clears the cached pages and reloads data from dataprovider when needed.
*/
clearCache() {
this._cache = new Vaadin.Grid.ItemCache(this);
this._cache.size = this.size;
this._cache.updateSize();
this._hasData = false;
this._loading = true;
this._assignModels();
}
_flushItemsDebouncer() {
if (this._debouncerLoad) {
this._debouncerLoad.flush();
}
}
_pageSizeChanged(pageSize, oldPageSize) {
if (oldPageSize !== undefined && pageSize !== oldPageSize) {
this.clearCache();
}
}
_checkSize() {
if (this.size === undefined) {
console.warn('The <vaadin-grid> needs a value for "size" property in order to display rows.');
}
}
_dataProviderChanged(dataProvider, oldDataProvider) {
if (oldDataProvider !== undefined) {
this.clearCache();
}
if (dataProvider && this.items && this.items.length) {
// Fixes possibly invalid cached lastVisibleIndex value in <iron-list>
this._scrollToIndex(this._firstVisibleIndex);
}
if (!this._hasData) {
// load data before adding rows to make sure they have content when
// rendered for the first time.
this._loading = true;
this._loadPage(0, this._cache, () => {
const hadData = this._hasData;
this._hasData = true;
if (!hadData) {
this.notifyResize();
}
});
}
this._debouncerCheckSize = Polymer.Debouncer.debounce(
this._debouncerCheckSize,
Polymer.Async.timeOut.after(2000),
this._checkSize.bind(this));
this._scrollHandler();
}
_itemsEqual(item1, item2) {
return this.getItemId(item1) === this.getItemId(item2);
}
_getItemIndexInArray(item, array) {
let result = -1;
array.forEach((i, idx) => {
if (this._itemsEqual(i, item)) {
result = idx;
}
});
return result;
}
};
</script>