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

Add mutation observer to detect draggable items

Mir Bhatia0a1273c083f9
diff --git a/frontend/src/stimulus/controllers/dynamic/generic-drag-and-drop.controller.ts b/frontend/src/stimulus/controllers/dynamic/generic-drag-and-drop.controller.ts
index 1ba36d77dbf..52f151d4059 100644
--- a/frontend/src/stimulus/controllers/dynamic/generic-drag-and-drop.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/generic-drag-and-drop.controller.ts
@@ -45,8 +45,11 @@ export default class GenericDragAndDropController extends Controller {
containerTargets:Element[];
+ observer:MutationObserver|null = null;
+
connect() {
this.initDrake();
+ this.startMutationObserver();
}
initDrake() {
@@ -65,8 +68,6 @@ export default class GenericDragAndDropController extends Controller {
revertOnSpill: true, // enable reverting of elements if they are dropped outside of a valid target
},
)
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
- .on('drag', this.drag.bind(this))
// eslint-disable-next-line @typescript-eslint/no-misused-promises
.on('drop', this.drop.bind(this));
@@ -109,7 +110,7 @@ export default class GenericDragAndDropController extends Controller {
};
// if the target has a container accessor, use that as the container instead of the element itself
- // we need this e.g. in Primer's boderbox component as we cannot add required data attributes to the ul element there
+ // we need this e.g. in Primer's borderbox component as we cannot add required data attributes to the ul element there
const containerAccessor = target.getAttribute('data-target-container-accessor');
if (containerAccessor) {
@@ -141,11 +142,6 @@ export default class GenericDragAndDropController extends Controller {
return true;
}
- drag(_:Element, _source:Element|null) {
- // discover new target containers if they have been added to the DOM via Turbo streams
- this.reInitDrakeContainers();
- }
-
async drop(el:Element, target:Element, _source:Element|null, _sibling:Element|null) {
const dropUrl = el.getAttribute('data-drop-url');
const data = this.buildData(el, target);
@@ -166,9 +162,6 @@ export default class GenericDragAndDropController extends Controller {
} else {
const text = await response.text();
Turbo.renderStreamMessage(text);
- // reinit drake containers as the DOM will be updated by Turbo streams
- // otherwise the DOM references in the Drake instance will be outdated
- setTimeout(() => this.reInitDrakeContainers(), 100);
}
}
@@ -181,6 +174,8 @@ export default class GenericDragAndDropController extends Controller {
if (this.drake) {
this.drake.destroy();
}
+
+ this.stopMutationObserver();
}
protected buildData(el:Element, target:Element):FormData {
@@ -205,4 +200,33 @@ export default class GenericDragAndDropController extends Controller {
return data;
}
+
+ private startMutationObserver() {
+ this.observer = new MutationObserver((mutations) => {
+ const addedNodes = mutations
+ .filter((mutation:MutationRecord) => mutation.type === 'childList')
+ .map((mutation:MutationRecord) => Array.from(mutation.addedNodes))
+ .reduce((acc, val) => acc.concat(val), []);
+
+ const newTarget = addedNodes.some((node) =>
+ node instanceof Element
+ && node.matches('[data-is-drag-and-drop-target="true"], [data-is-drag-and-drop-target="true"] *'));
+
+ if (newTarget) {
+ this.reInitDrakeContainers();
+ }
+ });
+
+ this.observer.observe(this.element, {
+ childList: true,
+ subtree: true,
+ });
+ }
+
+ private stopMutationObserver() {
+ if (this.observer) {
+ this.observer.disconnect();
+ this.observer = null;
+ }
+ }
}
diff --git a/modules/meeting/app/controllers/concerns/meetings/agenda_component_streams.rb b/modules/meeting/app/controllers/concerns/meetings/agenda_component_streams.rb
index 0753b0da31b..9f29330ed80 100644
--- a/modules/meeting/app/controllers/concerns/meetings/agenda_component_streams.rb
+++ b/modules/meeting/app/controllers/concerns/meetings/agenda_component_streams.rb
@@ -189,9 +189,7 @@ module Meetings
end
def update_list_via_turbo_stream(meeting: @meeting, form_hidden: true, form_type: :simple)
- # replace needs to be called in order to mount the drag and drop handlers again
- # update would not do that and drag and drop would stop working after the first update
- replace_via_turbo_stream(
+ update_via_turbo_stream(
component: MeetingAgendaItems::ListComponent.new(
meeting:,
form_hidden:,
@@ -204,7 +202,7 @@ module Meetings
end
def update_item_via_turbo_stream(state: :show, meeting_agenda_item: @meeting_agenda_item, display_notes_input: nil)
- replace_via_turbo_stream(
+ update_via_turbo_stream(
component: MeetingAgendaItems::ItemComponent.new(
state:,
meeting_agenda_item:,