@vaadin/component-base 24.2.3 → 24.3.0-alpha10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/data-provider-controller/cache.d.ts +146 -0
- package/src/data-provider-controller/cache.js +285 -0
- package/src/data-provider-controller/data-provider-controller.d.ts +172 -0
- package/src/data-provider-controller/data-provider-controller.js +275 -0
- package/src/data-provider-controller/helpers.d.ts +63 -0
- package/src/data-provider-controller/helpers.js +112 -0
- package/src/define.js +7 -0
- package/src/element-mixin.js +0 -4
- package/src/virtualizer-iron-list-adapter.js +33 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vaadin/component-base",
|
|
3
|
-
"version": "24.
|
|
3
|
+
"version": "24.3.0-alpha10",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -35,12 +35,12 @@
|
|
|
35
35
|
"@polymer/polymer": "^3.0.0",
|
|
36
36
|
"@vaadin/vaadin-development-mode-detector": "^2.0.0",
|
|
37
37
|
"@vaadin/vaadin-usage-statistics": "^2.1.0",
|
|
38
|
-
"lit": "^
|
|
38
|
+
"lit": "^3.0.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@esm-bundle/chai": "^4.3.4",
|
|
42
|
-
"@vaadin/testing-helpers": "^0.
|
|
42
|
+
"@vaadin/testing-helpers": "^0.6.0",
|
|
43
43
|
"sinon": "^13.0.2"
|
|
44
44
|
},
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "0271523d93fe5df0425ff64206886614f3c6f401"
|
|
46
46
|
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2021 - 2023 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import type { DataProviderCallback } from './data-provider-controller.js';
|
|
7
|
+
|
|
8
|
+
export type CacheContext<TItem> = { isExpanded(item: TItem): boolean };
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A class that stores items with their associated sub-caches.
|
|
12
|
+
*/
|
|
13
|
+
export class Cache<TItem> {
|
|
14
|
+
/**
|
|
15
|
+
* A context object.
|
|
16
|
+
*/
|
|
17
|
+
context: CacheContext<TItem>;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The number of items.
|
|
21
|
+
*/
|
|
22
|
+
size: number;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The number of items to display per page.
|
|
26
|
+
*/
|
|
27
|
+
pageSize: number;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* An array of cached items.
|
|
31
|
+
*/
|
|
32
|
+
items: TItem[];
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A map where the key is a requested page and the value is a callback
|
|
36
|
+
* that will be called with data once the request is complete.
|
|
37
|
+
*/
|
|
38
|
+
pendingRequests: Record<number, DataProviderCallback<TItem>>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* An item in the parent cache that the current cache is associated with.
|
|
42
|
+
*/
|
|
43
|
+
get parentItem(): TItem | undefined;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* An array of sub-caches sorted in the same order as their associated items
|
|
47
|
+
* appear in the `items` array.
|
|
48
|
+
*/
|
|
49
|
+
get subCaches(): Array<Cache<TItem>>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether the cache or any of its descendant caches have pending requests.
|
|
53
|
+
*/
|
|
54
|
+
get isLoading(): boolean;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The total number of items, including items from expanded sub-caches.
|
|
58
|
+
*/
|
|
59
|
+
get flatSize(): number;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The total number of items, including items from expanded sub-caches.
|
|
63
|
+
*
|
|
64
|
+
* @protected
|
|
65
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
66
|
+
*/
|
|
67
|
+
get effectiveSize(): number;
|
|
68
|
+
|
|
69
|
+
constructor(
|
|
70
|
+
context: CacheContext<TItem>,
|
|
71
|
+
pageSize: number,
|
|
72
|
+
size: number,
|
|
73
|
+
parentCache?: Cache<TItem>,
|
|
74
|
+
parentCacheIndex?: number,
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Recalculates the flattened size for the cache and its descendant caches recursively.
|
|
79
|
+
*/
|
|
80
|
+
recalculateFlatSize(): void;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Adds an array of items corresponding to the given page
|
|
84
|
+
* to the `items` array.
|
|
85
|
+
*/
|
|
86
|
+
setPage(page: number, items: unknown[]): void;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Retrieves the sub-cache associated with the item at the given index
|
|
90
|
+
* in the `items` array.
|
|
91
|
+
*/
|
|
92
|
+
getSubCache(index: number): Cache<TItem> | undefined;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Removes the sub-cache associated with the item at the given index
|
|
96
|
+
* in the `items` array.
|
|
97
|
+
*/
|
|
98
|
+
removeSubCache(index: number): void;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Removes all sub-caches.
|
|
102
|
+
*/
|
|
103
|
+
removeSubCaches(): void;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Creates and associates a sub-cache for the item at the given index
|
|
107
|
+
* in the `items` array.
|
|
108
|
+
*/
|
|
109
|
+
createSubCache(index: number): Cache<TItem>;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Retrieves the flattened index corresponding to the given index
|
|
113
|
+
* of an item in the `items` array.
|
|
114
|
+
*/
|
|
115
|
+
getFlatIndex(index: number): number;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
119
|
+
*/
|
|
120
|
+
getItemForIndex(index: number): TItem | undefined;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
124
|
+
*/
|
|
125
|
+
getCacheAndIndex(index: number): { cache: Cache<TItem>; scaledIndex: number };
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
129
|
+
*/
|
|
130
|
+
updateSize(): void;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
134
|
+
*/
|
|
135
|
+
ensureSubCacheForScaledIndex(scaledIndex: number): void;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
139
|
+
*/
|
|
140
|
+
get grid(): HTMLElement;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
144
|
+
*/
|
|
145
|
+
get itemCaches(): object;
|
|
146
|
+
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2021 - 2023 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import { getFlatIndexContext } from './helpers.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A class that stores items with their associated sub-caches.
|
|
10
|
+
*/
|
|
11
|
+
export class Cache {
|
|
12
|
+
/**
|
|
13
|
+
* A context object.
|
|
14
|
+
*
|
|
15
|
+
* @type {{ isExpanded: (item: unknown) => boolean }}
|
|
16
|
+
*/
|
|
17
|
+
context;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The number of items.
|
|
21
|
+
*
|
|
22
|
+
* @type {number}
|
|
23
|
+
*/
|
|
24
|
+
size = 0;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The number of items to display per page.
|
|
28
|
+
*
|
|
29
|
+
* @type {number}
|
|
30
|
+
*/
|
|
31
|
+
pageSize;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* An array of cached items.
|
|
35
|
+
*
|
|
36
|
+
* @type {object[]}
|
|
37
|
+
*/
|
|
38
|
+
items = [];
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A map where the key is a requested page and the value is a callback
|
|
42
|
+
* that will be called with data once the request is complete.
|
|
43
|
+
*
|
|
44
|
+
* @type {Record<number, Function>}
|
|
45
|
+
*/
|
|
46
|
+
pendingRequests = {};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A map where the key is the index of an item in the `items` array
|
|
50
|
+
* and the value is a sub-cache associated with that item.
|
|
51
|
+
*
|
|
52
|
+
* Note, it's intentionally defined as an object instead of a Map
|
|
53
|
+
* to ensure that Object.entries() returns an array with keys sorted
|
|
54
|
+
* in alphabetical order, rather than the order they were added.
|
|
55
|
+
*
|
|
56
|
+
* @type {Record<number, Cache>}
|
|
57
|
+
* @private
|
|
58
|
+
*/
|
|
59
|
+
__subCacheByIndex = {};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The total number of items, including items from expanded sub-caches.
|
|
63
|
+
*
|
|
64
|
+
* @type {number}
|
|
65
|
+
* @private
|
|
66
|
+
*/
|
|
67
|
+
__flatSize = 0;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {Cache['context']} context
|
|
71
|
+
* @param {number} pageSize
|
|
72
|
+
* @param {number | undefined} size
|
|
73
|
+
* @param {Cache | undefined} parentCache
|
|
74
|
+
* @param {number | undefined} parentCacheIndex
|
|
75
|
+
*/
|
|
76
|
+
constructor(context, pageSize, size, parentCache, parentCacheIndex) {
|
|
77
|
+
this.context = context;
|
|
78
|
+
this.pageSize = pageSize;
|
|
79
|
+
this.size = size || 0;
|
|
80
|
+
this.parentCache = parentCache;
|
|
81
|
+
this.parentCacheIndex = parentCacheIndex;
|
|
82
|
+
this.__flatSize = size || 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* An item in the parent cache that the current cache is associated with.
|
|
87
|
+
*
|
|
88
|
+
* @return {object | undefined}
|
|
89
|
+
*/
|
|
90
|
+
get parentItem() {
|
|
91
|
+
return this.parentCache && this.parentCache.items[this.parentCacheIndex];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* An array of sub-caches sorted in the same order as their associated items
|
|
96
|
+
* appear in the `items` array.
|
|
97
|
+
*
|
|
98
|
+
* @return {Cache[]}
|
|
99
|
+
*/
|
|
100
|
+
get subCaches() {
|
|
101
|
+
return Object.values(this.__subCacheByIndex);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Whether the cache or any of its descendant caches have pending requests.
|
|
106
|
+
*
|
|
107
|
+
* @return {boolean}
|
|
108
|
+
*/
|
|
109
|
+
get isLoading() {
|
|
110
|
+
if (Object.keys(this.pendingRequests).length > 0) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return this.subCaches.some((subCache) => subCache.isLoading);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The total number of items, including items from expanded sub-caches.
|
|
119
|
+
*
|
|
120
|
+
* @return {number}
|
|
121
|
+
*/
|
|
122
|
+
get flatSize() {
|
|
123
|
+
return this.__flatSize;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The total number of items, including items from expanded sub-caches.
|
|
128
|
+
*
|
|
129
|
+
* @protected
|
|
130
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
131
|
+
*/
|
|
132
|
+
get effectiveSize() {
|
|
133
|
+
console.warn(
|
|
134
|
+
'<vaadin-grid> The `effectiveSize` property of ItemCache is deprecated and will be removed in Vaadin 25.',
|
|
135
|
+
);
|
|
136
|
+
return this.flatSize;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Recalculates the flattened size for the cache and its descendant caches recursively.
|
|
141
|
+
*/
|
|
142
|
+
recalculateFlatSize() {
|
|
143
|
+
this.__flatSize =
|
|
144
|
+
!this.parentItem || this.context.isExpanded(this.parentItem)
|
|
145
|
+
? this.size +
|
|
146
|
+
this.subCaches.reduce((total, subCache) => {
|
|
147
|
+
subCache.recalculateFlatSize();
|
|
148
|
+
return total + subCache.flatSize;
|
|
149
|
+
}, 0)
|
|
150
|
+
: 0;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Adds an array of items corresponding to the given page
|
|
155
|
+
* to the `items` array.
|
|
156
|
+
*
|
|
157
|
+
* @param {number} page
|
|
158
|
+
* @param {object[]} items
|
|
159
|
+
*/
|
|
160
|
+
setPage(page, items) {
|
|
161
|
+
const startIndex = page * this.pageSize;
|
|
162
|
+
items.forEach((item, i) => {
|
|
163
|
+
this.items[startIndex + i] = item;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Retrieves the sub-cache associated with the item at the given index
|
|
169
|
+
* in the `items` array.
|
|
170
|
+
*
|
|
171
|
+
* @param {number} index
|
|
172
|
+
* @return {Cache | undefined}
|
|
173
|
+
*/
|
|
174
|
+
getSubCache(index) {
|
|
175
|
+
return this.__subCacheByIndex[index];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Removes the sub-cache associated with the item at the given index
|
|
180
|
+
* in the `items` array.
|
|
181
|
+
*
|
|
182
|
+
* @param {number} index
|
|
183
|
+
*/
|
|
184
|
+
removeSubCache(index) {
|
|
185
|
+
delete this.__subCacheByIndex[index];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Removes all sub-caches.
|
|
190
|
+
*/
|
|
191
|
+
removeSubCaches() {
|
|
192
|
+
this.__subCacheByIndex = {};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Creates and associates a sub-cache for the item at the given index
|
|
197
|
+
* in the `items` array.
|
|
198
|
+
*
|
|
199
|
+
* @param {number} index
|
|
200
|
+
* @return {Cache}
|
|
201
|
+
*/
|
|
202
|
+
createSubCache(index) {
|
|
203
|
+
const subCache = new Cache(this.context, this.pageSize, 0, this, index);
|
|
204
|
+
this.__subCacheByIndex[index] = subCache;
|
|
205
|
+
return subCache;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Retrieves the flattened index corresponding to the given index
|
|
210
|
+
* of an item in the `items` array.
|
|
211
|
+
*
|
|
212
|
+
* @param {number} index
|
|
213
|
+
* @return {number}
|
|
214
|
+
*/
|
|
215
|
+
getFlatIndex(index) {
|
|
216
|
+
const clampedIndex = Math.max(0, Math.min(this.size - 1, index));
|
|
217
|
+
|
|
218
|
+
return this.subCaches.reduce((prev, subCache) => {
|
|
219
|
+
const index = subCache.parentCacheIndex;
|
|
220
|
+
return clampedIndex > index ? prev + subCache.flatSize : prev;
|
|
221
|
+
}, clampedIndex);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
226
|
+
*/
|
|
227
|
+
getItemForIndex(index) {
|
|
228
|
+
console.warn(
|
|
229
|
+
'<vaadin-grid> The `getItemForIndex` method of ItemCache is deprecated and will be removed in Vaadin 25.',
|
|
230
|
+
);
|
|
231
|
+
const { item } = getFlatIndexContext(this, index);
|
|
232
|
+
return item;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
237
|
+
*/
|
|
238
|
+
getCacheAndIndex(index) {
|
|
239
|
+
console.warn(
|
|
240
|
+
'<vaadin-grid> The `getCacheAndIndex` method of ItemCache is deprecated and will be removed in Vaadin 25.',
|
|
241
|
+
);
|
|
242
|
+
const { cache, index: scaledIndex } = getFlatIndexContext(this, index);
|
|
243
|
+
return { cache, scaledIndex };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
248
|
+
*/
|
|
249
|
+
updateSize() {
|
|
250
|
+
console.warn('<vaadin-grid> The `updateSize` method of ItemCache is deprecated and will be removed in Vaadin 25.');
|
|
251
|
+
this.recalculateFlatSize();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
256
|
+
*/
|
|
257
|
+
ensureSubCacheForScaledIndex(scaledIndex) {
|
|
258
|
+
console.warn(
|
|
259
|
+
'<vaadin-grid> The `ensureSubCacheForScaledIndex` method of ItemCache is deprecated and will be removed in Vaadin 25.',
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
if (!this.getSubCache(scaledIndex)) {
|
|
263
|
+
const subCache = this.createSubCache(scaledIndex);
|
|
264
|
+
this.context.__controller.__loadCachePage(subCache, 0);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
270
|
+
*/
|
|
271
|
+
get grid() {
|
|
272
|
+
console.warn('<vaadin-grid> The `grid` property of ItemCache is deprecated and will be removed in Vaadin 25.');
|
|
273
|
+
return this.context.__controller.host;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* @deprecated since 24.3 and will be removed in Vaadin 25.
|
|
278
|
+
*/
|
|
279
|
+
get itemCaches() {
|
|
280
|
+
console.warn(
|
|
281
|
+
'<vaadin-grid> The `itemCaches` property of ItemCache is deprecated and will be removed in Vaadin 25.',
|
|
282
|
+
);
|
|
283
|
+
return this.__subCacheByIndex;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2021 - 2023 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import type { ReactiveController } from 'lit';
|
|
7
|
+
import type { Cache } from './cache.js';
|
|
8
|
+
import type { getFlatIndexByPath, getFlatIndexContext, getItemContext } from './helpers.js';
|
|
9
|
+
|
|
10
|
+
type DataProviderDefaultParams = {
|
|
11
|
+
page: number;
|
|
12
|
+
pageSize: number;
|
|
13
|
+
parentItem?: unknown;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type DataProviderCallback<TItem> = (items: TItem[], size?: number) => void;
|
|
17
|
+
|
|
18
|
+
export type DataProvider<TItem, TDataProviderParams extends Record<string, unknown>> = (
|
|
19
|
+
params: DataProviderDefaultParams & TDataProviderParams,
|
|
20
|
+
callback: DataProviderCallback<TItem>,
|
|
21
|
+
) => void;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A controller that stores and manages items loaded with a data provider.
|
|
25
|
+
*/
|
|
26
|
+
export class DataProviderController<TItem, TDataProviderParams extends Record<string, unknown>>
|
|
27
|
+
implements ReactiveController
|
|
28
|
+
{
|
|
29
|
+
/**
|
|
30
|
+
* The controller host element.
|
|
31
|
+
*/
|
|
32
|
+
host: HTMLElement;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A callback that returns data based on the passed params such as
|
|
36
|
+
* `page`, `pageSize`, `parentItem`, etc.
|
|
37
|
+
*/
|
|
38
|
+
dataProvider: DataProvider<TItem, TDataProviderParams>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A callback that returns additional params that need to be passed
|
|
42
|
+
* to the data provider callback with every request.
|
|
43
|
+
*/
|
|
44
|
+
dataProviderParams: () => TDataProviderParams;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A number of items in the root cache.
|
|
48
|
+
*/
|
|
49
|
+
size?: number;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A number of items to display per page.
|
|
53
|
+
*/
|
|
54
|
+
pageSize: number;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A callback that returns whether the given item is expanded.
|
|
58
|
+
*/
|
|
59
|
+
isExpanded: (item: TItem) => boolean;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A callback that returns the id for the given item and that
|
|
63
|
+
* is used when checking object items for equality.
|
|
64
|
+
*/
|
|
65
|
+
getItemId: (item: TItem) => unknown;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A reference to the root cache instance.
|
|
69
|
+
*/
|
|
70
|
+
rootCache: Cache<TItem>;
|
|
71
|
+
|
|
72
|
+
constructor(
|
|
73
|
+
host: HTMLElement,
|
|
74
|
+
config: {
|
|
75
|
+
size?: number;
|
|
76
|
+
pageSize: number;
|
|
77
|
+
getItemId(item: TItem): unknown;
|
|
78
|
+
isExpanded(item: TItem): boolean;
|
|
79
|
+
dataProvider: DataProvider<TItem, TDataProviderParams>;
|
|
80
|
+
dataProviderParams(): TDataProviderParams;
|
|
81
|
+
},
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The total number of items, including items from expanded sub-caches.
|
|
86
|
+
*/
|
|
87
|
+
get flatSize(): number;
|
|
88
|
+
|
|
89
|
+
hostConnected(): void;
|
|
90
|
+
|
|
91
|
+
hostDisconnected(): void;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Whether the root cache or any of its decendant caches have pending requests.
|
|
95
|
+
*/
|
|
96
|
+
isLoading(): boolean;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Sets the size for the root cache and recalculates the flattened size.
|
|
100
|
+
*/
|
|
101
|
+
setSize(size: number): void;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Sets the page size and clears the cache.
|
|
105
|
+
*/
|
|
106
|
+
setPageSize(pageSize: number): void;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Sets the data provider callback and clears the cache.
|
|
110
|
+
*/
|
|
111
|
+
setDataProvider(dataProvider: DataProvider<TItem, TDataProviderParams>): void;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Recalculates the flattened size.
|
|
115
|
+
*/
|
|
116
|
+
recalculateFlatSize(): void;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Clears the cache.
|
|
120
|
+
*/
|
|
121
|
+
clearCache(): void;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Returns context for the given flattened index, including:
|
|
125
|
+
* - the corresponding cache
|
|
126
|
+
* - the cache level
|
|
127
|
+
* - the corresponding item (if loaded)
|
|
128
|
+
* - the item's index in the cache's items array
|
|
129
|
+
* - the page containing the item
|
|
130
|
+
*/
|
|
131
|
+
getFlatIndexContext(flatIndex: number): ReturnType<typeof getFlatIndexContext<TItem>>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Returns context for the given item, including:
|
|
135
|
+
* - the cache containing the item
|
|
136
|
+
* - the cache level
|
|
137
|
+
* - the item
|
|
138
|
+
* - the item's index in the cache's items array
|
|
139
|
+
* - the item's flattened index
|
|
140
|
+
* - the item's sub-cache (if exists)
|
|
141
|
+
* - the page containing the item
|
|
142
|
+
*
|
|
143
|
+
* If the item isn't found, the method returns undefined.
|
|
144
|
+
*/
|
|
145
|
+
getItemContext(item: TItem): ReturnType<typeof getItemContext<TItem>>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Returns the flattened index for the item that the given indexes point to.
|
|
149
|
+
* Each index in the path array points to a sub-item of the previous index.
|
|
150
|
+
* Using `Infinity` as an index will point to the last item on the level.
|
|
151
|
+
*/
|
|
152
|
+
getFlatIndexByPath(path: number[]): ReturnType<typeof getFlatIndexByPath<TItem>>;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Requests the data provider to load the page with the item corresponding
|
|
156
|
+
* to the given flattened index. If the item is already loaded, the method
|
|
157
|
+
* returns immediatelly.
|
|
158
|
+
*/
|
|
159
|
+
ensureFlatIndexLoaded(flatIndex: number): void;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Creates a sub-cache for the item corresponding to the given flattened index and
|
|
163
|
+
* requests the data provider to load the first page into the created sub-cache.
|
|
164
|
+
* If the sub-cache already exists, the method returns immediatelly.
|
|
165
|
+
*/
|
|
166
|
+
ensureFlatIndexHierarchy(flatIndex: number): void;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Loads the first page into the root cache.
|
|
170
|
+
*/
|
|
171
|
+
loadFirstPage(): void;
|
|
172
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2021 - 2023 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import { Cache } from './cache.js';
|
|
7
|
+
import { getFlatIndexByPath, getFlatIndexContext, getItemContext } from './helpers.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A controller that stores and manages items loaded with a data provider.
|
|
11
|
+
*/
|
|
12
|
+
export class DataProviderController extends EventTarget {
|
|
13
|
+
/**
|
|
14
|
+
* The controller host element.
|
|
15
|
+
*
|
|
16
|
+
* @param {HTMLElement}
|
|
17
|
+
*/
|
|
18
|
+
host;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A callback that returns data based on the passed params such as
|
|
22
|
+
* `page`, `pageSize`, `parentItem`, etc.
|
|
23
|
+
*/
|
|
24
|
+
dataProvider;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A callback that returns additional params that need to be passed
|
|
28
|
+
* to the data provider callback with every request.
|
|
29
|
+
*/
|
|
30
|
+
dataProviderParams;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A number of items in the root cache.
|
|
34
|
+
*
|
|
35
|
+
* @type {number}
|
|
36
|
+
*/
|
|
37
|
+
size;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A number of items to display per page.
|
|
41
|
+
*
|
|
42
|
+
* @type {number}
|
|
43
|
+
*/
|
|
44
|
+
pageSize;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A callback that returns whether the given item is expanded.
|
|
48
|
+
*
|
|
49
|
+
* @type {(item: unknown) => boolean}
|
|
50
|
+
*/
|
|
51
|
+
isExpanded;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A callback that returns the id for the given item and that
|
|
55
|
+
* is used when checking object items for equality.
|
|
56
|
+
*
|
|
57
|
+
* @type { (item: unknown) => unknown}
|
|
58
|
+
*/
|
|
59
|
+
getItemId;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A reference to the root cache instance.
|
|
63
|
+
*
|
|
64
|
+
* @param {Cache}
|
|
65
|
+
*/
|
|
66
|
+
rootCache;
|
|
67
|
+
|
|
68
|
+
constructor(host, { size, pageSize, isExpanded, getItemId, dataProvider, dataProviderParams }) {
|
|
69
|
+
super();
|
|
70
|
+
this.host = host;
|
|
71
|
+
this.size = size;
|
|
72
|
+
this.pageSize = pageSize;
|
|
73
|
+
this.getItemId = getItemId;
|
|
74
|
+
this.isExpanded = isExpanded;
|
|
75
|
+
this.dataProvider = dataProvider;
|
|
76
|
+
this.dataProviderParams = dataProviderParams;
|
|
77
|
+
this.rootCache = this.__createRootCache();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The total number of items, including items from expanded sub-caches.
|
|
82
|
+
*/
|
|
83
|
+
get flatSize() {
|
|
84
|
+
return this.rootCache.flatSize;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** @private */
|
|
88
|
+
get __cacheContext() {
|
|
89
|
+
return {
|
|
90
|
+
isExpanded: this.isExpanded,
|
|
91
|
+
// The controller instance is needed to ensure deprecated cache methods work.
|
|
92
|
+
__controller: this,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Whether the root cache or any of its decendant caches have pending requests.
|
|
98
|
+
*
|
|
99
|
+
* @return {boolean}
|
|
100
|
+
*/
|
|
101
|
+
isLoading() {
|
|
102
|
+
return this.rootCache.isLoading;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Sets the size for the root cache and recalculates the flattened size.
|
|
107
|
+
*
|
|
108
|
+
* @param {number} size
|
|
109
|
+
*/
|
|
110
|
+
setSize(size) {
|
|
111
|
+
this.size = size;
|
|
112
|
+
this.rootCache.size = size;
|
|
113
|
+
this.recalculateFlatSize();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Sets the page size and clears the cache.
|
|
118
|
+
*
|
|
119
|
+
* @param {number} pageSize
|
|
120
|
+
*/
|
|
121
|
+
setPageSize(pageSize) {
|
|
122
|
+
this.pageSize = pageSize;
|
|
123
|
+
this.clearCache();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Sets the data provider callback and clears the cache.
|
|
128
|
+
*
|
|
129
|
+
* @type {Function}
|
|
130
|
+
*/
|
|
131
|
+
setDataProvider(dataProvider) {
|
|
132
|
+
this.dataProvider = dataProvider;
|
|
133
|
+
this.clearCache();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Recalculates the flattened size.
|
|
138
|
+
*/
|
|
139
|
+
recalculateFlatSize() {
|
|
140
|
+
this.rootCache.recalculateFlatSize();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Clears the cache.
|
|
145
|
+
*/
|
|
146
|
+
clearCache() {
|
|
147
|
+
this.rootCache = this.__createRootCache();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Returns context for the given flattened index, including:
|
|
152
|
+
* - the corresponding cache
|
|
153
|
+
* - the cache level
|
|
154
|
+
* - the corresponding item (if loaded)
|
|
155
|
+
* - the item's index in the cache's items array
|
|
156
|
+
* - the page containing the item
|
|
157
|
+
*
|
|
158
|
+
* @param {number} flatIndex
|
|
159
|
+
*/
|
|
160
|
+
getFlatIndexContext(flatIndex) {
|
|
161
|
+
return getFlatIndexContext(this.rootCache, flatIndex);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Returns context for the given item, including:
|
|
166
|
+
* - the cache containing the item
|
|
167
|
+
* - the cache level
|
|
168
|
+
* - the item
|
|
169
|
+
* - the item's index in the cache's items array
|
|
170
|
+
* - the item's flattened index
|
|
171
|
+
* - the item's sub-cache (if exists)
|
|
172
|
+
* - the page containing the item
|
|
173
|
+
*
|
|
174
|
+
* If the item isn't found, the method returns undefined.
|
|
175
|
+
*/
|
|
176
|
+
getItemContext(item) {
|
|
177
|
+
return getItemContext({ getItemId: this.getItemId }, this.rootCache, item);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Returns the flattened index for the item that the given indexes point to.
|
|
182
|
+
* Each index in the path array points to a sub-item of the previous index.
|
|
183
|
+
* Using `Infinity` as an index will point to the last item on the level.
|
|
184
|
+
*
|
|
185
|
+
* @param {number[]} path
|
|
186
|
+
* @return {number}
|
|
187
|
+
*/
|
|
188
|
+
getFlatIndexByPath(path) {
|
|
189
|
+
return getFlatIndexByPath(this.rootCache, path);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Requests the data provider to load the page with the item corresponding
|
|
194
|
+
* to the given flattened index. If the item is already loaded, the method
|
|
195
|
+
* returns immediatelly.
|
|
196
|
+
*
|
|
197
|
+
* @param {number} flatIndex
|
|
198
|
+
*/
|
|
199
|
+
ensureFlatIndexLoaded(flatIndex) {
|
|
200
|
+
const { cache, page, item } = this.getFlatIndexContext(flatIndex);
|
|
201
|
+
|
|
202
|
+
if (!item) {
|
|
203
|
+
this.__loadCachePage(cache, page);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Creates a sub-cache for the item corresponding to the given flattened index and
|
|
209
|
+
* requests the data provider to load the first page into the created sub-cache.
|
|
210
|
+
* If the sub-cache already exists, the method returns immediatelly.
|
|
211
|
+
*
|
|
212
|
+
* @param {number} flatIndex
|
|
213
|
+
*/
|
|
214
|
+
ensureFlatIndexHierarchy(flatIndex) {
|
|
215
|
+
const { cache, item, index } = this.getFlatIndexContext(flatIndex);
|
|
216
|
+
|
|
217
|
+
if (item && this.isExpanded(item) && !cache.getSubCache(index)) {
|
|
218
|
+
const subCache = cache.createSubCache(index);
|
|
219
|
+
this.__loadCachePage(subCache, 0);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Loads the first page into the root cache.
|
|
225
|
+
*/
|
|
226
|
+
loadFirstPage() {
|
|
227
|
+
this.__loadCachePage(this.rootCache, 0);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** @private */
|
|
231
|
+
__createRootCache() {
|
|
232
|
+
return new Cache(this.__cacheContext, this.pageSize, this.size);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** @private */
|
|
236
|
+
__loadCachePage(cache, page) {
|
|
237
|
+
if (!this.dataProvider || cache.pendingRequests[page]) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
let params = {
|
|
242
|
+
page,
|
|
243
|
+
pageSize: this.pageSize,
|
|
244
|
+
parentItem: cache.parentItem,
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
if (this.dataProviderParams) {
|
|
248
|
+
params = { ...params, ...this.dataProviderParams() };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const callback = (items, size) => {
|
|
252
|
+
if (size !== undefined) {
|
|
253
|
+
cache.size = size;
|
|
254
|
+
} else if (params.parentItem) {
|
|
255
|
+
cache.size = items.length;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
cache.setPage(page, items);
|
|
259
|
+
|
|
260
|
+
this.recalculateFlatSize();
|
|
261
|
+
|
|
262
|
+
this.dispatchEvent(new CustomEvent('page-received'));
|
|
263
|
+
|
|
264
|
+
delete cache.pendingRequests[page];
|
|
265
|
+
|
|
266
|
+
this.dispatchEvent(new CustomEvent('page-loaded'));
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
cache.pendingRequests[page] = callback;
|
|
270
|
+
|
|
271
|
+
this.dispatchEvent(new CustomEvent('page-requested'));
|
|
272
|
+
|
|
273
|
+
this.dataProvider(params, callback);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2021 - 2023 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import type { Cache } from './cache.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Returns context for the given flattened index, including:
|
|
10
|
+
* - the corresponding cache
|
|
11
|
+
* - the cache level
|
|
12
|
+
* - the corresponding item (if loaded)
|
|
13
|
+
* - the item's index in the cache's items array
|
|
14
|
+
* - the page containing the item
|
|
15
|
+
*/
|
|
16
|
+
export function getFlatIndexContext<TItem>(
|
|
17
|
+
cache: Cache<TItem>,
|
|
18
|
+
flatIndex: number,
|
|
19
|
+
level: number,
|
|
20
|
+
): {
|
|
21
|
+
cache: Cache<TItem>;
|
|
22
|
+
item: TItem | undefined;
|
|
23
|
+
index: number;
|
|
24
|
+
page: number;
|
|
25
|
+
level: number;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns context for the given item, including:
|
|
30
|
+
* - the cache containing the item
|
|
31
|
+
* - the cache level
|
|
32
|
+
* - the item
|
|
33
|
+
* - the item's index in the cache's items array
|
|
34
|
+
* - the item's flattened index
|
|
35
|
+
* - the item's sub-cache (if exists)
|
|
36
|
+
* - the page containing the item
|
|
37
|
+
*
|
|
38
|
+
* If the item isn't found, the method returns undefined.
|
|
39
|
+
*/
|
|
40
|
+
export function getItemContext<TItem>(
|
|
41
|
+
context: { getItemId(item: TItem): unknown },
|
|
42
|
+
cache: Cache<TItem>,
|
|
43
|
+
targetItem: TItem,
|
|
44
|
+
level: number,
|
|
45
|
+
levelFlatIndex: number,
|
|
46
|
+
):
|
|
47
|
+
| {
|
|
48
|
+
level: number;
|
|
49
|
+
item: TItem;
|
|
50
|
+
index: number;
|
|
51
|
+
page: number;
|
|
52
|
+
flatIndex: number;
|
|
53
|
+
cache: Cache<TItem>;
|
|
54
|
+
subCache: Cache<TItem> | undefined;
|
|
55
|
+
}
|
|
56
|
+
| undefined;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Recursively returns the globally flat index of the item the given indexes point to.
|
|
60
|
+
* Each index in the array points to a sub-item of the previous index.
|
|
61
|
+
* Using `Infinity` as an index will point to the last item on the level.
|
|
62
|
+
*/
|
|
63
|
+
export function getFlatIndexByPath<TItem>(cache: Cache<TItem>, path: number[], flatIndex: number): number;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2021 - 2023 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {import('./cache.js').Cache} Cache
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Returns context for the given flattened index, including:
|
|
13
|
+
* - the corresponding cache
|
|
14
|
+
* - the cache level
|
|
15
|
+
* - the corresponding item (if loaded)
|
|
16
|
+
* - the item's index in the cache's items array
|
|
17
|
+
* - the page containing the item
|
|
18
|
+
*
|
|
19
|
+
* @param {Cache} cache
|
|
20
|
+
* @param {number} flatIndex
|
|
21
|
+
*/
|
|
22
|
+
export function getFlatIndexContext(cache, flatIndex, level = 0) {
|
|
23
|
+
let levelIndex = flatIndex;
|
|
24
|
+
|
|
25
|
+
for (const subCache of cache.subCaches) {
|
|
26
|
+
const index = subCache.parentCacheIndex;
|
|
27
|
+
if (levelIndex <= index) {
|
|
28
|
+
break;
|
|
29
|
+
} else if (levelIndex <= index + subCache.flatSize) {
|
|
30
|
+
return getFlatIndexContext(subCache, levelIndex - index - 1, level + 1);
|
|
31
|
+
}
|
|
32
|
+
levelIndex -= subCache.flatSize;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
cache,
|
|
37
|
+
item: cache.items[levelIndex],
|
|
38
|
+
index: levelIndex,
|
|
39
|
+
page: Math.floor(levelIndex / cache.pageSize),
|
|
40
|
+
level,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Returns context for the given item, including:
|
|
46
|
+
* - the cache containing the item
|
|
47
|
+
* - the cache level
|
|
48
|
+
* - the item
|
|
49
|
+
* - the item's index in the cache's items array
|
|
50
|
+
* - the item's flattened index
|
|
51
|
+
* - the item's sub-cache (if exists)
|
|
52
|
+
* - the page containing the item
|
|
53
|
+
*
|
|
54
|
+
* If the item isn't found, the method returns undefined.
|
|
55
|
+
*
|
|
56
|
+
* @param {Cache} cache
|
|
57
|
+
* @param {{ getItemId: (item: unknown) => unknown}} context
|
|
58
|
+
* @param {Cache} cache
|
|
59
|
+
* @param {unknown} targetItem
|
|
60
|
+
* @param {number} level
|
|
61
|
+
* @param {number} levelFlatIndex
|
|
62
|
+
*/
|
|
63
|
+
export function getItemContext({ getItemId }, cache, targetItem, level = 0, levelFlatIndex = 0) {
|
|
64
|
+
// Start looking in this cache
|
|
65
|
+
for (let index = 0; index < cache.items.length; index++) {
|
|
66
|
+
const item = cache.items[index];
|
|
67
|
+
if (!!item && getItemId(item) === getItemId(targetItem)) {
|
|
68
|
+
return {
|
|
69
|
+
cache,
|
|
70
|
+
level,
|
|
71
|
+
item,
|
|
72
|
+
index,
|
|
73
|
+
page: Math.floor(index / cache.pageSize),
|
|
74
|
+
subCache: cache.getSubCache(index),
|
|
75
|
+
flatIndex: levelFlatIndex + cache.getFlatIndex(index),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Look through sub-caches
|
|
81
|
+
for (const subCache of cache.subCaches) {
|
|
82
|
+
const parentItemFlatIndex = levelFlatIndex + cache.getFlatIndex(subCache.parentCacheIndex);
|
|
83
|
+
const result = getItemContext({ getItemId }, subCache, targetItem, level + 1, parentItemFlatIndex + 1);
|
|
84
|
+
if (result) {
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Recursively returns the globally flat index of the item the given indexes point to.
|
|
92
|
+
* Each index in the array points to a sub-item of the previous index.
|
|
93
|
+
* Using `Infinity` as an index will point to the last item on the level.
|
|
94
|
+
*
|
|
95
|
+
* @param {Cache} cache
|
|
96
|
+
* @param {number[]} path
|
|
97
|
+
* @param {number} flatIndex
|
|
98
|
+
* @return {number}
|
|
99
|
+
*/
|
|
100
|
+
export function getFlatIndexByPath(cache, [levelIndex, ...subIndexes], flatIndex = 0) {
|
|
101
|
+
if (levelIndex === Infinity) {
|
|
102
|
+
// Treat Infinity as the last index on the level
|
|
103
|
+
levelIndex = cache.size - 1;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const flatIndexOnLevel = cache.getFlatIndex(levelIndex);
|
|
107
|
+
const subCache = cache.getSubCache(levelIndex);
|
|
108
|
+
if (subCache && subCache.flatSize > 0 && subIndexes.length) {
|
|
109
|
+
return getFlatIndexByPath(subCache, subIndexes, flatIndex + flatIndexOnLevel + 1);
|
|
110
|
+
}
|
|
111
|
+
return flatIndex + flatIndexOnLevel;
|
|
112
|
+
}
|
package/src/define.js
CHANGED
|
@@ -3,9 +3,16 @@
|
|
|
3
3
|
* Copyright (c) 2021 - 2023 Vaadin Ltd.
|
|
4
4
|
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
5
|
*/
|
|
6
|
+
|
|
6
7
|
export function defineCustomElement(CustomElement) {
|
|
7
8
|
const defined = customElements.get(CustomElement.is);
|
|
8
9
|
if (!defined) {
|
|
10
|
+
Object.defineProperty(CustomElement, 'version', {
|
|
11
|
+
get() {
|
|
12
|
+
return '24.3.0-alpha10';
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
|
|
9
16
|
customElements.define(CustomElement.is, CustomElement);
|
|
10
17
|
} else {
|
|
11
18
|
const definedVersion = defined.version;
|
package/src/element-mixin.js
CHANGED
|
@@ -44,10 +44,6 @@ const registered = new Set();
|
|
|
44
44
|
*/
|
|
45
45
|
export const ElementMixin = (superClass) =>
|
|
46
46
|
class VaadinElementMixin extends DirMixin(superClass) {
|
|
47
|
-
static get version() {
|
|
48
|
-
return '24.2.3';
|
|
49
|
-
}
|
|
50
|
-
|
|
51
47
|
/** @protected */
|
|
52
48
|
static finalize() {
|
|
53
49
|
super.finalize();
|
|
@@ -82,10 +82,24 @@ export class IronListAdapter {
|
|
|
82
82
|
return this.lastVisibleIndex + this._vidxOffset;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
__hasPlaceholders() {
|
|
86
|
+
return this.__getVisibleElements().some((el) => el.__virtualizerPlaceholder);
|
|
87
|
+
}
|
|
88
|
+
|
|
85
89
|
scrollToIndex(index) {
|
|
86
90
|
if (typeof index !== 'number' || isNaN(index) || this.size === 0 || !this.scrollTarget.offsetHeight) {
|
|
87
91
|
return;
|
|
88
92
|
}
|
|
93
|
+
delete this.__pendingScrollToIndex;
|
|
94
|
+
|
|
95
|
+
if (this._physicalCount <= 3 /* iron-list-core.DEFAULT_PHYSICAL_COUNT */) {
|
|
96
|
+
// The condition here is a performance improvement to avoid an unnecessary
|
|
97
|
+
// re-render when the physical item pool is already covered.
|
|
98
|
+
|
|
99
|
+
// Finish rendering at the current scroll position before scrolling
|
|
100
|
+
this.flush();
|
|
101
|
+
}
|
|
102
|
+
|
|
89
103
|
index = this._clamp(index, 0, this.size - 1);
|
|
90
104
|
|
|
91
105
|
const visibleElementCount = this.__getVisibleElements().length;
|
|
@@ -113,9 +127,16 @@ export class IronListAdapter {
|
|
|
113
127
|
this._scrollTop -= this.__getIndexScrollOffset(index) || 0;
|
|
114
128
|
}
|
|
115
129
|
this._scrollHandler();
|
|
130
|
+
|
|
131
|
+
if (this.__hasPlaceholders()) {
|
|
132
|
+
// After rendering synchronously, there are still placeholders in the DOM.
|
|
133
|
+
// Try again after the next elements update.
|
|
134
|
+
this.__pendingScrollToIndex = index;
|
|
135
|
+
}
|
|
116
136
|
}
|
|
117
137
|
|
|
118
138
|
flush() {
|
|
139
|
+
const startPhysicalCount = this._physicalCount;
|
|
119
140
|
// The scroll target is hidden.
|
|
120
141
|
if (this.scrollTarget.offsetHeight === 0) {
|
|
121
142
|
return;
|
|
@@ -133,6 +154,11 @@ export class IronListAdapter {
|
|
|
133
154
|
if (this.__debouncerWheelAnimationFrame) {
|
|
134
155
|
this.__debouncerWheelAnimationFrame.flush();
|
|
135
156
|
}
|
|
157
|
+
|
|
158
|
+
if (this._physicalCount !== startPhysicalCount) {
|
|
159
|
+
// Flushing again until physical count stabilizes fixes https://github.com/vaadin/flow-components/issues/5595#issuecomment-1770278913
|
|
160
|
+
this.flush();
|
|
161
|
+
}
|
|
136
162
|
}
|
|
137
163
|
|
|
138
164
|
update(startIndex = 0, endIndex = this.size - 1) {
|
|
@@ -199,8 +225,9 @@ export class IronListAdapter {
|
|
|
199
225
|
|
|
200
226
|
__updateElement(el, index, forceSameIndexUpdates) {
|
|
201
227
|
// Clean up temporary placeholder sizing
|
|
202
|
-
if (el.
|
|
228
|
+
if (el.__virtualizerPlaceholder) {
|
|
203
229
|
el.style.paddingTop = '';
|
|
230
|
+
el.__virtualizerPlaceholder = false;
|
|
204
231
|
}
|
|
205
232
|
|
|
206
233
|
if (!this.__preventElementUpdates && (el.__lastUpdatedIndex !== index || forceSameIndexUpdates)) {
|
|
@@ -224,6 +251,7 @@ export class IronListAdapter {
|
|
|
224
251
|
// Assign a temporary placeholder sizing to elements that would otherwise end up having
|
|
225
252
|
// no height.
|
|
226
253
|
el.style.paddingTop = `${this.__placeholderHeight}px`;
|
|
254
|
+
el.__virtualizerPlaceholder = true;
|
|
227
255
|
|
|
228
256
|
// Manually schedule the resize handler to make sure the placeholder padding is
|
|
229
257
|
// cleared in case the resize observer never triggers.
|
|
@@ -241,6 +269,10 @@ export class IronListAdapter {
|
|
|
241
269
|
this.__placeholderHeight = Math.round(filteredHeights.reduce((a, b) => a + b, 0) / filteredHeights.length);
|
|
242
270
|
}
|
|
243
271
|
});
|
|
272
|
+
|
|
273
|
+
if (this.__pendingScrollToIndex !== undefined && !this.__hasPlaceholders()) {
|
|
274
|
+
this.scrollToIndex(this.__pendingScrollToIndex);
|
|
275
|
+
}
|
|
244
276
|
}
|
|
245
277
|
|
|
246
278
|
__getIndexScrollOffset(index) {
|