-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathcomposite-menu-node.ts
114 lines (90 loc) · 4.2 KB
/
composite-menu-node.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
// *****************************************************************************
// Copyright (C) 2022 Ericsson and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { Disposable } from '../disposable';
import { CompoundMenuNode, CompoundMenuNodeRole, MenuNode, MutableCompoundMenuNode, SubMenuOptions } from './menu-types';
/**
* Node representing a (sub)menu in the menu tree structure.
*/
export class CompositeMenuNode implements MutableCompoundMenuNode {
protected readonly _children: MenuNode[] = [];
public iconClass?: string;
public order?: string;
protected _when?: string;
protected _role?: CompoundMenuNodeRole;
constructor(
public readonly id: string,
public label?: string,
options?: SubMenuOptions,
readonly parent?: MenuNode & CompoundMenuNode,
) {
this.updateOptions(options);
}
get when(): string | undefined { return this._when; }
get icon(): string | undefined { return this.iconClass; }
get children(): ReadonlyArray<MenuNode> { return this._children; }
get role(): CompoundMenuNodeRole { return this._role ?? (this.label ? CompoundMenuNodeRole.Submenu : CompoundMenuNodeRole.Group); }
addNode(node: MenuNode): Disposable {
this._children.push(node);
this._children.sort(CompoundMenuNode.sortChildren);
return {
dispose: () => {
const idx = this._children.indexOf(node);
if (idx >= 0) {
this._children.splice(idx, 1);
}
}
};
}
removeNode(id: string): void {
const idx = this._children.findIndex(n => n.id === id);
if (idx >= 0) {
this._children.splice(idx, 1);
}
}
updateOptions(options?: SubMenuOptions): void {
if (options) {
this.iconClass ??= options.icon ?? options.iconClass;
this.label ??= options.label;
this.order ??= options.order;
this._role ??= options.role;
this._when ??= options.when;
}
}
get sortString(): string {
return this.order || this.id;
}
get isSubmenu(): boolean {
return Boolean(this.label);
}
/** @deprecated @since 1.28 use CompoundMenuNode.isNavigationGroup instead */
static isNavigationGroup = CompoundMenuNode.isNavigationGroup;
}
export class CompositeMenuNodeWrapper implements MutableCompoundMenuNode {
constructor(protected readonly wrapped: Readonly<MutableCompoundMenuNode>, readonly parent: CompoundMenuNode, protected readonly options?: SubMenuOptions) { }
get id(): string { return this.wrapped.id; }
get label(): string | undefined { return this.wrapped.label; }
get sortString(): string { return this.options?.order || this.wrapped.sortString; }
get isSubmenu(): boolean { return Boolean(this.label); }
get role(): CompoundMenuNodeRole { return this.options?.role ?? this.wrapped.role; }
get icon(): string | undefined { return this.iconClass; }
get iconClass(): string | undefined { return this.options?.iconClass ?? this.wrapped.icon; }
get order(): string | undefined { return this.sortString; }
get when(): string | undefined { return this.options?.when ?? this.wrapped.when; }
get children(): ReadonlyArray<MenuNode> { return this.wrapped.children; }
addNode(node: MenuNode): Disposable { return this.wrapped.addNode(node); }
removeNode(id: string): void { return this.wrapped.removeNode(id); }
updateOptions(options: SubMenuOptions): void { return this.wrapped.updateOptions(options); }
}