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

WIP reordering by drag and drop

Bruno Pagno97e1f41dbec9
diff --git a/app/components/admin/custom_fields/hierarchy/item_component.html.erb b/app/components/admin/custom_fields/hierarchy/item_component.html.erb
index 8cf7afea3a1..80f2b831b8f 100644
--- a/app/components/admin/custom_fields/hierarchy/item_component.html.erb
+++ b/app/components/admin/custom_fields/hierarchy/item_component.html.erb
@@ -35,7 +35,7 @@ See COPYRIGHT and LICENSE files for more details.
flex_layout(align_items: :center, justify_content: :space_between, test_selector: "op-custom-fields--hierarchy-item") do |item_container|
item_container.with_column(flex_layout: true) do |item_information|
item_information.with_column(mr: 2) do
- render(Primer::OpenProject::DragHandle.new)
+ render(Primer::OpenProject::DragHandle.new(draggable: true))
end
item_information.with_column(mr: 2) do
render(Primer::Beta::Link.new(href: custom_field_item_path(@root.custom_field_id, model), underline: false)) do
diff --git a/app/components/admin/custom_fields/hierarchy/items_component.html.erb b/app/components/admin/custom_fields/hierarchy/items_component.html.erb
index 3889cc39311..480db99f87b 100644
--- a/app/components/admin/custom_fields/hierarchy/items_component.html.erb
+++ b/app/components/admin/custom_fields/hierarchy/items_component.html.erb
@@ -27,6 +27,8 @@ See COPYRIGHT and LICENSE files for more details.
++#%>
+<% helpers.content_controller "admin--hierarchy-item", dynamic: true %>
+
<%=
component_wrapper(tag: "turbo-frame", refresh: :morph, data: { turbo_action: :advance }) do
flex_layout do |container|
@@ -57,7 +59,24 @@ See COPYRIGHT and LICENSE files for more details.
end
else
children.each do |item|
- box.with_row do
+ drag_controls = if item.persisted?
+ {
+ "data-controller": "admin--hierarchy-item",
+ "data-action": "dragstart->admin--hierarchy-item#dragstart
+ dragenter->admin--hierarchy-item#dragenter
+ dragleave->admin--hierarchy-item#dragleave
+ drop->admin--hierarchy-item#drop",
+ "data-hierarchy-item-id": item.id,
+ "data-sort-order": item.sort_order,
+ "data-frame-id": Admin::CustomFields::Hierarchy::ItemsComponent.wrapper_key,
+ "data-move-url": move_custom_field_item_url(root.custom_field_id, item),
+ "data-index-url": custom_field_items_url(root.custom_field_id)
+ }
+ else
+ {}
+ end
+
+ box.with_row(**drag_controls) do
render Admin::CustomFields::Hierarchy::ItemComponent.new(item: item)
end
end
diff --git a/frontend/src/stimulus/controllers/dynamic/admin/hierarchy-item.controller.ts b/frontend/src/stimulus/controllers/dynamic/admin/hierarchy-item.controller.ts
new file mode 100644
index 00000000000..f304ca094c2
--- /dev/null
+++ b/frontend/src/stimulus/controllers/dynamic/admin/hierarchy-item.controller.ts
@@ -0,0 +1,110 @@
+/*
+ * -- copyright
+ * OpenProject is an open source project management software.
+ * Copyright (C) the OpenProject GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version 3.
+ *
+ * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+ * Copyright (C) 2006-2013 Jean-Philippe Lang
+ * Copyright (C) 2010-2013 the ChiliProject Team
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * See COPYRIGHT and LICENSE files for more details.
+ * ++
+ */
+
+import { Controller } from '@hotwired/stimulus';
+import * as Turbo from '@hotwired/turbo';
+
+export default class HierarchyItemController extends Controller {
+ connect() {}
+
+ dragstart(event:DragEvent) {
+ const element = dataNode(event.target);
+
+ if (event.dataTransfer) {
+ event.dataTransfer.setDragImage(element, 25, 25);
+ event.dataTransfer.effectAllowed = 'move';
+ event.dataTransfer.setData('application/drag-key', element.dataset.hierarchyItemId || '');
+ }
+ }
+
+ dragenter(event:DragEvent) {
+ const target = dataNode(event.target);
+ if (target != null) {
+ target.classList.add('border-dashed');
+ event.preventDefault();
+ }
+ }
+
+ dragleave(event:DragEvent) {
+ const target = dataNode(event.target);
+ if (target != null) {
+ target.classList.remove('border-dashed');
+ event.preventDefault();
+ }
+ }
+
+ drop(event:DragEvent) {
+ const targetElement = dataNode(event.target);
+
+ if (event.dataTransfer) {
+ const origin = event.dataTransfer.getData('application/drag-key');
+ const originElement = document.querySelector(`[data-hierarchy-item-id='${origin}']`) as HTMLElement;
+
+ if (targetElement.dataset.hierarchyItemId === originElement.dataset.hierarchyItemId) {
+ return;
+ }
+
+ let targetSortOrder = Number(targetElement.dataset.sortOrder);
+ if (targetElement.compareDocumentPosition(originElement) === Node.DOCUMENT_POSITION_PRECEDING) {
+ targetSortOrder += 1;
+ }
+
+ updateSortOrder(originElement, targetSortOrder);
+ }
+ }
+}
+
+function updateSortOrder(originElement:HTMLElement, sortOrder:number | string) {
+ const { frameId = '', indexUrl = '', moveUrl = '' } = originElement.dataset;
+
+ fetch(new URL(moveUrl), {
+ body: new URLSearchParams([['new_sort_order', sortOrder.toString()]]),
+ method: 'POST',
+ credentials: 'include',
+ headers: [
+ ['Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'],
+ ['X-CSRF-Token', getMetaValue('csrf-token')],
+ ],
+ redirect: 'manual',
+ }).then(() => {
+ Turbo.visit(indexUrl, { frame: frameId });
+ }).catch(() => {});
+}
+
+function getMetaValue(name:string) {
+ const element = document.head.querySelector(`meta[name='${name}']`);
+ return element?.getAttribute('content') || '';
+}
+
+function dataNode(node:EventTarget | null):HTMLElement {
+ if (!(node instanceof Element)) throw new Error('Cannot handle drag and drop of non HTML element');
+
+ return node.closest('[data-hierarchy-item-id]')!;
+}