1 import {EditorUiContext, EditorUiElement} from "./core";
2 import {EditorContainerUiElement} from "./containers";
3 import {el} from "../../helpers";
5 export interface EditorFormFieldDefinition {
8 type: 'text' | 'select';
11 export interface EditorSelectFormFieldDefinition extends EditorFormFieldDefinition {
13 valuesByLabel: Record<string, string>
16 export interface EditorFormDefinition {
18 action: (formData: FormData, context: EditorUiContext) => boolean;
19 fields: EditorFormFieldDefinition[];
22 export class EditorFormField extends EditorUiElement {
23 protected definition: EditorFormFieldDefinition;
25 constructor(definition: EditorFormFieldDefinition) {
27 this.definition = definition;
30 setValue(value: string) {
31 const input = this.getDOMElement().querySelector('input,select') as HTMLInputElement;
36 return this.definition.name;
39 protected buildDOM(): HTMLElement {
40 const id = `editor-form-field-${this.definition.name}-${Date.now()}`;
41 let input: HTMLElement;
43 if (this.definition.type === 'select') {
44 const options = (this.definition as EditorSelectFormFieldDefinition).valuesByLabel
45 const labels = Object.keys(options);
46 const optionElems = labels.map(label => el('option', {value: options[label]}, [label]));
47 input = el('select', {id, name: this.definition.name, class: 'editor-form-field-input'}, optionElems);
49 input = el('input', {id, name: this.definition.name, class: 'editor-form-field-input'});
52 return el('div', {class: 'editor-form-field-wrapper'}, [
53 el('label', {class: 'editor-form-field-label', for: id}, [this.trans(this.definition.label)]),
59 export class EditorForm extends EditorContainerUiElement {
60 protected definition: EditorFormDefinition;
61 protected onCancel: null|(() => void) = null;
63 constructor(definition: EditorFormDefinition) {
64 super(definition.fields.map(fieldDefinition => new EditorFormField(fieldDefinition)));
65 this.definition = definition;
68 setValues(values: Record<string, string>) {
69 for (const name of Object.keys(values)) {
70 const field = this.getFieldByName(name);
72 field.setValue(values[name]);
77 setOnCancel(callback: () => void) {
78 this.onCancel = callback;
81 protected getFieldByName(name: string): EditorFormField|null {
82 for (const child of this.children as EditorFormField[]) {
83 if (child.getName() === name) {
91 protected buildDOM(): HTMLElement {
92 const cancelButton = el('button', {type: 'button', class: 'editor-form-action-secondary'}, [this.trans('Cancel')]);
93 const form = el('form', {}, [
94 ...this.children.map(child => child.getDOMElement()),
95 el('div', {class: 'editor-form-actions'}, [
97 el('button', {type: 'submit', class: 'editor-form-action-primary'}, [this.trans(this.definition.submitText)]),
101 form.addEventListener('submit', (event) => {
102 event.preventDefault();
103 const formData = new FormData(form as HTMLFormElement);
104 this.definition.action(formData, this.getContext());
107 cancelButton.addEventListener('click', (event) => {