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

Fix creation of multiple dialogs with same id

Alexander Brandon Coles7f92387de964
diff --git a/frontend/src/turbo/dialog-stream-action.ts b/frontend/src/turbo/dialog-stream-action.ts
index 0ba00c9fa92..73ed411e1e4 100644
--- a/frontend/src/turbo/dialog-stream-action.ts
+++ b/frontend/src/turbo/dialog-stream-action.ts
@@ -1,4 +1,5 @@
import { StreamActions, StreamElement } from '@hotwired/turbo';
+import Idiomorph from 'idiomorph/dist/idiomorph.cjs';
export function registerDialogStreamAction() {
StreamActions.closeDialog = function closeDialogStreamAction(this:StreamElement) {
@@ -13,26 +14,33 @@ export function registerDialogStreamAction() {
StreamActions.dialog = function dialogStreamAction(this:StreamElement) {
const content = this.templateElement.content;
- const dialog = content.querySelector('dialog') as HTMLDialogElement;
-
- document.body.append(content);
+ const dialog = content.querySelector('dialog')!;
+ const existingElement = document.getElementById(dialog.id);
+
+ if (existingElement && existingElement instanceof HTMLDialogElement) {
+ // a dialog with this id already exists: update (morph) its contents.
+ Idiomorph.morph(existingElement, dialog.innerHTML, { morphStyle: 'innerHTML' });
+ } else {
+ // no dialog with this id exists: append <dialog-helper> to the body.
+ document.body.append(content);
+
+ // Remove the dialog on close
+ dialog.addEventListener('close', () => {
+ if (dialog.parentElement?.tagName === 'DIALOG-HELPER') {
+ dialog.parentElement.remove();
+ } else {
+ dialog.remove();
+ }
+
+ if (dialog.returnValue !== 'close-event-already-dispatched') {
+ document.dispatchEvent(new CustomEvent('dialog:close', { detail: { dialog, submitted: false } }));
+ }
+ });
+ }
// Auto-show the modal
dialog.showModal();
- // Remove the element on close
- dialog.addEventListener('close', () => {
- if (dialog.parentElement?.tagName === 'DIALOG-HELPER') {
- dialog.parentElement.remove();
- } else {
- dialog.remove();
- }
-
- if (dialog.returnValue !== 'close-event-already-dispatched') {
- document.dispatchEvent(new CustomEvent('dialog:close', { detail: { dialog, submitted: false } }));
- }
- });
-
// Hack to fix the width calculation of nested elements
// such as the CKEditor toolbar.
setTimeout(() => {