]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/misc/list-overflow.component.ts
Translated using Weblate (Polish)
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / misc / list-overflow.component.ts
CommitLineData
24e7916c 1import {
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
14import { NgbDropdown, NgbModal } from '@ng-bootstrap/ng-bootstrap'
15import { lowerFirst, uniqueId } from 'lodash-es'
24e7916c
RK
16import { ScreenService } from './screen.service'
17import { take } from 'rxjs/operators'
18
19export 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 30export class ListOverflowComponent<T extends ListOverflowItem> implements AfterViewInit {
24e7916c
RK
31 @ViewChild('modal', { static: true }) modal: ElementRef
32 @ViewChild('itemsParent', { static: true }) parent: ElementRef<HTMLDivElement>
33 @ViewChildren('itemsRendered') itemsRendered: QueryList<ElementRef>
34 @Input() items: T[]
35 @Input() itemTemplate: TemplateRef<{item: T}>
36
37 showItemsUntilIndexExcluded: number
38 active = false
39 isInTouchScreen = false
40 isInMobileView = false
41
42 private openedOnHover = false
43
44 constructor (
45 private cdr: ChangeDetectorRef,
46 private modalService: NgbModal,
47 private screenService: ScreenService
48 ) {}
49
68ca6194
C
50 ngAfterViewInit () {
51 setTimeout(() => this.onWindowResize(), 0)
52 }
53
24e7916c
RK
54 isMenuDisplayed () {
55 return !!this.showItemsUntilIndexExcluded
56 }
57
58 @HostListener('window:resize', ['$event'])
59 onWindowResize () {
60 this.isInTouchScreen = !!this.screenService.isInTouchScreen()
61 this.isInMobileView = !!this.screenService.isInMobileView()
62
63 const parentWidth = this.parent.nativeElement.getBoundingClientRect().width
64 let showItemsUntilIndexExcluded: number
65 let accWidth = 0
66
67 for (const [index, el] of this.itemsRendered.toArray().entries()) {
68 accWidth += el.nativeElement.getBoundingClientRect().width
69 if (showItemsUntilIndexExcluded === undefined) {
70 showItemsUntilIndexExcluded = (parentWidth < accWidth) ? index : undefined
71 }
72
73 const e = document.getElementById(this.getId(index))
74 const shouldBeVisible = showItemsUntilIndexExcluded ? index < showItemsUntilIndexExcluded : true
75 e.style.visibility = shouldBeVisible ? 'inherit' : 'hidden'
76 }
77
78 this.showItemsUntilIndexExcluded = showItemsUntilIndexExcluded
79 this.cdr.markForCheck()
80 }
81
82 openDropdownOnHover (dropdown: NgbDropdown) {
83 this.openedOnHover = true
84 dropdown.open()
85
86 // Menu was closed
87 dropdown.openChange
88 .pipe(take(1))
89 .subscribe(() => this.openedOnHover = false)
90 }
91
92 dropdownAnchorClicked (dropdown: NgbDropdown) {
93 if (this.openedOnHover) {
94 this.openedOnHover = false
95 return
96 }
97
98 return dropdown.toggle()
99 }
100
101 closeDropdownIfHovered (dropdown: NgbDropdown) {
102 if (this.openedOnHover === false) return
103
104 dropdown.close()
105 this.openedOnHover = false
106 }
107
108 toggleModal () {
109 this.modalService.open(this.modal, { centered: true })
110 }
111
112 dismissOtherModals () {
113 this.modalService.dismissAll()
114 }
115
116 getId (id: number | string = uniqueId()): string {
117 return lowerFirst(this.constructor.name) + '_' + id
118 }
119}