diff options
Diffstat (limited to 'client/src/app/shared/misc/list-overflow.component.ts')
-rw-r--r-- | client/src/app/shared/misc/list-overflow.component.ts | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/client/src/app/shared/misc/list-overflow.component.ts b/client/src/app/shared/misc/list-overflow.component.ts new file mode 100644 index 000000000..c493ab795 --- /dev/null +++ b/client/src/app/shared/misc/list-overflow.component.ts | |||
@@ -0,0 +1,119 @@ | |||
1 | import { | ||
2 | AfterViewInit, | ||
3 | ChangeDetectionStrategy, | ||
4 | ChangeDetectorRef, | ||
5 | Component, | ||
6 | ElementRef, | ||
7 | HostListener, | ||
8 | Input, | ||
9 | QueryList, | ||
10 | TemplateRef, | ||
11 | ViewChild, | ||
12 | ViewChildren | ||
13 | } from '@angular/core' | ||
14 | import { NgbDropdown, NgbModal } from '@ng-bootstrap/ng-bootstrap' | ||
15 | import { lowerFirst, uniqueId } from 'lodash-es' | ||
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 | }) | ||
30 | export class ListOverflowComponent<T extends ListOverflowItem> implements AfterViewInit { | ||
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 | |||
50 | ngAfterViewInit () { | ||
51 | setTimeout(() => this.onWindowResize(), 0) | ||
52 | } | ||
53 | |||
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 | } | ||