@itwin/presentation-frontend 4.0.0-dev.52 → 4.0.0-dev.55

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.
Files changed (88) hide show
  1. package/lib/cjs/presentation-frontend/ConnectivityInformationProvider.d.ts +29 -29
  2. package/lib/cjs/presentation-frontend/ConnectivityInformationProvider.js +47 -47
  3. package/lib/cjs/presentation-frontend/Diagnostics.d.ts +14 -14
  4. package/lib/cjs/presentation-frontend/Diagnostics.js +61 -61
  5. package/lib/cjs/presentation-frontend/FrontendLoggerCategory.d.ts +12 -12
  6. package/lib/cjs/presentation-frontend/FrontendLoggerCategory.js +20 -20
  7. package/lib/cjs/presentation-frontend/IpcRequestsHandler.d.ts +10 -10
  8. package/lib/cjs/presentation-frontend/IpcRequestsHandler.js +41 -41
  9. package/lib/cjs/presentation-frontend/LocalizationHelper.d.ts +10 -10
  10. package/lib/cjs/presentation-frontend/LocalizationHelper.js +31 -31
  11. package/lib/cjs/presentation-frontend/Presentation.d.ts +73 -73
  12. package/lib/cjs/presentation-frontend/Presentation.js +159 -159
  13. package/lib/cjs/presentation-frontend/PresentationManager.d.ts +205 -205
  14. package/lib/cjs/presentation-frontend/PresentationManager.js +438 -438
  15. package/lib/cjs/presentation-frontend/PresentationManager.js.map +1 -1
  16. package/lib/cjs/presentation-frontend/RulesetManager.d.ts +63 -63
  17. package/lib/cjs/presentation-frontend/RulesetManager.js +85 -85
  18. package/lib/cjs/presentation-frontend/RulesetVariablesManager.d.ts +143 -143
  19. package/lib/cjs/presentation-frontend/RulesetVariablesManager.js +195 -195
  20. package/lib/cjs/presentation-frontend/StateTracker.d.ts +38 -38
  21. package/lib/cjs/presentation-frontend/StateTracker.js +128 -128
  22. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesManager.d.ts +132 -132
  23. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesManager.d.ts.map +1 -1
  24. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesManager.js +519 -519
  25. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesManager.js.map +1 -1
  26. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesStorage.d.ts +111 -111
  27. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js +290 -290
  28. package/lib/cjs/presentation-frontend/selection/HiliteSetProvider.d.ts +52 -52
  29. package/lib/cjs/presentation-frontend/selection/HiliteSetProvider.js +97 -97
  30. package/lib/cjs/presentation-frontend/selection/ISelectionProvider.d.ts +19 -19
  31. package/lib/cjs/presentation-frontend/selection/ISelectionProvider.js +9 -9
  32. package/lib/cjs/presentation-frontend/selection/SelectionChangeEvent.d.ts +52 -52
  33. package/lib/cjs/presentation-frontend/selection/SelectionChangeEvent.js +33 -33
  34. package/lib/cjs/presentation-frontend/selection/SelectionHandler.d.ts +103 -103
  35. package/lib/cjs/presentation-frontend/selection/SelectionHandler.js +108 -108
  36. package/lib/cjs/presentation-frontend/selection/SelectionHelper.d.ts +17 -17
  37. package/lib/cjs/presentation-frontend/selection/SelectionHelper.js +40 -40
  38. package/lib/cjs/presentation-frontend/selection/SelectionManager.d.ts +139 -139
  39. package/lib/cjs/presentation-frontend/selection/SelectionManager.js +414 -414
  40. package/lib/cjs/presentation-frontend/selection/SelectionScopesManager.d.ts +59 -59
  41. package/lib/cjs/presentation-frontend/selection/SelectionScopesManager.js +91 -91
  42. package/lib/cjs/presentation-frontend.d.ts +34 -34
  43. package/lib/cjs/presentation-frontend.js +54 -50
  44. package/lib/cjs/presentation-frontend.js.map +1 -1
  45. package/lib/esm/presentation-frontend/ConnectivityInformationProvider.d.ts +29 -29
  46. package/lib/esm/presentation-frontend/ConnectivityInformationProvider.js +43 -43
  47. package/lib/esm/presentation-frontend/Diagnostics.d.ts +14 -14
  48. package/lib/esm/presentation-frontend/Diagnostics.js +56 -56
  49. package/lib/esm/presentation-frontend/FrontendLoggerCategory.d.ts +12 -12
  50. package/lib/esm/presentation-frontend/FrontendLoggerCategory.js +17 -17
  51. package/lib/esm/presentation-frontend/IpcRequestsHandler.d.ts +10 -10
  52. package/lib/esm/presentation-frontend/IpcRequestsHandler.js +37 -37
  53. package/lib/esm/presentation-frontend/LocalizationHelper.d.ts +10 -10
  54. package/lib/esm/presentation-frontend/LocalizationHelper.js +27 -27
  55. package/lib/esm/presentation-frontend/Presentation.d.ts +73 -73
  56. package/lib/esm/presentation-frontend/Presentation.js +155 -155
  57. package/lib/esm/presentation-frontend/PresentationManager.d.ts +205 -205
  58. package/lib/esm/presentation-frontend/PresentationManager.js +433 -433
  59. package/lib/esm/presentation-frontend/PresentationManager.js.map +1 -1
  60. package/lib/esm/presentation-frontend/RulesetManager.d.ts +63 -63
  61. package/lib/esm/presentation-frontend/RulesetManager.js +81 -81
  62. package/lib/esm/presentation-frontend/RulesetVariablesManager.d.ts +143 -143
  63. package/lib/esm/presentation-frontend/RulesetVariablesManager.js +191 -191
  64. package/lib/esm/presentation-frontend/StateTracker.d.ts +38 -38
  65. package/lib/esm/presentation-frontend/StateTracker.js +124 -124
  66. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.d.ts +132 -132
  67. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.d.ts.map +1 -1
  68. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.js +514 -513
  69. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.js.map +1 -1
  70. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.d.ts +111 -111
  71. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js +282 -282
  72. package/lib/esm/presentation-frontend/selection/HiliteSetProvider.d.ts +52 -52
  73. package/lib/esm/presentation-frontend/selection/HiliteSetProvider.js +93 -93
  74. package/lib/esm/presentation-frontend/selection/ISelectionProvider.d.ts +19 -19
  75. package/lib/esm/presentation-frontend/selection/ISelectionProvider.js +8 -8
  76. package/lib/esm/presentation-frontend/selection/SelectionChangeEvent.d.ts +52 -52
  77. package/lib/esm/presentation-frontend/selection/SelectionChangeEvent.js +29 -29
  78. package/lib/esm/presentation-frontend/selection/SelectionHandler.d.ts +103 -103
  79. package/lib/esm/presentation-frontend/selection/SelectionHandler.js +104 -104
  80. package/lib/esm/presentation-frontend/selection/SelectionHelper.d.ts +17 -17
  81. package/lib/esm/presentation-frontend/selection/SelectionHelper.js +36 -36
  82. package/lib/esm/presentation-frontend/selection/SelectionManager.d.ts +139 -139
  83. package/lib/esm/presentation-frontend/selection/SelectionManager.js +409 -409
  84. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.d.ts +59 -59
  85. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.js +85 -85
  86. package/lib/esm/presentation-frontend.d.ts +34 -34
  87. package/lib/esm/presentation-frontend.js +38 -38
  88. package/package.json +15 -15
