/
UiLayoutDataProvider.ts
214 lines (188 loc) · 7.57 KB
/
UiLayoutDataProvider.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
208
209
210
211
212
213
214
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Dialog
*/
import { BeUiEvent } from "@itwin/core-bentley";
import { PropertyEditorParams, PropertyEditorParamTypes, SuppressLabelEditorParams } from "../properties/EditorParams";
import { PropertyRecord } from "../properties/Record";
import { PrimitiveValue, PropertyValueFormat } from "../properties/Value";
import { BaseDialogItem, DialogItem, DialogPropertyItem, DialogPropertySyncItem } from "./DialogItem";
import { PropertyChangeResult, PropertyChangeStatus, UiDataProvider } from "./UiDataProvider";
/** Enum for button types. Determines button label, and default button style.
* @public
*/
export enum DialogButtonType {
None = "",
Close = "close",
OK = "ok",
Cancel = "cancel",
Yes = "yes",
No = "no",
Retry = "retry",
Next = "next",
Previous = "previous"
}
/** Enum for button style.
* @public
*/
export enum DialogButtonStyle {
None = "",
Primary = "iui-cta",
Hollow = "iui-default",
Blue = "iui-high-visibility",
}
/** Interface for a dialog button in a button cluster
* @public
*/
export interface DialogButtonDef {
/** type of button */
type: DialogButtonType;
/** Triggered on button click */
onClick: () => void;
/** Which button style to decorate button width */
buttonStyle?: DialogButtonStyle;
/** Disable the button */
disabled?: boolean;
/** Custom label */
label?: string;
/** Custom CSS class */
className?: string;
}
/** [[DialogRow]] is the interface that groups dialog items into rows for building UI
* @public
*/
export interface DialogRow {
priority: number;
items: DialogItem[];
}
/**
* @public
*/
export abstract class UiLayoutDataProvider extends UiDataProvider {
private _items: ReadonlyArray<DialogItem> | undefined;
/** Applies changes from one or more properties - some dialogs will use this to send a bulk set of changes back to the provider */
public override processChangesInUi(properties: DialogPropertyItem[]): PropertyChangeResult {
// Default implementation is to just pass each property to applyUiPropertyChange
properties.forEach((property) => this.applyUiPropertyChange(property));
return { status: PropertyChangeStatus.Success };
}
/** Applies change of a single property - this is the default method used when property editors are dynamically generated. */
// istanbul ignore next
public applyUiPropertyChange = (_updatedValue: DialogPropertySyncItem): void => {
throw (new Error("Derived UiDataProvider should implement this to apply change to a single property."));
};
private _rows: DialogRow[] | undefined;
/** Array of dialog rows */
public get rows(): DialogRow[] {
if (!this._rows) {
this._rows = this.layoutDialogRows();
}
return this._rows;
}
protected loadItemsInternal(items: ReadonlyArray<DialogItem> | undefined) {
this._items = items ? items : [];
this._rows = this.layoutDialogRows();
}
/** Called by UI to request available properties that can be bound to user supplied UI components (See Tool1UiProvider for example). */
// istanbul ignore next
public supplyDialogItems(): DialogItem[] | undefined {
throw (new Error("Derived UiDataProvider must implement this method to supply set of properties."));
}
public get items(): ReadonlyArray<DialogItem> {
if (undefined === this._items) {
this.loadItemsInternal(this.supplyDialogItems());
}
return this._items!;
}
/** Called to inform listeners that new properties are ready for display in UI. */
public reloadDialogItems(emitEvent = true) {
this.loadItemsInternal(this.supplyDialogItems());
// istanbul ignore else
if (emitEvent)
this.fireItemsReloadedEvent();
}
/**
* @internal
*/
public layoutDialogRows(): DialogRow[] {
const rows: DialogRow[] = [];
this.items.forEach((item) => {
const row = rows.find((value) => value.priority === item.editorPosition.rowPriority);
if (row) {
row.items.push(item);
} else {
rows.push({ priority: item.editorPosition.rowPriority, items: [item] });
}
});
// sort rows
rows.sort((a: DialogRow, b: DialogRow) => a.priority - b.priority);
// sort records
rows.forEach((row: DialogRow) => row.items.sort((a: DialogItem, b: DialogItem) => a.editorPosition.columnIndex - b.editorPosition.columnIndex));
return rows;
}
/** Determines if a dialog item editor wants a label */
public static editorWantsLabel(item: DialogItem): boolean {
if (item.property.editor && item.property.editor.params) {
const params = item.property.editor.params.find((param: PropertyEditorParams) => param.type === PropertyEditorParamTypes.SuppressEditorLabel) as SuppressLabelEditorParams;
// istanbul ignore else
if (params)
return false;
}
return true;
}
/** Determines if a dialog items has an associated lock property */
public static hasAssociatedLockProperty(item: DialogItem): boolean {
return !!item.lockProperty;
}
/** Gets the disabled state for a given dialog item */
public static getItemDisabledState(baseDialogItem: BaseDialogItem): boolean {
const dialogItem = baseDialogItem as DialogItem;
// istanbul ignore else
if (dialogItem === undefined || dialogItem.lockProperty === undefined)
return !!baseDialogItem.isDisabled;
const value = dialogItem.lockProperty.value;
// istanbul ignore next
if (value === undefined)
return !!baseDialogItem.isDisabled;
return !value.value;
}
/** Gets a property record for a given dialog item */
public static getPropertyRecord = (dialogItem: BaseDialogItem): PropertyRecord => {
const propertyValue = { valueFormat: PropertyValueFormat.Primitive, value: dialogItem.value.value, displayValue: dialogItem.value.displayValue };
const record = new PropertyRecord(propertyValue as PrimitiveValue, dialogItem.property);
record.isDisabled = UiLayoutDataProvider.getItemDisabledState(dialogItem);
return record;
};
/** Determines if a dialog row only contains button group editors */
public static onlyContainButtonGroupEditors(row: DialogRow): boolean {
for (const item of row.items) {
// istanbul ignore else
if (UiLayoutDataProvider.hasAssociatedLockProperty(item) || undefined === item.property.editor || "enum-buttongroup" !== item.property.editor.name || UiLayoutDataProvider.editorWantsLabel(item))
return false;
}
return true;
}
}
/** [[DialogLayoutDataProvider]] Abstract class that allows property values to be passed between hosting API and Dialog that generates and arranges components dynamically
* including the buttons at the bottom of the dialog.
* @public
*/
export abstract class DialogLayoutDataProvider extends UiLayoutDataProvider {
public onButtonsReloadedEvent = new BeUiEvent<void>();
/** Called to inform listeners that modal dialog button data needs to be refreshed. */
public fireDialogButtonsReloadEvent() {
this.onButtonsReloadedEvent.emit();
}
public supplyButtonData(): DialogButtonDef[] | undefined {
// Derived class should override
const buttons: DialogButtonDef[] = [];
// istanbul ignore next
buttons.push({ type: DialogButtonType.OK, onClick: () => { } });
// istanbul ignore next
buttons.push({ type: DialogButtonType.Cancel, onClick: () => { } });
return buttons;
}
}