all files / component/popup/model/ popup-base.class.ts

25.71% Statements 9/35
0% Branches 0/17
10% Functions 1/10
25% Lines 8/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91                                                                                                                                                                     
/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
 
import { ComponentPortal, Portal } from '@angular/cdk/portal';
import { ElementRef, Injector, OnInit, Renderer2 } from '@angular/core';
import { MatDialogRef } from '@angular/material';
import { Subscription } from 'rxjs';
import { filter, first, tap } from 'rxjs/operators';
import { DejaPopupAction } from './popup-action.model';
import { DejaPopupConfig } from './popup-config.model';
 
export abstract class DejaPopupBase implements OnInit {
 
    public actions: DejaPopupAction[];
    public actionSelected: DejaPopupAction;
    public isMinified = false;
    public isFullscreen = false;
    public dialogRef: MatDialogRef<DejaPopupBase>;
    public config: DejaPopupConfig;
    public actionsPortal: Portal<any>;
    protected injector: Injector;
    protected renderer?: Renderer2;
    protected elRef?: ElementRef;
    protected unlisten?: () => void;
 
    private aSub: Subscription[];
 
    public abstract doAction(action: DejaPopupAction): any;
 
    public ngOnInit() {
        this.aSub = [];
        this.dialogRef.beforeClose().pipe(
            first(),
            tap(() => {
                if (this.unlisten) {
                    this.unlisten();
                }
 
                this.destroy();
 
                if (this.config.dejaPopupCom$) {
                    const action = new DejaPopupAction('dialog-close', 'popup-tray');
                    this.config.dejaPopupCom$.next(action);
                }
            }))
            .subscribe();
 
        if (this.config.actionComponentRef) {
            this.actionsPortal = new ComponentPortal(this.config.actionComponentRef, undefined, this.injector);
        }
 
        if (this.config.dejaPopupCom$) {
            this.aSub.push(
                this.config.dejaPopupCom$.pipe(
                    filter((a: DejaPopupAction) => !!a && !!a.target && a.target === this.config.id),
                    tap((a: DejaPopupAction) => this.doAction(a)),
                    tap((action: DejaPopupAction) => {
                        this.actionSelected = action;
                        if (action.isFinalAction) {
                            this.dialogRef.close(action);
                        }
                    }))
                    .subscribe()
            );
        }
    }
 
    public dispatchAction(action: DejaPopupAction) {
 
        if (!action) {
            return false;
        }
 
        if (this.config.dejaPopupCom$) {
            this.config.dejaPopupCom$.next(action);
        }
 
    }
 
    protected destroy() {
        this.aSub.forEach((s: Subscription) => s.unsubscribe());
    }
 
}