/
Category.ts
81 lines (75 loc) · 2.76 KB
/
Category.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Content
*/
import { RendererDescription } from "./Renderer";
/**
* A data structure that describes a [[Field]] category.
* @public
*/
export interface CategoryDescription {
/** Unique name */
name: string;
/** Display label */
label: string;
/** Extensive description */
description: string;
/** Priority. Categories with higher priority should appear higher in the UI */
priority: number;
/** Should this category be auto-expanded when it's displayed in the UI */
expand: boolean;
/** Parent category description */
parent?: CategoryDescription;
/** Custom renderer description */
renderer?: RendererDescription;
}
/** @public */
export namespace CategoryDescription {
/** Serialize given category to JSON */
export function toJSON(category: CategoryDescription): CategoryDescriptionJSON {
const { parent, ...rest } = category;
return {
...rest,
...(parent ? { parent: parent.name } : undefined),
};
}
/**
* Deserialize [[CategoryDescription]] from JSON. The `parent` is not assigned - use [[CategoryDescription.listFromJSON]]
* to deserialize the whole categories list and set parents.
*/
export function fromJSON(json: CategoryDescriptionJSON): CategoryDescription {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { parent, ...rest } = json;
return rest;
}
function createCategoriesHierarchy(json: CategoryDescriptionJSON, categoriesMap: Map<string, CategoryDescription>): CategoryDescription {
const category = categoriesMap.get(json.name)!;
if (json.parent) {
// note: mutating categories in the `categoriesMap`
category.parent = categoriesMap.get(json.parent);
}
return category;
}
/** Deserialize a list of [[CategoryDescription]] from JSON. */
export function listFromJSON(json: CategoryDescriptionJSON[]): CategoryDescription[] {
const categoriesMap = new Map<string, CategoryDescription>();
json.forEach((categoryJson) => categoriesMap.set(categoryJson.name, CategoryDescription.fromJSON(categoryJson)));
return json.map((categoryJson) => createCategoriesHierarchy(categoryJson, categoriesMap));
}
}
/**
* Serialized [[CategoryDescription]] JSON representation.
* @public
*/
export interface CategoryDescriptionJSON {
name: string;
label: string;
description: string;
priority: number;
expand: boolean;
parent?: string;
renderer?: RendererDescription;
}