All files / src/components/zd-dialog dialog.ts

100% Statements 44/44
100% Branches 11/11
100% Functions 9/9
100% Lines 44/44

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 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 15393x 93x   93x           93x       97x         97x         97x         97x         97x         97x         97x         97x   97x             100x         1x         97x                                 97x 97x 97x               101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x       15x     1x 1x 1x 1x                 2x 1x 1x 1x   1x                       8x 8x             7x 7x      
import { KeyMap, Metadata } from '@zeedhi/core';
import { Button } from '..';
import { IButton } from '../zd-button/interfaces';
import { Component } from '../zd-component/component';
import { IDialog } from './interfaces';
 
/**
 * Base class for Dialog component.
 */
export class Dialog extends Component implements IDialog {
	/**
	 * Buttons collection that will be displayed on dialog footer
	 */
	public buttons: IButton[] = [];
 
	/**
	 * The maximum width of the content
	 */
	public maxWidth: string | number = 560;
 
	/**
	 * Clicking outside will not dismiss the dialog
	 */
	public persistent = true;
 
	/**
	 * The text displayed on dialog
	 */
	public text = '';
 
	/**
	 * The title displayed on dialog
	 */
	public title = '';
 
	/**
	 * Dialog type (normal, success, error, warning, info)
	 */
	public type = 'normal';
 
	/**
	 * Confirm button component name
	 */
	public confirmButton?: string = 'dialogButton';
 
	/**
	 * Deny button component name
	 */
	public denyButton?: string = undefined;
 
	public exit?: boolean = false;
 
	/**
	 * Default dialog buttons
	 * @private
	 */
	private getDefaultButtons(): IButton[] {
		return [
			{
				name: 'dialogButton',
				label: 'OK',
				component: 'ZdButton',
				events: { click: () => this.hide() },
			},
		];
	}
 
	protected defaultValues = {
		confirmButton: this.confirmButton,
		denyButton: this.denyButton,
		name: this.name,
		maxWidth: this.maxWidth,
		persistent: this.persistent,
		type: this.type,
		title: this.title,
		text: this.text,
		exit: this.exit,
	};
 
	/**
	 * Creates a new Dialog
	 * @param props Dialog structure
	 */
	constructor(props: IDialog) {
		super(props);
		this.assignDialogProperties(props);
		this.createAccessors();
	}
 
	/**
	 * Assign all dialog properties
	 * @param dialog Dialog structure
	 */
	public assignDialogProperties(dialog: IDialog) {
		this.buttons = dialog.buttons?.length ? dialog.buttons : this.getDefaultButtons();
		this.confirmButton = this.getInitValue('confirmButton', dialog.confirmButton, this.defaultValues.confirmButton);
		this.denyButton = this.getInitValue('denyButton', dialog.denyButton, this.defaultValues.denyButton);
		this.name = this.getInitValue('name', dialog.name, this.defaultValues.name);
		this.maxWidth = this.getInitValue('maxWidth', dialog.maxWidth, this.defaultValues.maxWidth);
		this.persistent = this.getInitValue('persistent', dialog.persistent, this.defaultValues.persistent);
		this.type = this.getInitValue('type', dialog.type, this.defaultValues.type);
		this.title = this.getInitValue('title', dialog.title, this.defaultValues.title);
		this.text = this.getInitValue('text', dialog.text, this.defaultValues.text);
		this.exit = this.getInitValue('exit', dialog.exit, this.defaultValues.exit);
		this.isVisible = false;
	}
 
	protected clickDefaultButtonKeyMapping(): any {
		return {
			enter: {
				event: () => {
					if (this.confirmButton) {
						const confirmButton = Metadata.getInstance<Button>(this.confirmButton);
						if (confirmButton?.events?.click && typeof confirmButton.events.click === 'function') {
							confirmButton.events.click({ component: new Button(confirmButton) });
						}
					}
				},
				stop: true,
				index: 89,
			},
			esc: {
				event: () => {
					if (this.denyButton) {
						const denyButton = Metadata.getInstance<Button>(this.denyButton);
						if (denyButton?.events?.click && typeof denyButton.events.click === 'function')
							denyButton.events.click({ component: new Button(denyButton) });
					} else {
						this.hide();
					}
				},
				index: 89,
				stop: true,
			},
		};
	}
	/**
	 * Displays dialog
	 */
	public show() {
		KeyMap.bind(this.clickDefaultButtonKeyMapping(), this);
		this.isVisible = true;
	}
 
	/**
	 * Hides dialog
	 */
	public hide() {
		this.isVisible = false;
		KeyMap.unbind(this.clickDefaultButtonKeyMapping(), this);
	}
}