@@ -1,291 +1,291 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- /** @packageDocumentation
7
- * @module Core
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.BrowserLocalFavoritePropertiesStorage = exports.NoopFavoritePropertiesStorage = exports.OfflineCachingFavoritePropertiesStorage = exports.IModelAppFavoritePropertiesStorage = exports.createFavoritePropertiesStorage = exports.DefaultFavoritePropertiesStorageTypes = exports.FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME = exports.FAVORITE_PROPERTIES_SETTING_NAME = exports.DEPRECATED_PROPERTIES_SETTING_NAMESPACE = exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE = void 0;
11
- const core_bentley_1 = require("@itwin/core-bentley");
12
- const core_common_1 = require("@itwin/core-common");
13
- const core_frontend_1 = require("@itwin/core-frontend");
14
- const presentation_common_1 = require("@itwin/presentation-common");
15
- const ConnectivityInformationProvider_1 = require("../ConnectivityInformationProvider");
16
- /** @internal */
17
- exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE = "imodeljs.presentation";
18
- /** @internal */
19
- exports.DEPRECATED_PROPERTIES_SETTING_NAMESPACE = "Properties";
20
- /** @internal */
21
- exports.FAVORITE_PROPERTIES_SETTING_NAME = "FavoriteProperties";
22
- /** @internal */
23
- exports.FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME = "FavoritePropertiesOrderInfo";
24
- /**
25
- * Available implementations of [[IFavoritePropertiesStorage]].
26
- * @public
27
- */
28
- var DefaultFavoritePropertiesStorageTypes;
29
- (function (DefaultFavoritePropertiesStorageTypes) {
30
- /** A no-op storage that doesn't store or return anything. Used for cases when favorite properties aren't used by the application. */
31
- DefaultFavoritePropertiesStorageTypes[DefaultFavoritePropertiesStorageTypes["Noop"] = 0] = "Noop";
32
- /** A storage that stores favorite properties information in a browser local storage. */
33
- DefaultFavoritePropertiesStorageTypes[DefaultFavoritePropertiesStorageTypes["BrowserLocalStorage"] = 1] = "BrowserLocalStorage";
34
- /** A storage that stores favorite properties in a user preferences storage (see [[IModelApp.userPreferences]]). */
35
- DefaultFavoritePropertiesStorageTypes[DefaultFavoritePropertiesStorageTypes["UserPreferencesStorage"] = 2] = "UserPreferencesStorage";
36
- })(DefaultFavoritePropertiesStorageTypes = exports.DefaultFavoritePropertiesStorageTypes || (exports.DefaultFavoritePropertiesStorageTypes = {}));
37
- /**
38
- * A factory method to create one of the available [[IFavoritePropertiesStorage]] implementations.
39
- * @public
40
- */
41
- function createFavoritePropertiesStorage(type) {
42
- switch (type) {
43
- case DefaultFavoritePropertiesStorageTypes.Noop: return new NoopFavoritePropertiesStorage();
44
- case DefaultFavoritePropertiesStorageTypes.BrowserLocalStorage: return new BrowserLocalFavoritePropertiesStorage();
45
- case DefaultFavoritePropertiesStorageTypes.UserPreferencesStorage: return new OfflineCachingFavoritePropertiesStorage({ impl: new IModelAppFavoritePropertiesStorage() });
46
- }
47
- }
48
- exports.createFavoritePropertiesStorage = createFavoritePropertiesStorage;
49
- /**
50
- * @internal
51
- */
52
- class IModelAppFavoritePropertiesStorage {
53
- async ensureIsSignedIn() {
54
- const accessToken = core_frontend_1.IModelApp.authorizationClient ? await core_frontend_1.IModelApp.authorizationClient.getAccessToken() : "";
55
- if (accessToken)
56
- return { accessToken };
57
- throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.Error, "Current user is not authorized to use the settings service");
58
- }
59
- async loadProperties(iTwinId, imodelId) {
60
- if (!core_frontend_1.IModelApp.userPreferences)
61
- throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.Error, "User preferences service is not set up");
62
- const { accessToken } = await this.ensureIsSignedIn();
63
- let setting = await core_frontend_1.IModelApp.userPreferences.get({
64
- accessToken,
65
- iTwinId,
66
- iModelId: imodelId,
67
- namespace: exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE,
68
- key: exports.FAVORITE_PROPERTIES_SETTING_NAME,
69
- });
70
- if (setting !== undefined)
71
- return new Set(setting);
72
- // try to check the old namespace
73
- setting = await core_frontend_1.IModelApp.userPreferences.get({
74
- accessToken,
75
- iTwinId,
76
- iModelId: imodelId,
77
- namespace: exports.DEPRECATED_PROPERTIES_SETTING_NAMESPACE,
78
- key: exports.FAVORITE_PROPERTIES_SETTING_NAME,
79
- });
80
- if (setting !== undefined && setting.hasOwnProperty("nestedContentInfos") && setting.hasOwnProperty("propertyInfos") && setting.hasOwnProperty("baseFieldInfos"))
81
- return new Set([...setting.nestedContentInfos, ...setting.propertyInfos, ...setting.baseFieldInfos]);
82
- return undefined;
83
- }
84
- async saveProperties(properties, iTwinId, imodelId) {
85
- if (!core_frontend_1.IModelApp.userPreferences)
86
- throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.Error, "User preferences service is not set up");
87
- const { accessToken } = await this.ensureIsSignedIn();
88
- await core_frontend_1.IModelApp.userPreferences.save({
89
- accessToken,
90
- iTwinId,
91
- iModelId: imodelId,
92
- namespace: exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE,
93
- key: exports.FAVORITE_PROPERTIES_SETTING_NAME,
94
- content: Array.from(properties),
95
- });
96
- }
97
- async loadPropertiesOrder(iTwinId, imodelId) {
98
- if (!core_frontend_1.IModelApp.userPreferences)
99
- throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.Error, "User preferences service is not set up");
100
- const { accessToken } = await this.ensureIsSignedIn();
101
- const setting = await core_frontend_1.IModelApp.userPreferences.get({
102
- accessToken,
103
- iTwinId,
104
- iModelId: imodelId,
105
- namespace: exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE,
106
- key: exports.FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME,
107
- });
108
- return setting;
109
- }
110
- async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
111
- if (!core_frontend_1.IModelApp.userPreferences)
112
- throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.Error, "User preferences service is not set up");
113
- const { accessToken } = await this.ensureIsSignedIn();
114
- await core_frontend_1.IModelApp.userPreferences.save({
115
- accessToken,
116
- iTwinId,
117
- iModelId: imodelId,
118
- namespace: exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE,
119
- key: exports.FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME,
120
- content: orderInfos,
121
- });
122
- }
123
- }
124
- exports.IModelAppFavoritePropertiesStorage = IModelAppFavoritePropertiesStorage;
125
- /** @internal */
126
- class OfflineCachingFavoritePropertiesStorage {
127
- constructor(props) {
128
- this._propertiesOfflineCache = new DictionaryWithReservations(iTwinAndIModelIdsKeyComparer);
129
- this._propertiesOrderOfflineCache = new DictionaryWithReservations(iTwinAndIModelIdsKeyComparer);
130
- // eslint-disable-next-line @typescript-eslint/naming-convention
131
- this.onConnectivityStatusChanged = (args) => {
132
- // istanbul ignore else
133
- if (args.status === core_common_1.InternetConnectivityStatus.Online) {
134
- // note: we're copying the cached values to temp arrays because `saveProperties` and `savePropertiesOrder` both
135
- // attempt to modify cache dictionaries
136
- const propertiesCache = new Array();
137
- this._propertiesOfflineCache.forEach((key, value) => propertiesCache.push({ properties: value, iTwinId: key[0], imodelId: key[1] }));
138
- propertiesCache.forEach(async (cached) => this.saveProperties(cached.properties, cached.iTwinId, cached.imodelId));
139
- const ordersCache = new Array();
140
- this._propertiesOrderOfflineCache.forEach((key, value) => ordersCache.push({ order: value, iTwinId: key[0], imodelId: key[1] }));
141
- ordersCache.forEach(async (cached) => this.savePropertiesOrder(cached.order, cached.iTwinId, cached.imodelId));
142
- }
143
- };
144
- this._impl = props.impl;
145
- // istanbul ignore next
146
- this._connectivityInfo = props.connectivityInfo ?? new ConnectivityInformationProvider_1.ConnectivityInformationProvider();
147
- this._connectivityInfo.onInternetConnectivityChanged.addListener(this.onConnectivityStatusChanged);
148
- }
149
- dispose() {
150
- if ((0, core_bentley_1.isIDisposable)(this._connectivityInfo))
151
- this._connectivityInfo.dispose();
152
- }
153
- get impl() { return this._impl; }
154
- async loadProperties(iTwinId, imodelId) {
155
- if (this._connectivityInfo.status === core_common_1.InternetConnectivityStatus.Online) {
156
- try {
157
- return await this._impl.loadProperties(iTwinId, imodelId);
158
- }
159
- catch {
160
- // return from offline cache if the above fails
161
- }
162
- }
163
- return this._propertiesOfflineCache.get([iTwinId, imodelId]);
164
- }
165
- async saveProperties(properties, iTwinId, imodelId) {
166
- const key = [iTwinId, imodelId];
167
- if (this._connectivityInfo.status === core_common_1.InternetConnectivityStatus.Offline) {
168
- this._propertiesOfflineCache.set(key, properties);
169
- return;
170
- }
171
- const reservationId = this._propertiesOfflineCache.reserve(key);
172
- try {
173
- await this._impl.saveProperties(properties, iTwinId, imodelId);
174
- this._propertiesOfflineCache.reservedDelete(key, reservationId);
175
- }
176
- catch {
177
- this._propertiesOfflineCache.reservedSet(key, properties, reservationId);
178
- }
179
- }
180
- async loadPropertiesOrder(iTwinId, imodelId) {
181
- if (this._connectivityInfo.status === core_common_1.InternetConnectivityStatus.Online) {
182
- try {
183
- return await this._impl.loadPropertiesOrder(iTwinId, imodelId);
184
- }
185
- catch {
186
- // return from offline cache if the above fails
187
- }
188
- }
189
- return this._propertiesOrderOfflineCache.get([iTwinId, imodelId]);
190
- }
191
- async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
192
- const key = [iTwinId, imodelId];
193
- if (this._connectivityInfo.status === core_common_1.InternetConnectivityStatus.Offline) {
194
- this._propertiesOrderOfflineCache.set(key, orderInfos);
195
- return;
196
- }
197
- const reservationId = this._propertiesOrderOfflineCache.reserve(key);
198
- try {
199
- await this._impl.savePropertiesOrder(orderInfos, iTwinId, imodelId);
200
- this._propertiesOrderOfflineCache.reservedDelete(key, reservationId);
201
- }
202
- catch {
203
- this._propertiesOrderOfflineCache.reservedSet(key, orderInfos, reservationId);
204
- }
205
- }
206
- }
207
- exports.OfflineCachingFavoritePropertiesStorage = OfflineCachingFavoritePropertiesStorage;
208
- class DictionaryWithReservations {
209
- constructor(compareKeys) {
210
- this._impl = new core_bentley_1.Dictionary(compareKeys);
211
- }
212
- get(key) { return this._impl.get(key)?.value; }
213
- forEach(func) {
214
- this._impl.forEach((key, entry) => {
215
- // istanbul ignore else
216
- if (entry.value)
217
- func(key, entry.value);
218
- });
219
- }
220
- reserve(key) {
221
- const reservationId = core_bentley_1.Guid.createValue();
222
- this._impl.set(key, { lastReservationId: reservationId });
223
- return reservationId;
224
- }
225
- set(key, value) { return this._impl.set(key, { value }); }
226
- reservedSet(key, value, reservationId) {
227
- const entry = this._impl.get(key);
228
- if (entry && entry.lastReservationId === reservationId)
229
- this._impl.set(key, { value });
230
- }
231
- reservedDelete(key, reservationId) {
232
- const entry = this._impl.get(key);
233
- if (entry && entry.lastReservationId === reservationId)
234
- this._impl.delete(key);
235
- }
236
- }
237
- // istanbul ignore next
238
- function iTwinAndIModelIdsKeyComparer(lhs, rhs) {
239
- const iTwinIdCompare = (0, core_bentley_1.compareStrings)(lhs[0] ?? "", rhs[0] ?? "");
240
- return (iTwinIdCompare !== 0) ? iTwinIdCompare : (0, core_bentley_1.compareStrings)(lhs[1] ?? "", rhs[1] ?? "");
241
- }
242
- /** @internal */
243
- class NoopFavoritePropertiesStorage {
244
- // istanbul ignore next
245
- async loadProperties(_iTwinId, _imodelId) { return undefined; }
246
- // istanbul ignore next
247
- async saveProperties(_properties, _iTwinId, _imodelId) { }
248
- // istanbul ignore next
249
- async loadPropertiesOrder(_iTwinId, _imodelId) { return undefined; }
250
- // istanbul ignore next
251
- async savePropertiesOrder(_orderInfos, _iTwinId, _imodelId) { }
252
- }
253
- exports.NoopFavoritePropertiesStorage = NoopFavoritePropertiesStorage;
254
- /** @internal */
255
- class BrowserLocalFavoritePropertiesStorage {
256
- constructor(props) {
257
- // istanbul ignore next
258
- this._localStorage = props?.localStorage ?? window.localStorage;
259
- }
260
- createFavoritesSettingItemKey(iTwinId, imodelId) {
261
- return `${exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE}${exports.FAVORITE_PROPERTIES_SETTING_NAME}?iTwinId=${iTwinId}&imodelId=${imodelId}`;
262
- }
263
- createOrderSettingItemKey(iTwinId, imodelId) {
264
- return `${exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE}${exports.FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME}?iTwinId=${iTwinId}&imodelId=${imodelId}`;
265
- }
266
- async loadProperties(iTwinId, imodelId) {
267
- const value = this._localStorage.getItem(this.createFavoritesSettingItemKey(iTwinId, imodelId));
268
- if (!value)
269
- return undefined;
270
- const properties = JSON.parse(value);
271
- return new Set(properties);
272
- }
273
- async saveProperties(properties, iTwinId, imodelId) {
274
- this._localStorage.setItem(this.createFavoritesSettingItemKey(iTwinId, imodelId), JSON.stringify([...properties]));
275
- }
276
- async loadPropertiesOrder(iTwinId, imodelId) {
277
- const value = this._localStorage.getItem(this.createOrderSettingItemKey(iTwinId, imodelId));
278
- if (!value)
279
- return undefined;
280
- const orderInfos = JSON.parse(value).map((json) => ({
281
- ...json,
282
- orderedTimestamp: new Date(json.orderedTimestamp),
283
- }));
284
- return orderInfos;
285
- }
286
- async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
287
- this._localStorage.setItem(this.createOrderSettingItemKey(iTwinId, imodelId), JSON.stringify(orderInfos));
288
- }
289
- }
290
- exports.BrowserLocalFavoritePropertiesStorage = BrowserLocalFavoritePropertiesStorage;
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ /** @packageDocumentation
7
+ * @module Core
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.BrowserLocalFavoritePropertiesStorage = exports.NoopFavoritePropertiesStorage = exports.OfflineCachingFavoritePropertiesStorage = exports.IModelAppFavoritePropertiesStorage = exports.createFavoritePropertiesStorage = exports.DefaultFavoritePropertiesStorageTypes = exports.FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME = exports.FAVORITE_PROPERTIES_SETTING_NAME = exports.DEPRECATED_PROPERTIES_SETTING_NAMESPACE = exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE = void 0;
11
+ const core_bentley_1 = require("@itwin/core-bentley");
12
+ const core_common_1 = require("@itwin/core-common");
13
+ const core_frontend_1 = require("@itwin/core-frontend");
14
+ const presentation_common_1 = require("@itwin/presentation-common");
15
+ const ConnectivityInformationProvider_1 = require("../ConnectivityInformationProvider");
16
+ /** @internal */
17
+ exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE = "imodeljs.presentation";
18
+ /** @internal */
19
+ exports.DEPRECATED_PROPERTIES_SETTING_NAMESPACE = "Properties";
20
+ /** @internal */
21
+ exports.FAVORITE_PROPERTIES_SETTING_NAME = "FavoriteProperties";
22
+ /** @internal */
23
+ exports.FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME = "FavoritePropertiesOrderInfo";
24
+ /**
25
+ * Available implementations of [[IFavoritePropertiesStorage]].
26
+ * @public
27
+ */
28
+ var DefaultFavoritePropertiesStorageTypes;
29
+ (function (DefaultFavoritePropertiesStorageTypes) {
30
+ /** A no-op storage that doesn't store or return anything. Used for cases when favorite properties aren't used by the application. */
31
+ DefaultFavoritePropertiesStorageTypes[DefaultFavoritePropertiesStorageTypes["Noop"] = 0] = "Noop";
32
+ /** A storage that stores favorite properties information in a browser local storage. */
33
+ DefaultFavoritePropertiesStorageTypes[DefaultFavoritePropertiesStorageTypes["BrowserLocalStorage"] = 1] = "BrowserLocalStorage";
34
+ /** A storage that stores favorite properties in a user preferences storage (see [[IModelApp.userPreferences]]). */
35
+ DefaultFavoritePropertiesStorageTypes[DefaultFavoritePropertiesStorageTypes["UserPreferencesStorage"] = 2] = "UserPreferencesStorage";
36
+ })(DefaultFavoritePropertiesStorageTypes = exports.DefaultFavoritePropertiesStorageTypes || (exports.DefaultFavoritePropertiesStorageTypes = {}));
37
+ /**
38
+ * A factory method to create one of the available [[IFavoritePropertiesStorage]] implementations.
39
+ * @public
40
+ */
41
+ function createFavoritePropertiesStorage(type) {
42
+ switch (type) {
43
+ case DefaultFavoritePropertiesStorageTypes.Noop: return new NoopFavoritePropertiesStorage();
44
+ case DefaultFavoritePropertiesStorageTypes.BrowserLocalStorage: return new BrowserLocalFavoritePropertiesStorage();
45
+ case DefaultFavoritePropertiesStorageTypes.UserPreferencesStorage: return new OfflineCachingFavoritePropertiesStorage({ impl: new IModelAppFavoritePropertiesStorage() });
46
+ }
47
+ }
48
+ exports.createFavoritePropertiesStorage = createFavoritePropertiesStorage;
49
+ /**
50
+ * @internal
51
+ */
52
+ class IModelAppFavoritePropertiesStorage {
53
+ async ensureIsSignedIn() {
54
+ const accessToken = core_frontend_1.IModelApp.authorizationClient ? await core_frontend_1.IModelApp.authorizationClient.getAccessToken() : "";
55
+ if (accessToken)
56
+ return { accessToken };
57
+ throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.Error, "Current user is not authorized to use the settings service");
58
+ }
59
+ async loadProperties(iTwinId, imodelId) {
60
+ if (!core_frontend_1.IModelApp.userPreferences)
61
+ throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.Error, "User preferences service is not set up");
62
+ const { accessToken } = await this.ensureIsSignedIn();
63
+ let setting = await core_frontend_1.IModelApp.userPreferences.get({
64
+ accessToken,
65
+ iTwinId,
66
+ iModelId: imodelId,
67
+ namespace: exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE,
68
+ key: exports.FAVORITE_PROPERTIES_SETTING_NAME,
69
+ });
70
+ if (setting !== undefined)
71
+ return new Set(setting);
72
+ // try to check the old namespace
73
+ setting = await core_frontend_1.IModelApp.userPreferences.get({
74
+ accessToken,
75
+ iTwinId,
76
+ iModelId: imodelId,
77
+ namespace: exports.DEPRECATED_PROPERTIES_SETTING_NAMESPACE,
78
+ key: exports.FAVORITE_PROPERTIES_SETTING_NAME,
79
+ });
80
+ if (setting !== undefined && setting.hasOwnProperty("nestedContentInfos") && setting.hasOwnProperty("propertyInfos") && setting.hasOwnProperty("baseFieldInfos"))
81
+ return new Set([...setting.nestedContentInfos, ...setting.propertyInfos, ...setting.baseFieldInfos]);
82
+ return undefined;
83
+ }
84
+ async saveProperties(properties, iTwinId, imodelId) {
85
+ if (!core_frontend_1.IModelApp.userPreferences)
86
+ throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.Error, "User preferences service is not set up");
87
+ const { accessToken } = await this.ensureIsSignedIn();
88
+ await core_frontend_1.IModelApp.userPreferences.save({
89
+ accessToken,
90
+ iTwinId,
91
+ iModelId: imodelId,
92
+ namespace: exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE,
93
+ key: exports.FAVORITE_PROPERTIES_SETTING_NAME,
94
+ content: Array.from(properties),
95
+ });
96
+ }
97
+ async loadPropertiesOrder(iTwinId, imodelId) {
98
+ if (!core_frontend_1.IModelApp.userPreferences)
99
+ throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.Error, "User preferences service is not set up");
100
+ const { accessToken } = await this.ensureIsSignedIn();
101
+ const setting = await core_frontend_1.IModelApp.userPreferences.get({
102
+ accessToken,
103
+ iTwinId,
104
+ iModelId: imodelId,
105
+ namespace: exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE,
106
+ key: exports.FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME,
107
+ });
108
+ return setting;
109
+ }
110
+ async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
111
+ if (!core_frontend_1.IModelApp.userPreferences)
112
+ throw new presentation_common_1.PresentationError(presentation_common_1.PresentationStatus.Error, "User preferences service is not set up");
113
+ const { accessToken } = await this.ensureIsSignedIn();
114
+ await core_frontend_1.IModelApp.userPreferences.save({
115
+ accessToken,
116
+ iTwinId,
117
+ iModelId: imodelId,
118
+ namespace: exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE,
119
+ key: exports.FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME,
120
+ content: orderInfos,
121
+ });
122
+ }
123
+ }
124
+ exports.IModelAppFavoritePropertiesStorage = IModelAppFavoritePropertiesStorage;
125
+ /** @internal */
126
+ class OfflineCachingFavoritePropertiesStorage {
127
+ constructor(props) {
128
+ this._propertiesOfflineCache = new DictionaryWithReservations(iTwinAndIModelIdsKeyComparer);
129
+ this._propertiesOrderOfflineCache = new DictionaryWithReservations(iTwinAndIModelIdsKeyComparer);
130
+ // eslint-disable-next-line @typescript-eslint/naming-convention
131
+ this.onConnectivityStatusChanged = (args) => {
132
+ // istanbul ignore else
133
+ if (args.status === core_common_1.InternetConnectivityStatus.Online) {
134
+ // note: we're copying the cached values to temp arrays because `saveProperties` and `savePropertiesOrder` both
135
+ // attempt to modify cache dictionaries
136
+ const propertiesCache = new Array();
137
+ this._propertiesOfflineCache.forEach((key, value) => propertiesCache.push({ properties: value, iTwinId: key[0], imodelId: key[1] }));
138
+ propertiesCache.forEach(async (cached) => this.saveProperties(cached.properties, cached.iTwinId, cached.imodelId));
139
+ const ordersCache = new Array();
140
+ this._propertiesOrderOfflineCache.forEach((key, value) => ordersCache.push({ order: value, iTwinId: key[0], imodelId: key[1] }));
141
+ ordersCache.forEach(async (cached) => this.savePropertiesOrder(cached.order, cached.iTwinId, cached.imodelId));
142
+ }
143
+ };
144
+ this._impl = props.impl;
145
+ // istanbul ignore next
146
+ this._connectivityInfo = props.connectivityInfo ?? new ConnectivityInformationProvider_1.ConnectivityInformationProvider();
147
+ this._connectivityInfo.onInternetConnectivityChanged.addListener(this.onConnectivityStatusChanged);
148
+ }
149
+ dispose() {
150
+ if ((0, core_bentley_1.isIDisposable)(this._connectivityInfo))
151
+ this._connectivityInfo.dispose();
152
+ }
153
+ get impl() { return this._impl; }
154
+ async loadProperties(iTwinId, imodelId) {
155
+ if (this._connectivityInfo.status === core_common_1.InternetConnectivityStatus.Online) {
156
+ try {
157
+ return await this._impl.loadProperties(iTwinId, imodelId);
158
+ }
159
+ catch {
160
+ // return from offline cache if the above fails
161
+ }
162
+ }
163
+ return this._propertiesOfflineCache.get([iTwinId, imodelId]);
164
+ }
165
+ async saveProperties(properties, iTwinId, imodelId) {
166
+ const key = [iTwinId, imodelId];
167
+ if (this._connectivityInfo.status === core_common_1.InternetConnectivityStatus.Offline) {
168
+ this._propertiesOfflineCache.set(key, properties);
169
+ return;
170
+ }
171
+ const reservationId = this._propertiesOfflineCache.reserve(key);
172
+ try {
173
+ await this._impl.saveProperties(properties, iTwinId, imodelId);
174
+ this._propertiesOfflineCache.reservedDelete(key, reservationId);
175
+ }
176
+ catch {
177
+ this._propertiesOfflineCache.reservedSet(key, properties, reservationId);
178
+ }
179
+ }
180
+ async loadPropertiesOrder(iTwinId, imodelId) {
181
+ if (this._connectivityInfo.status === core_common_1.InternetConnectivityStatus.Online) {
182
+ try {
183
+ return await this._impl.loadPropertiesOrder(iTwinId, imodelId);
184
+ }
185
+ catch {
186
+ // return from offline cache if the above fails
187
+ }
188
+ }
189
+ return this._propertiesOrderOfflineCache.get([iTwinId, imodelId]);
190
+ }
191
+ async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
192
+ const key = [iTwinId, imodelId];
193
+ if (this._connectivityInfo.status === core_common_1.InternetConnectivityStatus.Offline) {
194
+ this._propertiesOrderOfflineCache.set(key, orderInfos);
195
+ return;
196
+ }
197
+ const reservationId = this._propertiesOrderOfflineCache.reserve(key);
198
+ try {
199
+ await this._impl.savePropertiesOrder(orderInfos, iTwinId, imodelId);
200
+ this._propertiesOrderOfflineCache.reservedDelete(key, reservationId);
201
+ }
202
+ catch {
203
+ this._propertiesOrderOfflineCache.reservedSet(key, orderInfos, reservationId);
204
+ }
205
+ }
206
+ }
207
+ exports.OfflineCachingFavoritePropertiesStorage = OfflineCachingFavoritePropertiesStorage;
208
+ class DictionaryWithReservations {
209
+ constructor(compareKeys) {
210
+ this._impl = new core_bentley_1.Dictionary(compareKeys);
211
+ }
212
+ get(key) { return this._impl.get(key)?.value; }
213
+ forEach(func) {
214
+ this._impl.forEach((key, entry) => {
215
+ // istanbul ignore else
216
+ if (entry.value)
217
+ func(key, entry.value);
218
+ });
219
+ }
220
+ reserve(key) {
221
+ const reservationId = core_bentley_1.Guid.createValue();
222
+ this._impl.set(key, { lastReservationId: reservationId });
223
+ return reservationId;
224
+ }
225
+ set(key, value) { return this._impl.set(key, { value }); }
226
+ reservedSet(key, value, reservationId) {
227
+ const entry = this._impl.get(key);
228
+ if (entry && entry.lastReservationId === reservationId)
229
+ this._impl.set(key, { value });
230
+ }
231
+ reservedDelete(key, reservationId) {
232
+ const entry = this._impl.get(key);
233
+ if (entry && entry.lastReservationId === reservationId)
234
+ this._impl.delete(key);
235
+ }
236
+ }
237
+ // istanbul ignore next
238
+ function iTwinAndIModelIdsKeyComparer(lhs, rhs) {
239
+ const iTwinIdCompare = (0, core_bentley_1.compareStrings)(lhs[0] ?? "", rhs[0] ?? "");
240
+ return (iTwinIdCompare !== 0) ? iTwinIdCompare : (0, core_bentley_1.compareStrings)(lhs[1] ?? "", rhs[1] ?? "");
241
+ }
242
+ /** @internal */
243
+ class NoopFavoritePropertiesStorage {
244
+ // istanbul ignore next
245
+ async loadProperties(_iTwinId, _imodelId) { return undefined; }
246
+ // istanbul ignore next
247
+ async saveProperties(_properties, _iTwinId, _imodelId) { }
248
+ // istanbul ignore next
249
+ async loadPropertiesOrder(_iTwinId, _imodelId) { return undefined; }
250
+ // istanbul ignore next
251
+ async savePropertiesOrder(_orderInfos, _iTwinId, _imodelId) { }
252
+ }
253
+ exports.NoopFavoritePropertiesStorage = NoopFavoritePropertiesStorage;
254
+ /** @internal */
255
+ class BrowserLocalFavoritePropertiesStorage {
256
+ constructor(props) {
257
+ // istanbul ignore next
258
+ this._localStorage = props?.localStorage ?? window.localStorage;
259
+ }
260
+ createFavoritesSettingItemKey(iTwinId, imodelId) {
261
+ return `${exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE}${exports.FAVORITE_PROPERTIES_SETTING_NAME}?iTwinId=${iTwinId}&imodelId=${imodelId}`;
262
+ }
263
+ createOrderSettingItemKey(iTwinId, imodelId) {
264
+ return `${exports.IMODELJS_PRESENTATION_SETTING_NAMESPACE}${exports.FAVORITE_PROPERTIES_ORDER_INFO_SETTING_NAME}?iTwinId=${iTwinId}&imodelId=${imodelId}`;
265
+ }
266
+ async loadProperties(iTwinId, imodelId) {
267
+ const value = this._localStorage.getItem(this.createFavoritesSettingItemKey(iTwinId, imodelId));
268
+ if (!value)
269
+ return undefined;
270
+ const properties = JSON.parse(value);
271
+ return new Set(properties);
272
+ }
273
+ async saveProperties(properties, iTwinId, imodelId) {
274
+ this._localStorage.setItem(this.createFavoritesSettingItemKey(iTwinId, imodelId), JSON.stringify([...properties]));
275
+ }
276
+ async loadPropertiesOrder(iTwinId, imodelId) {
277
+ const value = this._localStorage.getItem(this.createOrderSettingItemKey(iTwinId, imodelId));
278
+ if (!value)
279
+ return undefined;
280
+ const orderInfos = JSON.parse(value).map((json) => ({
281
+ ...json,
282
+ orderedTimestamp: new Date(json.orderedTimestamp),
283
+ }));
284
+ return orderInfos;
285
+ }
286
+ async savePropertiesOrder(orderInfos, iTwinId, imodelId) {
287
+ this._localStorage.setItem(this.createOrderSettingItemKey(iTwinId, imodelId), JSON.stringify(orderInfos));
288
+ }
289
+ }
290
+ exports.BrowserLocalFavoritePropertiesStorage = BrowserLocalFavoritePropertiesStorage;
291
291
  //# sourceMappingURL=FavoritePropertiesStorage.js.map