aboutsummaryrefslogtreecommitdiffstats
path: root/src/angular/modals/modal.service.ts
blob: 231b062b6c1611c4063ddfecaf2787e8130376e9 (plain)
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
import { Injectable, Type, ComponentRef } from '@angular/core';
import { ModalComponent } from "./modal.component";
import { CreateDynamicComponentService } from "../utils/create-dynamic-component.service";
import { IModalConfig, ModalType, ModalSize, IModalButtonComponent } from "./models/modal-config";
import { ButtonType } from '../common/enums';
import { ModalButtonComponent } from './modal-button.component';

@Injectable()
export class ModalService {

    constructor(private createDynamicComponentService: CreateDynamicComponentService) {
    }

    private getBaseModal = (type: ModalType | ButtonType, title: string, message: string, testId: string, buttons?: ModalButtonComponent[]): ModalComponent => {
        const modalConfig = {
            size: ModalSize.small,
            title: title,
            message: message,
            testId: testId,
            buttons: buttons ? buttons : [{ text: 'OK', type: type, closeModal: true }],
            type: type
        } as IModalConfig;
        const modalInstance: ComponentRef<ModalComponent> = this.openModal(modalConfig);
        return modalInstance.instance;
    }

    /* Shortcut method to open basic modals with title, message, and OK button that simply closes the modal. */
    public openInfoModal = (title: string, message: string, testId: string, buttons?: ModalButtonComponent[]): ModalComponent => {
        return this.getBaseModal(ModalType.info, title, message, testId, buttons);
    }

    public openWarningModal = (title: string, message: string, testId: string, buttons?: ModalButtonComponent[]): ModalComponent => {
        return this.getBaseModal(ModalType.warning, title, message, testId, buttons);
    }

    public openErrorModal = (title: string, message: string, testId: string, buttons?: ModalButtonComponent[]): ModalComponent => {
        return this.getBaseModal(ModalType.error, title, message, testId, buttons);
    }

    public openSuccessModal = (title: string, message: string, testId: string, buttons?: ModalButtonComponent[]): ModalComponent => {
        return this.getBaseModal(ModalType.success, title, message, testId, buttons);
    }

    public openCustomModal = (modalConfig: IModalConfig, dynamicComponentType: Type<any>, dynamicComponentInput?: any) => {
        const modalInstance: ComponentRef<ModalComponent> = this.openModal(modalConfig);
        this.createInnnerComponent(modalInstance, dynamicComponentType, dynamicComponentInput);
        return modalInstance.instance;
    }

    public createInnnerComponent = (modalInstance: ComponentRef<ModalComponent>, dynamicComponentType: Type<any>, dynamicComponentInput?: any): void => {
        modalInstance.instance.innerModalContent = this.createDynamicComponentService.insertComponentDynamically(dynamicComponentType, dynamicComponentInput, modalInstance.instance.dynamicContentContainer);
    }

    public openModal = (customModalData: IModalConfig): ComponentRef<ModalComponent> => {
        let modalInstance: ComponentRef<ModalComponent> = this.createDynamicComponentService.createComponentDynamically(ModalComponent, customModalData);
        modalInstance.instance.instanceRef = modalInstance;
        return modalInstance;
    }

}