-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathTabContainer.ts
1509 lines (1242 loc) · 42.7 KB
/
TabContainer.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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import type { StyleData } from "@ui5/webcomponents-base/dist/types.js";
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.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 litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";
import { renderFinished } from "@ui5/webcomponents-base/dist/Render.js";
import slideDown from "@ui5/webcomponents-base/dist/animations/slideDown.js";
import slideUp from "@ui5/webcomponents-base/dist/animations/slideUp.js";
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js";
import AnimationMode from "@ui5/webcomponents-base/dist/types/AnimationMode.js";
import { getAnimationMode } from "@ui5/webcomponents-base/dist/config/AnimationMode.js";
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
import {
isSpace,
isEnter,
isDown,
isRight,
isLeft,
isUp,
} from "@ui5/webcomponents-base/dist/Keys.js";
import MediaRange from "@ui5/webcomponents-base/dist/MediaRange.js";
import { getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js";
import { getScopedVarName } from "@ui5/webcomponents-base/dist/CustomElementsScope.js";
import "@ui5/webcomponents-icons/dist/slim-arrow-up.js";
import "@ui5/webcomponents-icons/dist/slim-arrow-down.js";
import arraysAreEqual from "@ui5/webcomponents-base/dist/util/arraysAreEqual.js";
import findClosestPosition from "@ui5/webcomponents-base/dist/util/dragAndDrop/findClosestPosition.js";
import Orientation from "@ui5/webcomponents-base/dist/types/Orientation.js";
import DragRegistry from "@ui5/webcomponents-base/dist/util/dragAndDrop/DragRegistry.js";
import type { SetDraggedElementFunction } from "@ui5/webcomponents-base/dist/util/dragAndDrop/DragRegistry.js";
import longDragOverHandler from "@ui5/webcomponents-base/dist/util/dragAndDrop/longDragOverHandler.js";
import MovePlacement from "@ui5/webcomponents-base/dist/types/MovePlacement.js";
import {
TABCONTAINER_PREVIOUS_ICON_ACC_NAME,
TABCONTAINER_NEXT_ICON_ACC_NAME,
TABCONTAINER_OVERFLOW_MENU_TITLE,
TABCONTAINER_END_OVERFLOW,
TABCONTAINER_POPOVER_CANCEL_BUTTON,
TABCONTAINER_SUBTABS_DESCRIPTION,
} from "./generated/i18n/i18n-defaults.js";
import Button from "./Button.js";
import Icon from "./Icon.js";
import List from "./List.js";
import DropIndicator from "./DropIndicator.js";
import type Tab from "./Tab.js";
import type { ListItemClickEventDetail, ListMoveEventDetail } from "./List.js";
import type CustomListItem from "./CustomListItem.js";
import ResponsivePopover from "./ResponsivePopover.js";
import TabContainerTabsPlacement from "./types/TabContainerTabsPlacement.js";
import SemanticColor from "./types/SemanticColor.js";
import BackgroundDesign from "./types/BackgroundDesign.js";
import TabLayout from "./types/TabLayout.js";
import TabsOverflowMode from "./types/TabsOverflowMode.js";
import type { IButton } from "./Button.js";
// Templates
import TabContainerTemplate from "./generated/templates/TabContainerTemplate.lit.js";
import TabContainerPopoverTemplate from "./generated/templates/TabContainerPopoverTemplate.lit.js";
// Styles
import tabContainerCss from "./generated/themes/TabContainer.css.js";
import ResponsivePopoverCommonCss from "./generated/themes/ResponsivePopoverCommon.css.js";
/**
* Interface for components that may be slotted inside `ui5-tabcontainer` as items
* @public
*/
interface ITab extends UI5Element {
isSeparator: boolean;
getTabInStripDomRef: () => ITab | null;
additionalText?: string;
design?: `${SemanticColor}`;
disabled?: boolean;
icon?: string;
isSingleClickArea?: boolean;
requiresExpandButton?: boolean;
selected?: boolean;
subTabs?: Array<ITab>;
tabs?: Array<ITab>
text?: string;
hasOwnContent?: boolean;
forcedLevel?: number;
forcedSelected?: boolean;
getElementInStrip?: () => ITab | null;
isInline?: boolean;
forcedMixedMode?: boolean;
forcedPosinset?: number;
forcedSetsize?: number;
realTabReference: ITab;
isTopLevelTab?: boolean;
forcedStyle?: Record<string, any>;
}
type TabContainerPopoverOwner = "start-overflow" | "end-overflow" | Tab;
const tabStyles: Array<StyleData> = [];
const staticAreaTabStyles: Array<StyleData> = [];
const PAGE_UP_DOWN_SIZE = 5;
type TabContainerTabSelectEventDetail = {
tab: ITab;
tabIndex: number;
}
type TabContainerMoveEventDetail = {
source: {
element: HTMLElement;
},
destination: {
element: HTMLElement;
placement: `${MovePlacement}`
}
}
interface TabContainerExpandButton extends Button {
tab: Tab;
}
interface TabContainerTabInOverflow extends CustomListItem {
realTabReference: Tab;
}
/**
* @class
*
* ### Overview
*
* The `ui5-tabcontainer` represents a collection of tabs with associated content.
* Navigation through the tabs changes the content display of the currently active content area.
* A tab can be labeled with text only, or icons with text.
*
* ### Structure
*
* The `ui5-tabcontainer` can hold two types of entities:
*
* - `ui5-tab` - contains all the information on an item (text and icon)
* - `ui5-tab-separator` - used to separate tabs with a line
*
* ### Hierarchies
* Multiple sub tabs could be placed underneath one main tab. Nesting allows deeper hierarchies with indentations
* to indicate the level of each nested tab. When a tab has both sub tabs and own content its click area is split
* to allow the user to display the content or alternatively to expand / collapse the list of sub tabs.
*
* ### 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/dist/TabContainer.js";`
*
* `import "@ui5/webcomponents/dist/Tab.js";` (for `ui5-tab`)
*
* `import "@ui5/webcomponents/dist/TabSeparator.js";` (for `ui5-tab-separator`)
* @constructor
* @extends UI5Element
* @public
* @csspart content - Used to style the content of the component
* @csspart tabstrip - Used to style the tabstrip of the component
*/
@customElement({
tag: "ui5-tabcontainer",
languageAware: true,
fastNavigation: true,
styles: [tabStyles, tabContainerCss],
staticAreaStyles: [ResponsivePopoverCommonCss, staticAreaTabStyles],
renderer: litRender,
template: TabContainerTemplate,
staticAreaTemplate: TabContainerPopoverTemplate,
dependencies: [
Button,
Icon,
List,
ResponsivePopover,
DropIndicator,
],
})
/**
* Fired when a tab is selected.
* @param {ITab} tab The selected `tab`.
* @param {Integer} tabIndex The selected `tab` index in the flattened array of all tabs and their subTabs, provided by the `allItems` getter.
* @public
* @allowPreventDefault
*/
@event<TabContainerTabSelectEventDetail>("tab-select", {
detail: {
/**
* @public
*/
tab: { type: HTMLElement },
/**
* @public
*/
tabIndex: { type: Number },
},
})
class TabContainer extends UI5Element {
/**
* Defines whether the tabs are in a fixed state that is not
* expandable/collapsible by user interaction.
* @default false
* @public
*/
@property({ type: Boolean })
fixed!: boolean;
/**
* Defines whether the tab content is collapsed.
* @default false
* @public
*/
@property({ type: Boolean })
collapsed!: boolean;
/**
* Defines whether the overflow select list is displayed.
*
* The overflow select list represents a list, where all tabs are displayed
* so that it's easier for the user to select a specific tab.
* @default false
* @public
* @deprecated Since the introduction of TabsOverflowMode, overflows will always be visible if there is not enough space for all tabs,
* all hidden tabs are moved to a select list in the respective overflows and are accessible via the `overflowButton` and / or `startOverflowButton` slots.
*/
@property({ type: Boolean })
showOverflow!: boolean;
/**
* Defines the alignment of the content and the `additionalText` of a tab.
*
* **Note:**
* The content and the `additionalText` would be displayed vertically by default,
* but when set to `Inline`, they would be displayed horizontally.
* @default "Standard"
* @public
*/
@property({ type: TabLayout, defaultValue: TabLayout.Standard })
tabLayout!: `${TabLayout}`;
/**
* Defines the overflow mode of the header (the tab strip). If you have a large number of tabs, only the tabs that can fit on screen will be visible.
* All other tabs that can 't fit on the screen are available in an overflow tab "More".
*
* **Note:**
* Only one overflow at the end would be displayed by default,
* but when set to `StartAndEnd`, there will be two overflows on both ends, and tab order will not change on tab selection.
* @default "End"
* @since 1.1.0
* @public
*/
@property({ type: TabsOverflowMode, defaultValue: TabsOverflowMode.End })
tabsOverflowMode!: `${TabsOverflowMode}`;
/**
* Sets the background color of the Tab Container's header as `Solid`, `Transparent`, or `Translucent`.
* @default "Solid"
* @since 1.10.0
* @public
*/
@property({ type: BackgroundDesign, defaultValue: BackgroundDesign.Solid })
headerBackgroundDesign!: `${BackgroundDesign}`;
/**
* Sets the background color of the Tab Container's content as `Solid`, `Transparent`, or `Translucent`.
* @default "Solid"
* @since 1.10.0
* @public
*/
@property({ type: BackgroundDesign, defaultValue: BackgroundDesign.Solid })
contentBackgroundDesign!: `${BackgroundDesign}`;
/**
* Defines the placement of the tab strip relative to the actual tabs' content.
*
* **Note:** By default the tab strip is displayed above the tabs' content area and this is the recommended
* layout for most scenarios. Set to `Bottom` only when the component is at the
* bottom of the page and you want the tab strip to act as a menu.
* @default "Top"
* @since 1.0.0-rc.7
* @private
*/
@property({ type: TabContainerTabsPlacement, defaultValue: TabContainerTabsPlacement.Top })
tabsPlacement!: `${TabContainerTabsPlacement}`;
/**
* Defines the current media query size.
* @private
*/
@property()
mediaRange!: string;
@property({ type: Object })
_selectedTab!: Tab;
@property({ type: Boolean, noAttribute: true })
_animationRunning!: boolean;
@property({ type: Boolean, noAttribute: true })
_contentCollapsed!: boolean;
@property({ noAttribute: true, defaultValue: "0" })
_startOverflowText!: string;
@property({ noAttribute: true, defaultValue: "More" })
_endOverflowText!: string;
@property({ type: Object, multiple: true })
_popoverItemsFlat!: Array<ITab>;
@property({ validator: Integer, noAttribute: true })
_width?: number;
/**
* Defines the tabs.
*
* **Note:** Use `ui5-tab` and `ui5-tab-separator` for the intended design.
* @public
*/
@slot({
"default": true,
type: HTMLElement,
individualSlots: true,
invalidateOnChildChange: {
properties: true,
slots: true,
},
})
items!: Array<ITab>;
/**
* Defines the button which will open the overflow menu. If nothing is provided to this slot,
* the default button will be used.
* @public
* @since 1.0.0-rc.9
*/
@slot()
overflowButton!: Array<IButton>;
/**
* Defines the button which will open the start overflow menu if available. If nothing is provided to this slot,
* the default button will be used.
* @public
* @since 1.1.0
*/
@slot()
startOverflowButton!: Array<IButton>;
_itemNavigation: ItemNavigation;
_itemsFlat?: Array<ITab>;
responsivePopover?: ResponsivePopover;
_hasScheduledPopoverOpen = false;
_handleResizeBound: () => void;
_setDraggedElement?: SetDraggedElementFunction;
_setDraggedElementInStaticArea?: SetDraggedElementFunction;
static registerTabStyles(styles: StyleData) {
tabStyles.push(styles);
}
static registerStaticAreaTabStyles(styles: StyleData) {
staticAreaTabStyles.push(styles);
}
static i18nBundle: I18nBundle;
constructor() {
super();
this._handleResizeBound = this._handleResize.bind(this);
// Init ItemNavigation
this._itemNavigation = new ItemNavigation(this, {
getItemsCallback: () => this._getFocusableRefs(),
skipItemsSize: PAGE_UP_DOWN_SIZE,
});
}
onBeforeRendering() {
this._itemsFlat = this._flatten(this.items);
if (!this._itemsFlat.length) {
return;
}
// update selected tab
const selectedTabs = this._itemsFlat.filter(tab => tab.selected) as Array<Tab>;
if (selectedTabs.length) {
this._selectedTab.forcedSelected = false;
this._selectedTab = selectedTabs[0];
} else {
this._selectedTab = this._itemsFlat[0] as Tab;
this._selectedTab.forcedSelected = true;
}
this._setItemsPrivateProperties(this.items);
if (!this._animationRunning) {
this._contentCollapsed = this.collapsed;
}
if (this.showOverflow) {
console.warn(`The "show-overflow" property is deprecated and will be removed in a future release.`); // eslint-disable-line
}
}
onAfterRendering() {
if (!this.items.length) {
return;
}
this._setItemsForStrip();
if (!this.shadowRoot!.contains(document.activeElement)) {
const focusStart = this._getRootTab(this._selectedTab);
this._itemNavigation.setCurrentItem(focusStart);
}
if (this.responsivePopover?.opened) {
const popoverItems = this._getPopoverItemsFor(this._getPopoverOwner(this.responsivePopover._opener!));
if (popoverItems.length) {
this._setPopoverItems(popoverItems);
} else {
this._closePopover();
}
}
}
onEnterDOM() {
ResizeHandler.register(this._getHeader(), this._handleResizeBound);
DragRegistry.subscribe(this);
this._setDraggedElement = DragRegistry.addSelfManagedArea(this);
}
onExitDOM() {
ResizeHandler.deregister(this._getHeader(), this._handleResizeBound);
DragRegistry.unsubscribe(this);
DragRegistry.removeSelfManagedArea(this);
this._setDraggedElement = undefined;
if (this.staticAreaItem && this._setDraggedElementInStaticArea) {
DragRegistry.removeSelfManagedArea(this.staticAreaItem);
this._setDraggedElementInStaticArea = undefined;
}
}
_handleResize() {
if (this.responsivePopover && this.responsivePopover.opened) {
this._closePopover();
}
// invalidate
this._width = this.offsetWidth;
this._updateMediaRange(this._width);
}
_updateMediaRange(width: number) {
this.mediaRange = MediaRange.getCurrentRange(MediaRange.RANGESETS.RANGE_4STEPS, width);
}
_setItemsPrivateProperties(items: Array<ITab>) {
// set real dom ref to all items, then return only the tabs for further processing
const allTabs = items.filter(item => {
item.getElementInStrip = () => this.getDomRef()!.querySelector(`[id="${item._id}"]`);
return !item.isSeparator;
});
allTabs.forEach((tab, index, arr) => {
tab.isInline = this.tabLayout === TabLayout.Inline;
tab.forcedMixedMode = this.mixedMode;
tab.forcedPosinset = index + 1;
tab.forcedSetsize = arr.length;
tab.isTopLevelTab = items.some(i => i === tab);
});
walk(items, item => {
if (!item.isSeparator) {
(item as Tab)._selectedTabReference = this._selectedTab;
}
});
this._setIndentLevels(items);
}
_onHeaderFocusin(e: FocusEvent) {
const tab = getTab(e.target as HTMLElement);
if (tab) {
this._itemNavigation.setCurrentItem(tab.realTabReference);
}
}
_onHeaderDragStart(e: DragEvent) {
if (!e.dataTransfer || !(e.target instanceof HTMLElement)) {
return;
}
this._setDraggedElement!((e.target as Tab).realTabReference);
}
_onHeaderDragEnter(e: DragEvent) {
e.preventDefault();
}
@longDragOverHandler("[data-ui5-stable=overflow-start],[data-ui5-stable=overflow-end],[role=tab]")
_onHeaderDragOver(e: DragEvent, isLongDragOver: boolean) {
if (!(e.target instanceof HTMLElement) || !e.target.closest("[data-ui5-stable=overflow-start],[data-ui5-stable=overflow-end],[role=tab],[role=separator]")) {
this.dropIndicatorDOM!.targetReference = null;
return;
}
const draggedElement = DragRegistry.getDraggedElement();
const closestPosition = findClosestPosition(
[...this._getTabStrip().querySelectorAll<HTMLElement>(`[role="tab"]:not([hidden])`)],
e.clientX,
Orientation.Horizontal,
);
const overflowButton = e.target.closest<HTMLElement>("[data-ui5-stable=overflow-start],[data-ui5-stable=overflow-end]");
let popoverTarget = null;
if (overflowButton) {
popoverTarget = overflowButton;
e.preventDefault();
} else if (closestPosition) {
const dropTarget = (closestPosition.element as Tab).realTabReference;
let placements = closestPosition.placements;
if (dropTarget === draggedElement) {
placements = placements.filter(placement => placement !== MovePlacement.On);
}
const acceptedPlacement = placements.find(placement => {
const dragOverPrevented = !this.fireEvent<TabContainerMoveEventDetail>("move-over", {
source: {
element: draggedElement!,
},
destination: {
element: dropTarget,
placement,
},
}, true);
if (dragOverPrevented) {
e.preventDefault();
this.dropIndicatorDOM!.targetReference = closestPosition.element;
this.dropIndicatorDOM!.placement = placement;
return true;
}
return false;
});
if (acceptedPlacement === MovePlacement.On && (closestPosition.element as Tab).realTabReference.subTabs.length) {
popoverTarget = closestPosition.element;
} else if (!acceptedPlacement) {
this.dropIndicatorDOM!.targetReference = null;
}
}
if (popoverTarget && isLongDragOver) {
this._showPopoverAt(popoverTarget, false, true);
} else {
this._closePopover();
}
}
_onHeaderDrop(e: DragEvent) {
e.preventDefault();
const draggedElement = DragRegistry.getDraggedElement()!;
this.fireEvent<TabContainerMoveEventDetail>("move", {
source: {
element: draggedElement,
},
destination: {
element: (this.dropIndicatorDOM!.targetReference as Tab).realTabReference,
placement: this.dropIndicatorDOM!.placement,
},
});
this.dropIndicatorDOM!.targetReference = null;
draggedElement.focus();
}
_onHeaderDragLeave(e: DragEvent) {
if (e.relatedTarget instanceof Node && this.shadowRoot!.contains(e.relatedTarget)) {
return;
}
this.dropIndicatorDOM!.targetReference = null;
}
_onPopoverListMoveOver(e: CustomEvent<ListMoveEventDetail>) {
const { destination } = e.detail;
const draggedElement = DragRegistry.getDraggedElement();
const dropTarget = (destination.element as ITab).realTabReference;
if (destination.placement === MovePlacement.On && (dropTarget.isSeparator || draggedElement === dropTarget)) {
return;
}
const placementAccepted = !this.fireEvent<TabContainerMoveEventDetail>("move-over", {
source: {
element: draggedElement!,
},
destination: {
element: dropTarget,
placement: destination.placement,
},
}, true);
if (placementAccepted) {
e.preventDefault();
} else {
this.dropIndicatorDOM!.targetReference = null;
}
}
_onPopoverListMove(e: CustomEvent<ListMoveEventDetail>) {
const { destination } = e.detail;
const draggedElement = DragRegistry.getDraggedElement()!;
e.preventDefault();
this.fireEvent<TabContainerMoveEventDetail>("move", {
source: {
element: draggedElement,
},
destination: {
element: (destination.element as Tab).realTabReference,
placement: destination.placement,
},
}, true);
this.dropIndicatorDOM!.targetReference = null;
draggedElement.focus();
}
async _onTabStripClick(e: Event) {
const tab = getTab(e.target as HTMLElement);
if (!tab || tab.realTabReference.disabled) {
return;
}
e.stopPropagation();
e.preventDefault();
if ((e.target as HTMLElement).hasAttribute("ui5-button")) {
this._onTabExpandButtonClick(e);
return;
}
if (!tab.realTabReference.hasOwnContent && tab.realTabReference.tabs.length) {
await this._togglePopover(tab);
return;
}
this._onHeaderItemSelect(tab);
}
async _onTabExpandButtonClick(e: Event) {
e.stopPropagation();
e.preventDefault();
let tabInstance: Tab;
if (isTabInStrip(e.target as HTMLElement)) {
tabInstance = e.target as Tab;
} else {
tabInstance = (e.target as TabContainerExpandButton).tab;
}
let opener = e.target as HTMLElement;
if (tabInstance) {
tabInstance.focus();
}
if (e.type === "keydown" && !(e.target as Tab).realTabReference.isSingleClickArea) {
opener = (e.target as Tab).querySelector<TabContainerExpandButton>(".ui5-tab-expand-button [ui5-button]")!;
tabInstance = (e.target as Tab).realTabReference;
}
// if clicked between the expand button and the tab
if (!tabInstance) {
this._onHeaderItemSelect(opener.parentElement as HTMLElement);
return;
}
await this._togglePopover(opener, true);
}
_setPopoverInitialFocus() {
const selectedTabInOverflow = this._getSelectedTabInOverflow();
const tab = selectedTabInOverflow || this._getFirstFocusableItemInOverflow();
this.responsivePopover!.initialFocus = `${tab.realTabReference._id}-li`;
}
_getSelectedTabInOverflow() {
return <TabContainerTabInOverflow>(<List> this.responsivePopover!.content[0]).items.find(item => {
return (<TabContainerTabInOverflow>item).realTabReference && (<TabContainerTabInOverflow>item).realTabReference.selected;
});
}
_getFirstFocusableItemInOverflow() {
return <TabContainerTabInOverflow>(<List> this.responsivePopover!.content[0]).items.find(item => item.classList.contains("ui5-tab-overflow-item"));
}
_onTabStripKeyDown(e: KeyboardEvent) {
const tab = getTab(e.target as HTMLElement);
if (!tab || tab.realTabReference.disabled) {
return;
}
if (isEnter(e)) {
if (tab.realTabReference.isSingleClickArea) {
this._onTabStripClick(e);
} else {
this._onHeaderItemSelect(tab);
}
}
if (isSpace(e)) {
e.preventDefault(); // prevent scrolling
}
if (isDown(e) || isUp(e)) {
if (tab.realTabReference.requiresExpandButton) {
this._onTabExpandButtonClick(e);
}
if (tab.realTabReference.isSingleClickArea) {
this._onTabStripClick(e);
}
}
}
_onTabStripKeyUp(e: KeyboardEvent) {
const tab = getTab(e.target as HTMLElement);
if (!tab || tab.realTabReference.disabled) {
return;
}
if (isSpace(e)) {
e.preventDefault();
if (tab.realTabReference.isSingleClickArea) {
this._onTabStripClick(e);
} else {
this._onHeaderItemSelect(tab);
}
}
}
_onHeaderItemSelect(tab: HTMLElement) {
if (!tab.hasAttribute("disabled")) {
this._onItemSelect(tab.id);
}
}
async _onOverflowListItemClick(e: CustomEvent<ListItemClickEventDetail>) {
e.preventDefault(); // cancel the item selection
this._onItemSelect(e.detail.item.id.slice(0, -3)); // strip "-li" from end of id
this._closePopover();
await renderFinished();
const selectedTopLevel = this._getRootTab(this._selectedTab);
selectedTopLevel.getTabInStripDomRef()!.focus();
}
/**
* Returns all slotted tabs and their subTabs in a flattened array.
* The order of tabs is depth-first.
*
* @public
* @default []
*/
get allItems() : Array<ITab> {
return this._flatten(this.items);
}
_setIndentLevels(items: Array<ITab>, level = 1) {
items.forEach(item => {
if (item.hasAttribute("ui5-tab") || item.hasAttribute("ui5-tab-separator")) {
item.forcedLevel = level;
if (item.subTabs) {
this._setIndentLevels(item.subTabs, level + 1);
}
}
});
}
_flatten(items: Array<ITab>) {
const result: Array<ITab> = [];
walk(items, item => {
if (item.hasAttribute("ui5-tab") || item.hasAttribute("ui5-tab-separator")) {
result.push(item);
}
});
return result;
}
_onItemSelect(selectedTabId: string) {
const previousTab = this._selectedTab;
const selectedTabIndex = this._itemsFlat!.findIndex(item => item.__id === selectedTabId);
const selectedTab = this._itemsFlat![selectedTabIndex] as Tab;
const selectionSuccessful = this.selectTab(selectedTab, selectedTabIndex);
if (!selectionSuccessful) {
return;
}
// update selected property on all items
this._itemsFlat!.forEach((item, index) => {
const selected = selectedTabIndex === index;
item.selected = selected;
if (item.forcedSelected) {
item.forcedSelected = false;
}
});
if (this.fixed) {
return;
}
if (!this.shouldAnimate) {
this.toggle(selectedTab, previousTab);
} else {
this.toggleAnimated(selectedTab, previousTab);
}
}
async toggleAnimated(selectedTab: Tab, previousTab: Tab) {
const content = this.shadowRoot!.querySelector<HTMLElement>(".ui5-tc__content")!;
let animationPromise = null;
this._animationRunning = true;
if (selectedTab === previousTab) {
// click on already selected tab - animate both directions
this.collapsed = !this.collapsed;
animationPromise = this.collapsed ? this.slideContentUp(content) : this.slideContentDown(content);
} else {
// click on new tab - animate if the content is currently collapsed
animationPromise = this.collapsed ? this.slideContentDown(content) : Promise.resolve();
this.collapsed = false;
}
await animationPromise;
this._contentCollapsed = this.collapsed;
this._animationRunning = false;
}
toggle(selectedTab: Tab, previousTab: Tab) {
if (selectedTab === previousTab) {
this.collapsed = !this.collapsed;
} else {
this.collapsed = false;
}
}
/**
* Fires the `tab-select` event and changes the internal reference for the currently selected tab.
* If the event is prevented, the current tab is not changed.
* @private
* @param selectedTab selected tab instance
* @param selectedTabIndex selected tab index for an array containing all tabs and sub tabs. **Note:** Use the method `allTabs` to get this array.
* @returns true if the tab selection is successful, false if it was prevented
*/
selectTab(selectedTab: Tab, selectedTabIndex: number) {
if (!this.fireEvent<TabContainerTabSelectEventDetail>("tab-select", { tab: selectedTab, tabIndex: selectedTabIndex }, true)) {
return false;
}
// select the tab
this._selectedTab = selectedTab;
return true;
}
slideContentDown(element: HTMLElement) {
return slideDown(element).promise();
}
slideContentUp(element: HTMLElement) {
return slideUp(element).promise();
}
async _onOverflowClick(e: Event) {
if ((e.target as HTMLElement).classList.contains("ui5-tc__overflow")) {
// the empty area in the overflow was clicked
return;
}
const overflow = e.currentTarget as HTMLElement;
const isEndOverflow = overflow.classList.contains("ui5-tc__overflow--end");
let opener;
if (isEndOverflow) {
opener = this.overflowButton[0] || this._getEndOverflowBtnDOM();
} else {
opener = this.startOverflowButton[0] || this._getStartOverflowBtnDOM();
}
await this._togglePopover(opener, true);
}
_addStyleIndent(itemsFlat: Array<ITab>) {
const extraIndent = itemsFlat
.filter(tab => !tab.isSeparator)
.some(tab => tab.design !== SemanticColor.Default && tab.design !== SemanticColor.Neutral);
itemsFlat.forEach(item => {
let level = item.forcedLevel! - 1;
if (item.isSeparator) {
level += 1;
}
item.forcedStyle = {
[getScopedVarName("--_ui5-tab-indentation-level")]: level,
[getScopedVarName("--_ui5-tab-extra-indent")]: extraIndent ? 1 : null,
};
});
}
async _onOverflowKeyDown(e: KeyboardEvent) {
const overflow = e.currentTarget as HTMLElement;
const isEndOverflow = overflow.classList.contains("ui5-tc__overflow--end");
const isStartOverflow = overflow.classList.contains("ui5-tc__overflow--start");
if (isDown(e) || (isStartOverflow && isLeft(e)) || (isEndOverflow && isRight(e))) {
e.stopPropagation();
e.preventDefault();
await this._onOverflowClick(e);
}
}
_setItemsForStrip() {
const tabStrip = this._getTabStrip();
let allItemsWidth = 0;
if (!this._selectedTab) {
return;
}
const itemsDomRefs = this.items.map(item => item.getTabInStripDomRef()!);
// make sure the overflows are hidden
this._getStartOverflow().setAttribute("hidden", "");
this._getEndOverflow().setAttribute("hidden", "");
// show all tabs
for (let i = 0; i < itemsDomRefs.length; i++) {
itemsDomRefs[i].removeAttribute("hidden");
itemsDomRefs[i].removeAttribute("start-overflow");
itemsDomRefs[i].removeAttribute("end-overflow");
}
itemsDomRefs.forEach(item => {
allItemsWidth += this._getItemWidth(item);
});
const hasOverflow = tabStrip.offsetWidth < allItemsWidth;
if (!hasOverflow) {
return;
}
if (this.isModeStartAndEnd) {
this._updateStartAndEndOverflow(itemsDomRefs);
this._updateOverflowCounters();
} else {
this._updateEndOverflow(itemsDomRefs);
}
}
_getRootTab(tab: Tab) {
while (tab.hasAttribute("ui5-tab")) {
if (tab.parentElement!.hasAttribute("ui5-tabcontainer")) {
break;
}
tab = tab.parentElement as Tab;
}
return tab;
}
_updateEndOverflow(itemsDomRefs: Array<ITab>) {
// show end overflow