-
Notifications
You must be signed in to change notification settings - Fork 2
/
WidgetManager.ts
177 lines (158 loc) · 5.03 KB
/
WidgetManager.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Widget
*/
import { BeUiEvent, Logger } from "@itwin/core-bentley";
import { UiFramework } from "../UiFramework";
import { WidgetDef } from "./WidgetDef";
import { createStableWidgetDef } from "./StableWidgetDef";
import { StagePanelLocation } from "../stagepanels/StagePanelLocation";
import { StagePanelSection } from "../stagepanels/StagePanelSection";
import { UiItemsManager } from "../ui-items-provider/UiItemsManager";
/** Information about WidgetDefs in the WidgetManager
* @internal
*/
export interface WidgetInfo {
widgetDef: WidgetDef;
stageId?: string;
stageUsage?: string;
location: StagePanelLocation;
section: StagePanelSection;
}
/** Arguments of [[this.onWidgetsChanged]] event.
* @internal
*/
export interface WidgetsChangedEventArgs {
readonly items: ReadonlyArray<WidgetInfo>;
}
function getWidgetManagerStableWidgetId(
stageUsage: string | undefined,
location: StagePanelLocation,
section: StagePanelSection,
index: number
) {
return `uifw-wm-${stageUsage || ""}-${StagePanelLocation[location]}-${
StagePanelSection[section]
}-${index}`;
}
/** Widget Manager class.
* @beta
*/
export class WidgetManager {
private _widgets: ReadonlyArray<WidgetInfo> = [];
/** Event raised when Widgets are changed.
* @internal
*/
public readonly onWidgetsChanged = new BeUiEvent<WidgetsChangedEventArgs>();
/** @internal */
public get widgetCount(): number {
return this._widgets.length;
}
/** @internal */
public get widgets(): ReadonlyArray<WidgetInfo> {
return this._widgets;
}
public set widgets(w: ReadonlyArray<WidgetInfo>) {
this._widgets = w;
this.onWidgetsChanged.emit({ items: w });
}
/** Adds a WidgetDef for use in a Frontstage.
* @note Added `widgetDef` must return unique id to correctly save/restore App layout.
* Semi-stable id is generated when auto-generated `widgetDef` id is detected,
* but correctness of such id depends on `addWidgetDef` call order and widget location.
*/
public addWidgetDef(
widgetDef: WidgetDef,
stageId: string | undefined,
stageUsage: string | undefined,
location: StagePanelLocation,
section?: StagePanelSection
): boolean {
if (stageId === undefined && stageUsage === undefined) {
Logger.logError(
UiFramework.loggerCategory(this),
`addWidgetDef: stageId or stageUsage param must be specified`
);
return false;
}
section = section !== undefined ? section : StagePanelSection.Start;
const index = this._widgets.reduce((acc, info) => {
if (
info.stageId === stageId &&
info.stageUsage === stageUsage &&
info.location === location &&
info.section === section
)
return acc + 1;
return acc;
}, 0);
const stableId = getWidgetManagerStableWidgetId(
stageUsage,
location,
section,
index
);
const stableWidget = createStableWidgetDef(widgetDef, stableId);
const newWidget: WidgetInfo = {
widgetDef: stableWidget,
stageId,
stageUsage,
location,
section,
};
const oldWidgets = this._widgets.filter(
(info) => info.widgetDef.id !== newWidget.widgetDef.id
);
const updatedWidgets = [...oldWidgets, newWidget];
this.widgets = updatedWidgets;
return true;
}
/** Removes a WidgetDef.
*/
public removeWidgetDef(widgetId: string): boolean {
let result = false;
const updatedWidgets = this._widgets.filter(
(info) => info.widgetDef.id !== widgetId
);
if (updatedWidgets.length !== this._widgets.length) {
this.widgets = updatedWidgets;
result = true;
}
return result;
}
/** Gets WidgetDefs for a Frontstage location.
*/
public getWidgetDefs(
stageId: string,
stageUsage: string,
location: StagePanelLocation,
section?: StagePanelSection
): ReadonlyArray<WidgetDef> | undefined {
const definedSection =
section === undefined ? StagePanelSection.Start : section;
const widgetInfos = this._widgets.filter((info) => {
return (
(!info.stageId || info.stageId === stageId) &&
(!info.stageUsage || info.stageUsage === stageUsage) &&
info.location === location &&
info.section === definedSection
);
});
const widgetDefs = widgetInfos.map((info) => info.widgetDef);
// Consult the UiItemsManager to get any Abstract widgets
const widgets = UiItemsManager.getWidgets(
stageId,
stageUsage,
location,
definedSection
);
widgets.forEach((abstractProps) => {
const widgetDef = WidgetDef.create(abstractProps);
widgetDefs.push(widgetDef);
});
return widgetDefs.length > 0 ? widgetDefs : undefined;
}
}