/
MutableFlatGridItem.ts
348 lines (298 loc) · 10.3 KB
/
MutableFlatGridItem.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module PropertyGrid
*/
import { immerable } from "immer";
import shortid from "shortid";
import type { PropertyDescription } from "@itwin/appui-abstract";
import { PropertyRecord, PropertyValueFormat } from "@itwin/appui-abstract";
import type { PropertyCategory } from "../../PropertyDataProvider";
/**
* Types of FlatGridItems for differentiating what property FlatGridItem is wrapping.
* Category for PropertyCategory
* (Primitive, Array, Struct) for PropertyRecord with valueFormats: (Primitive, Array, Struct)
* @public
*/
export enum FlatGridItemType {
Category,
Primitive,
Array,
Struct,
}
/**
* Type which extracts FlatGridItemTypes relevant for working with CategorizedProperties
* @public
*/
export type CategorizedPropertyTypes =
| FlatGridItemType.Array
| FlatGridItemType.Primitive
| FlatGridItemType.Struct;
/**
* Base mutable data structure defining common methods and properties for both CategorizedProperties and GridCategoryItems
* @public
*/
export interface IMutableFlatPropertyGridItem {
readonly type: FlatGridItemType;
readonly key: string;
readonly selectionKey: string;
readonly parentSelectionKey: string | undefined;
readonly parentCategorySelectionKey: string | undefined;
readonly depth: number;
readonly label: string;
lastInNumberOfCategories: number;
isLastInRootCategory: boolean;
isExpanded: boolean;
getDescendantsAndSelf(): IMutableFlatGridItem[];
getVisibleDescendantsAndSelf(): IMutableFlatGridItem[];
getChildren(): IMutableFlatGridItem[];
getLastVisibleDescendantOrSelf(): IMutableFlatGridItem;
}
/**
* Base class for all FlatPropertyGrid items
* @public
*/
export abstract class MutableFlatPropertyGridItem
implements IMutableFlatPropertyGridItem
{
public [immerable] = true;
public readonly key: string = shortid.generate();
protected _isExpanded: boolean = false;
protected _lastInNumberOfCategories: number = 0;
protected _isLastInRootCategory: boolean = false;
public constructor(
private _depth: number,
private _parentSelectionKey: string | undefined,
private _parentCategorySelectionKey: string | undefined
) {
if (this._depth < 0) throw new Error("Depth cannot be negative");
}
public abstract type: FlatGridItemType;
public abstract label: string;
public abstract selectionKey: string;
public get depth() {
return this._depth;
}
public get parentSelectionKey() {
return this._parentSelectionKey;
}
public get parentCategorySelectionKey() {
return this._parentCategorySelectionKey;
}
public get isExpanded() {
return this._isExpanded;
}
public set isExpanded(value: boolean) {
this._isExpanded = value;
}
public abstract getChildren(): IMutableFlatGridItem[];
public abstract getSelf(): IMutableFlatGridItem;
protected getDescendants() {
const descendants: IMutableFlatGridItem[] = [];
this.getChildren().forEach((child) =>
descendants.push(...child.getDescendantsAndSelf())
);
return descendants;
}
/**
* Gets a flat list of all FlatGridItems beneath this flat grid item including itself in depth first visiting order.
*/
public getDescendantsAndSelf(): IMutableFlatGridItem[] {
return [this.getSelf(), ...this.getDescendants()];
}
/**
* Gets a flat list of visible FlatGridItems beneath this flat grid item.
*/
public getVisibleDescendants(): IMutableFlatGridItem[] {
const descendants: IMutableFlatGridItem[] = [];
if (this.isExpanded)
this.getChildren().forEach((child) =>
descendants.push(...child.getVisibleDescendantsAndSelf())
);
return descendants;
}
/**
* Gets a flat list of visible FlatGridItems beneath this flat grid item and itself in depth first visiting order.
*/
public getVisibleDescendantsAndSelf(): IMutableFlatGridItem[] {
return [this.getSelf(), ...this.getVisibleDescendants()];
}
/**
* @returns self if item is not expanded, last visible descendant of this item otherwise.
*/
public getLastVisibleDescendantOrSelf(): IMutableFlatGridItem {
if (!this.isExpanded) return this.getSelf();
const children = this.getChildren();
if (children.length < 1) return this.getSelf();
return children[children.length - 1].getLastVisibleDescendantOrSelf();
}
/**
* Sets lastInNumberOfCategories value and sends it down to this items last child
* @internal
*/
public get lastInNumberOfCategories(): number {
if (this.isExpanded && this.getChildren().length > 0) return 0;
return this._lastInNumberOfCategories;
}
public set lastInNumberOfCategories(value: number) {
this._lastInNumberOfCategories = value;
const children = this.getChildren();
if (children.length < 1) return;
children[children.length - 1].lastInNumberOfCategories = value;
}
/**
* Gets and sets isLastInRootCategory value and sends it down to this items last child
* @internal
*/
public get isLastInRootCategory(): boolean {
if (this.isExpanded && this.getChildren().length > 0) return false;
return this._isLastInRootCategory;
}
public set isLastInRootCategory(value: boolean) {
this._isLastInRootCategory = value;
const children = this.getChildren();
if (children.length < 1) return;
children[children.length - 1].isLastInRootCategory = value;
}
}
/**
* Data structure which describes methods and properties to be held by Mutable GridCategoryItems
* @public
*/
export interface IMutableGridCategoryItem extends IMutableFlatPropertyGridItem {
type: FlatGridItemType.Category;
name: string;
derivedCategory: PropertyCategory;
isRootCategory: boolean;
}
/**
* Data structure which describes methods and properties to be held by Mutable CategorizedPropertyItems
* @public
*/
export interface IMutableCategorizedPropertyItem
extends IMutableFlatPropertyGridItem {
readonly type: CategorizedPropertyTypes;
readonly derivedRecord: PropertyRecord;
readonly parentCategorySelectionKey: string;
readonly parentSelectionKey: string;
getChildren(): IMutableCategorizedPropertyItem[];
}
/**
* Type which describes mutable GridCategoryItem or CategorizedProperty
* @public
*/
export type IMutableFlatGridItem =
| IMutableCategorizedPropertyItem
| IMutableGridCategoryItem;
/**
* Base class for all Mutable CategorizedProperties
* @public
*/
export abstract class MutableCategorizedProperty
extends MutableFlatPropertyGridItem
implements Partial<IMutableCategorizedPropertyItem>
{
private _derivedRecord: PropertyRecord;
private _selectionKey: string;
public constructor(
type: CategorizedPropertyTypes,
record: PropertyRecord,
parentSelectionKey: string,
parentCategorySelectionKey: string,
depth: number,
overrideName?: string,
overrideDisplayLabel?: string
) {
super(depth, parentSelectionKey, parentCategorySelectionKey);
const recordType = this.valueTypeToFlatGridType(record.value.valueFormat);
if (recordType !== type) {
const expectedTypeStr = FlatGridItemType[type];
const actualTypeStr = FlatGridItemType[recordType];
throw Error(
`Record with incorrect value format passed to property: expected ${expectedTypeStr}, got ${actualTypeStr}`
);
}
const overridenProperty = this.makeOverridenProperty(
record.property,
overrideName,
overrideDisplayLabel
);
this._derivedRecord = this.makeDerivedRecord(record, overridenProperty);
this._selectionKey = `${this.parentSelectionKey}_${this.derivedRecord.property.name}`;
this._isExpanded = !!record.autoExpand;
}
public abstract override type: CategorizedPropertyTypes;
public abstract override getChildren(): IMutableCategorizedPropertyItem[];
/**
* Maps PropertyRecord valueFormat to FlatGridItemType
* @param valueType valueFormat to map
* @returns mapped FlatGridItemType
*/
private valueTypeToFlatGridType(valueType: PropertyValueFormat) {
switch (valueType) {
case PropertyValueFormat.Primitive:
return FlatGridItemType.Primitive;
case PropertyValueFormat.Array:
return FlatGridItemType.Array;
case PropertyValueFormat.Struct:
return FlatGridItemType.Struct;
/* istanbul ignore next */
default:
const unhandledType: never = valueType;
throw Error(`Property Value Format not handled: ${unhandledType}`);
}
}
/**
* Make new property description with overriden fields.
* @param propertyDescription property description to override.
* @param overrideName property description name to override.
* @param overrideDisplay property description display name to override.
*/
private makeOverridenProperty(
propertyDescription: PropertyDescription,
overrideName?: string,
overrideDisplay?: string
): PropertyDescription {
const { name, displayLabel, ...property } = { ...propertyDescription };
const newName = overrideName ?? name;
const newDisplayLabel = overrideDisplay ?? displayLabel;
return { ...property, name: newName, displayLabel: newDisplayLabel };
}
/**
* Gets derived property record that has it's property description field overriden
*/
private makeDerivedRecord(
record: PropertyRecord,
overridenPropertyDescription: PropertyDescription
) {
const { value, property, ...others } = record;
const newRecord = new PropertyRecord(value, overridenPropertyDescription);
return Object.assign(newRecord, others);
}
public getSelf(): IMutableCategorizedPropertyItem {
return this;
}
public override get parentCategorySelectionKey() {
return super.parentCategorySelectionKey!;
}
public override get parentSelectionKey() {
return super.parentSelectionKey!;
}
/**
* Unique selection key made of parent selectionKey and this property name.
*/
public get selectionKey() {
return this._selectionKey;
}
public get label() {
return this._derivedRecord.property.displayLabel;
}
/**
* Record with overriden property description.
*/
public get derivedRecord() {
return this._derivedRecord;
}
}