-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathSideNavigationSelectableItemBase.ts
More file actions
348 lines (297 loc) · 8.5 KB
/
SideNavigationSelectableItemBase.ts
File metadata and controls
348 lines (297 loc) · 8.5 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
import property from "@ui5/webcomponents-base/dist/decorators/property.js";
import event from "@ui5/webcomponents-base/dist/decorators/event-strict.js";
import {
isSpace,
isEnter,
isEnterShift,
isEnterCtrl,
isEnterAlt,
isLeft,
isRight,
} from "@ui5/webcomponents-base/dist/Keys.js";
import SideNavigationItemBase from "./SideNavigationItemBase.js";
import type SideNavigationItemDesign from "./types/SideNavigationItemDesign.js";
import type { AccessibilityAttributes } from "@ui5/webcomponents-base/dist/types.js";
import type { SideNavigationItemClickEventDetail } from "./SideNavigationItemBase.js";
import createInstanceChecker from "@ui5/webcomponents-base/dist/util/createInstanceChecker.js";
type SideNavigationItemAccessibilityAttributes = Pick<AccessibilityAttributes, "hasPopup">;
/**
* Fired when the component is activated either with a click/tap or by using the [Enter] or [Space] keys.
*
* @public
* @param {boolean} altKey Returns whether the "ALT" key was pressed when the event was triggered.
* @param {boolean} ctrlKey Returns whether the "CTRL" key was pressed when the event was triggered.
* @param {boolean} metaKey Returns whether the "META" key was pressed when the event was triggered.
* @param {boolean} shiftKey Returns whether the "SHIFT" key was pressed when the event was triggered.
*/
@event("click", {
bubbles: true,
cancelable: true,
})
/**
* @class
* Base class for the navigation items that support actions.
*
* @constructor
* @extends SideNavigationItemBase
* @abstract
* @public
* @since 1.24.0
*/
@customElement()
class SideNavigationSelectableItemBase extends SideNavigationItemBase {
eventDetails!: SideNavigationItemBase["eventDetails"] & {
"click": SideNavigationItemClickEventDetail
}
/**
* Defines if the item's parent is disabled.
* @private
* @default false
* @since 2.10.0
*/
@property({ type: Boolean, noAttribute: true })
_parentDisabled: boolean = false;
/**
* Defines the icon of the item.
*
* **Note:** Icons on second-level (child) navigation items are not recommended according to the design specification.
*
* The SAP-icons font provides numerous options.
*
* See all the available icons in the [Icon Explorer](https://sdk.openui5.org/test-resources/sap/m/demokit/iconExplorer/webapp/index.html).
* @public
* @default undefined
*/
@property()
icon?: string;
/**
* Defines whether the item is selected.
*
* **Note:** Items that have a set `href` and `target` set to `_blank` should not be selectable.
*
* @public
* @default false
*/
@property({ type: Boolean })
selected = false;
/**
* 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.19.0
*/
@property()
href?: string;
/**
* Defines the component target.
*
* Possible values:
*
* - `_self`
* - `_top`
* - `_blank`
* - `_parent`
* - `framename`
*
* **Note:** Items that have a defined `href` and `target`
* attribute set to `_blank` should not be selectable.
*
* @public
* @default undefined
* @since 1.19.0
*/
@property()
target?: string;
/**
* Item design.
*
* **Note:** Items with "Action" design must not have sub-items.
*
* @public
* @default "Default"
* @since 2.7.0
*/
@property()
design: `${SideNavigationItemDesign}` = "Default";
/**
* Indicates whether the navigation item is selectable. By default, all items are selectable unless specifically marked as unselectable.
*
* When a parent item is marked as unselectable, selecting it will only expand or collapse its sub-items.
* To improve user experience do not mix unselectable parent items with selectable parent items in a single side navigation.
*
*
* **Guidelines**:
* - Items with an assigned `href` and a target of `_blank` should be marked as unselectable.
* - Items that trigger actions (with design "Action") should be marked as unselectable.
*
* @public
* @default false
* @since 2.7.0
*/
@property({ type: Boolean })
unselectable = false;
/**
* Defines the additional accessibility attributes that will be applied to the component.
* The following fields are supported:
*
* - **hasPopup**: Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by the button.
* Accepts the following string values: `dialog`, `grid`, `listbox`, `menu` or `tree`.
*
* **Note:** Do not use it on parent items, as it will be overridden if the item is in the overflow menu.
*
* @public
* @default {}
* @since 2.7.0
*/
@property({ type: Object })
accessibilityAttributes: SideNavigationItemAccessibilityAttributes = {};
/**
* @private
* @default false
*/
@property({ type: Boolean })
isOverflow = false;
/**
* Reference to the original side navigation item that opened the popover.
*
* @private
*/
associatedItem?: SideNavigationItemBase;
get ariaRole() {
if (this.sideNavCollapsed) {
return this.isOverflow || this.unselectable ? "menuitem" : "menuitemradio";
}
return "treeitem";
}
get isSelectable() {
return !this.unselectable && !this.effectiveDisabled;
}
get _href() {
return (!this.effectiveDisabled && this.href) ? this.href : undefined;
}
get _target() {
return (!this.effectiveDisabled && this.href && this.target) ? this.target : undefined;
}
get isExternalLink() {
return this.href && this.target === "_blank";
}
get _selected() {
return this.selected;
}
get _effectiveTag() {
return this._href ? "a" : "div";
}
get effectiveDisabled() {
return this.disabled || this._parentDisabled;
}
get _ariaHasPopup() {
if (this.accessibilityAttributes?.hasPopup) {
return this.accessibilityAttributes.hasPopup;
}
return undefined;
}
get classesArray() {
const classes = [];
if (this.effectiveDisabled) {
classes.push("ui5-sn-item-disabled");
}
if (this._selected) {
classes.push("ui5-sn-item-selected");
}
return classes;
}
get _classes() {
return this.classesArray.join(" ");
}
get _ariaCurrent() {
if (!this.sideNavCollapsed && !this.selected) {
return undefined;
}
return "page";
}
get _ariaSelected() {
if (!this.sideNavCollapsed) {
return undefined;
}
return this.selected;
}
get _tagId() {
return `${this._id}-tag`;
}
_onkeydown(e: KeyboardEvent) {
const isRTL = this.effectiveDir === "rtl";
if (isSpace(e) || isRight(e) || isLeft(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)) {
if (!this.unselectable) {
this._activate(e);
}
}
if ((isRTL ? isLeft(e) : isRight(e)) && this.sideNavCollapsed && this.hasSubItems) {
this._activate(e);
}
if ((isRTL ? isRight(e) : isLeft(e)) && this.inPopover) {
this.associatedItem?.sideNavigation?.closePicker();
}
}
_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);
}
}
}
}
_onclick(e: MouseEvent) {
this._activate(e);
}
_onfocusin(e: FocusEvent) {
e.stopPropagation();
this.sideNavigation?.focusItem(this);
}
_activate(e: KeyboardEvent | MouseEvent) {
const {
altKey,
ctrlKey,
metaKey,
shiftKey,
} = e;
e.stopPropagation();
if (this.isOverflow) {
const executeEvent = this.fireDecoratorEvent("click", {
altKey,
ctrlKey,
metaKey,
shiftKey,
});
if (!executeEvent) {
e.preventDefault();
}
} else {
this.sideNavigation?._handleItemClick(e, this);
}
}
get isSideNavigationSelectableItemBase() {
return true;
}
}
export default SideNavigationSelectableItemBase;
export const isInstanceOfSideNavigationSelectableItemBase = createInstanceChecker<SideNavigationSelectableItemBase>("isSideNavigationSelectableItemBase");
export type {
SideNavigationItemAccessibilityAttributes,
};