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

Extend attachments drop helper to allow a stopover

Oliver Günthere6d50f635752
diff --git a/spec/features/work_packages/attachments/attachment_upload_spec.rb b/spec/features/work_packages/attachments/attachment_upload_spec.rb
index 81883160314..d1f6d653ee4 100644
--- a/spec/features/work_packages/attachments/attachment_upload_spec.rb
+++ b/spec/features/work_packages/attachments/attachment_upload_spec.rb
@@ -182,6 +182,18 @@ describe 'Upload attachment to work package', js: true do
end
describe 'attachment dropzone' do
+ it 'can drag something to the files tab and have it open' do
+ wp_page.expect_tab 'Activity'
+ attachments.drag_and_drop_file '.wp-attachment-upload',
+ image_fixture.path,
+ :center,
+ page.find('[data-qa-tab-id="files"]')
+
+ expect(page).to have_selector('.work-package--attachments--filename', text: 'image.png', wait: 10)
+ expect(page).not_to have_selector('op-toasters-upload-progress')
+ wp_page.expect_tab 'Files'
+ end
+
it 'can upload an image via attaching and drag & drop' do
wp_page.switch_to_tab(tab: 'files')
container = page.find('.wp-attachment-upload')
diff --git a/spec/support/components/attachments/attachments.rb b/spec/support/components/attachments/attachments.rb
index f1d1e034ff3..0a7e014cd75 100644
--- a/spec/support/components/attachments/attachments.rb
+++ b/spec/support/components/attachments/attachments.rb
@@ -8,19 +8,27 @@ module Components
##
# Drag and Drop the file loaded from path on to the (native) target element
- def drag_and_drop_file(target, path, position = :center)
+ def drag_and_drop_file(target, path, position = :center, stopover = nil)
# Remove any previous input, if any
page.execute_script <<-JS
jQuery('#temporary_attachment_files').remove()
JS
- # Use the HTML5 file dropper to create a fake drop event
- scroll_to_element(target)
+ element =
+ if target.is_a?(String)
+ target
+ else
+ # Use the HTML5 file dropper to create a fake drop event
+ scroll_to_element(target)
+ target.native
+ end
+
page.execute_script(
js_drop_files,
- target.native,
+ element,
'temporary_attachment_files',
- position.to_s
+ position.to_s,
+ stopover
)
attach_file_on_input(path, 'temporary_attachment_files')
diff --git a/spec/support/components/attachments/attachments_input.js b/spec/support/components/attachments/attachments_input.js
index 25216c84d31..f8bc3a38895 100644
--- a/spec/support/components/attachments/attachments_input.js
+++ b/spec/support/components/attachments/attachments_input.js
@@ -1,33 +1,55 @@
-var params = arguments;
+let params = arguments;
// Target element to drag & drop to
-var target = params[0];
+let target = params[0];
// name of the hidden file input field
// must exist on the page, create if needed.
-var name = params[1];
-
-var position = params[2];
-
-// We need coordinates to drop to the element
-var box = target.getBoundingClientRect();
-var targetX;
-var targetY;
-
-switch (position) {
- case 'center':
- targetX = box.left + (box.width / 2);
- targetY = box.top + (box.height / 2);
- break;
- case 'bottom':
- targetX = box.left + (box.width / 2);
- targetY = box.bottom - 1;
- break;
- default:
- throw new Error("Wrong position " + position);
+let name = params[1];
+
+let position = params[2];
+
+// We might want to drag the file over something, then wait a bit and drag it elsewhere
+let stopover = params[3];
+
+function dropOnTarget(dataTransfer) {
+ // Look up the selector
+ if (typeof target === 'string') {
+ target = document.querySelector(target);
+ }
+
+ // We need coordinates to drop to the element
+ let box = target.getBoundingClientRect();
+ let targetX;
+ let targetY;
+
+ switch (position) {
+ case 'center':
+ targetX = box.left + (box.width / 2);
+ targetY = box.top + (box.height / 2);
+ break;
+ case 'bottom':
+ targetX = box.left + (box.width / 2);
+ targetY = box.bottom - 1;
+ break;
+ default:
+ throw new Error("Wrong position " + position);
+ }
+
+ ['dragenter', 'dragover', 'drop'].forEach(function (type) {
+ let event = new MouseEvent(type, { clientX: targetX, clientY: targetY });
+
+ // Override the constructor to the DragEvent class
+ Object.setPrototypeOf(event, null);
+ event.dataTransfer = dataTransfer;
+ Object.setPrototypeOf(event, DragEvent.prototype);
+
+ console.log("Dispatching event %O", event);
+ target.dispatchEvent(event);
+ });
}
-var input = jQuery('<input>')
+let input = jQuery('<input>')
.attr('id', name)
.attr('name', name)
.attr('type', 'file')
@@ -37,7 +59,7 @@ var input = jQuery('<input>')
input.remove();
event.stopPropagation();
- var dataTransfer = {
+ let dataTransfer = {
constructor : DataTransfer,
effectAllowed : 'all',
dropEffect : 'none',
@@ -49,16 +71,28 @@ var input = jQuery('<input>')
setDragImage : function setDragImage(){}
};
- ['dragenter', 'dragover', 'drop'].forEach(function (type) {
- var event = new MouseEvent(type, { clientX: targetX, clientY: targetY });
+ // If we have a stopover, do that first and then get the target
+ if (stopover) {
+ // We need coordinates to drop to the element
+ let stopbox = stopover.getBoundingClientRect();
+ let stopX;
+ let stopY;
+
+ stopX = stopbox.left + (stopbox.width / 2);
+ stopY = stopbox.top + (stopbox.height / 2);
+
+ ['dragenter', 'dragover'].forEach(function (type) {
+ let event = new MouseEvent(type, { clientX: stopX, clientY: stopY });
+ // Override the constructor to the DragEvent class
+ Object.setPrototypeOf(event, null);
+ event.dataTransfer = dataTransfer;
+ Object.setPrototypeOf(event, DragEvent.prototype);
- // Override the constructor to the DragEvent class
- Object.setPrototypeOf(event, null);
- event.dataTransfer = dataTransfer;
- Object.setPrototypeOf(event, DragEvent.prototype);
+ console.log("Dispatching event %O", event);
+ stopover.dispatchEvent(event);
+ });
+ }
- console.log("Dispatching event %O", event);
- target.dispatchEvent(event);
- });
+ setTimeout(() => dropOnTarget(dataTransfer), 2000);
});