]>
Commit | Line | Data |
---|---|---|
24e7916c | 1 | import { |
68ca6194 C |
2 | AfterViewInit, |
3 | ChangeDetectionStrategy, | |
4 | ChangeDetectorRef, | |
24e7916c | 5 | Component, |
68ca6194 C |
6 | ElementRef, |
7 | HostListener, | |
24e7916c | 8 | Input, |
68ca6194 | 9 | QueryList, |
24e7916c | 10 | TemplateRef, |
24e7916c | 11 | ViewChild, |
68ca6194 | 12 | ViewChildren |
24e7916c | 13 | } from '@angular/core' |
68ca6194 C |
14 | import { NgbDropdown, NgbModal } from '@ng-bootstrap/ng-bootstrap' |
15 | import { lowerFirst, uniqueId } from 'lodash-es' | |
24e7916c RK |
16 | import { ScreenService } from './screen.service' |
17 | import { take } from 'rxjs/operators' | |
18 | ||
19 | export interface ListOverflowItem { | |
20 | label: string | |
21 | routerLink: string | any[] | |
22 | } | |
23 | ||
24 | @Component({ | |
25 | selector: 'list-overflow', | |
26 | templateUrl: './list-overflow.component.html', | |
27 | styleUrls: [ './list-overflow.component.scss' ], | |
28 | changeDetection: ChangeDetectionStrategy.OnPush | |
29 | }) | |
68ca6194 | 30 | export class ListOverflowComponent<T extends ListOverflowItem> implements AfterViewInit { |
340ce5c5 C |
31 | @Input() items: T[] |
32 | @Input() itemTemplate: TemplateRef<{item: T}> | |
33 | ||
24e7916c RK |
34 | @ViewChild('modal', { static: true }) modal: ElementRef |
35 | @ViewChild('itemsParent', { static: true }) parent: ElementRef<HTMLDivElement> | |
36 | @ViewChildren('itemsRendered') itemsRendered: QueryList<ElementRef> | |
24e7916c RK |
37 | |
38 | showItemsUntilIndexExcluded: number | |
39 | active = false | |
40 | isInTouchScreen = false | |
41 | isInMobileView = false | |
42 | ||
43 | private openedOnHover = false | |
44 | ||
45 | constructor ( | |
46 | private cdr: ChangeDetectorRef, | |
47 | private modalService: NgbModal, | |
48 | private screenService: ScreenService | |
49 | ) {} | |
50 | ||
68ca6194 C |
51 | ngAfterViewInit () { |
52 | setTimeout(() => this.onWindowResize(), 0) | |
53 | } | |
54 | ||
24e7916c RK |
55 | isMenuDisplayed () { |
56 | return !!this.showItemsUntilIndexExcluded | |
57 | } | |
58 | ||
340ce5c5 | 59 | @HostListener('window:resize') |
24e7916c RK |
60 | onWindowResize () { |
61 | this.isInTouchScreen = !!this.screenService.isInTouchScreen() | |
62 | this.isInMobileView = !!this.screenService.isInMobileView() | |
63 | ||
64 | const parentWidth = this.parent.nativeElement.getBoundingClientRect().width | |
65 | let showItemsUntilIndexExcluded: number | |
66 | let accWidth = 0 | |
67 | ||
68 | for (const [index, el] of this.itemsRendered.toArray().entries()) { | |
69 | accWidth += el.nativeElement.getBoundingClientRect().width | |
70 | if (showItemsUntilIndexExcluded === undefined) { | |
71 | showItemsUntilIndexExcluded = (parentWidth < accWidth) ? index : undefined | |
72 | } | |
73 | ||
74 | const e = document.getElementById(this.getId(index)) | |
75 | const shouldBeVisible = showItemsUntilIndexExcluded ? index < showItemsUntilIndexExcluded : true | |
76 | e.style.visibility = shouldBeVisible ? 'inherit' : 'hidden' | |
77 | } | |
78 | ||
79 | this.showItemsUntilIndexExcluded = showItemsUntilIndexExcluded | |
80 | this.cdr.markForCheck() | |
81 | } | |
82 | ||
83 | openDropdownOnHover (dropdown: NgbDropdown) { | |
84 | this.openedOnHover = true | |
85 | dropdown.open() | |
86 | ||
87 | // Menu was closed | |
88 | dropdown.openChange | |
89 | .pipe(take(1)) | |
90 | .subscribe(() => this.openedOnHover = false) | |
91 | } | |
92 | ||
93 | dropdownAnchorClicked (dropdown: NgbDropdown) { | |
94 | if (this.openedOnHover) { | |
95 | this.openedOnHover = false | |
96 | return | |
97 | } | |
98 | ||
99 | return dropdown.toggle() | |
100 | } | |
101 | ||
102 | closeDropdownIfHovered (dropdown: NgbDropdown) { | |
103 | if (this.openedOnHover === false) return | |
104 | ||
105 | dropdown.close() | |
106 | this.openedOnHover = false | |
107 | } | |
108 | ||
109 | toggleModal () { | |
110 | this.modalService.open(this.modal, { centered: true }) | |
111 | } | |
112 | ||
113 | dismissOtherModals () { | |
114 | this.modalService.dismissAll() | |
115 | } | |
116 | ||
117 | getId (id: number | string = uniqueId()): string { | |
118 | return lowerFirst(this.constructor.name) + '_' + id | |
119 | } | |
120 | } |