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