/
MutableGridCategory.ts
139 lines (118 loc) · 3.92 KB
/
MutableGridCategory.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
/*---------------------------------------------------------------------------------------------
* 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 type { PropertyRecord } from "@itwin/appui-abstract";
import type { PropertyCategory } from "../../PropertyDataProvider";
import type {
IMutableFlatGridItem,
IMutableGridCategoryItem,
} from "./MutableFlatGridItem";
import {
FlatGridItemType,
MutableFlatPropertyGridItem,
} from "./MutableFlatGridItem";
import type { IMutableGridItemFactory } from "./MutableGridItemFactory";
/**
* Category name to PropertyRecord[] key pair interface used to describe which records belong to which category.
* @public
*/
export interface CategoryRecordsDict {
[categoryName: string]: PropertyRecord[];
}
/**
* Mutable wrapper object for PropertyCategory which provides methods for working with and managing category and record children hierarchies
* @public
*/
export class MutableGridCategory
extends MutableFlatPropertyGridItem
implements IMutableGridCategoryItem
{
private _children: IMutableFlatGridItem[];
private _selectionKey: string;
private _category: PropertyCategory;
constructor(
category: PropertyCategory,
recordsDict: CategoryRecordsDict,
gridItemFactory: IMutableGridItemFactory,
parentSelectionKey?: string,
depth: number = 0
) {
super(depth, parentSelectionKey, parentSelectionKey);
this._category = {
name: category.name,
label: category.label,
expand: category.expand,
};
if (parentSelectionKey !== undefined)
this._selectionKey = `${parentSelectionKey}_${category.name}`;
else this._selectionKey = category.name;
this._isExpanded = category.expand;
// Even though categories are nested and have their own depth, categorized properties depth is counted starting with the parent category.
const categoryRecords = recordsDict[category.name] ?? [];
this._children = categoryRecords.map((value) =>
gridItemFactory.createCategorizedProperty(
value,
this.selectionKey,
this.selectionKey,
0
)
);
const childCategories = category.childCategories ?? [];
const child = childCategories.map((childCategory) =>
gridItemFactory.createGridCategory(
childCategory,
recordsDict,
this.selectionKey,
this.depth + 1
)
);
this._children.push(...child);
// Assign lastInNumberOfCategories and isLastInRootCategory for entire children hierarchy
this.lastInNumberOfCategories = 0;
this.isLastInRootCategory = this.isRootCategory;
}
/**
* Category is considered to be root category if its depth is 0
*/
public get isRootCategory() {
return this.depth === 0;
}
public get selectionKey() {
return this._selectionKey;
}
public get type(): FlatGridItemType.Category {
return FlatGridItemType.Category;
}
public get name() {
return this._category.name;
}
public get label() {
return this._category.label;
}
public get derivedCategory(): PropertyCategory {
return { ...this._category, expand: this.isExpanded };
}
public getSelf() {
return this;
}
public getChildren(): IMutableFlatGridItem[] {
return this._children;
}
/**
* Gets and Sets lastInNumberOfCategories.
* Setter increments set value by one to account self as category.
* New value is sent down to this items last child
* @internal
*/
public override get lastInNumberOfCategories(): number {
if (this.isExpanded && this.getChildren().length > 0) return 0;
return this._lastInNumberOfCategories - 1;
}
public override set lastInNumberOfCategories(value: number) {
super.lastInNumberOfCategories = value + 1;
}
}