aboutsummaryrefslogtreecommitdiffstats
path: root/src/angular/modals/modal.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/angular/modals/modal.service.ts')
-rw-r--r--src/angular/modals/modal.service.ts90
1 files changed, 25 insertions, 65 deletions
diff --git a/src/angular/modals/modal.service.ts b/src/angular/modals/modal.service.ts
index d80ad1f..231b062 100644
--- a/src/angular/modals/modal.service.ts
+++ b/src/angular/modals/modal.service.ts
@@ -2,99 +2,59 @@ 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 {
- private currentModal: ComponentRef<any>;
-
constructor(private createDynamicComponentService: CreateDynamicComponentService) {
}
- /* Shortcut method to open an alert modal with title, message, and close button that simply closes the modal. */
- public openAlertModal(title: string, message: string, actionButtonText?: string, actionButtonCallback?: Function, testId?: string) {
+ 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: this.createButtons('secondary', actionButtonText, actionButtonCallback),
- type: ModalType.alert
- } as IModalConfig;
- const modalInstance: ComponentRef<ModalComponent> = this.openModal(modalConfig);
- this.currentModal = modalInstance;
- return modalInstance;
- }
-
- public openActionModal = (title: string, message: string, actionButtonText?: string, actionButtonCallback?: Function, testId?: string): ComponentRef<ModalComponent> => {
- const modalConfig = {
- size: ModalSize.small,
- title: title,
- message: message,
- testId: testId,
- type: ModalType.standard,
- buttons: this.createButtons('primary', actionButtonText, actionButtonCallback)
- } as IModalConfig;
- const modalInstance: ComponentRef<ModalComponent> = this.openModal(modalConfig);
- this.currentModal = modalInstance;
- return modalInstance;
- }
-
- public openErrorModal = (errorMessage?: string, testId?: string): ComponentRef<ModalComponent> => {
- const modalConfig = {
- size: ModalSize.small,
- title: 'Error',
- message: errorMessage,
- testId: testId,
- buttons: [{text: "OK", type: "alert", closeModal: true}],
- type: ModalType.error
+ buttons: buttons ? buttons : [{ text: 'OK', type: type, closeModal: true }],
+ type: type
} as IModalConfig;
const modalInstance: ComponentRef<ModalComponent> = this.openModal(modalConfig);
- this.currentModal = modalInstance;
- return modalInstance;
+ return modalInstance.instance;
}
- public openCustomModal = (modalConfig: IModalConfig, dynamicComponentType: Type<any>, dynamicComponentInput?: any) => {
- const modalInstance: ComponentRef<ModalComponent> = this.openModal(modalConfig);
- this.createInnnerComponent(dynamicComponentType, dynamicComponentInput);
- return modalInstance;
+ /* 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 createInnnerComponent = (dynamicComponentType: Type<any>, dynamicComponentInput?: any): void => {
- this.currentModal.instance.innerModalContent = this.createDynamicComponentService.insertComponentDynamically(dynamicComponentType, dynamicComponentInput, this.currentModal.instance.dynamicContentContainer);
+ public openWarningModal = (title: string, message: string, testId: string, buttons?: ModalButtonComponent[]): ModalComponent => {
+ return this.getBaseModal(ModalType.warning, title, message, testId, buttons);
}
- public openModal = (customModalData: IModalConfig): ComponentRef<ModalComponent> => {
- const modalInstance: ComponentRef<ModalComponent> = this.createDynamicComponentService.createComponentDynamically(ModalComponent, customModalData);
- modalInstance.instance.closeAnimationComplete.subscribe(() => {
- this.destroyModal();
- });
- this.currentModal = modalInstance;
- return modalInstance;
+ public openErrorModal = (title: string, message: string, testId: string, buttons?: ModalButtonComponent[]): ModalComponent => {
+ return this.getBaseModal(ModalType.error, title, message, testId, buttons);
}
- public getCurrentInstance = () => {
- return this.currentModal.instance;
+ public openSuccessModal = (title: string, message: string, testId: string, buttons?: ModalButtonComponent[]): ModalComponent => {
+ return this.getBaseModal(ModalType.success, title, message, testId, buttons);
}
- public closeModal = (): void => { // triggers closeModal animation, which then triggers toggleModal.done and the subscription to destroyModal
- this.currentModal.instance.modalVisible = false;
+ public openCustomModal = (modalConfig: IModalConfig, dynamicComponentType: Type<any>, dynamicComponentInput?: any) => {
+ const modalInstance: ComponentRef<ModalComponent> = this.openModal(modalConfig);
+ this.createInnnerComponent(modalInstance, dynamicComponentType, dynamicComponentInput);
+ return modalInstance.instance;
}
- private createButtons = (type: string, actionButtonText?: string, actionButtonCallback?: Function): Array<IModalButtonComponent> => {
- const buttons: Array<IModalButtonComponent> = [];
- if (actionButtonText && actionButtonCallback) {
- buttons.push({text: actionButtonText, type: type, callback: actionButtonCallback, closeModal: true});
- buttons.push({text: 'Cancel', type: 'secondary', closeModal: true});
- } else {
- buttons.push({text: 'Cancel', type: type, closeModal: true});
- }
-
- return buttons;
+ public createInnnerComponent = (modalInstance: ComponentRef<ModalComponent>, dynamicComponentType: Type<any>, dynamicComponentInput?: any): void => {
+ modalInstance.instance.innerModalContent = this.createDynamicComponentService.insertComponentDynamically(dynamicComponentType, dynamicComponentInput, modalInstance.instance.dynamicContentContainer);
}
- private destroyModal = (): void => {
- this.currentModal.destroy();
+ public openModal = (customModalData: IModalConfig): ComponentRef<ModalComponent> => {
+ let modalInstance: ComponentRef<ModalComponent> = this.createDynamicComponentService.createComponentDynamically(ModalComponent, customModalData);
+ modalInstance.instance.instanceRef = modalInstance;
+ return modalInstance;
}
}