|
@@ -1,4 +1,32 @@
|
|
|
-import { getElements, scrollTo } from '../util';
|
|
|
|
|
|
|
+import { getElements, scrollTo, isTruthy } from '../util';
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * When editing an object, it is sometimes desirable to customize the form action *without*
|
|
|
|
|
+ * overriding the form's `submit` event. For example, the 'Save & Continue' button. We don't want
|
|
|
|
|
+ * to use the `formaction` attribute on that element because it will be included on the form even
|
|
|
|
|
+ * if the button isn't clicked.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @example
|
|
|
|
|
+ * ```html
|
|
|
|
|
+ * <button type="button" return-url="/special-url/">
|
|
|
|
|
+ * Save & Continue
|
|
|
|
|
+ * </button>
|
|
|
|
|
+ * ```
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param event Click event.
|
|
|
|
|
+ */
|
|
|
|
|
+function handleSubmitWithReturnUrl(event: MouseEvent): void {
|
|
|
|
|
+ const element = event.target as HTMLElement;
|
|
|
|
|
+ if (element.tagName === 'BUTTON') {
|
|
|
|
|
+ const button = element as HTMLButtonElement;
|
|
|
|
|
+ const action = button.getAttribute('return-url');
|
|
|
|
|
+ const form = button.form;
|
|
|
|
|
+ if (form !== null && isTruthy(action)) {
|
|
|
|
|
+ form.action = action;
|
|
|
|
|
+ form.submit();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
function handleFormSubmit(event: Event, form: HTMLFormElement): void {
|
|
function handleFormSubmit(event: Event, form: HTMLFormElement): void {
|
|
|
// Track the names of each invalid field.
|
|
// Track the names of each invalid field.
|
|
@@ -38,6 +66,15 @@ function handleFormSubmit(event: Event, form: HTMLFormElement): void {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * Attach event listeners to form buttons with the `return-url` attribute present.
|
|
|
|
|
+ */
|
|
|
|
|
+function initReturnUrlSubmitButtons(): void {
|
|
|
|
|
+ for (const button of getElements<HTMLButtonElement>('button[return-url]')) {
|
|
|
|
|
+ button.addEventListener('click', handleSubmitWithReturnUrl);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Attach an event listener to each form's submitter (button[type=submit]). When called, the
|
|
* Attach an event listener to each form's submitter (button[type=submit]). When called, the
|
|
|
* callback checks the validity of each form field and adds the appropriate Bootstrap CSS class
|
|
* callback checks the validity of each form field and adds the appropriate Bootstrap CSS class
|
|
@@ -54,4 +91,5 @@ export function initFormElements(): void {
|
|
|
submitter.addEventListener('click', (event: Event) => handleFormSubmit(event, form));
|
|
submitter.addEventListener('click', (event: Event) => handleFormSubmit(event, form));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ initReturnUrlSubmitButtons();
|
|
|
}
|
|
}
|