import {Directive, OnDestroy } from '@angular/core'; import { Subscription } from 'rxjs'; import { ModalService } from './modal.service'; /** * Common functionality across modals. */ // TODO: Add Angular decorator. @Directive() // eslint-disable-next-line @angular-eslint/directive-class-suffix export class Modal implements OnDestroy { destroy: Function = () => {}; // TODO: change back to next line if start seeing issues with modals derived from Modal class. Initialized to fix download modal karma tests // destroy: Function; ok: Function; // Cancel callback function cancelCb: Function; data: any = {}; text: any = {}; subscription: Subscription; isDismissable: boolean = false; dismissed: boolean = false; // modalService is renamed to baseModalService, because Angular doesn't like when inherited class has the same // name for the constructor parameter, even if it's the same thing constructor(private baseModalService: ModalService) { // Listens to hideMessages, so you can automatically close messages from outside this.subscription = this.baseModalService.hideMessages$.subscribe(() => { this.destroy(); }); } ngOnDestroy(): void { if (this.subscription) this.subscription.unsubscribe(); } closeModal(): void { this.destroy(); } cancel(): void { if (typeof this.cancelCb === 'function') { this.cancelCb(); } this.dismissed = false; this.closeModal(); } confirm(): void { this.closeModal(); this.ok(this.data); // If further confirmation dialogs dismissed, fire event to set cookie if (this.isDismissable && this.dismissed) { this.baseModalService.noModalConfirmation(); } } }