Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Use separate form attribute on submit button to trigger with turbo

Oliver Güntherf9e0190db5a4
diff --git a/frontend/src/turbo/setup.ts b/frontend/src/turbo/setup.ts
index 694d3c041ab..b6ee05e2c33 100644
--- a/frontend/src/turbo/setup.ts
+++ b/frontend/src/turbo/setup.ts
@@ -1,29 +1,12 @@
import "../typings/shims.d.ts"
import * as Turbo from '@hotwired/turbo';
import { registerDialogStreamAction } from './dialog-stream-action';
+import { addTurboEventListeners } from './turbo-event-listeners';
// Disable default turbo-drive for now as we don't need it for now AND it breaks angular routing
Turbo.session.drive = false;
// Start turbo
Turbo.start();
-// Error handling when "Content missing" returned
-document.addEventListener('turbo:frame-missing', (event:CustomEvent) => {
- const { detail: { response, visit } } = event as { detail:{ response:Response, visit:(url:string) => void } };
- event.preventDefault();
- visit(response.url);
-});
-
-// Close the primer dialog when the form inside has been submitted with a success response
-// It is necessary to close the primer dialog using the `close()` method, otherwise
-// it will leave an overflow:hidden attribute on the body, which prevents scrolling on the page.
-document.addEventListener('turbo:submit-end', (event:CustomEvent) => {
- const { detail: { success }, target } = event as { detail:{ success:boolean }, target:EventTarget };
-
- if (success && target instanceof HTMLFormElement) {
- const dialog = target.closest('dialog') as HTMLDialogElement;
- dialog && dialog.close();
- }
-});
-
+addTurboEventListeners();
registerDialogStreamAction();
diff --git a/frontend/src/turbo/turbo-event-listeners.ts b/frontend/src/turbo/turbo-event-listeners.ts
new file mode 100644
index 00000000000..551d56a8b9c
--- /dev/null
+++ b/frontend/src/turbo/turbo-event-listeners.ts
@@ -0,0 +1,24 @@
+import { navigator } from '@hotwired/turbo';
+
+export function addTurboEventListeners() {
+ // Close the primer dialog when the form inside has been submitted with a success response
+ // It is necessary to close the primer dialog using the `close()` method, otherwise
+ // it will leave an overflow:hidden attribute on the body, which prevents scrolling on the page.
+ document.addEventListener('turbo:submit-end', (event:CustomEvent) => {
+ const { detail: { success }, target } = event as { detail:{ success:boolean }, target:EventTarget };
+
+ if (success && target instanceof HTMLFormElement) {
+ const dialog = target.closest('dialog') as HTMLDialogElement;
+ dialog && dialog.close();
+ }
+ });
+
+ // Forward a form submit on a button[type=submit][form=formid] to the turbo navigator
+ // cf. https://github.com/hotwired/turbo/issues/1246
+ document.addEventListener('submit', (event:SubmitEvent) => {
+ if (event.submitter?.getAttribute('form')) {
+ event.preventDefault();
+ navigator.submitForm(event.target as HTMLFormElement, event.submitter);
+ }
+ });
+}