-
Notifications
You must be signed in to change notification settings - Fork 208
/
Description.ts
303 lines (271 loc) · 8.82 KB
/
Description.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
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
300
301
302
303
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Properties
*/
import {
BasePropertyEditorParams, ColorEditorParams, ImageCheckBoxParams, PropertyEditorParams, PropertyEditorParamTypes, RangeEditorParams,
SuppressLabelEditorParams,
} from "./EditorParams";
import { StandardEditorNames } from "./StandardEditorNames";
import { StandardTypeNames } from "./StandardTypeNames";
// cSpell:ignore Picklist
/**
* Information about an enumeration choice
* @public
*/
export interface EnumerationChoice {
label: string;
value: string | number;
}
/**
* Information about a set of enumeration choices
* @public
*/
export interface EnumerationChoicesInfo {
choices: Promise<EnumerationChoice[]> | EnumerationChoice[];
isStrict?: boolean;
maxDisplayedRows?: number;
}
/**
* Property renderer identification and customization attributes
* @public
*/
export interface PropertyRendererInfo {
name: string;
}
/**
* Information about a Property Editor
* @public
*/
export interface PropertyEditorInfo {
/** Editor name used in addition to the typename to find the registered property editor */
name?: string;
/** Editor params provided to the property editor */
params?: PropertyEditorParams[];
}
/**
* Information about a Property Converter
* @public
*/
export interface PropertyConverterInfo {
/** Converter name used in addition to the typename to find the registered property converter */
name?: string;
/** map of options for type converter */
options?: { [key: string]: any };
}
/**
* [[PropertyDescription]] contains metadata about a Property
* @public
*/
export interface PropertyDescription {
/** Name of the property description */
name: string;
/** Display label for the property description */
displayLabel: string;
/** Type name used to determine applicable Type Converter and Property Editor */
typename: string;
/** Additional information for enumerations */
enum?: EnumerationChoicesInfo;
/** Information for property renderer customization */
renderer?: PropertyRendererInfo;
/** Information for a property editor */
editor?: PropertyEditorInfo;
/** Information for a property converter */
converter?: PropertyConverterInfo;
/** Quantity type key used to look up formatting and parsing specs. This is typically either the name of a quantity type used by a tool
* or the full name of a KOQ (schema:koq).
* @alpha
*/
quantityType?: string;
/** Get the custom DataController by this name and register it with the property editor */
dataController?: string;
/** Should property label for composite (struct & array) properties be rendered. */
hideCompositePropertyLabel?: boolean;
}
/** Helper class that builds property descriptions for specific PropertyEditors and processes descriptions.
* @public
*/
export class PropertyDescriptionHelper {
/** Builds a number description with a "weight-picker" editor name
* @public
*/
public static buildWeightPickerDescription(name: string, label: string, additionalParams: BasePropertyEditorParams[] = []): PropertyDescription {
return {
name,
displayLabel: label,
typename: StandardTypeNames.Number,
editor: {
name: StandardEditorNames.WeightPicker,
params: additionalParams,
},
};
}
/** Builds an editor that uses [NumberInput]($core-react) control
* @public
*/
public static buildNumberEditorDescription(name: string, label: string, overrideParams?: RangeEditorParams, additionalParams: BasePropertyEditorParams[] = []): PropertyDescription {
const editorParams = [{
type: PropertyEditorParamTypes.Range,
step: 1,
precision: 0,
...overrideParams,
} as RangeEditorParams, ...additionalParams];
const editor = {
name: StandardEditorNames.NumericInput,
params: editorParams,
};
return {
name,
displayLabel: label,
typename: StandardTypeNames.Number,
editor,
};
}
/** Builds a string description
* @public
*/
public static buildTextEditorDescription(name: string, label: string, additionalParams: BasePropertyEditorParams[] = []): PropertyDescription {
const editor = {
params: additionalParams,
};
return {
name,
displayLabel: label,
typename: StandardTypeNames.String,
editor,
};
}
/** Builds an enum description
* @public
*/
public static buildEnumPicklistEditorDescription(name: string, label: string,
choices: Promise<EnumerationChoice[]> | EnumerationChoice[],
additionalParams: BasePropertyEditorParams[] = []): PropertyDescription {
const editor = additionalParams.length ? {
params: additionalParams,
} : undefined;
return {
name,
displayLabel: label,
typename: StandardTypeNames.Enum,
editor,
enum: {
choices,
},
};
}
/** Builds a number description for a tool settings or dialog property that will display a "color-picker" control.
* @public
*/
public static buildColorPickerDescription(name: string, label: string, colorValues: number[], numColumns: number,
additionalParams: BasePropertyEditorParams[] = []): PropertyDescription {
const editorParams = [
{
type: PropertyEditorParamTypes.ColorData,
colorValues,
numColumns,
} as ColorEditorParams,
...additionalParams,
];
return {
name,
displayLabel: label,
typename: StandardTypeNames.Number,
editor: {
name: StandardEditorNames.ColorPicker,
params: editorParams,
},
};
}
/** Builds a boolean description for a tool settings or dialog property that will display a "toggle" control.
* @public
*/
public static buildToggleDescription(name: string, label: string, additionalParams: BasePropertyEditorParams[] = []): PropertyDescription {
return {
name,
displayLabel: label,
typename: StandardTypeNames.Boolean,
editor: {
name: StandardEditorNames.Toggle,
params: additionalParams,
},
};
}
/** Builds a boolean description for a tool settings or dialog property that will display a "image-check-box" control.
* @public
*/
public static buildImageCheckBoxDescription(name: string, label: string, imageOff: string, imageOn: string, additionalParams: BasePropertyEditorParams[] = []): PropertyDescription {
const editorParams = [{
type: PropertyEditorParamTypes.CheckBoxImages,
imageOff,
imageOn,
} as ImageCheckBoxParams, ...additionalParams];
return {
name,
displayLabel: label,
typename: StandardTypeNames.Boolean,
editor: {
name: StandardEditorNames.ImageCheckBox,
params: editorParams,
},
};
}
/** Builds a boolean description for a tool settings or dialog property that will display a checkbox control.
* @public
*/
public static buildCheckboxDescription(name: string, label: string, additionalParams: BasePropertyEditorParams[] = []): PropertyDescription {
const editor = {
params: additionalParams,
};
return {
name,
displayLabel: label,
typename: StandardTypeNames.Boolean,
editor,
};
}
/** Builds a property description for a tool settings or dialog `lock` property. This will create a checkbox control with no label.
* @public
*/
public static buildLockPropertyDescription(name: string, additionalParams: BasePropertyEditorParams[] = []): PropertyDescription {
const defaultParams = {
type: PropertyEditorParamTypes.SuppressEditorLabel,
suppressLabelPlaceholder: true,
} as SuppressLabelEditorParams;
const editor = {
params: [defaultParams, ...additionalParams],
};
return {
name,
displayLabel: "",
typename: StandardTypeNames.Boolean,
editor,
};
}
/** Bumps an enum property description value
* @public
*/
public static async bumpEnumProperty(description: PropertyDescription, value: string | number): Promise<string | number> {
let choices: EnumerationChoice[] | undefined;
if (description.enum) {
if (description.enum.choices instanceof Promise) {
choices = await description.enum.choices;
} else {
choices = description.enum.choices;
}
}
if (!choices || choices.length === 0)
return value;
let choiceIndex = choices.findIndex((choice) => choice.value === value);
if (choiceIndex < 0)
return value;
choiceIndex++;
if (choiceIndex >= choices.length)
choiceIndex = 0;
const newValue = choices[choiceIndex].value;
return newValue;
}
}