4 EditorContainerUiElement,
5 EditorUiBuilderDefinition,
8 import {uniqueId} from "../../../services/util";
9 import {el} from "../../utils/dom";
11 export interface EditorFormFieldDefinition {
14 type: 'text' | 'select' | 'textarea';
17 export interface EditorSelectFormFieldDefinition extends EditorFormFieldDefinition {
19 valuesByLabel: Record<string, string>
22 export type EditorFormFields = (EditorFormFieldDefinition|EditorUiBuilderDefinition)[];
24 interface EditorFormTabDefinition {
26 contents: EditorFormFields;
29 export interface EditorFormDefinition {
31 action: (formData: FormData, context: EditorUiContext) => Promise<boolean>;
32 fields: EditorFormFields;
35 export class EditorFormField extends EditorUiElement {
36 protected definition: EditorFormFieldDefinition;
38 constructor(definition: EditorFormFieldDefinition) {
40 this.definition = definition;
43 setValue(value: string) {
44 const input = this.getDOMElement().querySelector('input,select,textarea') as HTMLInputElement;
46 input.dispatchEvent(new Event('change'));
50 return this.definition.name;
53 protected buildDOM(): HTMLElement {
54 const id = `editor-form-field-${this.definition.name}-${Date.now()}`;
55 let input: HTMLElement;
57 if (this.definition.type === 'select') {
58 const options = (this.definition as EditorSelectFormFieldDefinition).valuesByLabel
59 const labels = Object.keys(options);
60 const optionElems = labels.map(label => el('option', {value: options[label]}, [this.trans(label)]));
61 input = el('select', {id, name: this.definition.name, class: 'editor-form-field-input'}, optionElems);
62 } else if (this.definition.type === 'textarea') {
63 input = el('textarea', {id, name: this.definition.name, class: 'editor-form-field-input'});
65 input = el('input', {id, name: this.definition.name, class: 'editor-form-field-input'});
68 return el('div', {class: 'editor-form-field-wrapper'}, [
69 el('label', {class: 'editor-form-field-label', for: id}, [this.trans(this.definition.label)]),
75 export class EditorForm extends EditorContainerUiElement {
76 protected definition: EditorFormDefinition;
77 protected onCancel: null|(() => void) = null;
78 protected onSuccessfulSubmit: null|(() => void) = null;
80 constructor(definition: EditorFormDefinition) {
81 let children: (EditorFormField|EditorUiElement)[] = definition.fields.map(fieldDefinition => {
82 if (isUiBuilderDefinition(fieldDefinition)) {
83 return fieldDefinition.build();
85 return new EditorFormField(fieldDefinition)
89 this.definition = definition;
92 setValues(values: Record<string, string>) {
93 for (const name of Object.keys(values)) {
94 const field = this.getFieldByName(name);
96 field.setValue(values[name]);
101 setOnCancel(callback: () => void) {
102 this.onCancel = callback;
105 setOnSuccessfulSubmit(callback: () => void) {
106 this.onSuccessfulSubmit = callback;
109 protected getFieldByName(name: string): EditorFormField|null {
111 const search = (children: EditorUiElement[]): EditorFormField|null => {
112 for (const child of children) {
113 if (child instanceof EditorFormField && child.getName() === name) {
115 } else if (child instanceof EditorContainerUiElement) {
116 const matchingChild = search(child.getChildren());
118 return matchingChild;
126 return search(this.getChildren());
129 protected buildDOM(): HTMLElement {
130 const cancelButton = el('button', {type: 'button', class: 'editor-form-action-secondary'}, [this.trans('Cancel')]);
131 const form = el('form', {}, [
132 ...this.children.map(child => child.getDOMElement()),
133 el('div', {class: 'editor-form-actions'}, [
135 el('button', {type: 'submit', class: 'editor-form-action-primary'}, [this.trans(this.definition.submitText)]),
139 form.addEventListener('submit', async (event) => {
140 event.preventDefault();
141 const formData = new FormData(form as HTMLFormElement);
142 const result = await this.definition.action(formData, this.getContext());
143 if (result && this.onSuccessfulSubmit) {
144 this.onSuccessfulSubmit();
148 cancelButton.addEventListener('click', (event) => {
158 export class EditorFormTab extends EditorContainerUiElement {
160 protected definition: EditorFormTabDefinition;
161 protected fields: EditorUiElement[];
162 protected id: string;
164 constructor(definition: EditorFormTabDefinition) {
165 const fields = definition.contents.map(fieldDef => {
166 if (isUiBuilderDefinition(fieldDef)) {
167 return fieldDef.build();
169 return new EditorFormField(fieldDef)
174 this.definition = definition;
175 this.fields = fields;
176 this.id = uniqueId();
179 public getLabel(): string {
180 return this.getContext().translate(this.definition.label);
183 public getId(): string {
187 protected buildDOM(): HTMLElement {
191 class: 'editor-form-tab-content',
193 id: `editor-tabpanel-${this.id}`,
194 'aria-labelledby': `editor-tab-${this.id}`,
196 this.fields.map(f => f.getDOMElement())
200 export class EditorFormTabs extends EditorContainerUiElement {
202 protected definitions: EditorFormTabDefinition[] = [];
203 protected tabs: EditorFormTab[] = [];
205 constructor(definitions: EditorFormTabDefinition[]) {
206 const tabs: EditorFormTab[] = definitions.map(d => new EditorFormTab(d));
209 this.definitions = definitions;
213 protected buildDOM(): HTMLElement {
214 const controls: HTMLElement[] = [];
215 const contents: HTMLElement[] = [];
217 const selectTab = (tabIndex: number) => {
218 for (let i = 0; i < controls.length; i++) {
219 controls[i].setAttribute('aria-selected', (i === tabIndex) ? 'true' : 'false');
221 for (let i = 0; i < contents.length; i++) {
222 contents[i].hidden = !(i === tabIndex);
226 for (const tab of this.tabs) {
227 const button = el('button', {
228 class: 'editor-form-tab-control',
231 id: `editor-tab-${tab.getId()}`,
232 'aria-controls': `editor-tabpanel-${tab.getId()}`
233 }, [tab.getLabel()]);
234 contents.push(tab.getDOMElement());
235 controls.push(button);
237 button.addEventListener('click', event => {
238 selectTab(controls.indexOf(button));
244 return el('div', {class: 'editor-form-tab-container'}, [
245 el('div', {class: 'editor-form-tab-controls'}, controls),
246 el('div', {class: 'editor-form-tab-contents'}, contents),