From ddb83e49ece4e76df1af98aeea30c1d8d133f48c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 5 Dec 2018 14:21:10 +0100 Subject: My account menu -> open entries on hover --- .../shared/menu/top-menu-dropdown.component.html | 18 ++++++ .../shared/menu/top-menu-dropdown.component.scss | 14 ++++ .../app/shared/menu/top-menu-dropdown.component.ts | 75 ++++++++++++++++++++++ client/src/app/shared/shared.module.ts | 5 +- 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 client/src/app/shared/menu/top-menu-dropdown.component.html create mode 100644 client/src/app/shared/menu/top-menu-dropdown.component.scss create mode 100644 client/src/app/shared/menu/top-menu-dropdown.component.ts (limited to 'client/src/app/shared') diff --git a/client/src/app/shared/menu/top-menu-dropdown.component.html b/client/src/app/shared/menu/top-menu-dropdown.component.html new file mode 100644 index 000000000..2d6d1c4bf --- /dev/null +++ b/client/src/app/shared/menu/top-menu-dropdown.component.html @@ -0,0 +1,18 @@ + diff --git a/client/src/app/shared/menu/top-menu-dropdown.component.scss b/client/src/app/shared/menu/top-menu-dropdown.component.scss new file mode 100644 index 000000000..f3ef8f814 --- /dev/null +++ b/client/src/app/shared/menu/top-menu-dropdown.component.scss @@ -0,0 +1,14 @@ +.parent-entry { + span[role=button] { + cursor: pointer; + } + + a { + display: block; + } +} + +/deep/ .dropdown-toggle::after { + position: relative; + top: 2px; +} diff --git a/client/src/app/shared/menu/top-menu-dropdown.component.ts b/client/src/app/shared/menu/top-menu-dropdown.component.ts new file mode 100644 index 000000000..272b721b2 --- /dev/null +++ b/client/src/app/shared/menu/top-menu-dropdown.component.ts @@ -0,0 +1,75 @@ +import { Component, Input, OnDestroy, OnInit } from '@angular/core' +import { filter, take } from 'rxjs/operators' +import { NavigationStart, Router } from '@angular/router' +import { Subscription } from 'rxjs' +import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap' +import { drop } from 'lodash-es' + +export type TopMenuDropdownParam = { + label: string + routerLink?: string + + children?: { + label: string + routerLink: string + }[] +} + +@Component({ + selector: 'my-top-menu-dropdown', + templateUrl: './top-menu-dropdown.component.html', + styleUrls: [ './top-menu-dropdown.component.scss' ] +}) +export class TopMenuDropdownComponent implements OnInit, OnDestroy { + @Input() menuEntries: TopMenuDropdownParam[] = [] + + suffixLabels: { [ parentLabel: string ]: string } + + private openedOnHover = false + private routeSub: Subscription + + constructor (private router: Router) {} + + ngOnInit () { + this.updateChildLabels(window.location.pathname) + + this.routeSub = this.router.events + .pipe(filter(event => event instanceof NavigationStart)) + .subscribe(() => this.updateChildLabels(window.location.pathname)) + } + + ngOnDestroy () { + if (this.routeSub) this.routeSub.unsubscribe() + } + + openDropdownOnHover (dropdown: NgbDropdown) { + this.openedOnHover = true + dropdown.open() + + // Menu was closed + dropdown.openChange + .pipe(take(1)) + .subscribe(e => this.openedOnHover = false) + } + + closeDropdownIfHovered (dropdown: NgbDropdown) { + if (this.openedOnHover === false) return + + dropdown.close() + this.openedOnHover = false + } + + private updateChildLabels (path: string) { + this.suffixLabels = {} + + for (const entry of this.menuEntries) { + if (!entry.children) continue + + for (const child of entry.children) { + if (path.startsWith(child.routerLink)) { + this.suffixLabels[entry.label] = child.label + } + } + } + } +} diff --git a/client/src/app/shared/shared.module.ts b/client/src/app/shared/shared.module.ts index a2fa27b72..9810e9485 100644 --- a/client/src/app/shared/shared.module.ts +++ b/client/src/app/shared/shared.module.ts @@ -61,6 +61,7 @@ import { OverviewService } from '@app/shared/overview' import { UserBanModalComponent } from '@app/shared/moderation' import { UserModerationDropdownComponent } from '@app/shared/moderation/user-moderation-dropdown.component' import { BlocklistService } from '@app/shared/blocklist' +import { TopMenuDropdownComponent } from '@app/shared/menu/top-menu-dropdown.component' @NgModule({ imports: [ @@ -102,7 +103,8 @@ import { BlocklistService } from '@app/shared/blocklist' RemoteSubscribeComponent, InstanceFeaturesTableComponent, UserBanModalComponent, - UserModerationDropdownComponent + UserModerationDropdownComponent, + TopMenuDropdownComponent ], exports: [ @@ -141,6 +143,7 @@ import { BlocklistService } from '@app/shared/blocklist' InstanceFeaturesTableComponent, UserBanModalComponent, UserModerationDropdownComponent, + TopMenuDropdownComponent, NumberFormatterPipe, ObjectLengthPipe, -- cgit v1.2.3