-
Notifications
You must be signed in to change notification settings - Fork 255
/
SideNavigation.ts
662 lines (541 loc) · 19.2 KB
/
SideNavigation.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import ResponsivePopover from "@ui5/webcomponents/dist/ResponsivePopover.js";
import NavigationMenu from "@ui5/webcomponents/dist/NavigationMenu.js";
import type { MenuItemClickEventDetail } from "@ui5/webcomponents/dist/Menu.js";
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";
import event from "@ui5/webcomponents-base/dist/decorators/event.js";
import property from "@ui5/webcomponents-base/dist/decorators/property.js";
import slot from "@ui5/webcomponents-base/dist/decorators/slot.js";
import { getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js";
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
import type { ITabbable } from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
import "@ui5/webcomponents-icons/dist/overflow.js";
import {
isPhone,
isTablet,
isCombi,
} from "@ui5/webcomponents-base/dist/Device.js";
import {
isSpace,
isEnter,
} from "@ui5/webcomponents-base/dist/Keys.js";
import NavigationMode from "@ui5/webcomponents-base/dist/types/NavigationMode.js";
import type SideNavigationItemBase from "./SideNavigationItemBase.js";
import { isInstanceOfSideNavigationItemBase } from "./SideNavigationItemBase.js";
import type SideNavigationSelectableItemBase from "./SideNavigationSelectableItemBase.js";
import { isInstanceOfSideNavigationSelectableItemBase } from "./SideNavigationSelectableItemBase.js";
import SideNavigationItem, { isInstanceOfSideNavigationItem } from "./SideNavigationItem.js";
import SideNavigationSubItem from "./SideNavigationSubItem.js";
import SideNavigationGroup from "./SideNavigationGroup.js";
import SideNavigationTemplate from "./generated/templates/SideNavigationTemplate.lit.js";
import {
SIDE_NAVIGATION_POPOVER_HIDDEN_TEXT,
SIDE_NAVIGATION_COLLAPSED_LIST_ARIA_ROLE_DESC,
SIDE_NAVIGATION_LIST_ARIA_ROLE_DESC,
SIDE_NAVIGATION_OVERFLOW_ACCESSIBLE_NAME,
} from "./generated/i18n/i18n-defaults.js";
// Styles
import SideNavigationCss from "./generated/themes/SideNavigation.css.js";
import SideNavigationPopoverCss from "./generated/themes/SideNavigationPopover.css.js";
const PAGE_UP_DOWN_SIZE = 10;
type SideNavigationPopoverContents = {
item: SideNavigationItem,
subItems: Array<SideNavigationSubItem>,
};
type SideNavigationSelectionChangeEventDetail = {
item: SideNavigationItemBase,
};
type PopupSideNavigationItem = SideNavigationItem & { associatedItem: SideNavigationSelectableItemBase };
// used for the inner side navigation used in the SideNavigationPopoverTemplate
type NavigationMenuClickEventDetail = MenuItemClickEventDetail & {
item: Pick<MenuItemClickEventDetail, "item"> & {
associatedItem: SideNavigationSelectableItemBase,
}
};
/**
* @class
*
* ### Overview
*
* The `SideNavigation` is used as a standard menu in applications.
* It consists of three containers: header (top-aligned), main navigation section (top-aligned) and the secondary section (bottom-aligned).
*
* - The header is meant for displaying user related information - profile data, avatar, etc.
* - The main navigation section is related to the user’s current work context
* - The secondary section is mostly used to link additional information that may be of interest (legal information, developer communities, external help, contact information and so on).
*
* ### Usage
*
* Use the available `ui5-side-navigation-group`, `ui5-side-navigation-item`
* and `ui5-side-navigation-sub-item` components to build your menu.
* The items can consist of text only or an icon with text. The use or non-use of icons must be consistent for all items on one level.
* You must not combine entries with and without icons on the same level. We strongly recommend that you do not use icons on the second level.
*
* ### Keyboard Handling
*
* ### Fast Navigation
* This component provides a build in fast navigation group which can be used via [F6] / [Shift] + [F6] / [Ctrl] + [Alt/Option] / [Down] or [Ctrl] + [Alt/Option] + [Up].
* In order to use this functionality, you need to import the following module:
* `import "@ui5/webcomponents-base/dist/features/F6Navigation.js"`
*
*
* ### ES6 Module Import
*
* `import "@ui5/webcomponents-fiori/dist/SideNavigation.js"`
*
* `import "@ui5/webcomponents-fiori/dist/SideNavigationGroup.js";` (for `ui5-side-navigation-group`)
*
* `import "@ui5/webcomponents-fiori/dist/SideNavigationItem.js";` (for `ui5-side-navigation-item`)
*
* `import "@ui5/webcomponents-fiori/dist/SideNavigationSubItem.js";` (for `ui5-side-navigation-sub-item`)
* @constructor
* @extends UI5Element
* @since 1.0.0-rc.8
* @public
*/
@customElement({
tag: "ui5-side-navigation",
fastNavigation: true,
renderer: litRender,
template: SideNavigationTemplate,
styles: [SideNavigationCss, SideNavigationPopoverCss],
dependencies: [
ResponsivePopover,
SideNavigationGroup,
SideNavigationItem,
SideNavigationSubItem,
NavigationMenu,
],
})
/**
* Fired when the selection has changed via user interaction
*
* @param {SideNavigationSelectableItemBase} item the clicked item.
* @allowPreventDefault
* @public
*/
@event<SideNavigationSelectionChangeEventDetail>("selection-change", {
detail: {
/**
* @public
*/
item: { type: HTMLElement },
},
})
class SideNavigation extends UI5Element {
/**
* Defines whether the `ui5-side-navigation` is expanded or collapsed.
*
* @public
* @default false
*/
@property({ type: Boolean })
collapsed = false;
/**
* Defines the main items of the `ui5-side-navigation`. Use the `ui5-side-navigation-item` component
* for the top-level items, and the `ui5-side-navigation-sub-item` component for second-level items, nested
* inside the items.
*
* @public
*/
@slot({ type: HTMLElement, invalidateOnChildChange: true, "default": true })
items!: Array<SideNavigationItemBase>;
/**
* Defines the fixed items at the bottom of the `ui5-side-navigation`. Use the `ui5-side-navigation-item` component
* for the fixed items, and optionally the `ui5-side-navigation-sub-item` component to provide second-level items inside them.
*
* **Note:** In order to achieve the best user experience, it is recommended that you keep the fixed items "flat" (do not pass sub-items)
*
* @public
*/
@slot({ type: HTMLElement, invalidateOnChildChange: true })
fixedItems!: Array<SideNavigationItemBase>;
/**
* Defines the header of the `ui5-side-navigation`.
*
* **Note:** The header is displayed when the component is expanded - the property `collapsed` is false;
*
* @public
* @since 1.0.0-rc.11
*/
@slot()
header!: Array<HTMLElement>;
/**
* @private
*/
@property({ type: Object })
_popoverContents!: SideNavigationPopoverContents;
@property({ type: Boolean })
inPopover= false;
@property({ type: Array })
_menuPopoverItems: Array<HTMLElement> = [];
_isOverflow = false;;
_flexibleItemNavigation: ItemNavigation;
_fixedItemNavigation: ItemNavigation;
/**
* @private
*/
@property({ type: Boolean })
isTouchDevice = false;;
static i18nBundle: I18nBundle;
constructor() {
super();
this._flexibleItemNavigation = new ItemNavigation(this, {
skipItemsSize: PAGE_UP_DOWN_SIZE, // PAGE_UP and PAGE_DOWN will skip trough 10 items
navigationMode: NavigationMode.Vertical,
getItemsCallback: () => this.getEnabledFlexibleItems(),
});
this._fixedItemNavigation = new ItemNavigation(this, {
skipItemsSize: PAGE_UP_DOWN_SIZE, // PAGE_UP and PAGE_DOWN will skip trough 10 items
navigationMode: NavigationMode.Vertical,
getItemsCallback: () => this.getEnabledFixedItems(),
});
this._handleResizeBound = this.handleResize.bind(this);
this._isOverflow = false;
}
_handleResizeBound: () => void;
onBeforeRendering() {
super.onBeforeRendering();
this._getAllItems(this.items as Array<SideNavigationItem | SideNavigationGroup>).concat(this._getAllItems(this.fixedItems as Array<SideNavigationItem | SideNavigationGroup>)).forEach(item => {
item.sideNavCollapsed = this.collapsed;
item.inPopover = this.inPopover;
item.sideNavigation = this;
});
}
_onAfterPopoverOpen() {
// as the tree/list inside the popover is never destroyed,
// item navigation index should be managed, because items are
// dynamically recreated and tabIndexes are not updated
const tree = this.getPickerTree();
const selectedItem = tree._findSelectedItem(tree.items as Array<SideNavigationItem | SideNavigationGroup>);
if (selectedItem) {
selectedItem.focus();
} else {
tree.items[0]?.focus();
}
}
_onBeforePopoverOpen() {
const popover = this.getPicker();
(popover?.opener as HTMLElement)?.classList.add("ui5-sn-item-active");
}
_onBeforePopoverClose() {
const popover = this.getPicker();
(popover?.opener as HTMLElement)?.classList.remove("ui5-sn-item-active");
}
_onBeforeMenuOpen() {
const popover = this.getOverflowPopover();
(popover?.opener as HTMLElement)?.classList.add("ui5-sn-item-active");
}
_onBeforeMenuClose() {
const popover = this.getOverflowPopover();
(popover?.opener as HTMLElement)?.classList.remove("ui5-sn-item-active");
}
get accSideNavigationPopoverHiddenText() {
return SideNavigation.i18nBundle.getText(SIDE_NAVIGATION_POPOVER_HIDDEN_TEXT);
}
get ariaRoleDescNavigationList() {
let key = SIDE_NAVIGATION_LIST_ARIA_ROLE_DESC;
if (this.collapsed) {
key = SIDE_NAVIGATION_COLLAPSED_LIST_ARIA_ROLE_DESC;
}
return SideNavigation.i18nBundle.getText(key);
}
get overflowAccessibleName() {
return SideNavigation.i18nBundle.getText(SIDE_NAVIGATION_OVERFLOW_ACCESSIBLE_NAME);
}
handlePopupItemClick(e: KeyboardEvent | PointerEvent) {
const associatedItem = (e.target as PopupSideNavigationItem).associatedItem;
associatedItem.fireEvent("click");
if (associatedItem.selected) {
this.closePicker();
return;
}
this._selectItem(associatedItem);
this.closePicker();
this._popoverContents.item?.getDomRef()!.classList.add("ui5-sn-item-no-hover-effect");
}
handleOverflowItemClick(e: CustomEvent<NavigationMenuClickEventDetail>) {
const associatedItem = e.detail?.item.associatedItem;
associatedItem.fireEvent("click");
if (associatedItem.selected) {
this.closeMenu();
return;
}
this._selectItem(associatedItem);
this.closeMenu();
// When subitem is selected in collapsed mode parent element should be focused
if (associatedItem.nodeName.toLowerCase() === "ui5-side-navigation-sub-item") {
const parent = associatedItem.parentElement as SideNavigationItem;
this.focusItem(parent);
parent?.focus();
} else {
this.focusItem(associatedItem);
associatedItem?.focus();
}
}
getOverflowPopover() {
return this.shadowRoot!.querySelector<NavigationMenu>(".ui5-side-navigation-overflow-menu")!;
}
getPicker() {
return this.shadowRoot!.querySelector<ResponsivePopover>("[ui5-responsive-popover]")!;
}
openPicker(opener: HTMLElement) {
opener.classList.add("ui5-sn-item-active");
const responsivePopover = this.getPicker();
responsivePopover.opener = opener;
responsivePopover.open = true;
}
openOverflowMenu(opener: HTMLElement) {
opener.classList.add("ui5-sn-item-active");
const menu = this.getOverflowPopover();
menu.opener = opener;
menu.open = true;
}
closePicker() {
const responsivePopover = this.getPicker();
responsivePopover.open = false;
}
closeMenu() {
const menu = this.getOverflowPopover();
menu.open = false;
}
getPickerTree() {
const picker = this.getPicker();
return picker.querySelector<SideNavigation>("[ui5-side-navigation]")!;
}
get hasHeader() {
return !!this.header.length;
}
get showHeader() {
return this.hasHeader && !this.collapsed;
}
get hasFixedItems() {
return !!this.fixedItems.length;
}
get _rootRole() {
return this.inPopover ? "none" : undefined;
}
get classes() {
return {
root: {
"ui5-sn-collapsed": this.collapsed,
},
};
}
getEnabledFixedItems() : Array<ITabbable> {
return this.getEnabledItems(this.fixedItems as Array<SideNavigationItem | SideNavigationGroup>);
}
getEnabledFlexibleItems() : Array<ITabbable> {
const items = this.getEnabledItems(this.items as Array<SideNavigationItem | SideNavigationGroup>);
if (this._overflowItem) {
items.push(this._overflowItem);
}
return items;
}
getEnabledItems(items: Array<SideNavigationItem | SideNavigationGroup>) : Array<ITabbable> {
const result = new Array<ITabbable>();
this._getFocusableItems(items).forEach(item => {
if (item.classList.contains("ui5-sn-item-hidden")) {
return;
}
if (!item.disabled) {
result.push(item);
}
});
return result;
}
focusItem(item: SideNavigationItemBase) {
if (item.isFixedItem) {
this._fixedItemNavigation.setCurrentItem(item);
} else {
this._flexibleItemNavigation.setCurrentItem(item);
}
}
onAfterRendering() {
if (!this.getDomRef()?.matches(":focus-within")) {
let selectedItem = this._findSelectedItem(this.items as Array<SideNavigationItem | SideNavigationGroup>);
if (selectedItem) {
this._flexibleItemNavigation.setCurrentItem(selectedItem);
}
selectedItem = this._findSelectedItem(this.fixedItems as Array<SideNavigationItem | SideNavigationGroup>);
if (selectedItem) {
this._fixedItemNavigation.setCurrentItem(selectedItem);
}
}
if (this.collapsed) {
this.handleResize();
}
}
onEnterDOM() {
ResizeHandler.register(this, this._handleResizeBound);
this.isTouchDevice = isPhone() || (isTablet() && !isCombi());
}
onExitDOM() {
ResizeHandler.deregister(this, this._handleResizeBound);
}
handleResize() {
this._updateOverflowItems();
}
_updateOverflowItems() {
const domRef = this.getDomRef();
if (!this.collapsed || !domRef) {
return null;
}
const overflowItem = this._overflowItem!;
const flexibleContentDomRef : HTMLElement = domRef.querySelector(".ui5-sn-flexible")!;
if (!overflowItem) {
return null;
}
overflowItem.classList.add("ui5-sn-item-hidden");
const overflowItems = this.overflowItems;
let itemsHeight = overflowItems.reduce<number>((sum, itemRef) => {
itemRef.classList.remove("ui5-sn-item-hidden");
return sum + itemRef.offsetHeight;
}, 0);
const { paddingTop, paddingBottom } = window.getComputedStyle(flexibleContentDomRef);
const listHeight = flexibleContentDomRef?.offsetHeight - parseInt(paddingTop) - parseInt(paddingBottom);
if (itemsHeight <= listHeight) {
return;
}
overflowItem.classList.remove("ui5-sn-item-hidden");
itemsHeight = overflowItem.offsetHeight;
const selectedItem = overflowItems.find(item => {
return isInstanceOfSideNavigationSelectableItemBase(item) && item._selected;
});
if (selectedItem && isInstanceOfSideNavigationItemBase(selectedItem)) {
const selectedItemDomRef = selectedItem.getDomRef();
const { marginTop, marginBottom } = window.getComputedStyle(selectedItemDomRef!);
itemsHeight += selectedItemDomRef!.offsetHeight + parseFloat(marginTop) + parseFloat(marginBottom);
}
overflowItems.forEach(item => {
if (item === selectedItem) {
return;
}
let itemDomRef;
if (isInstanceOfSideNavigationItemBase(item)) {
itemDomRef = item.getDomRef()!;
} else {
itemDomRef = item;
}
const { marginTop, marginBottom } = window.getComputedStyle(itemDomRef);
itemsHeight += itemDomRef.offsetHeight + parseFloat(marginTop) + parseFloat(marginBottom);
if (itemsHeight > listHeight) {
item.classList.add("ui5-sn-item-hidden");
}
});
this._flexibleItemNavigation._init();
}
_findFocusedItem(items: Array<SideNavigationItem | SideNavigationGroup>) : SideNavigationItemBase | undefined {
return this._getFocusableItems(items).find(item => item.forcedTabIndex === "0");
}
_getSelectableItems(items: Array<SideNavigationItem | SideNavigationGroup>) : Array<SideNavigationSelectableItemBase> {
return items.reduce((result, item) => {
return result.concat(item.selectableItems);
}, new Array<SideNavigationSelectableItemBase>());
}
_getFocusableItems(items: Array<SideNavigationItem | SideNavigationGroup>) : Array<SideNavigationItemBase> {
return items.reduce((result, item) => {
return result.concat(item.focusableItems);
}, new Array<SideNavigationItemBase>());
}
_getAllItems(items: Array<SideNavigationItem | SideNavigationGroup>) : Array<SideNavigationItemBase> {
return items.reduce((result, item) => {
return result.concat(item.allItems);
}, new Array<SideNavigationItemBase>());
}
_findSelectedItem(items: Array<SideNavigationItem | SideNavigationGroup>) : SideNavigationSelectableItemBase | undefined {
return this._getSelectableItems(items).find(item => item._selected);
}
get overflowItems() : Array<HTMLElement> {
return (this.items as Array<SideNavigationItem | SideNavigationGroup>).reduce((result, item) => {
return result.concat(item.overflowItems);
}, new Array<HTMLElement>());
}
_handleItemClick(e: KeyboardEvent | PointerEvent, item: SideNavigationSelectableItemBase) {
if (item.selected && !this.collapsed) {
item.fireEvent("click");
return;
}
if (this.collapsed && isInstanceOfSideNavigationItem(item) && item.items.length) {
e.preventDefault();
this._isOverflow = false;
this._popoverContents = {
item,
subItems: item.items,
};
this.openPicker(item.getFocusDomRef() as HTMLElement);
} else {
item.fireEvent("click");
if (!item.selected) {
this._selectItem(item);
}
}
}
_handleOverflowClick() {
this._isOverflow = true;
this._menuPopoverItems = this._getOverflowItems();
this.openOverflowMenu(this._overflowItem!.getFocusDomRef() as HTMLElement);
}
_getOverflowItems(): Array<SideNavigationSelectableItemBase> {
const overflowClass = "ui5-sn-item-hidden";
const result: Array<SideNavigationSelectableItemBase> = [];
this.overflowItems.forEach(item => {
if (isInstanceOfSideNavigationSelectableItemBase(item)
&& item.classList.contains(overflowClass)) {
result.push(item);
}
});
return result;
}
_selectItem(item: SideNavigationSelectableItemBase) {
if (item.disabled) {
return;
}
if (!this.fireEvent<SideNavigationSelectionChangeEventDetail>("selection-change", { item }, true)) {
return;
}
let items = this._getSelectableItems(this.items as Array<SideNavigationItem | SideNavigationGroup>);
items = items.concat(this._getSelectableItems(this.fixedItems as Array<SideNavigationItem | SideNavigationGroup>));
items.forEach(current => {
current.selected = false;
});
item.selected = true;
}
get _overflowItem() {
const overflowItem = this.shadowRoot!.querySelector<SideNavigationItem>(".ui5-sn-item-overflow");
if (overflowItem) {
overflowItem.sideNavigation = this;
}
return overflowItem;
}
get isOverflow() {
return this._isOverflow;
}
_onkeydownOverflow(e: KeyboardEvent) {
if (isSpace(e)) {
e.preventDefault();
}
if (isEnter(e)) {
this._handleOverflowClick();
}
}
_onkeyupOverflow(e: KeyboardEvent) {
if (isSpace(e)) {
this._handleOverflowClick();
}
}
static async onDefine() {
[SideNavigation.i18nBundle] = await Promise.all([
getI18nBundle("@ui5/webcomponents-fiori"),
super.onDefine(),
]);
}
}
SideNavigation.define();
export default SideNavigation;
export type {
SideNavigationSelectionChangeEventDetail,
};