@vaadin/component-base 24.3.0-alpha2 → 24.3.0-alpha4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/component-base",
3
- "version": "24.3.0-alpha2",
3
+ "version": "24.3.0-alpha4",
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": "0fd437292fa2a2f65e29b424d2456909ad2d684b"
45
+ "gitHead": "d6884bc788b98a4e8dfd14f3f399c6eb9022bd44"
46
46
  }
@@ -5,6 +5,7 @@
5
5
  */
6
6
  import type { ReactiveController } from 'lit';
7
7
  import type { Cache } from './cache.js';
8
+ import type { getFlatIndexByPath, getFlatIndexContext, getItemContext } from './helpers.js';
8
9
 
9
10
  type DataProviderDefaultParams = {
10
11
  page: number;
@@ -57,6 +58,12 @@ export class DataProviderController<TItem, TDataProviderParams extends Record<st
57
58
  */
58
59
  isExpanded: (item: TItem) => boolean;
59
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
+
60
67
  /**
61
68
  * A reference to the root cache instance.
62
69
  */
@@ -67,6 +74,7 @@ export class DataProviderController<TItem, TDataProviderParams extends Record<st
67
74
  config: {
68
75
  size?: number;
69
76
  pageSize: number;
77
+ getItemId(item: TItem): unknown;
70
78
  isExpanded(item: TItem): boolean;
71
79
  dataProvider: DataProvider<TItem, TDataProviderParams>;
72
80
  dataProviderParams(): TDataProviderParams;
@@ -115,25 +123,33 @@ export class DataProviderController<TItem, TDataProviderParams extends Record<st
115
123
  /**
116
124
  * Returns context for the given flattened index, including:
117
125
  * - 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
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.
122
144
  */
123
- getFlatIndexContext(flatIndex: number): {
124
- cache: Cache<TItem>;
125
- item: TItem | undefined;
126
- index: number;
127
- page: number;
128
- level: number;
129
- };
145
+ getItemContext(item: TItem): ReturnType<typeof getItemContext<TItem>>;
130
146
 
131
147
  /**
132
148
  * Returns the flattened index for the item that the given indexes point to.
133
149
  * Each index in the path array points to a sub-item of the previous index.
134
150
  * Using `Infinity` as an index will point to the last item on the level.
135
151
  */
136
- getFlatIndexByPath(path: number[]): number;
152
+ getFlatIndexByPath(path: number[]): ReturnType<typeof getFlatIndexByPath<TItem>>;
137
153
 
138
154
  /**
139
155
  * Requests the data provider to load the page with the item corresponding
@@ -4,7 +4,7 @@
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { Cache } from './cache.js';
7
- import { getFlatIndexByPath, getFlatIndexContext } from './helpers.js';
7
+ import { getFlatIndexByPath, getFlatIndexContext, getItemContext } from './helpers.js';
8
8
 
9
9
  /**
10
10
  * A controller that stores and manages items loaded with a data provider.
@@ -50,6 +50,14 @@ export class DataProviderController extends EventTarget {
50
50
  */
51
51
  isExpanded;
52
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
+
53
61
  /**
54
62
  * A reference to the root cache instance.
55
63
  *
@@ -57,11 +65,12 @@ export class DataProviderController extends EventTarget {
57
65
  */
58
66
  rootCache;
59
67
 
60
- constructor(host, { size, pageSize, isExpanded, dataProvider, dataProviderParams }) {
68
+ constructor(host, { size, pageSize, isExpanded, getItemId, dataProvider, dataProviderParams }) {
61
69
  super();
62
70
  this.host = host;
63
71
  this.size = size;
64
72
  this.pageSize = pageSize;
73
+ this.getItemId = getItemId;
65
74
  this.isExpanded = isExpanded;
66
75
  this.dataProvider = dataProvider;
67
76
  this.dataProviderParams = dataProviderParams;
@@ -141,15 +150,33 @@ export class DataProviderController extends EventTarget {
141
150
  /**
142
151
  * Returns context for the given flattened index, including:
143
152
  * - the corresponding cache
144
- * - the associated item (if loaded)
145
- * - the corresponding index in the cache's items array.
146
- * - the page containing the index.
147
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
148
159
  */
149
160
  getFlatIndexContext(flatIndex) {
150
161
  return getFlatIndexContext(this.rootCache, flatIndex);
151
162
  }
152
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
+
153
180
  /**
154
181
  * Returns the flattened index for the item that the given indexes point to.
155
182
  * Each index in the path array points to a sub-item of the previous index.
@@ -3,30 +3,61 @@
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 type { Cache } from './cache.js';
6
7
 
7
8
  /**
8
9
  * Returns context for the given flattened index, including:
9
10
  * - 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
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
14
15
  */
15
- export function getFlatIndexContext(
16
- cache: Cache,
16
+ export function getFlatIndexContext<TItem>(
17
+ cache: Cache<TItem>,
17
18
  flatIndex: number,
18
19
  level: number,
19
20
  ): {
20
- cache: Cache;
21
- item: unknown | undefined;
21
+ cache: Cache<TItem>;
22
+ item: TItem | undefined;
22
23
  index: number;
23
24
  page: number;
24
25
  level: number;
25
26
  };
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
+
27
58
  /**
28
59
  * Recursively returns the globally flat index of the item the given indexes point to.
29
60
  * Each index in the array points to a sub-item of the previous index.
30
61
  * Using `Infinity` as an index will point to the last item on the level.
31
62
  */
32
- export function getFlatIndexByPath(cache: Cache, path: number[], flatIndex: number): number;
63
+ export function getFlatIndexByPath<TItem>(cache: Cache<TItem>, path: number[], flatIndex: number): number;
@@ -4,17 +4,20 @@
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
 
7
+ /**
8
+ * @typedef {import('./cache.js').Cache} Cache
9
+ */
10
+
7
11
  /**
8
12
  * Returns context for the given flattened index, including:
9
13
  * - 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
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
14
18
  *
15
- * @param {import('./cache.js').Cache} cache
19
+ * @param {Cache} cache
16
20
  * @param {number} flatIndex
17
- * @return {{ cache: Cache, item: object | undefined, index: number, page: number, level: number }}
18
21
  */
19
22
  export function getFlatIndexContext(cache, flatIndex, level = 0) {
20
23
  let levelIndex = flatIndex;
@@ -38,6 +41,52 @@ export function getFlatIndexContext(cache, flatIndex, level = 0) {
38
41
  };
39
42
  }
40
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
+
41
90
  /**
42
91
  * Recursively returns the globally flat index of the item the given indexes point to.
43
92
  * Each index in the array points to a sub-item of the previous index.
package/src/define.js CHANGED
@@ -9,7 +9,7 @@ export function defineCustomElement(CustomElement) {
9
9
  if (!defined) {
10
10
  Object.defineProperty(CustomElement, 'version', {
11
11
  get() {
12
- return '24.3.0-alpha2';
12
+ return '24.3.0-alpha4';
13
13
  },
14
14
  });
15
15