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));
31 form.setOnSuccessfulSubmit(this.hide.bind(this));
33 this.getContext().manager.setModalActive(this.key, this);
37 this.getDOMElement().remove();
38 this.getContext().manager.setModalInactive(this.key);
41 getForm(): EditorForm {
42 return this.children[0] as EditorForm;
45 protected buildDOM(): HTMLElement {
46 const closeButton = el('button', {
47 class: 'editor-modal-close',
49 title: this.trans('Close'),
51 closeButton.innerHTML = closeIcon;
52 closeButton.addEventListener('click', this.hide.bind(this));
54 const modal = el('div', {class: 'editor-modal editor-form-modal'}, [
55 el('div', {class: 'editor-modal-header'}, [
56 el('div', {class: 'editor-modal-title'}, [this.trans(this.definition.title)]),
59 el('div', {class: 'editor-modal-body'}, [
60 this.getForm().getDOMElement(),
64 const wrapper = el('div', {class: 'editor-modal-wrapper'}, [modal]);
66 wrapper.addEventListener('click', event => {
67 if (event.target && !modal.contains(event.target as HTMLElement)) {