all files / component/popup/service/ popup.service.ts

38.1% Statements 24/63
10.34% Branches 3/29
14.29% Functions 2/14
35% Lines 21/60
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                                                                                                                                                                                                                                                                                     
/*
 *  @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 { Injectable } from '@angular/core';
import { TemplateRef } from '@angular/core/src/linker/template_ref';
import { MatDialog, MatDialogRef } from '@angular/material';
import { BehaviorSubject, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { DejaPopupActionsComponent } from '../component/popup-actions/popup-actions.component';
import { DejaPopupAdvancedComponent } from '../component/popup-advanced/popup-advanced.component';
import { DejaPopupComponent } from '../component/popup/popup.component';
import { DejaPopupAction } from '../model/popup-action.model';
import { DejaPopupBase } from '../model/popup-base.class';
import { DejaPopupConfig } from '../model/popup-config.model';
import { DejaPopupReponse } from '../model/popup-response.model';
 
@Injectable()
export class DejaPopupService extends MatDialog {
 
    private _dialogCom$: BehaviorSubject<DejaPopupAction>;
    public readonly openDialogs: MatDialogRef<DejaPopupBase>[];
    public get dejaPopupCom$(): BehaviorSubject<DejaPopupAction> {
        if (!this._dialogCom$) {
            this._dialogCom$ = new BehaviorSubject(null);
        }
        return this._dialogCom$;
    }
 
    public defaultActionComponent: ComponentType<any> = DejaPopupActionsComponent;
 
    /**
     * Displays a modal dialog, with the given buttons.
     * @param title
     * @param message
     * @param buttons types de DpiButton
     * @param customComponent Custom Component that must extend DpiDialogComponent.
     * @param data Données que l'on peux passer au Dialog
     * @return La réponse du dialog sous forme d'un Observable
     */
    public openInline(
        title: string,
        content: string,
        buttons: DejaPopupAction[],
        config: DejaPopupConfig = new DejaPopupConfig(),
    ): Observable<DejaPopupReponse> {
 
        config.title = title;
        config.content = content;
        config.actions = buttons;
 
        const dialogRef: MatDialogRef<DejaPopupBase> = this.open(DejaPopupComponent, config);
 
        return dialogRef.afterClosed().pipe(
            map((resp: any) => {
                return new DejaPopupReponse(resp, dialogRef.componentInstance);
            }));
    }
 
    public openCustom(
        customComponent: ComponentType<DejaPopupBase> | TemplateRef<any>,
        config: DejaPopupConfig = new DejaPopupConfig(),
    ): Observable<DejaPopupReponse> {
 
        const dialogRef: MatDialogRef<DejaPopupBase> = this.open(customComponent, config);
 
        return dialogRef.afterClosed().pipe(
            map((resp: any) => {
                return new DejaPopupReponse(resp, dialogRef.componentInstance);
            }));
    }
 
    public openUrl(
        url: string,
        config: DejaPopupConfig = new DejaPopupConfig(),
    ): Observable<DejaPopupReponse> {
 
        config.url = url;
        config.ensureDimension();
 
        const dialogRef: MatDialogRef<DejaPopupAdvancedComponent> = this.open(DejaPopupAdvancedComponent, config);
 
        return dialogRef.afterClosed().pipe(
            map((resp: any) => {
                return new DejaPopupReponse(resp, dialogRef.componentInstance);
            }));
    }
 
    public openAdvanced(
        config: DejaPopupConfig = new DejaPopupConfig(),
    ): MatDialogRef<DejaPopupAdvancedComponent> {
 
        const dialogRef: MatDialogRef<DejaPopupAdvancedComponent> = this.open(DejaPopupAdvancedComponent, config);
 
        return dialogRef;
 
    }
    public openAdvanced$(
        config: DejaPopupConfig = new DejaPopupConfig(),
    ): Observable<DejaPopupReponse> {
 
        const dialogRef = this.openAdvanced(config);
        return dialogRef.afterClosed().pipe(
            map((resp: any) => {
                return new DejaPopupReponse(resp, dialogRef.componentInstance);
            }));
 
    }
 
    public openPopUp(
        config: DejaPopupConfig = new DejaPopupConfig(),
    ): Observable<DejaPopupReponse> {
 
        config.hasBackdrop = false;
        // config.isModal = false;
 
        // config.ensureDimension();
        if (config.autoposition === undefined) {
            config.autoposition = true;
        }
 
        if (config.toolbarType === undefined) {
            config.toolbarType = 'window';
        }
 
        return this.openAdvanced$(config);
    }
 
    /**
      * Opens a modal dialog containing the given component.
      * @param componentOrTemplateRef Type of the component to load into the dialog,
      *     or a TemplateRef to instantiate as the dialog content.
      * @param config Extra configuration options.
      * @returns Reference to the newly-opened dialog.
      */
    public open<T>(
        componentOrTemplateRef: ComponentType<T> | TemplateRef<T>,
        config?: DejaPopupConfig
    ): MatDialogRef<T> {
 
        if (config.shareActions && !config.dejaPopupCom$) {
            config.dejaPopupCom$ = this.dejaPopupCom$;
        }
 
        if (!config.actionComponentRef && this.defaultActionComponent) {
            config.actionComponentRef = this.defaultActionComponent;
        }
 
        return super.open(componentOrTemplateRef, config.getMatDialogConfig());
 
    }
 
}