-
Notifications
You must be signed in to change notification settings - Fork 2
/
StageContentLayout.ts
119 lines (107 loc) · 3.82 KB
/
StageContentLayout.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module ContentView
*/
import type { IModelConnection, ViewState } from "@itwin/core-frontend";
import type {
ContentCallback,
ContentGroup,
ContentGroupProps,
} from "./ContentGroup";
import type { ContentLayoutDef } from "./ContentLayout";
import type { ViewStateHelperProps } from "./ViewStateHelper";
import { ViewStateHelper } from "./ViewStateHelper";
import type { ContentLayoutProps } from "@itwin/appui-abstract";
/** StageContentLayoutProps interface for sharing view layout information.
* @public
*/
export interface StageContentLayoutProps {
contentLayoutProps?: ContentLayoutProps;
contentGroupProps: ContentGroupProps;
viewStateProps: ViewStateHelperProps[];
}
/** ViewLayout interface for sharing view layout information.
* @public
*/
export interface ViewLayout {
contentLayoutDef: ContentLayoutDef;
contentGroup: ContentGroup;
viewStates: Array<ViewState | undefined>;
}
/** StageContentLayout class. Used to serialize/deserialize a View Layout with Saved Views.
* @public
*/
export class StageContentLayout {
/** Create props for a View Layout */
public static viewLayoutToProps(
contentLayoutDef: ContentLayoutDef,
contentGroup: ContentGroup,
emphasizeElements: boolean = false,
contentCallback?: ContentCallback
): StageContentLayoutProps {
const contentLayoutProps = contentLayoutDef.toJSON();
// update layout in contentGroup to contain latest values from contentLayoutDef this way we don't need to save both.
const contentGroupProps = {
...contentGroup.toJSON(contentCallback),
layout: contentLayoutProps,
};
const viewStateProps = new Array<ViewStateHelperProps>();
const viewports = contentGroup.getViewports();
for (const viewport of viewports) {
// istanbul ignore else
if (viewport) {
const savedViewProps = ViewStateHelper.viewStateToProps(viewport.view);
if (emphasizeElements)
ViewStateHelper.emphasizeElementsToProps(viewport, savedViewProps);
viewStateProps.push(savedViewProps);
}
}
const savedViewLayoutProps: StageContentLayoutProps = {
contentGroupProps,
viewStateProps,
};
return savedViewLayoutProps;
}
/** Create an array of ViewStates from the StageContentLayout */
public static async viewStatesFromProps(
iModelConnection: IModelConnection,
savedProps: StageContentLayoutProps
): Promise<Array<ViewState | undefined>> {
const viewStates = new Array<ViewState | undefined>();
if (savedProps.viewStateProps) {
for (const savedViewProps of savedProps.viewStateProps) {
const viewState = await ViewStateHelper.viewStateFromProps(
iModelConnection,
savedViewProps
);
viewStates.push(viewState);
}
}
return viewStates;
}
/** Apply EmphasizeElements from the SavedView */
public static emphasizeElementsFromProps(
contentGroup: ContentGroup,
savedProps: StageContentLayoutProps
): boolean {
const changedList = new Array<boolean>();
const viewports = contentGroup.getViewports();
let index = 0;
for (const savedViewProps of savedProps.viewStateProps) {
const viewport = viewports[index];
// istanbul ignore else
if (viewport) {
const changed = ViewStateHelper.emphasizeElementsFromProps(
viewport,
savedViewProps
);
changedList.push(changed);
}
index++;
}
return changedList.some((changed: boolean) => changed);
}
}