Skip to content

Commit 0cb5bfd

Browse files
committed
Refactor generic drag and drop for pragmatic DnD
Replace the shared Stimulus drag-and-drop controller internals with Atlassian Pragmatic Drag and Drop using explicit container and draggable targets.\n\nUpdate backlogs markup and component specs to use the explicit draggable contract, and add focused frontend coverage for request payloads, registration cleanup, and revert behavior.\n\nVerified with the focused frontend spec and ESLint for touched Stimulus files. Ruby component specs are not runnable in this shell because the required Bundler version from Gemfile.lock is unavailable under the current system Ruby.
1 parent 90ae29a commit 0cb5bfd

8 files changed

Lines changed: 485 additions & 77 deletions

File tree

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/*
2+
* -- copyright
3+
* OpenProject is an open source project management software.
4+
* Copyright (C) the OpenProject GmbH
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU General Public License version 3.
8+
*
9+
* OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
10+
* Copyright (C) 2006-2013 Jean-Philippe Lang
11+
* Copyright (C) 2010-2013 the ChiliProject Team
12+
*
13+
* This program is free software; you can redistribute it and/or
14+
* modify it under the terms of the GNU General Public License
15+
* as published by the Free Software Foundation; either version 2
16+
* of the License, or (at your option) any later version.
17+
*
18+
* This program is distributed in the hope that it will be useful,
19+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
* GNU General Public License for more details.
22+
*
23+
* You should have received a copy of the GNU General Public License
24+
* along with this program; if not, write to the Free Software
25+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26+
*
27+
* See COPYRIGHT and LICENSE files for more details.
28+
* ++
29+
*/
30+
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-empty-function */
31+
32+
import { Application } from '@hotwired/stimulus';
33+
import { FetchRequest, FetchResponse } from '@rails/request.js';
34+
import GenericDragAndDropController from './generic-drag-and-drop.controller';
35+
36+
const nextFrame = () => new Promise((resolve) => requestAnimationFrame(resolve));
37+
38+
describe('GenericDragAndDropController', () => {
39+
let Stimulus:Application;
40+
let fixturesElement:HTMLElement;
41+
42+
const DomAutoscrollService = class {
43+
destroy() {}
44+
};
45+
46+
interface TestableController {
47+
monitorCleanup:(() => void)|null;
48+
dropTargetCleanups:Map<HTMLElement, () => void>;
49+
draggableCleanups:Map<HTMLElement, () => void>;
50+
dragOriginSource:Element|null;
51+
dragOriginNextSibling:Element|null;
52+
disconnect():void;
53+
drop(el:Element, target:Element, source:Element|null, sibling:Element|null):Promise<void>;
54+
buildData(el:Element, target:Element):FormData;
55+
}
56+
57+
function appendTemplate(html:string) {
58+
const template = document.createElement('template');
59+
template.innerHTML = html.trim();
60+
fixturesElement.appendChild(template.content.cloneNode(true));
61+
}
62+
63+
function findController():TestableController {
64+
const controller:unknown = Stimulus.getControllerForElementAndIdentifier(
65+
fixturesElement.querySelector('[data-controller~="generic-drag-and-drop"]')!,
66+
'generic-drag-and-drop',
67+
);
68+
69+
return controller as TestableController;
70+
}
71+
72+
function buildFixture(options:{ positionMode?:'index'|'prev_id' } = {}) {
73+
const positionMode = options.positionMode ? `data-generic-drag-and-drop-position-mode-value="${options.positionMode}"` : '';
74+
75+
return `
76+
<div data-controller="generic-drag-and-drop" ${positionMode}>
77+
<ul
78+
id="stories"
79+
data-generic-drag-and-drop-target="container"
80+
data-target-id="sprint:1"
81+
data-target-allowed-drag-type="story">
82+
<li
83+
id="story-1"
84+
data-generic-drag-and-drop-target="draggable"
85+
data-draggable-id="1"
86+
data-draggable-type="story"
87+
data-drop-url="/stories/1">
88+
<button type="button" class="DragHandle">Drag 1</button>
89+
</li>
90+
<li
91+
id="story-2"
92+
data-generic-drag-and-drop-target="draggable"
93+
data-draggable-id="2"
94+
data-draggable-type="story"
95+
data-drop-url="/stories/2">
96+
<button type="button" class="DragHandle">Drag 2</button>
97+
</li>
98+
</ul>
99+
</div>
100+
`;
101+
}
102+
103+
beforeEach(() => {
104+
fixturesElement = document.createElement('div');
105+
document.body.appendChild(fixturesElement);
106+
107+
(window as any).OpenProject = {
108+
getPluginContext: () => Promise.resolve({
109+
classes: {
110+
DomAutoscrollService,
111+
},
112+
}),
113+
};
114+
});
115+
116+
afterEach(() => {
117+
Stimulus?.stop();
118+
fixturesElement.remove();
119+
});
120+
121+
async function startWithFixture(html:string) {
122+
appendTemplate(html);
123+
124+
Stimulus = Application.start();
125+
Stimulus.handleError = (error, message, detail) => {
126+
console.error(error, message, detail);
127+
};
128+
Stimulus.register('generic-drag-and-drop', GenericDragAndDropController);
129+
130+
await nextFrame();
131+
await nextFrame();
132+
}
133+
134+
it('tracks cleanup handles for the monitor and explicit target registrations', async () => {
135+
await startWithFixture(buildFixture());
136+
137+
const controller = findController();
138+
139+
expect(controller.monitorCleanup).toEqual(jasmine.any(Function));
140+
expect(controller.dropTargetCleanups.size).toBe(3);
141+
expect(controller.draggableCleanups.size).toBe(2);
142+
});
143+
144+
it('builds index-based form data with the target id', async () => {
145+
await startWithFixture(buildFixture());
146+
147+
const controller = findController();
148+
const container = fixturesElement.querySelector('#stories')!;
149+
const story2 = fixturesElement.querySelector('#story-2')!;
150+
151+
container.insertBefore(story2, container.firstElementChild);
152+
153+
const data = controller.buildData(story2, container);
154+
155+
expect(data.get('position')).toBe('1');
156+
expect(data.get('target_id')).toBe('sprint:1');
157+
});
158+
159+
it('builds prev_id form data from the preceding draggable', async () => {
160+
await startWithFixture(buildFixture({ positionMode: 'prev_id' }));
161+
162+
const controller = findController();
163+
const container = fixturesElement.querySelector('#stories')!;
164+
const story2 = fixturesElement.querySelector('#story-2')!;
165+
166+
const dataAtBottom = controller.buildData(story2, container);
167+
168+
expect(dataAtBottom.get('prev_id')).toBe('1');
169+
170+
container.insertBefore(story2, container.firstElementChild);
171+
172+
const dataAtTop = controller.buildData(story2, container);
173+
174+
expect(dataAtTop.get('prev_id')).toBe('');
175+
});
176+
177+
it('disconnect clears registered cleanup handles', async () => {
178+
await startWithFixture(buildFixture());
179+
180+
const controller = findController();
181+
182+
controller.disconnect();
183+
184+
expect(controller.monitorCleanup).toBeNull();
185+
expect(controller.dropTargetCleanups.size).toBe(0);
186+
expect(controller.draggableCleanups.size).toBe(0);
187+
});
188+
189+
it('reverts an optimistic move when the drop request fails', async () => {
190+
await startWithFixture(buildFixture());
191+
192+
const controller = findController();
193+
const container = fixturesElement.querySelector<HTMLElement>('#stories')!;
194+
const story1 = fixturesElement.querySelector<HTMLElement>('#story-1')!;
195+
const story2 = fixturesElement.querySelector<HTMLElement>('#story-2')!;
196+
197+
container.appendChild(story1);
198+
controller.dragOriginSource = container;
199+
controller.dragOriginNextSibling = story2;
200+
201+
spyOn(FetchRequest.prototype, 'perform').and.resolveTo({
202+
ok: false,
203+
statusCode: 500,
204+
} as unknown as FetchResponse);
205+
206+
await controller.drop(story1, container, null, null);
207+
208+
expect(Array.from(container.children).map((child) => (child as HTMLElement).id)).toEqual(['story-1', 'story-2']);
209+
});
210+
});

0 commit comments

Comments
 (0)