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

100% Statements 21/21
100% Branches 3/3
100% Functions 8/8
100% Lines 18/18

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 72 73 74 75 76 77 78  93x 93x 93x               93x                   5x 1x   4x 1x   3x       2x                   2x             2x 2x               1x 1x             1x 3x               5x      
import { IAlert } from '../../components/zd-alert/interfaces';
import { AlertQueue } from './alert-queue';
import { AlertReplace } from './alert-replace';
import { AlertStack } from './alert-stack';
import { IAlertsManager } from './interfaces';
 
export type Multiple = 'replace' | 'queue' | 'stack';
 
/**
 * Alert Service Class
 */
export class AlertService {
	/**
	 * TODO: conferir se o alertsManager será necessário após a atualização 3.6.0 do vuetify,
	 * que vai implementar a funcionalidade internamente
	 * https://github.com/vuetifyjs/vuetify/milestone/72
	 * https://github.com/vuetifyjs/vuetify/issues/2384
	 */
	public static alertsManager: IAlertsManager;
 
	public static instantiateManager(multiple: Multiple) {
		if (multiple === 'queue') {
			return new AlertQueue();
		}
		if (multiple === 'stack') {
			return new AlertStack();
		}
		return new AlertReplace();
	}
 
	public static registerManager(instance: IAlertsManager) {
		AlertService.alertsManager = instance;
	}
 
	/**
	 * Displays an alert.
	 * If has an opened alert it will be closed.
	 * @param alert Alert structure
	 * @returns Alert id
	 */
	public static show(alert: IAlert): number {
		return AlertService.alertsManager.show(alert);
	}
 
	/**
	 * Hides alert by index. Default index is 0
	 */
	public static hide(index = 0): void {
		AlertService.alertsManager.visibleInstances[index].hide();
		AlertService.remove(index);
	}
 
	/**
	 * Hides alert by alert id (returned by the `show` method)
	 * @param id
	 */
	public static hideById(id: number) {
		const index = AlertService.alertsManager.visibleInstances.findIndex((instance) => instance.id === id);
		AlertService.hide(index);
	}
 
	/**
	 * Hides all of the alerts
	 */
	public static hideAll() {
		const { length } = AlertService.alertsManager.visibleInstances;
		for (let i = 0; i < length; i += 1) AlertService.remove(0);
	}
 
	/**
	 * Removes an alert from the alert queue
	 * @param index index of the alert to be removed
	 */
	public static remove(index: number) {
		AlertService.alertsManager.remove(index);
	}
}