-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathNavigationMenuItem.ts
More file actions
231 lines (188 loc) · 5.63 KB
/
NavigationMenuItem.ts
File metadata and controls
231 lines (188 loc) · 5.63 KB
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
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
import jsxRenderer from "@ui5/webcomponents-base/dist/renderer/JsxRenderer.js";
import i18n from "@ui5/webcomponents-base/dist/decorators/i18n.js";
import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js";
import type { ClassMap } from "@ui5/webcomponents-base/dist/types.js";
import property from "@ui5/webcomponents-base/dist/decorators/property.js";
import MenuItem from "@ui5/webcomponents/dist/MenuItem.js";
import type SideNavigationItemDesign from "./types/SideNavigationItemDesign.js";
import {
isSpace,
isEnter,
isEnterShift,
isEnterCtrl,
isEnterAlt,
} from "@ui5/webcomponents-base/dist/Keys.js";
import type SideNavigationSelectableItemBase from "./SideNavigationSelectableItemBase.js";
// Templates
import NavigationMenuItemTemplate from "./NavigationMenuItemTemplate.js";
// Styles
import navigationMenuItemCss from "./generated/themes/NavigationMenuItem.css.js";
import {
NAVIGATION_MENU_SELECTABLE_ITEM_HIDDEN_TEXT,
} from "./generated/i18n/i18n-defaults.js";
/**
* @class
*
* ### Overview
*
* `ui5-navigation-menu-item` is the item to use inside a `ui5-navigation-menu`.
* An arbitrary hierarchy structure can be represented by recursively nesting navigation menu items.
*
* ### Usage
*
* `ui5-navigation-menu-item` represents a node in a `ui5-navigation-menu`. The navigation menu itself is rendered as a list,
* and each `ui5-navigation-menu-item` is represented by a list item in that list. Therefore, you should only use
* `ui5-navigation-menu-item` directly in your apps. The `ui5-li` list item is internal for the list, and not intended for public use.
*
* ### ES6 Module Import
*
* `import "@ui5/webcomponents-fiori/dist/NavigationMenuItem.js";`
* @constructor
* @extends MenuItem
* @since 1.22.0
* @private
*/
@customElement({
renderer: jsxRenderer,
tag: "ui5-navigation-menu-item",
template: NavigationMenuItemTemplate,
styles: [MenuItem.styles, navigationMenuItemCss],
})
class NavigationMenuItem extends MenuItem {
/**
* Defines the link target URI. Supports standard hyperlink behavior.
* If a JavaScript action should be triggered,
* this should not be set, but instead an event handler
* for the `click` event should be registered.
* @public
* @default undefined
* @since 1.22.0
*/
@property()
href?: string;
/**
* Defines the component target.
*
* **Notes:**
*
* - `_self`
* - `_top`
* - `_blank`
* - `_parent`
* - `_search`
*
* **This property must only be used when the `href` property is set.**
* @public
* @default undefined
* @since 1.22.0
*/
@property()
target?: string;
@property()
design: `${SideNavigationItemDesign}` = "Default";
@i18n("@ui5/webcomponents-fiori")
static i18nBundleFiori: I18nBundle;
associatedItem?: SideNavigationSelectableItemBase;
get isExternalLink() {
return this.href && this.target === "_blank";
}
get _href() {
return (!this.disabled && this.href) ? this.href : undefined;
}
get _tagContainerId() {
return `${this._id}-tag-container`;
}
get _ariaDescribedByIds() {
const ids = [
`${this._id}-invisibleText-describedby`,
];
if (this.hasEndContent) {
ids.push(this._tagContainerId);
}
return ids.filter(Boolean).join(" ");
}
get _accInfo() {
const accInfo = super._accInfo;
accInfo.role = "none";
if (this.hasSubmenu && this.associatedItem?.isSelectable) {
accInfo.ariaSelectedText = NavigationMenuItem.i18nBundleFiori.getText(NAVIGATION_MENU_SELECTABLE_ITEM_HIDDEN_TEXT);
}
return accInfo;
}
get classes(): ClassMap {
const result = super.classes;
result.main["ui5-navigation-menu-item-root"] = true;
return result;
}
_onclick(e: MouseEvent) {
this._activate(e);
}
_activate(e: MouseEvent | KeyboardEvent) {
e.stopPropagation();
const item = this.associatedItem;
if (this.disabled || !item) {
return;
}
const sideNav = item.sideNavigation;
const overflowMenu = sideNav?.getOverflowPopover();
const isSelectable = item.isSelectable;
const executeEvent = item.fireDecoratorEvent("click", {
altKey: e.altKey,
ctrlKey: e.ctrlKey,
metaKey: e.metaKey,
shiftKey: e.shiftKey,
});
if (!executeEvent) {
e.preventDefault();
if (this.hasSubmenu) {
overflowMenu?._openItemSubMenu(this);
} else {
sideNav?.closeMenu();
}
return;
}
const shouldSelect = !this.hasSubmenu && isSelectable;
if (this.hasSubmenu) {
overflowMenu?._openItemSubMenu(this);
}
if (shouldSelect) {
sideNav?._selectItem(item);
}
if (!this.hasSubmenu) {
sideNav?.closeMenu(shouldSelect);
}
}
async _onkeydown(e: KeyboardEvent): Promise<void> {
if (isSpace(e)) {
e.preventDefault();
}
// "Enter" + "Meta" is missing since it is often reserved by the operating system or window manager
if (isEnter(e) || isEnterShift(e) || isEnterCtrl(e) || isEnterAlt(e)) {
this._activate(e);
}
return Promise.resolve();
}
_onkeyup(e: KeyboardEvent) {
// "Space" + modifier is often reserved by the operating system or window manager
if (isSpace(e)) {
this._activate(e);
if (this.href && !e.defaultPrevented) {
const customEvent = new MouseEvent("click");
customEvent.stopImmediatePropagation();
if (this.getDomRef()!.querySelector("a")) {
this.getDomRef()!.querySelector("a")!.dispatchEvent(customEvent);
} else {
// when Side Navigation is collapsed and it is first level item we have directly <a> element
this.getDomRef()!.dispatchEvent(customEvent);
}
}
}
}
get accessibleNameText() {
// For the submenu's dialog
return this.text ?? "";
}
}
NavigationMenuItem.define();
export default NavigationMenuItem;