@itwin/presentation-frontend 4.0.0-dev.21 → 4.0.0-dev.23

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