-
Notifications
You must be signed in to change notification settings - Fork 208
/
Presentation.ts
207 lines (184 loc) · 7.05 KB
/
Presentation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Core
*/
import { IModelApp } from "@itwin/core-frontend";
import { Localization } from "@itwin/core-common";
import { PresentationError, PresentationStatus } from "@itwin/presentation-common";
import { FavoritePropertiesManager, FavoritePropertiesManagerProps } from "./favorite-properties/FavoritePropertiesManager";
import { createFavoritePropertiesStorage, DefaultFavoritePropertiesStorageTypes } from "./favorite-properties/FavoritePropertiesStorage";
import { FrontendLocalizationHelper } from "./LocalizationHelper";
import { PresentationManager, PresentationManagerProps } from "./PresentationManager";
import { SelectionManager, SelectionManagerProps } from "./selection/SelectionManager";
import { SelectionScopesManager } from "./selection/SelectionScopesManager";
let localization: Localization | undefined;
let presentationManager: PresentationManager | undefined;
let selectionManager: SelectionManager | undefined;
let favoritePropertiesManager: FavoritePropertiesManager | undefined;
const initializationHandlers: Array<() => Promise<(() => void) | void>> = [];
const terminationHandlers: Array<() => void> = [];
/**
* Props for initializing [[Presentation]].
* @public
*/
export interface PresentationProps {
/** Props for [[PresentationManager]]. */
presentation?: PresentationManagerProps;
/** Props for [[SelectionManager]]. */
selection?: Partial<SelectionManagerProps>;
/** Props for [[FavoritePropertiesManager]]. */
favorites?: FavoritePropertiesManagerProps;
}
/**
* Static class used to statically set up Presentation library for the frontend.
* Basically what it does is:
* - Create a singleton [[PresentationManager]] instance
* - Create a singleton [[SelectionManager]] instance
* - Create a singleton [[FavoritePropertiesManager]]] instance
*
* @public
*/
export class Presentation {
/* istanbul ignore next */
private constructor() {}
/**
* Initializes Presentation library for the frontend.
*
* Example:
* ``` ts
* [[include:Presentation.Frontend.Initialization]]
* ```
*
* The method should be called after a call to [IModelApp.startup]($core-frontend).
*/
public static async initialize(props?: PresentationProps): Promise<void> {
if (!IModelApp.initialized) {
throw new PresentationError(PresentationStatus.NotInitialized, "IModelApp.startup must be called before calling Presentation.initialize");
}
if (!localization) {
localization = IModelApp.localization;
}
if (!presentationManager) {
const managerProps = props?.presentation ?? {};
if (!managerProps.activeLocale) {
const languages = Presentation.localization.getLanguageList();
managerProps.activeLocale = languages.length ? languages[0] : undefined;
}
presentationManager = PresentationManager.create(managerProps);
}
if (!selectionManager) {
selectionManager = new SelectionManager({
...props?.selection,
scopes:
props?.selection?.scopes ??
new SelectionScopesManager({
rpcRequestsHandler: presentationManager.rpcRequestsHandler,
localeProvider: () => this.presentation.activeLocale,
}),
});
}
if (!favoritePropertiesManager) {
favoritePropertiesManager = new FavoritePropertiesManager({
storage: props?.favorites ? props.favorites.storage : createFavoritePropertiesStorage(DefaultFavoritePropertiesStorageTypes.Noop),
});
}
presentationManager.startIModelInitialization = (imodel) => favoritePropertiesManager?.startConnectionInitialization(imodel);
presentationManager.ensureIModelInitialized = async (imodel) => favoritePropertiesManager?.ensureInitialized(imodel);
await FrontendLocalizationHelper.registerNamespaces();
for (const handler of initializationHandlers) {
const cleanup = await handler();
if (cleanup) {
terminationHandlers.push(cleanup);
}
}
}
/**
* Terminates Presentation library frontend. This method should be called
* before a call to [IModelApp.shutdown]($core-frontend)
*/
public static terminate(): void {
terminationHandlers.forEach((handler) => handler());
terminationHandlers.length = 0;
if (localization) {
FrontendLocalizationHelper.unregisterNamespaces();
}
if (presentationManager) {
presentationManager.dispose();
}
presentationManager = undefined;
if (favoritePropertiesManager) {
favoritePropertiesManager.dispose();
}
favoritePropertiesManager = undefined;
if (selectionManager) {
selectionManager.dispose();
}
selectionManager = undefined;
localization = undefined;
}
/**
* Registers an additional handler which will be invoked during Presentation library frontend
* initialization.
*/
public static registerInitializationHandler(handler: () => Promise<() => void>): void {
initializationHandlers.push(handler);
}
/** The singleton [[PresentationManager]] */
public static get presentation(): PresentationManager {
if (!presentationManager) {
throw new Error("Presentation must be first initialized by calling Presentation.initialize");
}
return presentationManager;
}
/** @internal */
public static setPresentationManager(value: PresentationManager) {
if (presentationManager) {
presentationManager.dispose();
}
presentationManager = value;
}
/** The singleton [[SelectionManager]] */
public static get selection(): SelectionManager {
if (!selectionManager) {
throw new Error("Presentation must be first initialized by calling Presentation.initialize");
}
return selectionManager;
}
/** @internal */
public static setSelectionManager(value: SelectionManager) {
selectionManager = value;
}
/**
* The singleton [[FavoritePropertiesManager]]
* @public
*/
public static get favoriteProperties(): FavoritePropertiesManager {
if (!favoritePropertiesManager) {
throw new Error("Favorite Properties must be first initialized by calling Presentation.initialize");
}
return favoritePropertiesManager;
}
/** @internal */
public static setFavoritePropertiesManager(value: FavoritePropertiesManager) {
if (favoritePropertiesManager) {
favoritePropertiesManager.dispose();
}
favoritePropertiesManager = value;
}
/**
* The localization manager used by Presentation frontend. Returns the result of `IModelApp.i18n`.
*/
public static get localization(): Localization {
if (!localization) {
throw new Error("Presentation must be first initialized by calling Presentation.initialize");
}
return localization;
}
/** @internal */
public static setLocalization(value: Localization) {
localization = value;
}
}