/
PropertyGridModel.ts
127 lines (112 loc) · 4.11 KB
/
PropertyGridModel.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
/*---------------------------------------------------------------------------------------------
* 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 type { PropertyCategory, PropertyData } from "../PropertyDataProvider";
import type { FlatGridItem, GridCategoryItem } from "./flat-items/FlatGridItem";
import type {
IMutableFlatGridItem,
IMutableGridCategoryItem,
} from "./flat-items/MutableFlatGridItem";
import type { IMutableGridItemFactory } from "./flat-items/MutableGridItemFactory";
/**
* PropertyGridModel interface for working with immutable FlatGridItems
* @public
*/
export interface IPropertyGridModel {
getItem: (selectionKey: string) => FlatGridItem;
getRootCategories: () => GridCategoryItem[];
getFlatGrid: () => FlatGridItem[];
getVisibleFlatGrid: () => FlatGridItem[];
}
/**
* PropertyGridModel interface for working with mutable FlatGridItems
* @public
*/
export interface IMutablePropertyGridModel {
getItem: (selectionKey: string) => IMutableFlatGridItem;
getRootCategories: () => IMutableGridCategoryItem[];
getFlatGrid: () => IMutableFlatGridItem[];
getVisibleFlatGrid: () => IMutableFlatGridItem[];
}
/**
* Implementation of PropertyGridModel for working with and converting PropertyData to mutable FlatGridItems
* @public
*/
export class MutablePropertyGridModel
implements IPropertyGridModel, IMutablePropertyGridModel
{
public [immerable] = true;
private _categories: IMutableGridCategoryItem[];
public constructor(
propertyData: PropertyData,
private _gridItemFactory: IMutableGridItemFactory
) {
this._categories = propertyData.categories.map(
(category: PropertyCategory) =>
this._gridItemFactory.createGridCategory(category, propertyData.records)
);
}
/**
* Retrieves grid item from model.
* @param selectionKey unique key for identifying item to retrieve.
*/
public getItem(selectionKey: string) {
const item = this.findItem(this._categories, selectionKey);
if (!item)
throw new Error(`Grid item at provided key not found: ${selectionKey}`);
return item;
}
/**
* Walks the grid item hierarchy and finds item matching key
* @param items items and their descendants to check
* @param selectionKey unique key for identifying item
* @returns FlatGridItem if items with key exists, undefined otherwise
*/
private findItem(
items: IMutableFlatGridItem[],
selectionKey: string
): IMutableFlatGridItem | undefined {
for (const item of items) {
// Each items key is prefixed with it's parent key, so we can ignore the ones that aren't prefixed to reduce checking unnecessary branches.
if (!selectionKey.startsWith(item.selectionKey)) continue;
if (item.selectionKey === selectionKey) return item;
const foundItem = this.findItem(item.getChildren(), selectionKey);
if (foundItem) return foundItem;
}
return undefined;
}
/**
* Gets all GridCategoryItems that do not have parent categories.
* @returns array of GridCategoryItems
*/
public getRootCategories(): IMutableGridCategoryItem[] {
return this._categories;
}
/**
* Gets an array of all FlatGridItems.
* @returns 1-Dimensional array of GridCategories and CategorizedProperties
*/
public getFlatGrid(): IMutableFlatGridItem[] {
const flatGrid: IMutableFlatGridItem[] = [];
this._categories.forEach((category) =>
flatGrid.push(...category.getDescendantsAndSelf())
);
return flatGrid;
}
/**
* Gets an array of all currently visible FlatGridItems.
* @returns 1-Dimensional array of GridCategories and CategorizedProperties
*/
public getVisibleFlatGrid(): IMutableFlatGridItem[] {
const visibleItems: IMutableFlatGridItem[] = [];
this._categories.forEach((category) =>
visibleItems.push(...category.getVisibleDescendantsAndSelf())
);
return visibleItems;
}
}