import { Overlay, OverlayConfig, OverlayRef } from '@angular/cdk/overlay'; import { TemplatePortal } from '@angular/cdk/portal'; import { Component, ElementRef, Input, TemplateRef, ViewChild, ViewContainerRef } from '@angular/core'; import { Store } from '@ngrx/store'; import { ConfigActions, DashboardActions, DashboardSelectors, SubscriptionnerDirective, YapVisibility, YapVisibilityType } from '@onepoint-yap/dashboard'; import { withLatestFrom } from 'rxjs/operators'; @Component({ selector: 'yap-save-view', templateUrl: './save-view.component.html' }) export class SaveViewComponent extends SubscriptionnerDirective { @Input() label = true; @ViewChild('input') input!: ElementRef; manifestNameOpened: boolean = false; id?: string; value: string = ''; descr: string = ''; private overlayRef?: OverlayRef; @ViewChild('template', { read: TemplateRef }) template!: TemplateRef; visibility: YapVisibility = { type: YapVisibilityType.PRIVATE }; constructor(private store: Store, private overlay: Overlay, private viewContainerRef: ViewContainerRef) { super(); this.subscriptions.add( this.store .select(DashboardSelectors.currentManifestId) .pipe(withLatestFrom(this.store.select(DashboardSelectors.manifestsList))) .subscribe(([manifestId, list]) => { this.value = list.find((man) => man.id === manifestId)?.name ?? ''; this.id = manifestId; }) ); } toggleManifestName() { this.manifestNameOpened = !this.manifestNameOpened; if (this.manifestNameOpened) { this.store.dispatch(DashboardActions.requestAllRoutes()); this.openWithTemplate(); setTimeout(() => { this.input.nativeElement.focus(); }, 10); } else { this.overlayRef?.dispose(); this.store.dispatch(ConfigActions.closeOverlay({ id: this.constructor.name })); } } submit() { this.toggleManifestName(); this.store.dispatch( DashboardActions.saveManifest({ name: this.value, id: this.id, description: this.descr, visibility: this.visibility }) ); } changeValue($event: string) { this.value = $event; } changeDescr($event: string) { this.descr = $event; } openWithTemplate() { this.overlayRef = this.overlay.create( new OverlayConfig({ hasBackdrop: true, panelClass: ['o-modal-wrapper'], backdropClass: 'backdrop' }) ); this.store.dispatch(ConfigActions.addingOverlay({ id: this.constructor.name })); this.overlayRef.backdropClick().subscribe((_) => this.toggleManifestName()); this.overlayRef.attach(new TemplatePortal(this.template, this.viewContainerRef)); } }