all files / component/popup/model/ popup-config.model.ts

21.33% Statements 16/75
0% Branches 0/28
8.33% Functions 1/12
19.18% Lines 14/73
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161                                                                                                                                                                                                                                                                                                     
/*
 *  @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 { ComponentType } from '@angular/cdk/portal';
import { TemplateRef } from '@angular/core';
import { MatDialogConfig } from '@angular/material';
import { BehaviorSubject } from 'rxjs';
import { DejaPopupAction } from './popup-action.model';
 
export class DejaPopupConfig extends MatDialogConfig {
 
    public static dialogCount = 0;
    public readonly positionStart = { top: 100, left: 100 };
    public readonly dimensionDefault = { width: '60vw', height: '65vh' };
 
    public dejaPopupCom$?: BehaviorSubject<DejaPopupAction>;
 
    public actionComponentRef: ComponentType<any>;
    public contentComponentRef: ComponentType<any>;
    public contentTemplate: DejaPopupContentTemplate<any>;
    public actions: DejaPopupAction[];
    public autoFocus = false;
    public autoposition?: boolean;
    public content?: string | string[];
    public data?: any;
    public dialogPanelId: string;
    public fullscreen = false;
    public maxWidth = '100vw';
    public maxHeight = '100vh';
    public padding?: boolean;
    public shareActions = true;
    public title?: string;
    public toolbarActions?: DejaPopupAction[];
    public toolbarColor?: DialogToolbarColor = 'primary';
    public toolbarIconName?: string;
    public toolbarType?: DialogToolbarType;
    public url?: string;
    public buttonFullscreenExit?: boolean;
 
    constructor() {
        super();
    }
 
    public ensureDimension() {
        if (!this.width) {
            this.setDefaultWidth();
        }
        if (!this.height) {
            this.setDefaultHeight();
        }
    }
 
    public setDefaultWidth() {
        this.width = this.dimensionDefault.width;
    }
 
    public setDefaultHeight() {
        this.height = this.dimensionDefault.height;
    }
 
    public getDefaultPosition() {
        const shift = (10 * DejaPopupConfig.dialogCount) - (100 * Math.ceil(DejaPopupConfig.dialogCount / 10));
        return {
            top: `${(this.positionStart.top + shift)}px`,
            left: `${(this.positionStart.left + shift)}px`,
        };
    }
 
    public getMatDialogConfig(): MatDialogConfig {
 
        this.createIds();
 
        if (this.autoposition) {
            this.ensurePosition();
        }
        if (!this.title) {
            this.title = `Popup ${DejaPopupConfig.dialogCount}`;
        }
 
        const config: MatDialogConfig<DejaPopupConfig> = new MatDialogConfig();
 
        config.ariaDescribedBy = this.ariaDescribedBy;
        config.backdropClass = this.backdropClass;
        config.direction = this.direction;
        config.disableClose = this.disableClose;
        config.hasBackdrop = this.hasBackdrop;
        config.height = this.height;
        config.id = this.id;
        config.maxHeight = this.maxHeight;
        config.maxWidth = this.maxWidth;
        config.minHeight = this.minHeight;
        config.minWidth = this.minWidth;
        config.panelClass = this.panelClass;
        config.position = this.position;
        config.role = this.role;
        config.viewContainerRef = this.viewContainerRef;
        config.width = this.width;
 
        config.data = this;
        return config;
 
    }
 
    public addPanelClass(panelClass: string) {
        if (!this.panelClass) {
            this.panelClass = [panelClass];
        } else {
            if (Array.isArray(this.panelClass)) {
                this.panelClass.push(panelClass);
            } else {
                this.panelClass = [panelClass, this.panelClass];
            }
        }
    }
 
    public hasContent(): boolean {
        return !!this.content && this.content.length > 0;
    }
 
    public hasActions(): boolean {
        return !!this.actions && !!this.actionComponentRef;
    }
 
    public ensurePosition() {
 
        if (!this.position) {
            this.position = this.getDefaultPosition();
            return true;
        }
 
        if (!this.position.top && !this.position.bottom) {
            this.position.top = `${(this.positionStart.top)}px`;
        }
 
        if (!this.position.left && !this.position.right) {
            this.position.left = `${(this.positionStart.left)}px`;
        }
 
    }
 
    private createIds() {
        this.id = `deja-popup-${++DejaPopupConfig.dialogCount}`;
        const className = `deja-popup-pane-${DejaPopupConfig.dialogCount}`;
        this.addPanelClass(className);
        this.dialogPanelId = `.${className}`;
    }
 
}
 
export type DialogToolbarType = 'base' | 'window';
export type DialogToolbarColor = null | 'primary' | 'accent' | 'warn';
export interface DejaPopupContentTemplate<T> {
    templateRef: TemplateRef<T>;
    templateContext?: any;
}