@itwin/presentation-frontend 3.7.0-dev.7 → 3.7.0-dev.8

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 (124) hide show
  1. package/CHANGELOG.md +6 -1
  2. package/lib/cjs/presentation-frontend/ConnectivityInformationProvider.d.ts +29 -29
  3. package/lib/cjs/presentation-frontend/ConnectivityInformationProvider.js +47 -47
  4. package/lib/cjs/presentation-frontend/ConnectivityInformationProvider.js.map +1 -1
  5. package/lib/cjs/presentation-frontend/Diagnostics.d.ts +14 -14
  6. package/lib/cjs/presentation-frontend/Diagnostics.js +61 -61
  7. package/lib/cjs/presentation-frontend/Diagnostics.js.map +1 -1
  8. package/lib/cjs/presentation-frontend/FrontendLoggerCategory.d.ts +12 -12
  9. package/lib/cjs/presentation-frontend/FrontendLoggerCategory.js +20 -20
  10. package/lib/cjs/presentation-frontend/FrontendLoggerCategory.js.map +1 -1
  11. package/lib/cjs/presentation-frontend/IpcRequestsHandler.d.ts +10 -10
  12. package/lib/cjs/presentation-frontend/IpcRequestsHandler.js +41 -41
  13. package/lib/cjs/presentation-frontend/IpcRequestsHandler.js.map +1 -1
  14. package/lib/cjs/presentation-frontend/LocalizationHelper.d.ts +10 -10
  15. package/lib/cjs/presentation-frontend/LocalizationHelper.js +31 -31
  16. package/lib/cjs/presentation-frontend/LocalizationHelper.js.map +1 -1
  17. package/lib/cjs/presentation-frontend/Presentation.d.ts +75 -75
  18. package/lib/cjs/presentation-frontend/Presentation.js +162 -162
  19. package/lib/cjs/presentation-frontend/Presentation.js.map +1 -1
  20. package/lib/cjs/presentation-frontend/PresentationManager.d.ts +186 -186
  21. package/lib/cjs/presentation-frontend/PresentationManager.js +436 -436
  22. package/lib/cjs/presentation-frontend/PresentationManager.js.map +1 -1
  23. package/lib/cjs/presentation-frontend/RulesetManager.d.ts +63 -63
  24. package/lib/cjs/presentation-frontend/RulesetManager.js +85 -85
  25. package/lib/cjs/presentation-frontend/RulesetManager.js.map +1 -1
  26. package/lib/cjs/presentation-frontend/RulesetVariablesManager.d.ts +143 -143
  27. package/lib/cjs/presentation-frontend/RulesetVariablesManager.js +195 -195
  28. package/lib/cjs/presentation-frontend/RulesetVariablesManager.js.map +1 -1
  29. package/lib/cjs/presentation-frontend/StateTracker.d.ts +38 -38
  30. package/lib/cjs/presentation-frontend/StateTracker.js +129 -129
  31. package/lib/cjs/presentation-frontend/StateTracker.js.map +1 -1
  32. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesManager.d.ts +132 -132
  33. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesManager.js +526 -526
  34. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesManager.js.map +1 -1
  35. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesStorage.d.ts +111 -111
  36. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js +293 -293
  37. package/lib/cjs/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js.map +1 -1
  38. package/lib/cjs/presentation-frontend/selection/HiliteRules.json +172 -172
  39. package/lib/cjs/presentation-frontend/selection/HiliteSetProvider.d.ts +52 -52
  40. package/lib/cjs/presentation-frontend/selection/HiliteSetProvider.js +97 -97
  41. package/lib/cjs/presentation-frontend/selection/HiliteSetProvider.js.map +1 -1
  42. package/lib/cjs/presentation-frontend/selection/ISelectionProvider.d.ts +19 -19
  43. package/lib/cjs/presentation-frontend/selection/ISelectionProvider.js +9 -9
  44. package/lib/cjs/presentation-frontend/selection/ISelectionProvider.js.map +1 -1
  45. package/lib/cjs/presentation-frontend/selection/SelectionChangeEvent.d.ts +52 -52
  46. package/lib/cjs/presentation-frontend/selection/SelectionChangeEvent.js +33 -33
  47. package/lib/cjs/presentation-frontend/selection/SelectionChangeEvent.js.map +1 -1
  48. package/lib/cjs/presentation-frontend/selection/SelectionHandler.d.ts +103 -103
  49. package/lib/cjs/presentation-frontend/selection/SelectionHandler.js +108 -108
  50. package/lib/cjs/presentation-frontend/selection/SelectionHandler.js.map +1 -1
  51. package/lib/cjs/presentation-frontend/selection/SelectionHelper.d.ts +14 -14
  52. package/lib/cjs/presentation-frontend/selection/SelectionHelper.js +37 -37
  53. package/lib/cjs/presentation-frontend/selection/SelectionHelper.js.map +1 -1
  54. package/lib/cjs/presentation-frontend/selection/SelectionManager.d.ts +139 -139
  55. package/lib/cjs/presentation-frontend/selection/SelectionManager.js +414 -414
  56. package/lib/cjs/presentation-frontend/selection/SelectionManager.js.map +1 -1
  57. package/lib/cjs/presentation-frontend/selection/SelectionScopesManager.d.ts +59 -59
  58. package/lib/cjs/presentation-frontend/selection/SelectionScopesManager.js +91 -91
  59. package/lib/cjs/presentation-frontend/selection/SelectionScopesManager.js.map +1 -1
  60. package/lib/cjs/presentation-frontend.d.ts +34 -34
  61. package/lib/cjs/presentation-frontend.js +50 -50
  62. package/lib/cjs/presentation-frontend.js.map +1 -1
  63. package/lib/esm/presentation-frontend/ConnectivityInformationProvider.d.ts +29 -29
  64. package/lib/esm/presentation-frontend/ConnectivityInformationProvider.js +43 -43
  65. package/lib/esm/presentation-frontend/ConnectivityInformationProvider.js.map +1 -1
  66. package/lib/esm/presentation-frontend/Diagnostics.d.ts +14 -14
  67. package/lib/esm/presentation-frontend/Diagnostics.js +56 -56
  68. package/lib/esm/presentation-frontend/Diagnostics.js.map +1 -1
  69. package/lib/esm/presentation-frontend/FrontendLoggerCategory.d.ts +12 -12
  70. package/lib/esm/presentation-frontend/FrontendLoggerCategory.js +17 -17
  71. package/lib/esm/presentation-frontend/FrontendLoggerCategory.js.map +1 -1
  72. package/lib/esm/presentation-frontend/IpcRequestsHandler.d.ts +10 -10
  73. package/lib/esm/presentation-frontend/IpcRequestsHandler.js +37 -37
  74. package/lib/esm/presentation-frontend/IpcRequestsHandler.js.map +1 -1
  75. package/lib/esm/presentation-frontend/LocalizationHelper.d.ts +10 -10
  76. package/lib/esm/presentation-frontend/LocalizationHelper.js +27 -27
  77. package/lib/esm/presentation-frontend/LocalizationHelper.js.map +1 -1
  78. package/lib/esm/presentation-frontend/Presentation.d.ts +75 -75
  79. package/lib/esm/presentation-frontend/Presentation.js +158 -158
  80. package/lib/esm/presentation-frontend/Presentation.js.map +1 -1
  81. package/lib/esm/presentation-frontend/PresentationManager.d.ts +186 -186
  82. package/lib/esm/presentation-frontend/PresentationManager.js +431 -431
  83. package/lib/esm/presentation-frontend/PresentationManager.js.map +1 -1
  84. package/lib/esm/presentation-frontend/RulesetManager.d.ts +63 -63
  85. package/lib/esm/presentation-frontend/RulesetManager.js +81 -81
  86. package/lib/esm/presentation-frontend/RulesetManager.js.map +1 -1
  87. package/lib/esm/presentation-frontend/RulesetVariablesManager.d.ts +143 -143
  88. package/lib/esm/presentation-frontend/RulesetVariablesManager.js +191 -191
  89. package/lib/esm/presentation-frontend/RulesetVariablesManager.js.map +1 -1
  90. package/lib/esm/presentation-frontend/StateTracker.d.ts +38 -38
  91. package/lib/esm/presentation-frontend/StateTracker.js +125 -125
  92. package/lib/esm/presentation-frontend/StateTracker.js.map +1 -1
  93. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.d.ts +132 -132
  94. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.js +520 -520
  95. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesManager.js.map +1 -1
  96. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.d.ts +111 -111
  97. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js +285 -285
  98. package/lib/esm/presentation-frontend/favorite-properties/FavoritePropertiesStorage.js.map +1 -1
  99. package/lib/esm/presentation-frontend/selection/HiliteRules.json +172 -172
  100. package/lib/esm/presentation-frontend/selection/HiliteSetProvider.d.ts +52 -52
  101. package/lib/esm/presentation-frontend/selection/HiliteSetProvider.js +93 -93
  102. package/lib/esm/presentation-frontend/selection/HiliteSetProvider.js.map +1 -1
  103. package/lib/esm/presentation-frontend/selection/ISelectionProvider.d.ts +19 -19
  104. package/lib/esm/presentation-frontend/selection/ISelectionProvider.js +8 -8
  105. package/lib/esm/presentation-frontend/selection/ISelectionProvider.js.map +1 -1
  106. package/lib/esm/presentation-frontend/selection/SelectionChangeEvent.d.ts +52 -52
  107. package/lib/esm/presentation-frontend/selection/SelectionChangeEvent.js +29 -29
  108. package/lib/esm/presentation-frontend/selection/SelectionChangeEvent.js.map +1 -1
  109. package/lib/esm/presentation-frontend/selection/SelectionHandler.d.ts +103 -103
  110. package/lib/esm/presentation-frontend/selection/SelectionHandler.js +104 -104
  111. package/lib/esm/presentation-frontend/selection/SelectionHandler.js.map +1 -1
  112. package/lib/esm/presentation-frontend/selection/SelectionHelper.d.ts +14 -14
  113. package/lib/esm/presentation-frontend/selection/SelectionHelper.js +33 -33
  114. package/lib/esm/presentation-frontend/selection/SelectionHelper.js.map +1 -1
  115. package/lib/esm/presentation-frontend/selection/SelectionManager.d.ts +139 -139
  116. package/lib/esm/presentation-frontend/selection/SelectionManager.js +409 -409
  117. package/lib/esm/presentation-frontend/selection/SelectionManager.js.map +1 -1
  118. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.d.ts +59 -59
  119. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.js +85 -85
  120. package/lib/esm/presentation-frontend/selection/SelectionScopesManager.js.map +1 -1
  121. package/lib/esm/presentation-frontend.d.ts +34 -34
  122. package/lib/esm/presentation-frontend.js +38 -38
  123. package/lib/esm/presentation-frontend.js.map +1 -1
  124. package/package.json +14 -14
