1 import {EditorForm, EditorFormDefinition} from "./forms";
2 import {EditorContainerUiElement} from "./core";
3 import closeIcon from "@icons/close.svg";
4 import {el} from "../../utils/dom";
6 export interface EditorModalDefinition {
10 export interface EditorFormModalDefinition extends EditorModalDefinition {
11 form: EditorFormDefinition;
14 export class EditorFormModal extends EditorContainerUiElement {
15 protected definition: EditorFormModalDefinition;
16 protected key: string;
18 constructor(definition: EditorFormModalDefinition, key: string) {
19 super([new EditorForm(definition.form)]);
20 this.definition = definition;
24 show(defaultValues: Record<string, string>) {
25 const dom = this.getDOMElement();
26 document.body.append(dom);
28 const form = this.getForm();
29 form.setValues(defaultValues);
30 form.setOnCancel(this.hide.bind(this));
32 this.getContext().manager.setModalActive(this.key, this);
36 this.getDOMElement().remove();
37 this.getContext().manager.setModalInactive(this.key);
40 getForm(): EditorForm {
41 return this.children[0] as EditorForm;
44 protected buildDOM(): HTMLElement {
45 const closeButton = el('button', {
46 class: 'editor-modal-close',
48 title: this.trans('Close'),
50 closeButton.innerHTML = closeIcon;
51 closeButton.addEventListener('click', this.hide.bind(this));
53 const modal = el('div', {class: 'editor-modal editor-form-modal'}, [
54 el('div', {class: 'editor-modal-header'}, [
55 el('div', {class: 'editor-modal-title'}, [this.trans(this.definition.title)]),
58 el('div', {class: 'editor-modal-body'}, [
59 this.getForm().getDOMElement(),
63 const wrapper = el('div', {class: 'editor-modal-wrapper'}, [modal]);
65 wrapper.addEventListener('click', event => {
66 if (event.target && !modal.contains(event.target as HTMLElement)) {