@vaadin/component-base 24.3.0-alpha1 → 24.3.0-alpha2
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 +2 -2
- package/src/data-provider-controller/cache.d.ts +146 -0
- package/src/data-provider-controller/cache.js +90 -12
- package/src/data-provider-controller/data-provider-controller.d.ts +156 -0
- package/src/data-provider-controller/data-provider-controller.js +21 -14
- package/src/data-provider-controller/helpers.d.ts +32 -0
- package/src/data-provider-controller/helpers.js +3 -3
- package/src/define.js +7 -0
- package/src/element-mixin.js +0 -4
- package/src/virtualizer-iron-list-adapter.js +27 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vaadin/component-base",
|
|
3
|
-
"version": "24.3.0-
|
|
3
|
+
"version": "24.3.0-alpha2",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -42,5 +42,5 @@
|
|
|
42
42
|
"@vaadin/testing-helpers": "^0.5.0",
|
|
43
43
|
"sinon": "^13.0.2"
|
|
44
44
|
},
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "0fd437292fa2a2f65e29b424d2456909ad2d684b"
|
|
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
|
+
}
|
|
@@ -3,12 +3,15 @@
|
|
|
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
|
+
import { getFlatIndexContext } from './helpers.js';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* A class that stores items with their associated sub-caches.
|
|
9
10
|
*/
|
|
10
11
|
export class Cache {
|
|
11
12
|
/**
|
|
13
|
+
* A context object.
|
|
14
|
+
*
|
|
12
15
|
* @type {{ isExpanded: (item: unknown) => boolean }}
|
|
13
16
|
*/
|
|
14
17
|
context;
|
|
@@ -38,9 +41,9 @@ export class Cache {
|
|
|
38
41
|
* A map where the key is a requested page and the value is a callback
|
|
39
42
|
* that will be called with data once the request is complete.
|
|
40
43
|
*
|
|
41
|
-
* @type {
|
|
44
|
+
* @type {Record<number, Function>}
|
|
42
45
|
*/
|
|
43
|
-
pendingRequests =
|
|
46
|
+
pendingRequests = {};
|
|
44
47
|
|
|
45
48
|
/**
|
|
46
49
|
* A map where the key is the index of an item in the `items` array
|
|
@@ -61,7 +64,7 @@ export class Cache {
|
|
|
61
64
|
* @type {number}
|
|
62
65
|
* @private
|
|
63
66
|
*/
|
|
64
|
-
|
|
67
|
+
__flatSize = 0;
|
|
65
68
|
|
|
66
69
|
/**
|
|
67
70
|
* @param {Cache['context']} context
|
|
@@ -76,7 +79,7 @@ export class Cache {
|
|
|
76
79
|
this.size = size || 0;
|
|
77
80
|
this.parentCache = parentCache;
|
|
78
81
|
this.parentCacheIndex = parentCacheIndex;
|
|
79
|
-
this.
|
|
82
|
+
this.__flatSize = size || 0;
|
|
80
83
|
}
|
|
81
84
|
|
|
82
85
|
/**
|
|
@@ -104,7 +107,7 @@ export class Cache {
|
|
|
104
107
|
* @return {boolean}
|
|
105
108
|
*/
|
|
106
109
|
get isLoading() {
|
|
107
|
-
if (this.pendingRequests.
|
|
110
|
+
if (Object.keys(this.pendingRequests).length > 0) {
|
|
108
111
|
return true;
|
|
109
112
|
}
|
|
110
113
|
|
|
@@ -116,20 +119,33 @@ export class Cache {
|
|
|
116
119
|
*
|
|
117
120
|
* @return {number}
|
|
118
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
|
+
*/
|
|
119
132
|
get effectiveSize() {
|
|
120
|
-
|
|
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;
|
|
121
137
|
}
|
|
122
138
|
|
|
123
139
|
/**
|
|
124
|
-
* Recalculates the
|
|
140
|
+
* Recalculates the flattened size for the cache and its descendant caches recursively.
|
|
125
141
|
*/
|
|
126
|
-
|
|
127
|
-
this.
|
|
142
|
+
recalculateFlatSize() {
|
|
143
|
+
this.__flatSize =
|
|
128
144
|
!this.parentItem || this.context.isExpanded(this.parentItem)
|
|
129
145
|
? this.size +
|
|
130
146
|
this.subCaches.reduce((total, subCache) => {
|
|
131
|
-
subCache.
|
|
132
|
-
return total + subCache.
|
|
147
|
+
subCache.recalculateFlatSize();
|
|
148
|
+
return total + subCache.flatSize;
|
|
133
149
|
}, 0)
|
|
134
150
|
: 0;
|
|
135
151
|
}
|
|
@@ -201,7 +217,69 @@ export class Cache {
|
|
|
201
217
|
|
|
202
218
|
return this.subCaches.reduce((prev, subCache) => {
|
|
203
219
|
const index = subCache.parentCacheIndex;
|
|
204
|
-
return clampedIndex > index ? prev + subCache.
|
|
220
|
+
return clampedIndex > index ? prev + subCache.flatSize : prev;
|
|
205
221
|
}, clampedIndex);
|
|
206
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
|
+
}
|
|
207
285
|
}
|
|
@@ -0,0 +1,156 @@
|
|
|
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
|
+
|
|
9
|
+
type DataProviderDefaultParams = {
|
|
10
|
+
page: number;
|
|
11
|
+
pageSize: number;
|
|
12
|
+
parentItem?: unknown;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type DataProviderCallback<TItem> = (items: TItem[], size?: number) => void;
|
|
16
|
+
|
|
17
|
+
export type DataProvider<TItem, TDataProviderParams extends Record<string, unknown>> = (
|
|
18
|
+
params: DataProviderDefaultParams & TDataProviderParams,
|
|
19
|
+
callback: DataProviderCallback<TItem>,
|
|
20
|
+
) => void;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A controller that stores and manages items loaded with a data provider.
|
|
24
|
+
*/
|
|
25
|
+
export class DataProviderController<TItem, TDataProviderParams extends Record<string, unknown>>
|
|
26
|
+
implements ReactiveController
|
|
27
|
+
{
|
|
28
|
+
/**
|
|
29
|
+
* The controller host element.
|
|
30
|
+
*/
|
|
31
|
+
host: HTMLElement;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A callback that returns data based on the passed params such as
|
|
35
|
+
* `page`, `pageSize`, `parentItem`, etc.
|
|
36
|
+
*/
|
|
37
|
+
dataProvider: DataProvider<TItem, TDataProviderParams>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A callback that returns additional params that need to be passed
|
|
41
|
+
* to the data provider callback with every request.
|
|
42
|
+
*/
|
|
43
|
+
dataProviderParams: () => TDataProviderParams;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A number of items in the root cache.
|
|
47
|
+
*/
|
|
48
|
+
size?: number;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A number of items to display per page.
|
|
52
|
+
*/
|
|
53
|
+
pageSize: number;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A callback that returns whether the given item is expanded.
|
|
57
|
+
*/
|
|
58
|
+
isExpanded: (item: TItem) => boolean;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A reference to the root cache instance.
|
|
62
|
+
*/
|
|
63
|
+
rootCache: Cache<TItem>;
|
|
64
|
+
|
|
65
|
+
constructor(
|
|
66
|
+
host: HTMLElement,
|
|
67
|
+
config: {
|
|
68
|
+
size?: number;
|
|
69
|
+
pageSize: number;
|
|
70
|
+
isExpanded(item: TItem): boolean;
|
|
71
|
+
dataProvider: DataProvider<TItem, TDataProviderParams>;
|
|
72
|
+
dataProviderParams(): TDataProviderParams;
|
|
73
|
+
},
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The total number of items, including items from expanded sub-caches.
|
|
78
|
+
*/
|
|
79
|
+
get flatSize(): number;
|
|
80
|
+
|
|
81
|
+
hostConnected(): void;
|
|
82
|
+
|
|
83
|
+
hostDisconnected(): void;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Whether the root cache or any of its decendant caches have pending requests.
|
|
87
|
+
*/
|
|
88
|
+
isLoading(): boolean;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Sets the size for the root cache and recalculates the flattened size.
|
|
92
|
+
*/
|
|
93
|
+
setSize(size: number): void;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Sets the page size and clears the cache.
|
|
97
|
+
*/
|
|
98
|
+
setPageSize(pageSize: number): void;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Sets the data provider callback and clears the cache.
|
|
102
|
+
*/
|
|
103
|
+
setDataProvider(dataProvider: DataProvider<TItem, TDataProviderParams>): void;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Recalculates the flattened size.
|
|
107
|
+
*/
|
|
108
|
+
recalculateFlatSize(): void;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Clears the cache.
|
|
112
|
+
*/
|
|
113
|
+
clearCache(): void;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Returns context for the given flattened index, including:
|
|
117
|
+
* - the corresponding cache
|
|
118
|
+
* - the associated item (if loaded)
|
|
119
|
+
* - the corresponding index in the cache's items array.
|
|
120
|
+
* - the page containing the index.
|
|
121
|
+
* - the cache level
|
|
122
|
+
*/
|
|
123
|
+
getFlatIndexContext(flatIndex: number): {
|
|
124
|
+
cache: Cache<TItem>;
|
|
125
|
+
item: TItem | undefined;
|
|
126
|
+
index: number;
|
|
127
|
+
page: number;
|
|
128
|
+
level: number;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Returns the flattened index for the item that the given indexes point to.
|
|
133
|
+
* Each index in the path array points to a sub-item of the previous index.
|
|
134
|
+
* Using `Infinity` as an index will point to the last item on the level.
|
|
135
|
+
*/
|
|
136
|
+
getFlatIndexByPath(path: number[]): number;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Requests the data provider to load the page with the item corresponding
|
|
140
|
+
* to the given flattened index. If the item is already loaded, the method
|
|
141
|
+
* returns immediatelly.
|
|
142
|
+
*/
|
|
143
|
+
ensureFlatIndexLoaded(flatIndex: number): void;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Creates a sub-cache for the item corresponding to the given flattened index and
|
|
147
|
+
* requests the data provider to load the first page into the created sub-cache.
|
|
148
|
+
* If the sub-cache already exists, the method returns immediatelly.
|
|
149
|
+
*/
|
|
150
|
+
ensureFlatIndexHierarchy(flatIndex: number): void;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Loads the first page into the root cache.
|
|
154
|
+
*/
|
|
155
|
+
loadFirstPage(): void;
|
|
156
|
+
}
|
|
@@ -71,13 +71,17 @@ export class DataProviderController extends EventTarget {
|
|
|
71
71
|
/**
|
|
72
72
|
* The total number of items, including items from expanded sub-caches.
|
|
73
73
|
*/
|
|
74
|
-
get
|
|
75
|
-
return this.rootCache.
|
|
74
|
+
get flatSize() {
|
|
75
|
+
return this.rootCache.flatSize;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
/** @private */
|
|
79
79
|
get __cacheContext() {
|
|
80
|
-
return {
|
|
80
|
+
return {
|
|
81
|
+
isExpanded: this.isExpanded,
|
|
82
|
+
// The controller instance is needed to ensure deprecated cache methods work.
|
|
83
|
+
__controller: this,
|
|
84
|
+
};
|
|
81
85
|
}
|
|
82
86
|
|
|
83
87
|
/**
|
|
@@ -90,14 +94,14 @@ export class DataProviderController extends EventTarget {
|
|
|
90
94
|
}
|
|
91
95
|
|
|
92
96
|
/**
|
|
93
|
-
* Sets the size for the root cache and recalculates the
|
|
97
|
+
* Sets the size for the root cache and recalculates the flattened size.
|
|
94
98
|
*
|
|
95
99
|
* @param {number} size
|
|
96
100
|
*/
|
|
97
101
|
setSize(size) {
|
|
98
102
|
this.size = size;
|
|
99
103
|
this.rootCache.size = size;
|
|
100
|
-
this.
|
|
104
|
+
this.recalculateFlatSize();
|
|
101
105
|
}
|
|
102
106
|
|
|
103
107
|
/**
|
|
@@ -121,10 +125,10 @@ export class DataProviderController extends EventTarget {
|
|
|
121
125
|
}
|
|
122
126
|
|
|
123
127
|
/**
|
|
124
|
-
* Recalculates the
|
|
128
|
+
* Recalculates the flattened size.
|
|
125
129
|
*/
|
|
126
|
-
|
|
127
|
-
this.rootCache.
|
|
130
|
+
recalculateFlatSize() {
|
|
131
|
+
this.rootCache.recalculateFlatSize();
|
|
128
132
|
}
|
|
129
133
|
|
|
130
134
|
/**
|
|
@@ -203,17 +207,20 @@ export class DataProviderController extends EventTarget {
|
|
|
203
207
|
|
|
204
208
|
/** @private */
|
|
205
209
|
__loadCachePage(cache, page) {
|
|
206
|
-
if (!this.dataProvider || cache.pendingRequests
|
|
210
|
+
if (!this.dataProvider || cache.pendingRequests[page]) {
|
|
207
211
|
return;
|
|
208
212
|
}
|
|
209
213
|
|
|
210
|
-
|
|
214
|
+
let params = {
|
|
211
215
|
page,
|
|
212
216
|
pageSize: this.pageSize,
|
|
213
217
|
parentItem: cache.parentItem,
|
|
214
|
-
...this.dataProviderParams(),
|
|
215
218
|
};
|
|
216
219
|
|
|
220
|
+
if (this.dataProviderParams) {
|
|
221
|
+
params = { ...params, ...this.dataProviderParams() };
|
|
222
|
+
}
|
|
223
|
+
|
|
217
224
|
const callback = (items, size) => {
|
|
218
225
|
if (size !== undefined) {
|
|
219
226
|
cache.size = size;
|
|
@@ -223,16 +230,16 @@ export class DataProviderController extends EventTarget {
|
|
|
223
230
|
|
|
224
231
|
cache.setPage(page, items);
|
|
225
232
|
|
|
226
|
-
this.
|
|
233
|
+
this.recalculateFlatSize();
|
|
227
234
|
|
|
228
235
|
this.dispatchEvent(new CustomEvent('page-received'));
|
|
229
236
|
|
|
230
|
-
cache.pendingRequests
|
|
237
|
+
delete cache.pendingRequests[page];
|
|
231
238
|
|
|
232
239
|
this.dispatchEvent(new CustomEvent('page-loaded'));
|
|
233
240
|
};
|
|
234
241
|
|
|
235
|
-
cache.pendingRequests
|
|
242
|
+
cache.pendingRequests[page] = callback;
|
|
236
243
|
|
|
237
244
|
this.dispatchEvent(new CustomEvent('page-requested'));
|
|
238
245
|
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
* Returns context for the given flattened index, including:
|
|
9
|
+
* - the corresponding cache
|
|
10
|
+
* - the associated item (if loaded)
|
|
11
|
+
* - the corresponding index in the cache's items array.
|
|
12
|
+
* - the page containing the index.
|
|
13
|
+
* - the cache level
|
|
14
|
+
*/
|
|
15
|
+
export function getFlatIndexContext(
|
|
16
|
+
cache: Cache,
|
|
17
|
+
flatIndex: number,
|
|
18
|
+
level: number,
|
|
19
|
+
): {
|
|
20
|
+
cache: Cache;
|
|
21
|
+
item: unknown | undefined;
|
|
22
|
+
index: number;
|
|
23
|
+
page: number;
|
|
24
|
+
level: number;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Recursively returns the globally flat index of the item the given indexes point to.
|
|
29
|
+
* Each index in the array points to a sub-item of the previous index.
|
|
30
|
+
* Using `Infinity` as an index will point to the last item on the level.
|
|
31
|
+
*/
|
|
32
|
+
export function getFlatIndexByPath(cache: Cache, path: number[], flatIndex: number): number;
|
|
@@ -23,10 +23,10 @@ export function getFlatIndexContext(cache, flatIndex, level = 0) {
|
|
|
23
23
|
const index = subCache.parentCacheIndex;
|
|
24
24
|
if (levelIndex <= index) {
|
|
25
25
|
break;
|
|
26
|
-
} else if (levelIndex <= index + subCache.
|
|
26
|
+
} else if (levelIndex <= index + subCache.flatSize) {
|
|
27
27
|
return getFlatIndexContext(subCache, levelIndex - index - 1, level + 1);
|
|
28
28
|
}
|
|
29
|
-
levelIndex -= subCache.
|
|
29
|
+
levelIndex -= subCache.flatSize;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
return {
|
|
@@ -56,7 +56,7 @@ export function getFlatIndexByPath(cache, [levelIndex, ...subIndexes], flatIndex
|
|
|
56
56
|
|
|
57
57
|
const flatIndexOnLevel = cache.getFlatIndex(levelIndex);
|
|
58
58
|
const subCache = cache.getSubCache(levelIndex);
|
|
59
|
-
if (subCache && subCache.
|
|
59
|
+
if (subCache && subCache.flatSize > 0 && subIndexes.length) {
|
|
60
60
|
return getFlatIndexByPath(subCache, subIndexes, flatIndex + flatIndexOnLevel + 1);
|
|
61
61
|
}
|
|
62
62
|
return flatIndex + flatIndexOnLevel;
|
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-alpha2';
|
|
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.3.0-alpha1';
|
|
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,6 +127,12 @@ 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() {
|
|
@@ -199,8 +219,9 @@ export class IronListAdapter {
|
|
|
199
219
|
|
|
200
220
|
__updateElement(el, index, forceSameIndexUpdates) {
|
|
201
221
|
// Clean up temporary placeholder sizing
|
|
202
|
-
if (el.
|
|
222
|
+
if (el.__virtualizerPlaceholder) {
|
|
203
223
|
el.style.paddingTop = '';
|
|
224
|
+
el.__virtualizerPlaceholder = false;
|
|
204
225
|
}
|
|
205
226
|
|
|
206
227
|
if (!this.__preventElementUpdates && (el.__lastUpdatedIndex !== index || forceSameIndexUpdates)) {
|
|
@@ -224,6 +245,7 @@ export class IronListAdapter {
|
|
|
224
245
|
// Assign a temporary placeholder sizing to elements that would otherwise end up having
|
|
225
246
|
// no height.
|
|
226
247
|
el.style.paddingTop = `${this.__placeholderHeight}px`;
|
|
248
|
+
el.__virtualizerPlaceholder = true;
|
|
227
249
|
|
|
228
250
|
// Manually schedule the resize handler to make sure the placeholder padding is
|
|
229
251
|
// cleared in case the resize observer never triggers.
|
|
@@ -241,6 +263,10 @@ export class IronListAdapter {
|
|
|
241
263
|
this.__placeholderHeight = Math.round(filteredHeights.reduce((a, b) => a + b, 0) / filteredHeights.length);
|
|
242
264
|
}
|
|
243
265
|
});
|
|
266
|
+
|
|
267
|
+
if (this.__pendingScrollToIndex !== undefined && !this.__hasPlaceholders()) {
|
|
268
|
+
this.scrollToIndex(this.__pendingScrollToIndex);
|
|
269
|
+
}
|
|
244
270
|
}
|
|
245
271
|
|
|
246
272
|
__getIndexScrollOffset(index) {
|