2020-07-10 13:34:49 +03:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Akveo. All Rights Reserved.
|
|
|
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-10-30 14:42:59 +03:00
|
|
|
import { Component, ViewChild, AfterViewInit, Input, OnDestroy } from '@angular/core';
|
2020-03-12 13:31:36 +03:00
|
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
2020-10-30 14:42:59 +03:00
|
|
|
import { Subject, merge } from 'rxjs';
|
|
|
|
|
import { map, take, takeUntil } from 'rxjs/operators';
|
|
|
|
|
import { NbMediaBreakpointsService, NbMediaBreakpoint, NbPopoverDirective, NbThemeService } from '@nebular/theme';
|
2020-03-12 13:31:36 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'ngx-material-theme-link',
|
|
|
|
|
templateUrl: './material-theme-link.component.html',
|
|
|
|
|
styleUrls: ['./material-theme-link.component.scss'],
|
|
|
|
|
})
|
2020-10-30 14:42:59 +03:00
|
|
|
export class MaterialThemeLinkComponent implements AfterViewInit, OnDestroy {
|
2020-03-12 13:31:36 +03:00
|
|
|
|
2020-10-30 14:42:59 +03:00
|
|
|
private destroy$ = new Subject<void>();
|
|
|
|
|
private hidePopover$ = new Subject<void>();
|
|
|
|
|
|
|
|
|
|
showPopover: boolean = false;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set withPopover(value: any) {
|
2020-03-12 13:31:36 +03:00
|
|
|
this.showPopover = coerceBooleanProperty(value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 14:42:59 +03:00
|
|
|
@ViewChild(NbPopoverDirective) popover: NbPopoverDirective;
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
private breakpointService: NbMediaBreakpointsService,
|
|
|
|
|
private themeService: NbThemeService,
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
ngAfterViewInit(): void {
|
|
|
|
|
if (!this.showPopover) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.themeService.onMediaQueryChange()
|
|
|
|
|
.pipe(
|
|
|
|
|
map(([, currentBreakpoint]: NbMediaBreakpoint[]) => this.shouldShowPopover(currentBreakpoint)),
|
|
|
|
|
takeUntil(merge(this.destroy$, this.hidePopover$)),
|
|
|
|
|
)
|
|
|
|
|
.subscribe((showPopover: boolean) => {
|
|
|
|
|
if (showPopover) {
|
|
|
|
|
this.popover.show();
|
|
|
|
|
} else {
|
|
|
|
|
this.popover.hide();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.hidePopover$
|
|
|
|
|
.pipe(
|
|
|
|
|
take(1),
|
|
|
|
|
takeUntil(this.destroy$),
|
|
|
|
|
)
|
|
|
|
|
.subscribe(() => this.popover.hide());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
this.destroy$.next();
|
|
|
|
|
}
|
2020-03-12 13:31:36 +03:00
|
|
|
|
2020-10-30 14:42:59 +03:00
|
|
|
hidePopover(): void {
|
|
|
|
|
this.hidePopover$.next();
|
2020-03-12 13:31:36 +03:00
|
|
|
}
|
|
|
|
|
|
2020-10-30 14:42:59 +03:00
|
|
|
private shouldShowPopover(breakpoint: NbMediaBreakpoint): boolean {
|
|
|
|
|
return breakpoint.width >= this.breakpointService.getByName('is').width;
|
2020-03-12 13:31:36 +03:00
|
|
|
}
|
2020-03-25 19:07:02 +03:00
|
|
|
}
|