@@ -1,521 +1,521 @@
1
- /*---------------------------------------------------------------------------------------------
2
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
- * See LICENSE.md in the project root for license terms and full copyright notice.
4
- *--------------------------------------------------------------------------------------------*/
5
- /** @packageDocumentation
6
- * @module Core
7
- */
8
- import { BeEvent, isIDisposable } from "@itwin/core-bentley";
9
- import { QueryRowFormat } from "@itwin/core-common";
10
- /**
11
- * Scopes that favorite properties can be stored in.
12
- * @public
13
- */
14
- export var FavoritePropertiesScope;
15
- (function (FavoritePropertiesScope) {
16
- FavoritePropertiesScope[FavoritePropertiesScope["Global"] = 0] = "Global";
17
- FavoritePropertiesScope[FavoritePropertiesScope["ITwin"] = 1] = "ITwin";
18
- FavoritePropertiesScope[FavoritePropertiesScope["IModel"] = 2] = "IModel";
19
- })(FavoritePropertiesScope || (FavoritePropertiesScope = {}));
20
- /**
21
- * The favorite property manager which lets to store favorite properties
22
- * and check if field contains favorite properties.
23
- *
24
- * @public
25
- */
26
- export class FavoritePropertiesManager {
27
- constructor(props) {
28
- /** Event raised after favorite properties have changed. */
29
- this.onFavoritesChanged = new BeEvent();
30
- /**
31
- * Initialize favorite properties for the provided IModelConnection.
32
- */
33
- this.initializeConnection = async (imodel) => {
34
- const imodelId = imodel.iModelId;
35
- const iTwinId = imodel.iTwinId;
36
- if (this._globalProperties === undefined)
37
- this._globalProperties = await this._storage.loadProperties() || new Set();
38
- if (!this._iTwinProperties.has(iTwinId)) {
39
- const iTwinProperties = await this._storage.loadProperties(iTwinId) || new Set();
40
- this._iTwinProperties.set(iTwinId, iTwinProperties);
41
- }
42
- if (!this._imodelProperties.has(getiModelInfo(iTwinId, imodelId))) {
43
- const imodelProperties = await this._storage.loadProperties(iTwinId, imodelId) || new Set();
44
- this._imodelProperties.set(getiModelInfo(iTwinId, imodelId), imodelProperties);
45
- }
46
- const propertiesOrder = await this._storage.loadPropertiesOrder(iTwinId, imodelId) || [];
47
- this._propertiesOrder.set(getiModelInfo(iTwinId, imodelId), propertiesOrder);
48
- await this._adjustPropertyOrderInfos(iTwinId, imodelId);
49
- };
50
- /**
51
- * Function that removes order information of properties that are no longer
52
- * favorited and adds missing order information for favorited properties.
53
- */
54
- this._adjustPropertyOrderInfos = async (iTwinId, imodelId) => {
55
- const propertiesOrder = this._propertiesOrder.get(getiModelInfo(iTwinId, imodelId));
56
- const globalProperties = this._globalProperties;
57
- const iTwinProperties = this._iTwinProperties.get(iTwinId);
58
- const imodelProperties = this._imodelProperties.get(getiModelInfo(iTwinId, imodelId));
59
- // favorite property infos that need to be added to the propertiesOrder array
60
- const infosToAdd = new Set([...globalProperties, ...iTwinProperties, ...imodelProperties]);
61
- for (let i = propertiesOrder.length - 1; i >= 0; i--) {
62
- if (infosToAdd.has(propertiesOrder[i].name))
63
- infosToAdd.delete(propertiesOrder[i].name);
64
- else
65
- propertiesOrder.splice(i, 1);
66
- }
67
- infosToAdd.forEach((info) => propertiesOrder.push({
68
- name: info,
69
- parentClassName: getPropertyClassName(info),
70
- orderedTimestamp: new Date(),
71
- priority: 0,
72
- }));
73
- let priority = propertiesOrder.length;
74
- propertiesOrder.forEach((oi) => oi.priority = priority--);
75
- };
76
- /**
77
- * Sorts an array of fields with respect to favorite property order.
78
- * Non-favorited fields get sorted by their default priority and always have lower priority than favorited fields.
79
- * @param imodel IModelConnection.
80
- * @param fields Array of Field's that needs to be sorted.
81
- * @note `initializeConnection` must be called with the `imodel` before calling this function.
82
- */
83
- this.sortFields = (imodel, fields) => {
84
- this.validateInitialization(imodel);
85
- const iTwinId = imodel.iTwinId;
86
- const imodelId = imodel.iModelId;
87
- const fieldPriority = new Map();
88
- fields.forEach((field) => fieldPriority.set(field, this.getFieldPriority(field, iTwinId, imodelId)));
89
- const sortFunction = (left, right) => {
90
- const lp = fieldPriority.get(left);
91
- const rp = fieldPriority.get(right);
92
- return lp < rp ? 1 :
93
- lp > rp ? -1 :
94
- left.priority < right.priority ? 1 : // if favorite fields have equal priorities, sort by field priority
95
- left.priority > right.priority ? -1 :
96
- left.name.localeCompare(right.name);
97
- };
98
- return fields.sort(sortFunction);
99
- };
100
- this._getBaseClassesByClass = async (imodel, neededClasses) => {
101
- const iTwinId = imodel.iTwinId;
102
- const imodelId = imodel.iModelId;
103
- const imodelInfo = getiModelInfo(iTwinId, imodelId);
104
- let baseClasses;
105
- if (this._imodelBaseClassesByClass.has(imodelInfo))
106
- baseClasses = this._imodelBaseClassesByClass.get(imodelInfo);
107
- else
108
- this._imodelBaseClassesByClass.set(imodelInfo, baseClasses = {});
109
- const missingClasses = new Set();
110
- neededClasses.forEach((className) => {
111
- if (!baseClasses.hasOwnProperty(className))
112
- missingClasses.add(className);
113
- });
114
- if (missingClasses.size === 0)
115
- return baseClasses;
116
- const query = `
117
- SELECT (derivedSchema.Name || ':' || derivedClass.Name) AS "ClassFullName", (baseSchema.Name || ':' || baseClass.Name) AS "BaseClassFullName"
118
- FROM ECDbMeta.ClassHasAllBaseClasses baseClassRels
119
- INNER JOIN ECDbMeta.ECClassDef derivedClass ON derivedClass.ECInstanceId = baseClassRels.SourceECInstanceId
120
- INNER JOIN ECDbMeta.ECSchemaDef derivedSchema ON derivedSchema.ECInstanceId = derivedClass.Schema.Id
121
- INNER JOIN ECDbMeta.ECClassDef baseClass ON baseClass.ECInstanceId = baseClassRels.TargetECInstanceId
122
- INNER JOIN ECDbMeta.ECSchemaDef baseSchema ON baseSchema.ECInstanceId = baseClass.Schema.Id
123
- WHERE (derivedSchema.Name || ':' || derivedClass.Name) IN (${[...missingClasses].map((className) => `'${className}'`).join(",")})`;
124
- // eslint-disable-next-line deprecation/deprecation
125
- for await (const row of imodel.query(query, undefined, { rowFormat: QueryRowFormat.UseJsPropertyNames })) {
126
- if (!(row.classFullName in baseClasses))
127
- baseClasses[row.classFullName] = [];
128
- baseClasses[row.classFullName].push(row.baseClassFullName);
129
- }
130
- return baseClasses;
131
- };
132
- this._storage = props.storage;
133
- this._iTwinProperties = new Map();
134
- this._imodelProperties = new Map();
135
- this._propertiesOrder = new Map();
136
- this._imodelBaseClassesByClass = new Map();
137
- }
138
- dispose() {
139
- // istanbul ignore else
140
- if (isIDisposable(this._storage))
141
- this._storage.dispose();
142
- }
143
- validateInitialization(imodel) {
144
- const iTwinId = imodel.iTwinId;
145
- const imodelId = imodel.iModelId;
146
- if (!this._imodelProperties.has(getiModelInfo(iTwinId, imodelId)))
147
- throw Error(`Favorite properties are not initialized for iModel: '${imodelId}', in iTwin: '${iTwinId}'. Call initializeConnection() with an IModelConnection to initialize.`);
148
- }
149
- /**
150
- * Adds favorite properties into a certain scope.
151
- * @param field Field that contains properties. If field contains multiple properties, all of them will be favorited.
152
- * @param imodel IModelConnection.
153
- * @param scope FavoritePropertiesScope to put the favorite properties into.
154
- * @note `initializeConnection` must be called with the `imodel` before calling this function.
155
- */
156
- async add(field, imodel, scope) {
157
- this.validateInitialization(imodel);
158
- const iTwinId = imodel.iTwinId;
159
- const imodelId = imodel.iModelId;
160
- let favoriteProperties;
161
- let saveProperties;
162
- switch (scope) {
163
- case FavoritePropertiesScope.Global:
164
- favoriteProperties = this._globalProperties;
165
- saveProperties = async (properties) => this._storage.saveProperties(properties);
166
- break;
167
- case FavoritePropertiesScope.ITwin:
168
- favoriteProperties = this._iTwinProperties.get(iTwinId);
169
- saveProperties = async (properties) => this._storage.saveProperties(properties, iTwinId);
170
- break;
171
- default:
172
- favoriteProperties = this._imodelProperties.get(getiModelInfo(iTwinId, imodelId));
173
- saveProperties = async (properties) => this._storage.saveProperties(properties, iTwinId, imodelId);
174
- }
175
- const countBefore = favoriteProperties.size;
176
- const fieldInfos = getFieldInfos(field);
177
- fieldInfos.forEach((info) => favoriteProperties.add(info));
178
- if (favoriteProperties.size !== countBefore) {
179
- const saves = [];
180
- saves.push(saveProperties(favoriteProperties));
181
- const propertiesOrder = this._propertiesOrder.get(getiModelInfo(iTwinId, imodelId));
182
- addOrderInfos(propertiesOrder, createFieldOrderInfos(field));
183
- saves.push(this._storage.savePropertiesOrder(propertiesOrder, iTwinId, imodelId));
184
- await Promise.all(saves);
185
- this.onFavoritesChanged.raiseEvent();
186
- }
187
- }
188
- /**
189
- * Removes favorite properties from a scope specified and all the more general scopes.
190
- * @param field Field that contains properties. If field contains multiple properties, all of them will be un-favorited.
191
- * @param imodel IModelConnection.
192
- * @param scope FavoritePropertiesScope to remove the favorite properties from. It also removes from more general scopes.
193
- * @note `initializeConnection` must be called with the `imodel` before calling this function.
194
- */
195
- async remove(field, imodel, scope) {
196
- this.validateInitialization(imodel);
197
- const iTwinId = imodel.iTwinId;
198
- const imodelId = imodel.iModelId;
199
- const fieldInfos = getFieldInfos(field);
200
- const workingScopes = [];
201
- workingScopes.push({
202
- properties: this._globalProperties,
203
- save: async (properties) => this._storage.saveProperties(properties),
204
- });
205
- if (scope === FavoritePropertiesScope.ITwin || scope === FavoritePropertiesScope.IModel) {
206
- workingScopes.push({
207
- properties: this._iTwinProperties.get(iTwinId),
208
- save: async (properties) => this._storage.saveProperties(properties, iTwinId),
209
- });
210
- }
211
- if (scope === FavoritePropertiesScope.IModel) {
212
- workingScopes.push({
213
- properties: this._imodelProperties.get(getiModelInfo(iTwinId, imodelId)),
214
- save: async (properties) => this._storage.saveProperties(properties, iTwinId, imodelId),
215
- });
216
- }
217
- const saves = [];
218
- let favoritesChanged = false;
219
- for (const { properties, save } of workingScopes) {
220
- const countBefore = properties.size;
221
- fieldInfos.forEach((info) => properties.delete(info));
222
- if (properties.size !== countBefore) {
223
- saves.push(save(properties));
224
- favoritesChanged = true;
225
- }
226
- }
227
- if (!favoritesChanged)
228
- return;
229
- const propertiesOrder = this._propertiesOrder.get(getiModelInfo(iTwinId, imodelId));
230
- removeOrderInfos(propertiesOrder, createFieldOrderInfos(field));
231
- saves.push(this._storage.savePropertiesOrder(propertiesOrder, iTwinId, imodelId));
232
- await Promise.all(saves);
233
- this.onFavoritesChanged.raiseEvent();
234
- }
235
- /**
236
- * Removes all favorite properties from a certain scope.
237
- * @param imodel IModelConnection.
238
- * @param scope FavoritePropertiesScope to remove the favorite properties from.
239
- * @note `initializeConnection` must be called with the `imodel` before calling this function.
240
- */
241
- async clear(imodel, scope) {
242
- this.validateInitialization(imodel);
243
- const iTwinId = imodel.iTwinId;
244
- const imodelId = imodel.iModelId;
245
- let favoriteProperties;
246
- let saveProperties;
247
- switch (scope) {
248
- case FavoritePropertiesScope.Global:
249
- favoriteProperties = this._globalProperties;
250
- saveProperties = async () => this._storage.saveProperties(new Set());
251
- break;
252
- case FavoritePropertiesScope.ITwin:
253
- favoriteProperties = this._iTwinProperties.get(iTwinId);
254
- saveProperties = async () => this._storage.saveProperties(new Set(), iTwinId);
255
- break;
256
- default:
257
- favoriteProperties = this._imodelProperties.get(getiModelInfo(iTwinId, imodelId));
258
- saveProperties = async () => this._storage.saveProperties(new Set(), iTwinId, imodelId);
259
- }
260
- if (favoriteProperties.size === 0)
261
- return;
262
- favoriteProperties.clear();
263
- const saves = [];
264
- saves.push(saveProperties());
265
- saves.push(this._adjustPropertyOrderInfos(iTwinId, imodelId));
266
- await Promise.all(saves);
267
- this.onFavoritesChanged.raiseEvent();
268
- }
269
- /**
270
- * Check if field contains at least one favorite property.
271
- * @param field Field that contains properties.
272
- * @param imodel IModelConnection.
273
- * @param scope FavoritePropertiesScope to check for favorite properties. It also checks the more general scopes.
274
- * @note `initializeConnection` must be called with the `imodel` before calling this function.
275
- */
276
- has(field, imodel, scope) {
277
- this.validateInitialization(imodel);
278
- const iTwinId = imodel.iTwinId;
279
- const imodelId = imodel.iModelId;
280
- const fieldInfos = getFieldInfos(field);
281
- return setHasAny(this._globalProperties, fieldInfos) ||
282
- (scope !== FavoritePropertiesScope.Global && setHasAny(this._iTwinProperties.get(iTwinId), fieldInfos)) ||
283
- (scope === FavoritePropertiesScope.IModel && setHasAny(this._imodelProperties.get(getiModelInfo(iTwinId, imodelId)), fieldInfos));
284
- }
285
- getFieldPriority(field, iTwinId, imodelId) {
286
- const orderInfos = this._propertiesOrder.get(getiModelInfo(iTwinId, imodelId));
287
- const fieldOrderInfos = getFieldOrderInfos(field, orderInfos);
288
- if (fieldOrderInfos.length === 0)
289
- return -1;
290
- const mostRecent = getMostRecentOrderInfo(fieldOrderInfos);
291
- return mostRecent.priority;
292
- }
293
- /** Changes field properties priorities to lower than another fields priority
294
- * @param imodel IModelConnection.
295
- * @param field Field that priority is being changed.
296
- * @param afterField Field that goes before the moved field. If undefined the moving field is changed to the highest priority (to the top).
297
- * @param visibleFields Array of fields to move the field in.
298
- * @note `initializeConnection` must be called with the `imodel` before calling this function.
299
- */
300
- async changeFieldPriority(imodel, field, afterField, visibleFields) {
301
- /**
302
- * How it works:
303
- * 1. Gets the orderInfo's for `field` (`orderInfo`) and `afterField` (`afterOrderInfo`) by selecting the most recent order informations for each field
304
- * 2. Iterates all orderInfo's that are in between `afterOrderInfo` and `orderInfo` when sorted by priority
305
- * 3. For each iterated orderInfo it checks if it is relevant:
306
- * 3.1. If orderInfo belongs to a primitive property, orderInfo is relevant
307
- * 3.2. If orderInfo's field is visible, orderInfo is relevant
308
- * 3.3. If orderInfo's class has a base class or itself in previously labeled relevant orderInfo's, orderInfo is relevant
309
- * 3.4. If 3.1 - 3.3 don't pass, orderInfo is irrelevant
310
- * 4. Irrelevant orderInfos's get moved after `orderInfo` (depends on the direction)
311
- * 5. All `field` orderInfo's get moved after `afterOrderInfo`
312
- */
313
- this.validateInitialization(imodel);
314
- const iTwinId = imodel.iTwinId;
315
- const imodelId = imodel.iModelId;
316
- if (field === afterField)
317
- throw Error("`field` can not be the same as `afterField`.");
318
- const allOrderInfos = this._propertiesOrder.get(getiModelInfo(iTwinId, imodelId));
319
- const findFieldOrderInfoData = (f) => {
320
- if (!visibleFields.includes(f))
321
- throw Error("Field is not contained in visible fields.");
322
- const infos = getFieldOrderInfos(f, allOrderInfos);
323
- if (infos.length === 0)
324
- throw Error("Field has no property order information.");
325
- const info = getMostRecentOrderInfo(infos);
326
- const index = allOrderInfos.indexOf(info);
327
- return { infos, mostRecent: { info, index } };
328
- };
329
- const { infos: movingOrderInfos, mostRecent: { index: orderInfoIndex } } = findFieldOrderInfoData(field);
330
- let afterOrderInfo;
331
- let afterOrderInfoIndex;
332
- if (afterField === undefined) {
333
- afterOrderInfo = undefined;
334
- afterOrderInfoIndex = -1;
335
- }
336
- else {
337
- ({ mostRecent: { info: afterOrderInfo, index: afterOrderInfoIndex } } = findFieldOrderInfoData(afterField));
338
- }
339
- let direction; // where to go from `afterOrderInfo` to `orderInfo`
340
- let startIndex;
341
- if (orderInfoIndex < afterOrderInfoIndex) {
342
- direction = Direction.Up;
343
- startIndex = afterOrderInfoIndex;
344
- }
345
- else {
346
- direction = Direction.Down;
347
- startIndex = afterOrderInfoIndex + 1;
348
- }
349
- const neededClassNames = allOrderInfos.reduce((classNames, oi) => {
350
- if (oi.parentClassName)
351
- classNames.add(oi.parentClassName);
352
- return classNames;
353
- }, new Set());
354
- const baseClassesByClass = await this._getBaseClassesByClass(imodel, neededClassNames);
355
- const visibleOrderInfos = visibleFields.reduce((union, currField) => union.concat(getFieldOrderInfos(currField, allOrderInfos)), []);
356
- const irrelevantOrderInfos = []; // orderInfos's that won't change their logical order in respect to other properties
357
- const relevantClasses = new Set(); // currently relevant classes
358
- for (let i = startIndex; i !== orderInfoIndex; i += direction) {
359
- const currOrderInfo = allOrderInfos[i];
360
- // primitive properties are always relevant, because we can't determine their relevance based on the class hierarchy
361
- if (currOrderInfo.parentClassName === undefined)
362
- continue;
363
- const visible = visibleOrderInfos.includes(currOrderInfo);
364
- if (visible) {
365
- relevantClasses.add(currOrderInfo.parentClassName);
366
- continue;
367
- }
368
- const hasBaseClasses = baseClassesByClass[currOrderInfo.parentClassName].some((classId) => relevantClasses.has(classId));
369
- if (hasBaseClasses)
370
- continue;
371
- if (direction === Direction.Down)
372
- irrelevantOrderInfos.push(currOrderInfo);
373
- else
374
- irrelevantOrderInfos.unshift(currOrderInfo);
375
- }
376
- // remove irrelevantOrderInfo's to add them after the `orderInfo`
377
- irrelevantOrderInfos.forEach((foi) => {
378
- const index = allOrderInfos.findIndex((oi) => oi.parentClassName === foi.parentClassName && oi.name === foi.name);
379
- allOrderInfos.splice(index, 1);
380
- });
381
- // remove movingOrderInfos's to add them after the `afterOrderInfo`
382
- movingOrderInfos.forEach((foi) => {
383
- const index = allOrderInfos.findIndex((oi) => oi.parentClassName === foi.parentClassName && oi.name === foi.name);
384
- allOrderInfos.splice(index, 1);
385
- });
386
- movingOrderInfos.forEach((oi) => oi.orderedTimestamp = new Date());
387
- afterOrderInfoIndex = afterOrderInfo === undefined ? -1 : allOrderInfos.indexOf(afterOrderInfo);
388
- allOrderInfos.splice(afterOrderInfoIndex + 1, 0, ...movingOrderInfos);
389
- allOrderInfos.splice(afterOrderInfoIndex + 1 + (direction === Direction.Up ? movingOrderInfos.length : 0), 0, ...irrelevantOrderInfos);
390
- // reassign priority numbers
391
- let priority = allOrderInfos.length;
392
- allOrderInfos.forEach((oi) => oi.priority = priority--);
393
- await this._storage.savePropertiesOrder(allOrderInfos, iTwinId, imodelId);
394
- this.onFavoritesChanged.raiseEvent();
395
- }
396
- }
397
- /**
398
- * Used in tests to avoid collisions between multiple runs using the same storage
399
- * @internal
400
- */
401
- FavoritePropertiesManager.FAVORITES_IDENTIFIER_PREFIX = "";
402
- var Direction;
403
- (function (Direction) {
404
- Direction[Direction["Up"] = -1] = "Up";
405
- Direction[Direction["Down"] = 1] = "Down";
406
- })(Direction || (Direction = {}));
407
- const getiModelInfo = (iTwinId, imodelId) => `${iTwinId}/${imodelId}`;
408
- const getPropertiesFieldPropertyNames = (field) => {
409
- const nestingPrefix = getNestingPrefix(field.parent);
410
- return field.properties.map((property) => `${FavoritePropertiesManager.FAVORITES_IDENTIFIER_PREFIX}${nestingPrefix}${property.property.classInfo.name}:${property.property.name}`);
411
- };
412
- const getNestedContentFieldPropertyName = (field) => {
413
- const nestingPrefix = getNestingPrefix(field);
414
- return `${FavoritePropertiesManager.FAVORITES_IDENTIFIER_PREFIX}${nestingPrefix}${field.contentClassInfo.name}`;
415
- };
416
- const getNestingPrefix = (field) => {
417
- const path = [];
418
- let curr = field;
419
- while (curr !== undefined) {
420
- curr.pathToPrimaryClass.forEach((rel) => {
421
- // Relationship directions are reversed, because we are generating a relationship list starting from the parent
422
- path.push(`${rel.isForwardRelationship ? "B" : "F"}:${rel.relationshipInfo.name}`);
423
- path.push(rel.targetClassInfo.name);
424
- });
425
- curr = curr.parent;
426
- }
427
- if (path.length === 0)
428
- return "";
429
- path.reverse();
430
- return `${path.join("-")}-`;
431
- };
432
- const getPropertyClassName = (propertyName) => {
433
- const propertyNameStart = propertyName.split("-")[0];
434
- const parts = propertyNameStart.split(":").length;
435
- if (parts === 1) // primitive
436
- return undefined;
437
- if (parts === 2) // nested property OR nested property parent class OR regular property parent class
438
- return propertyNameStart;
439
- // regular property without parent class
440
- return propertyNameStart.substr(0, propertyName.lastIndexOf(":"));
441
- };
442
- /** @internal */
443
- export const getFieldInfos = (field) => {
444
- const fieldInfos = new Set();
445
- if (field.isPropertiesField())
446
- getPropertiesFieldPropertyNames(field).forEach((info) => fieldInfos.add(info));
447
- else if (field.isNestedContentField())
448
- fieldInfos.add(getNestedContentFieldPropertyName(field));
449
- else
450
- fieldInfos.add(`${FavoritePropertiesManager.FAVORITES_IDENTIFIER_PREFIX}${field.name}`);
451
- return fieldInfos;
452
- };
453
- const setHasAny = (set, lookup) => {
454
- for (const key of lookup) {
455
- if (set.has(key))
456
- return true;
457
- }
458
- return false;
459
- };
460
- const addOrderInfos = (dest, source) => {
461
- source.forEach((si) => {
462
- const index = dest.findIndex((di) => di.name === si.name);
463
- if (index === -1) {
464
- si.orderedTimestamp = new Date();
465
- dest.push(si);
466
- }
467
- });
468
- let priority = dest.length;
469
- dest.forEach((info) => info.priority = priority--);
470
- };
471
- const removeOrderInfos = (container, toRemove) => {
472
- toRemove.forEach((roi) => {
473
- const index = container.findIndex((oi) => oi.name === roi.name);
474
- /* istanbul ignore else */
475
- if (index >= 0)
476
- container.splice(index, 1);
477
- });
478
- };
479
- /** @internal */
480
- export const createFieldOrderInfos = (field) => {
481
- if (field.isNestedContentField()) {
482
- const propertyName = getNestedContentFieldPropertyName(field);
483
- return [{
484
- parentClassName: getPropertyClassName(propertyName),
485
- name: propertyName,
486
- priority: 0,
487
- orderedTimestamp: new Date(),
488
- }];
489
- }
490
- if (field.isPropertiesField()) {
491
- return getPropertiesFieldPropertyNames(field).map((propertyName) => ({
492
- parentClassName: getPropertyClassName(propertyName),
493
- name: propertyName,
494
- priority: 0,
495
- orderedTimestamp: new Date(),
496
- }));
497
- }
498
- return [{
499
- parentClassName: undefined,
500
- name: field.name,
501
- priority: 0,
502
- orderedTimestamp: new Date(),
503
- }];
504
- };
505
- const getFieldOrderInfos = (field, orderInfos) => {
506
- const fieldOrderInfos = [];
507
- const tryAddOrderInfo = (name) => {
508
- const fieldOrderInfo = orderInfos.find((oi) => oi.name === name);
509
- if (fieldOrderInfo !== undefined)
510
- fieldOrderInfos.push(fieldOrderInfo);
511
- };
512
- if (field.isPropertiesField())
513
- getPropertiesFieldPropertyNames(field).forEach(tryAddOrderInfo);
514
- else if (field.isNestedContentField())
515
- tryAddOrderInfo(getNestedContentFieldPropertyName(field));
516
- else
517
- tryAddOrderInfo(field.name);
518
- return fieldOrderInfos;
519
- };
520
- const getMostRecentOrderInfo = (orderInfos) => orderInfos.reduce((recent, curr) => (recent && recent.orderedTimestamp >= curr.orderedTimestamp) ? recent : curr);
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ /** @packageDocumentation
6
+ * @module Core
7
+ */
8
+ import { BeEvent, isIDisposable } from "@itwin/core-bentley";
9
+ import { QueryRowFormat } from "@itwin/core-common";
10
+ /**
11
+ * Scopes that favorite properties can be stored in.
12
+ * @public
13
+ */
14
+ export var FavoritePropertiesScope;
15
+ (function (FavoritePropertiesScope) {
16
+ FavoritePropertiesScope[FavoritePropertiesScope["Global"] = 0] = "Global";
17
+ FavoritePropertiesScope[FavoritePropertiesScope["ITwin"] = 1] = "ITwin";
18
+ FavoritePropertiesScope[FavoritePropertiesScope["IModel"] = 2] = "IModel";
19
+ })(FavoritePropertiesScope || (FavoritePropertiesScope = {}));
20
+ /**
21
+ * The favorite property manager which lets to store favorite properties
22
+ * and check if field contains favorite properties.
23
+ *
24
+ * @public
25
+ */
26
+ export class FavoritePropertiesManager {
27
+ constructor(props) {
28
+ /** Event raised after favorite properties have changed. */
29
+ this.onFavoritesChanged = new BeEvent();
30
+ /**
31
+ * Initialize favorite properties for the provided IModelConnection.
32
+ */
33
+ this.initializeConnection = async (imodel) => {
34
+ const imodelId = imodel.iModelId;
35
+ const iTwinId = imodel.iTwinId;
36
+ if (this._globalProperties === undefined)
37
+ this._globalProperties = await this._storage.loadProperties() || new Set();
38
+ if (!this._iTwinProperties.has(iTwinId)) {
39
+ const iTwinProperties = await this._storage.loadProperties(iTwinId) || new Set();
40
+ this._iTwinProperties.set(iTwinId, iTwinProperties);
41
+ }
42
+ if (!this._imodelProperties.has(getiModelInfo(iTwinId, imodelId))) {
43
+ const imodelProperties = await this._storage.loadProperties(iTwinId, imodelId) || new Set();
44
+ this._imodelProperties.set(getiModelInfo(iTwinId, imodelId), imodelProperties);
45
+ }
46
+ const propertiesOrder = await this._storage.loadPropertiesOrder(iTwinId, imodelId) || [];
47
+ this._propertiesOrder.set(getiModelInfo(iTwinId, imodelId), propertiesOrder);
48
+ await this._adjustPropertyOrderInfos(iTwinId, imodelId);
49
+ };
50
+ /**
51
+ * Function that removes order information of properties that are no longer
52
+ * favorited and adds missing order information for favorited properties.
53
+ */
54
+ this._adjustPropertyOrderInfos = async (iTwinId, imodelId) => {
55
+ const propertiesOrder = this._propertiesOrder.get(getiModelInfo(iTwinId, imodelId));
56
+ const globalProperties = this._globalProperties;
57
+ const iTwinProperties = this._iTwinProperties.get(iTwinId);
58
+ const imodelProperties = this._imodelProperties.get(getiModelInfo(iTwinId, imodelId));
59
+ // favorite property infos that need to be added to the propertiesOrder array
60
+ const infosToAdd = new Set([...globalProperties, ...iTwinProperties, ...imodelProperties]);
61
+ for (let i = propertiesOrder.length - 1; i >= 0; i--) {
62
+ if (infosToAdd.has(propertiesOrder[i].name))
63
+ infosToAdd.delete(propertiesOrder[i].name);
64
+ else
65
+ propertiesOrder.splice(i, 1);
66
+ }
67
+ infosToAdd.forEach((info) => propertiesOrder.push({
68
+ name: info,
69
+ parentClassName: getPropertyClassName(info),
70
+ orderedTimestamp: new Date(),
71
+ priority: 0,
72
+ }));
73
+ let priority = propertiesOrder.length;
74
+ propertiesOrder.forEach((oi) => oi.priority = priority--);
75
+ };
76
+ /**
77
+ * Sorts an array of fields with respect to favorite property order.
78
+ * Non-favorited fields get sorted by their default priority and always have lower priority than favorited fields.
79
+ * @param imodel IModelConnection.
80
+ * @param fields Array of Field's that needs to be sorted.
81
+ * @note `initializeConnection` must be called with the `imodel` before calling this function.
82
+ */
83
+ this.sortFields = (imodel, fields) => {
84
+ this.validateInitialization(imodel);
85
+ const iTwinId = imodel.iTwinId;
86
+ const imodelId = imodel.iModelId;
87
+ const fieldPriority = new Map();
88
+ fields.forEach((field) => fieldPriority.set(field, this.getFieldPriority(field, iTwinId, imodelId)));
89
+ const sortFunction = (left, right) => {
90
+ const lp = fieldPriority.get(left);
91
+ const rp = fieldPriority.get(right);
92
+ return lp < rp ? 1 :
93
+ lp > rp ? -1 :
94
+ left.priority < right.priority ? 1 : // if favorite fields have equal priorities, sort by field priority
95
+ left.priority > right.priority ? -1 :
96
+ left.name.localeCompare(right.name);
97
+ };
98
+ return fields.sort(sortFunction);
99
+ };
100
+ this._getBaseClassesByClass = async (imodel, neededClasses) => {
101
+ const iTwinId = imodel.iTwinId;
102
+ const imodelId = imodel.iModelId;
103
+ const imodelInfo = getiModelInfo(iTwinId, imodelId);
104
+ let baseClasses;
105
+ if (this._imodelBaseClassesByClass.has(imodelInfo))
106
+ baseClasses = this._imodelBaseClassesByClass.get(imodelInfo);
107
+ else
108
+ this._imodelBaseClassesByClass.set(imodelInfo, baseClasses = {});
109
+ const missingClasses = new Set();
110
+ neededClasses.forEach((className) => {
111
+ if (!baseClasses.hasOwnProperty(className))
112
+ missingClasses.add(className);
113
+ });
114
+ if (missingClasses.size === 0)
115
+ return baseClasses;
116
+ const query = `
117
+ SELECT (derivedSchema.Name || ':' || derivedClass.Name) AS "ClassFullName", (baseSchema.Name || ':' || baseClass.Name) AS "BaseClassFullName"
118
+ FROM ECDbMeta.ClassHasAllBaseClasses baseClassRels
119
+ INNER JOIN ECDbMeta.ECClassDef derivedClass ON derivedClass.ECInstanceId = baseClassRels.SourceECInstanceId
120
+ INNER JOIN ECDbMeta.ECSchemaDef derivedSchema ON derivedSchema.ECInstanceId = derivedClass.Schema.Id
121
+ INNER JOIN ECDbMeta.ECClassDef baseClass ON baseClass.ECInstanceId = baseClassRels.TargetECInstanceId
122
+ INNER JOIN ECDbMeta.ECSchemaDef baseSchema ON baseSchema.ECInstanceId = baseClass.Schema.Id
123
+ WHERE (derivedSchema.Name || ':' || derivedClass.Name) IN (${[...missingClasses].map((className) => `'${className}'`).join(",")})`;
124
+ // eslint-disable-next-line deprecation/deprecation
125
+ for await (const row of imodel.query(query, undefined, { rowFormat: QueryRowFormat.UseJsPropertyNames })) {
126
+ if (!(row.classFullName in baseClasses))
127
+ baseClasses[row.classFullName] = [];
128
+ baseClasses[row.classFullName].push(row.baseClassFullName);
129
+ }
130
+ return baseClasses;
131
+ };
132
+ this._storage = props.storage;
133
+ this._iTwinProperties = new Map();
134
+ this._imodelProperties = new Map();
135
+ this._propertiesOrder = new Map();
136
+ this._imodelBaseClassesByClass = new Map();
137
+ }
138
+ dispose() {
139
+ // istanbul ignore else
140
+ if (isIDisposable(this._storage))
141
+ this._storage.dispose();
142
+ }
143
+ validateInitialization(imodel) {
144
+ const iTwinId = imodel.iTwinId;
145
+ const imodelId = imodel.iModelId;
146
+ if (!this._imodelProperties.has(getiModelInfo(iTwinId, imodelId)))
147
+ throw Error(`Favorite properties are not initialized for iModel: '${imodelId}', in iTwin: '${iTwinId}'. Call initializeConnection() with an IModelConnection to initialize.`);
148
+ }
149
+ /**
150
+ * Adds favorite properties into a certain scope.
151
+ * @param field Field that contains properties. If field contains multiple properties, all of them will be favorited.
152
+ * @param imodel IModelConnection.
153
+ * @param scope FavoritePropertiesScope to put the favorite properties into.
154
+ * @note `initializeConnection` must be called with the `imodel` before calling this function.
155
+ */
156
+ async add(field, imodel, scope) {
157
+ this.validateInitialization(imodel);
158
+ const iTwinId = imodel.iTwinId;
159
+ const imodelId = imodel.iModelId;
160
+ let favoriteProperties;
161
+ let saveProperties;
162
+ switch (scope) {
163
+ case FavoritePropertiesScope.Global:
164
+ favoriteProperties = this._globalProperties;
165
+ saveProperties = async (properties) => this._storage.saveProperties(properties);
166
+ break;
167
+ case FavoritePropertiesScope.ITwin:
168
+ favoriteProperties = this._iTwinProperties.get(iTwinId);
169
+ saveProperties = async (properties) => this._storage.saveProperties(properties, iTwinId);
170
+ break;
171
+ default:
172
+ favoriteProperties = this._imodelProperties.get(getiModelInfo(iTwinId, imodelId));
173
+ saveProperties = async (properties) => this._storage.saveProperties(properties, iTwinId, imodelId);
174
+ }
175
+ const countBefore = favoriteProperties.size;
176
+ const fieldInfos = getFieldInfos(field);
177
+ fieldInfos.forEach((info) => favoriteProperties.add(info));
178
+ if (favoriteProperties.size !== countBefore) {
179
+ const saves = [];
180
+ saves.push(saveProperties(favoriteProperties));
181
+ const propertiesOrder = this._propertiesOrder.get(getiModelInfo(iTwinId, imodelId));
182
+ addOrderInfos(propertiesOrder, createFieldOrderInfos(field));
183
+ saves.push(this._storage.savePropertiesOrder(propertiesOrder, iTwinId, imodelId));
184
+ await Promise.all(saves);
185
+ this.onFavoritesChanged.raiseEvent();
186
+ }
187
+ }
188
+ /**
189
+ * Removes favorite properties from a scope specified and all the more general scopes.
190
+ * @param field Field that contains properties. If field contains multiple properties, all of them will be un-favorited.
191
+ * @param imodel IModelConnection.
192
+ * @param scope FavoritePropertiesScope to remove the favorite properties from. It also removes from more general scopes.
193
+ * @note `initializeConnection` must be called with the `imodel` before calling this function.
194
+ */
195
+ async remove(field, imodel, scope) {
196
+ this.validateInitialization(imodel);
197
+ const iTwinId = imodel.iTwinId;
198
+ const imodelId = imodel.iModelId;
199
+ const fieldInfos = getFieldInfos(field);
200
+ const workingScopes = [];
201
+ workingScopes.push({
202
+ properties: this._globalProperties,
203
+ save: async (properties) => this._storage.saveProperties(properties),
204
+ });
205
+ if (scope === FavoritePropertiesScope.ITwin || scope === FavoritePropertiesScope.IModel) {
206
+ workingScopes.push({
207
+ properties: this._iTwinProperties.get(iTwinId),
208
+ save: async (properties) => this._storage.saveProperties(properties, iTwinId),
209
+ });
210
+ }
211
+ if (scope === FavoritePropertiesScope.IModel) {
212
+ workingScopes.push({
213
+ properties: this._imodelProperties.get(getiModelInfo(iTwinId, imodelId)),
214
+ save: async (properties) => this._storage.saveProperties(properties, iTwinId, imodelId),
215
+ });
216
+ }
217
+ const saves = [];
218
+ let favoritesChanged = false;
219
+ for (const { properties, save } of workingScopes) {
220
+ const countBefore = properties.size;
221
+ fieldInfos.forEach((info) => properties.delete(info));
222
+ if (properties.size !== countBefore) {
223
+ saves.push(save(properties));
224
+ favoritesChanged = true;
225
+ }
226
+ }
227
+ if (!favoritesChanged)
228
+ return;
229
+ const propertiesOrder = this._propertiesOrder.get(getiModelInfo(iTwinId, imodelId));
230
+ removeOrderInfos(propertiesOrder, createFieldOrderInfos(field));
231
+ saves.push(this._storage.savePropertiesOrder(propertiesOrder, iTwinId, imodelId));
232
+ await Promise.all(saves);
233
+ this.onFavoritesChanged.raiseEvent();
234
+ }
235
+ /**
236
+ * Removes all favorite properties from a certain scope.
237
+ * @param imodel IModelConnection.
238
+ * @param scope FavoritePropertiesScope to remove the favorite properties from.
239
+ * @note `initializeConnection` must be called with the `imodel` before calling this function.
240
+ */
241
+ async clear(imodel, scope) {
242
+ this.validateInitialization(imodel);
243
+ const iTwinId = imodel.iTwinId;
244
+ const imodelId = imodel.iModelId;
245
+ let favoriteProperties;
246
+ let saveProperties;
247
+ switch (scope) {
248
+ case FavoritePropertiesScope.Global:
249
+ favoriteProperties = this._globalProperties;
250
+ saveProperties = async () => this._storage.saveProperties(new Set());
251
+ break;
252
+ case FavoritePropertiesScope.ITwin:
253
+ favoriteProperties = this._iTwinProperties.get(iTwinId);
254
+ saveProperties = async () => this._storage.saveProperties(new Set(), iTwinId);
255
+ break;
256
+ default:
257
+ favoriteProperties = this._imodelProperties.get(getiModelInfo(iTwinId, imodelId));
258
+ saveProperties = async () => this._storage.saveProperties(new Set(), iTwinId, imodelId);
259
+ }
260
+ if (favoriteProperties.size === 0)
261
+ return;
262
+ favoriteProperties.clear();
263
+ const saves = [];
264
+ saves.push(saveProperties());
265
+ saves.push(this._adjustPropertyOrderInfos(iTwinId, imodelId));
266
+ await Promise.all(saves);
267
+ this.onFavoritesChanged.raiseEvent();
268
+ }
269
+ /**
270
+ * Check if field contains at least one favorite property.
271
+ * @param field Field that contains properties.
272
+ * @param imodel IModelConnection.
273
+ * @param scope FavoritePropertiesScope to check for favorite properties. It also checks the more general scopes.
274
+ * @note `initializeConnection` must be called with the `imodel` before calling this function.
275
+ */
276
+ has(field, imodel, scope) {
277
+ this.validateInitialization(imodel);
278
+ const iTwinId = imodel.iTwinId;
279
+ const imodelId = imodel.iModelId;
280
+ const fieldInfos = getFieldInfos(field);
281
+ return setHasAny(this._globalProperties, fieldInfos) ||
282
+ (scope !== FavoritePropertiesScope.Global && setHasAny(this._iTwinProperties.get(iTwinId), fieldInfos)) ||
283
+ (scope === FavoritePropertiesScope.IModel && setHasAny(this._imodelProperties.get(getiModelInfo(iTwinId, imodelId)), fieldInfos));
284
+ }
285
+ getFieldPriority(field, iTwinId, imodelId) {
286
+ const orderInfos = this._propertiesOrder.get(getiModelInfo(iTwinId, imodelId));
287
+ const fieldOrderInfos = getFieldOrderInfos(field, orderInfos);
288
+ if (fieldOrderInfos.length === 0)
289
+ return -1;
290
+ const mostRecent = getMostRecentOrderInfo(fieldOrderInfos);
291
+ return mostRecent.priority;
292
+ }
293
+ /** Changes field properties priorities to lower than another fields priority
294
+ * @param imodel IModelConnection.
295
+ * @param field Field that priority is being changed.
296
+ * @param afterField Field that goes before the moved field. If undefined the moving field is changed to the highest priority (to the top).
297
+ * @param visibleFields Array of fields to move the field in.
298
+ * @note `initializeConnection` must be called with the `imodel` before calling this function.
299
+ */
300
+ async changeFieldPriority(imodel, field, afterField, visibleFields) {
301
+ /**
302
+ * How it works:
303
+ * 1. Gets the orderInfo's for `field` (`orderInfo`) and `afterField` (`afterOrderInfo`) by selecting the most recent order informations for each field
304
+ * 2. Iterates all orderInfo's that are in between `afterOrderInfo` and `orderInfo` when sorted by priority
305
+ * 3. For each iterated orderInfo it checks if it is relevant:
306
+ * 3.1. If orderInfo belongs to a primitive property, orderInfo is relevant
307
+ * 3.2. If orderInfo's field is visible, orderInfo is relevant
308
+ * 3.3. If orderInfo's class has a base class or itself in previously labeled relevant orderInfo's, orderInfo is relevant
309
+ * 3.4. If 3.1 - 3.3 don't pass, orderInfo is irrelevant
310
+ * 4. Irrelevant orderInfos's get moved after `orderInfo` (depends on the direction)
311
+ * 5. All `field` orderInfo's get moved after `afterOrderInfo`
312
+ */
313
+ this.validateInitialization(imodel);
314
+ const iTwinId = imodel.iTwinId;
315
+ const imodelId = imodel.iModelId;
316
+ if (field === afterField)
317
+ throw Error("`field` can not be the same as `afterField`.");
318
+ const allOrderInfos = this._propertiesOrder.get(getiModelInfo(iTwinId, imodelId));
319
+ const findFieldOrderInfoData = (f) => {
320
+ if (!visibleFields.includes(f))
321
+ throw Error("Field is not contained in visible fields.");
322
+ const infos = getFieldOrderInfos(f, allOrderInfos);
323
+ if (infos.length === 0)
324
+ throw Error("Field has no property order information.");
325
+ const info = getMostRecentOrderInfo(infos);
326
+ const index = allOrderInfos.indexOf(info);
327
+ return { infos, mostRecent: { info, index } };
328
+ };
329
+ const { infos: movingOrderInfos, mostRecent: { index: orderInfoIndex } } = findFieldOrderInfoData(field);
330
+ let afterOrderInfo;
331
+ let afterOrderInfoIndex;
332
+ if (afterField === undefined) {
333
+ afterOrderInfo = undefined;
334
+ afterOrderInfoIndex = -1;
335
+ }
336
+ else {
337
+ ({ mostRecent: { info: afterOrderInfo, index: afterOrderInfoIndex } } = findFieldOrderInfoData(afterField));
338
+ }
339
+ let direction; // where to go from `afterOrderInfo` to `orderInfo`
340
+ let startIndex;
341
+ if (orderInfoIndex < afterOrderInfoIndex) {
342
+ direction = Direction.Up;
343
+ startIndex = afterOrderInfoIndex;
344
+ }
345
+ else {
346
+ direction = Direction.Down;
347
+ startIndex = afterOrderInfoIndex + 1;
348
+ }
349
+ const neededClassNames = allOrderInfos.reduce((classNames, oi) => {
350
+ if (oi.parentClassName)
351
+ classNames.add(oi.parentClassName);
352
+ return classNames;
353
+ }, new Set());
354
+ const baseClassesByClass = await this._getBaseClassesByClass(imodel, neededClassNames);
355
+ const visibleOrderInfos = visibleFields.reduce((union, currField) => union.concat(getFieldOrderInfos(currField, allOrderInfos)), []);
356
+ const irrelevantOrderInfos = []; // orderInfos's that won't change their logical order in respect to other properties
357
+ const relevantClasses = new Set(); // currently relevant classes
358
+ for (let i = startIndex; i !== orderInfoIndex; i += direction) {
359
+ const currOrderInfo = allOrderInfos[i];
360
+ // primitive properties are always relevant, because we can't determine their relevance based on the class hierarchy
361
+ if (currOrderInfo.parentClassName === undefined)
362
+ continue;
363
+ const visible = visibleOrderInfos.includes(currOrderInfo);
364
+ if (visible) {
365
+ relevantClasses.add(currOrderInfo.parentClassName);
366
+ continue;
367
+ }
368
+ const hasBaseClasses = baseClassesByClass[currOrderInfo.parentClassName].some((classId) => relevantClasses.has(classId));
369
+ if (hasBaseClasses)
370
+ continue;
371
+ if (direction === Direction.Down)
372
+ irrelevantOrderInfos.push(currOrderInfo);
373
+ else
374
+ irrelevantOrderInfos.unshift(currOrderInfo);
375
+ }
376
+ // remove irrelevantOrderInfo's to add them after the `orderInfo`
377
+ irrelevantOrderInfos.forEach((foi) => {
378
+ const index = allOrderInfos.findIndex((oi) => oi.parentClassName === foi.parentClassName && oi.name === foi.name);
379
+ allOrderInfos.splice(index, 1);
380
+ });
381
+ // remove movingOrderInfos's to add them after the `afterOrderInfo`
382
+ movingOrderInfos.forEach((foi) => {
383
+ const index = allOrderInfos.findIndex((oi) => oi.parentClassName === foi.parentClassName && oi.name === foi.name);
384
+ allOrderInfos.splice(index, 1);
385
+ });
386
+ movingOrderInfos.forEach((oi) => oi.orderedTimestamp = new Date());
387
+ afterOrderInfoIndex = afterOrderInfo === undefined ? -1 : allOrderInfos.indexOf(afterOrderInfo);
388
+ allOrderInfos.splice(afterOrderInfoIndex + 1, 0, ...movingOrderInfos);
389
+ allOrderInfos.splice(afterOrderInfoIndex + 1 + (direction === Direction.Up ? movingOrderInfos.length : 0), 0, ...irrelevantOrderInfos);
390
+ // reassign priority numbers
391
+ let priority = allOrderInfos.length;
392
+ allOrderInfos.forEach((oi) => oi.priority = priority--);
393
+ await this._storage.savePropertiesOrder(allOrderInfos, iTwinId, imodelId);
394
+ this.onFavoritesChanged.raiseEvent();
395
+ }
396
+ }
397
+ /**
398
+ * Used in tests to avoid collisions between multiple runs using the same storage
399
+ * @internal
400
+ */
401
+ FavoritePropertiesManager.FAVORITES_IDENTIFIER_PREFIX = "";
402
+ var Direction;
403
+ (function (Direction) {
404
+ Direction[Direction["Up"] = -1] = "Up";
405
+ Direction[Direction["Down"] = 1] = "Down";
406
+ })(Direction || (Direction = {}));
407
+ const getiModelInfo = (iTwinId, imodelId) => `${iTwinId}/${imodelId}`;
408
+ const getPropertiesFieldPropertyNames = (field) => {
409
+ const nestingPrefix = getNestingPrefix(field.parent);
410
+ return field.properties.map((property) => `${FavoritePropertiesManager.FAVORITES_IDENTIFIER_PREFIX}${nestingPrefix}${property.property.classInfo.name}:${property.property.name}`);
411
+ };
412
+ const getNestedContentFieldPropertyName = (field) => {
413
+ const nestingPrefix = getNestingPrefix(field);
414
+ return `${FavoritePropertiesManager.FAVORITES_IDENTIFIER_PREFIX}${nestingPrefix}${field.contentClassInfo.name}`;
415
+ };
416
+ const getNestingPrefix = (field) => {
417
+ const path = [];
418
+ let curr = field;
419
+ while (curr !== undefined) {
420
+ curr.pathToPrimaryClass.forEach((rel) => {
421
+ // Relationship directions are reversed, because we are generating a relationship list starting from the parent
422
+ path.push(`${rel.isForwardRelationship ? "B" : "F"}:${rel.relationshipInfo.name}`);
423
+ path.push(rel.targetClassInfo.name);
424
+ });
425
+ curr = curr.parent;
426
+ }
427
+ if (path.length === 0)
428
+ return "";
429
+ path.reverse();
430
+ return `${path.join("-")}-`;
431
+ };
432
+ const getPropertyClassName = (propertyName) => {
433
+ const propertyNameStart = propertyName.split("-")[0];
434
+ const parts = propertyNameStart.split(":").length;
435
+ if (parts === 1) // primitive
436
+ return undefined;
437
+ if (parts === 2) // nested property OR nested property parent class OR regular property parent class
438
+ return propertyNameStart;
439
+ // regular property without parent class
440
+ return propertyNameStart.substr(0, propertyName.lastIndexOf(":"));
441
+ };
442
+ /** @internal */
443
+ export const getFieldInfos = (field) => {
444
+ const fieldInfos = new Set();
445
+ if (field.isPropertiesField())
446
+ getPropertiesFieldPropertyNames(field).forEach((info) => fieldInfos.add(info));
447
+ else if (field.isNestedContentField())
448
+ fieldInfos.add(getNestedContentFieldPropertyName(field));
449
+ else
450
+ fieldInfos.add(`${FavoritePropertiesManager.FAVORITES_IDENTIFIER_PREFIX}${field.name}`);
451
+ return fieldInfos;
452
+ };
453
+ const setHasAny = (set, lookup) => {
454
+ for (const key of lookup) {
455
+ if (set.has(key))
456
+ return true;
457
+ }
458
+ return false;
459
+ };
460
+ const addOrderInfos = (dest, source) => {
461
+ source.forEach((si) => {
462
+ const index = dest.findIndex((di) => di.name === si.name);
463
+ if (index === -1) {
464
+ si.orderedTimestamp = new Date();
465
+ dest.push(si);
466
+ }
467
+ });
468
+ let priority = dest.length;
469
+ dest.forEach((info) => info.priority = priority--);
470
+ };
471
+ const removeOrderInfos = (container, toRemove) => {
472
+ toRemove.forEach((roi) => {
473
+ const index = container.findIndex((oi) => oi.name === roi.name);
474
+ /* istanbul ignore else */
475
+ if (index >= 0)
476
+ container.splice(index, 1);
477
+ });
478
+ };
479
+ /** @internal */
480
+ export const createFieldOrderInfos = (field) => {
481
+ if (field.isNestedContentField()) {
482
+ const propertyName = getNestedContentFieldPropertyName(field);
483
+ return [{
484
+ parentClassName: getPropertyClassName(propertyName),
485
+ name: propertyName,
486
+ priority: 0,
487
+ orderedTimestamp: new Date(),
488
+ }];
489
+ }
490
+ if (field.isPropertiesField()) {
491
+ return getPropertiesFieldPropertyNames(field).map((propertyName) => ({
492
+ parentClassName: getPropertyClassName(propertyName),
493
+ name: propertyName,
494
+ priority: 0,
495
+ orderedTimestamp: new Date(),
496
+ }));
497
+ }
498
+ return [{
499
+ parentClassName: undefined,
500
+ name: field.name,
501
+ priority: 0,
502
+ orderedTimestamp: new Date(),
503
+ }];
504
+ };
505
+ const getFieldOrderInfos = (field, orderInfos) => {
506
+ const fieldOrderInfos = [];
507
+ const tryAddOrderInfo = (name) => {
508
+ const fieldOrderInfo = orderInfos.find((oi) => oi.name === name);
509
+ if (fieldOrderInfo !== undefined)
510
+ fieldOrderInfos.push(fieldOrderInfo);
511
+ };
512
+ if (field.isPropertiesField())
513
+ getPropertiesFieldPropertyNames(field).forEach(tryAddOrderInfo);
514
+ else if (field.isNestedContentField())
515
+ tryAddOrderInfo(getNestedContentFieldPropertyName(field));
516
+ else
517
+ tryAddOrderInfo(field.name);
518
+ return fieldOrderInfos;
519
+ };
520
+ const getMostRecentOrderInfo = (orderInfos) => orderInfos.reduce((recent, curr) => (recent && recent.orderedTimestamp >= curr.orderedTimestamp) ? recent : curr);
521
521
  //# sourceMappingURL=FavoritePropertiesManager.js.map