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;
17 constructor(definition: EditorFormModalDefinition) {
18 super([new EditorForm(definition.form)]);
19 this.definition = definition;
22 show(defaultValues: Record<string, string>) {
23 const dom = this.getDOMElement();
24 document.body.append(dom);
26 const form = this.getForm();
27 form.setValues(defaultValues);
28 form.setOnCancel(this.hide.bind(this));
32 this.getDOMElement().remove();
35 protected getForm(): EditorForm {
36 return this.children[0] as EditorForm;
39 protected buildDOM(): HTMLElement {
40 const closeButton = el('button', {
41 class: 'editor-modal-close',
43 title: this.trans('Close'),
45 closeButton.innerHTML = closeIcon;
46 closeButton.addEventListener('click', this.hide.bind(this));
48 const modal = el('div', {class: 'editor-modal editor-form-modal'}, [
49 el('div', {class: 'editor-modal-header'}, [
50 el('div', {class: 'editor-modal-title'}, [this.trans(this.definition.title)]),
53 el('div', {class: 'editor-modal-body'}, [
54 this.getForm().getDOMElement(),
58 const wrapper = el('div', {class: 'editor-modal-wrapper'}, [modal]);
60 wrapper.addEventListener('click', event => {
61 if (event.target && !modal.contains(event.target as HTMLElement)) {