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' | 'checkbox';
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;
45 if (this.definition.type === 'checkbox') {
46 input.checked = Boolean(value);
50 input.dispatchEvent(new Event('change'));
54 return this.definition.name;
57 protected buildDOM(): HTMLElement {
58 const id = `editor-form-field-${this.definition.name}-${Date.now()}`;
59 let input: HTMLElement;
61 if (this.definition.type === 'select') {
62 const options = (this.definition as EditorSelectFormFieldDefinition).valuesByLabel
63 const labels = Object.keys(options);
64 const optionElems = labels.map(label => el('option', {value: options[label]}, [this.trans(label)]));
65 input = el('select', {id, name: this.definition.name, class: 'editor-form-field-input'}, optionElems);
66 } else if (this.definition.type === 'textarea') {
67 input = el('textarea', {id, name: this.definition.name, class: 'editor-form-field-input'});
68 } else if (this.definition.type === 'checkbox') {
69 input = el('input', {id, name: this.definition.name, type: 'checkbox', class: 'editor-form-field-input-checkbox', value: 'true'});
71 input = el('input', {id, name: this.definition.name, class: 'editor-form-field-input'});
74 return el('div', {class: 'editor-form-field-wrapper'}, [
75 el('label', {class: 'editor-form-field-label', for: id}, [this.trans(this.definition.label)]),
81 export class EditorForm extends EditorContainerUiElement {
82 protected definition: EditorFormDefinition;
83 protected onCancel: null|(() => void) = null;
84 protected onSuccessfulSubmit: null|(() => void) = null;
86 constructor(definition: EditorFormDefinition) {
87 let children: (EditorFormField|EditorUiElement)[] = definition.fields.map(fieldDefinition => {
88 if (isUiBuilderDefinition(fieldDefinition)) {
89 return fieldDefinition.build();
91 return new EditorFormField(fieldDefinition)
95 this.definition = definition;
98 setValues(values: Record<string, string>) {
99 for (const name of Object.keys(values)) {
100 const field = this.getFieldByName(name);
102 field.setValue(values[name]);
107 setOnCancel(callback: () => void) {
108 this.onCancel = callback;
111 setOnSuccessfulSubmit(callback: () => void) {
112 this.onSuccessfulSubmit = callback;
115 protected getFieldByName(name: string): EditorFormField|null {
117 const search = (children: EditorUiElement[]): EditorFormField|null => {
118 for (const child of children) {
119 if (child instanceof EditorFormField && child.getName() === name) {
121 } else if (child instanceof EditorContainerUiElement) {
122 const matchingChild = search(child.getChildren());
124 return matchingChild;
132 return search(this.getChildren());
135 protected buildDOM(): HTMLElement {
136 const cancelButton = el('button', {type: 'button', class: 'editor-form-action-secondary'}, [this.trans('Cancel')]);
137 const form = el('form', {}, [
138 ...this.children.map(child => child.getDOMElement()),
139 el('div', {class: 'editor-form-actions'}, [
141 el('button', {type: 'submit', class: 'editor-form-action-primary'}, [this.trans(this.definition.submitText)]),
145 form.addEventListener('submit', async (event) => {
146 event.preventDefault();
147 const formData = new FormData(form as HTMLFormElement);
148 const result = await this.definition.action(formData, this.getContext());
149 if (result && this.onSuccessfulSubmit) {
150 this.onSuccessfulSubmit();
154 cancelButton.addEventListener('click', (event) => {
164 export class EditorFormTab extends EditorContainerUiElement {
166 protected definition: EditorFormTabDefinition;
167 protected fields: EditorUiElement[];
168 protected id: string;
170 constructor(definition: EditorFormTabDefinition) {
171 const fields = definition.contents.map(fieldDef => {
172 if (isUiBuilderDefinition(fieldDef)) {
173 return fieldDef.build();
175 return new EditorFormField(fieldDef)
180 this.definition = definition;
181 this.fields = fields;
182 this.id = uniqueId();
185 public getLabel(): string {
186 return this.getContext().translate(this.definition.label);
189 public getId(): string {
193 protected buildDOM(): HTMLElement {
197 class: 'editor-form-tab-content',
199 id: `editor-tabpanel-${this.id}`,
200 'aria-labelledby': `editor-tab-${this.id}`,
202 this.fields.map(f => f.getDOMElement())
206 export class EditorFormTabs extends EditorContainerUiElement {
208 protected definitions: EditorFormTabDefinition[] = [];
209 protected tabs: EditorFormTab[] = [];
211 constructor(definitions: EditorFormTabDefinition[]) {
212 const tabs: EditorFormTab[] = definitions.map(d => new EditorFormTab(d));
215 this.definitions = definitions;
219 protected buildDOM(): HTMLElement {
220 const controls: HTMLElement[] = [];
221 const contents: HTMLElement[] = [];
223 const selectTab = (tabIndex: number) => {
224 for (let i = 0; i < controls.length; i++) {
225 controls[i].setAttribute('aria-selected', (i === tabIndex) ? 'true' : 'false');
227 for (let i = 0; i < contents.length; i++) {
228 contents[i].hidden = !(i === tabIndex);
232 for (const tab of this.tabs) {
233 const button = el('button', {
234 class: 'editor-form-tab-control',
237 id: `editor-tab-${tab.getId()}`,
238 'aria-controls': `editor-tabpanel-${tab.getId()}`
239 }, [tab.getLabel()]);
240 contents.push(tab.getDOMElement());
241 controls.push(button);
243 button.addEventListener('click', event => {
244 selectTab(controls.indexOf(button));
250 return el('div', {class: 'editor-form-tab-container'}, [
251 el('div', {class: 'editor-form-tab-controls'}, controls),
252 el('div', {class: 'editor-form-tab-contents'}, contents),