All files / src/services/zd-modal modal-service.ts

100% Statements 22/22
100% Branches 3/3
100% Functions 9/9
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 7293x 93x         93x       93x             7x 7x 7x               1x 1x               1x 1x               5x 5x 4x               3x 3x 2x 1x                   2x      
import { Metadata } from '@zeedhi/core';
import { IModal, Modal } from '../../components';
 
/**
 * Modal service class
 */
export class ModalService {
	/**
	 * Modals collection
	 */
	public static modals: Modal[] = [];
 
	/**
	 * Creates a new modal instance
	 * @param modal modal structure
	 */
	public static create(modal: IModal) {
		const instance = new Modal(modal);
		ModalService.modals.push(instance);
		return instance;
	}
 
	/**
	 * Creates a new modal instance from JSON
	 * @param modal modal structure
	 */
	public static async createFromJson(modalPath: string, local?: boolean, JSONCache?: boolean) {
		const modal = await Metadata.get(modalPath, local, JSONCache);
		return ModalService.create(modal as IModal);
	}
 
	/**
	 * Creates a new modal instance from JSON
	 * @param modal modal structure
	 */
	public static async createFromJsonPost(modalPath: string, params?: object) {
		const modal = await Metadata.post(modalPath, params);
		return ModalService.create(modal as IModal);
	}
 
	/**
	 * Deletes modal
	 * @param modal modal instance
	 */
	public static delete(modal: Modal) {
		const index = ModalService.modals.findIndex((m) => m.name === modal.name);
		if (index !== -1) {
			ModalService.modals.splice(index, 1);
		}
	}
 
	/**
	 * Closes modal by name
	 */
	public static closeByName(name: string) {
		if (ModalService.modals.length) {
			const modal = ModalService.modals.filter((m) => m.name === name);
			if (modal.length) {
				modal[0].hide();
			}
		}
	}
 
	/**
	 * Gets a modal by name
	 * @param name modal name
	 */
	public static getModal(name: string): Modal | undefined {
		return ModalService.modals.find((m) => m.name === name);
	}
}