forked from imodeljs/simple-viewer-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
299 lines (268 loc) · 11.8 KB
/
App.tsx
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2019 Bentley Systems, Incorporated. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license terms.
*--------------------------------------------------------------------------------------------*/
import * as React from "react";
import { Id64String, OpenMode } from "@bentley/bentleyjs-core";
import { AccessToken, ConnectClient, IModelQuery, Project, Config } from "@bentley/imodeljs-clients";
import { IModelApp, IModelConnection, FrontendRequestContext, AuthorizedFrontendRequestContext } from "@bentley/imodeljs-frontend";
import { Presentation, SelectionChangeEventArgs, ISelectionProvider } from "@bentley/presentation-frontend";
import { Button, ButtonSize, ButtonType, Spinner, SpinnerSize } from "@bentley/ui-core";
import { SignIn } from "@bentley/ui-components";
import { SimpleViewerApp } from "../api/SimpleViewerApp";
import PropertiesWidget from "./Properties";
import GridWidget from "./Table";
import TreeWidget from "./Tree";
import ViewportContentControl from "./Viewport";
import "@bentley/icons-generic-webfont/dist/bentley-icons-generic-webfont.css";
import "./App.css";
import { Sum } from "calculator-frontend";
// tslint:disable: no-console
// cSpell:ignore imodels
/** React state of the App component */
export interface AppState {
user: {
accessToken?: AccessToken;
isLoading?: boolean;
};
offlineIModel: boolean;
imodel?: IModelConnection;
viewDefinitionId?: Id64String;
}
/** A component the renders the whole application UI */
export default class App extends React.Component<{}, AppState> {
/** Creates an App instance */
constructor(props?: any, context?: any) {
super(props, context);
this.state = {
user: {
isLoading: false,
accessToken: undefined,
},
offlineIModel: false,
};
}
public componentDidMount() {
// subscribe for unified selection changes
Presentation.selection.selectionChange.addListener(this._onSelectionChanged);
// Initialize authorization state, and add listener to changes
SimpleViewerApp.oidcClient.onUserStateChanged.addListener(this._onUserStateChanged);
if (SimpleViewerApp.oidcClient.isAuthorized) {
SimpleViewerApp.oidcClient.getAccessToken(new FrontendRequestContext()) // tslint:disable-line: no-floating-promises
.then((accessToken: AccessToken | undefined) => {
this.setState((prev) => ({ user: { ...prev.user, accessToken, isLoading: false } }));
});
}
}
public componentWillUnmount() {
// unsubscribe from unified selection changes
Presentation.selection.selectionChange.removeListener(this._onSelectionChanged);
// unsubscribe from user state changes
SimpleViewerApp.oidcClient.onUserStateChanged.removeListener(this._onUserStateChanged);
}
private _onSelectionChanged = (evt: SelectionChangeEventArgs, selectionProvider: ISelectionProvider) => {
const s = new Sum();
s.Add(15);
s.Subtract(1.3);
s.TestBind();
const sumVal = s.GetValue();
console.log(sumVal);
s.Dispose();
const selection = selectionProvider.getSelection(evt.imodel, evt.level);
if (selection.isEmpty) {
console.log("========== Selection cleared ==========");
} else {
console.log("========== Selection change ===========");
if (selection.instanceKeys.size !== 0) {
// log all selected ECInstance ids grouped by ECClass name
console.log("ECInstances:");
selection.instanceKeys.forEach((ids, ecclass) => {
console.log(`${ecclass}: [${[...ids].join(",")}]`);
});
}
if (selection.nodeKeys.size !== 0) {
// log all selected node keys
console.log("Nodes:");
selection.nodeKeys.forEach((key) => console.log(JSON.stringify(key)));
}
console.log("=======================================");
}
}
private _onRegister = () => {
window.open("https://imodeljs.github.io/iModelJs-docs-output/getting-started/#developer-registration", "_blank");
}
private _onOffline = () => {
this.setState((prev) => ({ user: { ...prev.user, isLoading: false }, offlineIModel: true }));
}
private _onStartSignin = async () => {
this.setState((prev) => ({ user: { ...prev.user, isLoading: true } }));
await SimpleViewerApp.oidcClient.signIn(new FrontendRequestContext());
}
private _onUserStateChanged = (accessToken: AccessToken | undefined) => {
this.setState((prev) => ({ user: { ...prev.user, accessToken, isLoading: false } }));
}
/** Pick the first available spatial view definition in the imodel */
private async getFirstViewDefinitionId(imodel: IModelConnection): Promise<Id64String> {
const viewSpecs = await imodel.views.queryProps({});
const acceptedViewClasses = [
"BisCore:SpatialViewDefinition",
"BisCore:DrawingViewDefinition",
];
const acceptedViewSpecs = viewSpecs.filter((spec) => (-1 !== acceptedViewClasses.indexOf(spec.classFullName)));
if (0 === acceptedViewSpecs.length)
throw new Error("No valid view definitions in imodel");
// Prefer spatial view over drawing.
const spatialViews = acceptedViewSpecs.filter((v) => {
return v.classFullName === "BisCore:SpatialViewDefinition";
});
if (spatialViews.length > 0)
return spatialViews[0].id!;
return acceptedViewSpecs[0].id!;
}
/** Handle iModel open event */
private _onIModelSelected = async (imodel: IModelConnection | undefined) => {
if (!imodel) {
// reset the state when imodel is closed
this.setState({ imodel: undefined, viewDefinitionId: undefined });
return;
}
try {
// attempt to get a view definition
const viewDefinitionId = imodel ? await this.getFirstViewDefinitionId(imodel) : undefined;
this.setState({ imodel, viewDefinitionId });
} catch (e) {
// if failed, close the imodel and reset the state
if (this.state.offlineIModel) {
await imodel.closeSnapshot();
} else {
await imodel.close();
}
this.setState({ imodel: undefined, viewDefinitionId: undefined });
alert(e.message);
}
}
private get _signInRedirectUri() {
const split = (Config.App.get("imjs_browser_test_redirect_uri") as string).split("://");
return split[split.length - 1];
}
/** The component's render method */
public render() {
let ui: React.ReactNode;
if (this.state.user.isLoading || window.location.href.includes(this._signInRedirectUri)) {
// if user is currently being loaded, just tell that
ui = `${IModelApp.i18n.translate("SimpleViewer:signing-in")}...`;
} else if (!this.state.user.accessToken && !this.state.offlineIModel) {
// if user doesn't have and access token, show sign in page
ui = (<SignIn onSignIn={this._onStartSignin} onRegister={this._onRegister} onOffline={this._onOffline} />);
} else if (!this.state.imodel || !this.state.viewDefinitionId) {
// if we don't have an imodel / view definition id - render a button that initiates imodel open
ui = (<OpenIModelButton accessToken={this.state.user.accessToken} offlineIModel={this.state.offlineIModel} onIModelSelected={this._onIModelSelected} />);
} else {
// if we do have an imodel and view definition id - render imodel components
ui = (<IModelComponents imodel={this.state.imodel} viewDefinitionId={this.state.viewDefinitionId} />);
}
// render the app
return (
<div className="app">
<div className="app-header">
<h2>{IModelApp.i18n.translate("SimpleViewer:welcome-message")}</h2>
</div>
{ui}
</div>
);
}
}
/** React props for [[OpenIModelButton]] component */
interface OpenIModelButtonProps {
accessToken: AccessToken | undefined;
offlineIModel: boolean;
onIModelSelected: (imodel: IModelConnection | undefined) => void;
}
/** React state for [[OpenIModelButton]] component */
interface OpenIModelButtonState {
isLoading: boolean;
}
/** Renders a button that opens an iModel identified in configuration */
class OpenIModelButton extends React.PureComponent<OpenIModelButtonProps, OpenIModelButtonState> {
public state = { isLoading: false };
/** Finds project and imodel ids using their names */
private async getIModelInfo(): Promise<{ projectId: string, imodelId: string }> {
const projectName = Config.App.get("imjs_test_project");
const imodelName = Config.App.get("imjs_test_imodel");
const requestContext: AuthorizedFrontendRequestContext = await AuthorizedFrontendRequestContext.create();
const connectClient = new ConnectClient();
let project: Project;
try {
project = await connectClient.getProject(requestContext, { $filter: `Name+eq+'${projectName}'` });
} catch (e) {
throw new Error(`Project with name "${projectName}" does not exist`);
}
const imodelQuery = new IModelQuery();
imodelQuery.byName(imodelName);
const imodels = await IModelApp.iModelClient.iModels.get(requestContext, project.wsgId, imodelQuery);
if (imodels.length === 0)
throw new Error(`iModel with name "${imodelName}" does not exist in project "${projectName}"`);
return { projectId: project.wsgId, imodelId: imodels[0].wsgId };
}
/** Handle iModel open event */
private async onIModelSelected(imodel: IModelConnection | undefined) {
this.props.onIModelSelected(imodel);
this.setState({ isLoading: false });
}
private _onClick = async () => {
this.setState({ isLoading: true });
let imodel: IModelConnection | undefined;
try {
// attempt to open the imodel
if (this.props.offlineIModel) {
const offlineIModel = Config.App.getString("imjs_offline_imodel");
imodel = await IModelConnection.openSnapshot(offlineIModel);
} else {
const info = await this.getIModelInfo();
imodel = await IModelConnection.open(info.projectId, info.imodelId, OpenMode.Readonly);
}
} catch (e) {
alert(e.message);
}
await this.onIModelSelected(imodel);
}
public render() {
return (
<Button size={ButtonSize.Large} buttonType={ButtonType.Primary} className="button-open-imodel" onClick={this._onClick}>
<span>{IModelApp.i18n.translate("SimpleViewer:components.imodel-picker.open-imodel")}</span>
{this.state.isLoading ? <span style={{ marginLeft: "8px" }}><Spinner size={SpinnerSize.Small} /></span> : undefined}
</Button>
);
}
}
/** React props for [[IModelComponents]] component */
interface IModelComponentsProps {
imodel: IModelConnection;
viewDefinitionId: Id64String;
}
/** Renders a viewport, a tree, a property grid and a table */
class IModelComponents extends React.PureComponent<IModelComponentsProps> {
public render() {
// ID of the presentation ruleset used by all of the controls; the ruleset
// can be found at `assets/presentation_rules/Default.PresentationRuleSet.xml`
const rulesetId = "Default";
return (
<div className="app-content">
<div className="top-left">
<ViewportContentControl imodel={this.props.imodel} rulesetId={rulesetId} viewDefinitionId={this.props.viewDefinitionId} />
</div>
<div className="right">
<div className="top">
<TreeWidget imodel={this.props.imodel} rulesetId={rulesetId} />
</div>
<div className="bottom">
<PropertiesWidget imodel={this.props.imodel} rulesetId={rulesetId} />
</div>
</div>
<div className="bottom">
<GridWidget imodel={this.props.imodel} rulesetId={rulesetId} />
</div>
</div>
);
}
}