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

Show tree view next to hierachy CF

Henriette Darge36d390c8aeae
diff --git a/app/components/admin/custom_fields/hierarchy/tree_view_component.html.erb b/app/components/admin/custom_fields/hierarchy/tree_view_component.html.erb
new file mode 100644
index 00000000000..13751bf8a3e
--- /dev/null
+++ b/app/components/admin/custom_fields/hierarchy/tree_view_component.html.erb
@@ -0,0 +1,37 @@
+<%#-- 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.
+
+++#%>
+<%= render(Primer::Beta::Heading.new(tag: :h3, font_size: 4, mb: 2)) { @custom_field.name } %>
+
+<%=
+ render(Primer::OpenProject::TreeView.new(node_variant: :anchor)) do |tree_view|
+ hierarchy_items.each do |item|
+ add_sub_tree(tree_view, item)
+ end
+ end
+%>
diff --git a/app/components/admin/custom_fields/hierarchy/tree_view_component.rb b/app/components/admin/custom_fields/hierarchy/tree_view_component.rb
new file mode 100644
index 00000000000..88d8c4b0c74
--- /dev/null
+++ b/app/components/admin/custom_fields/hierarchy/tree_view_component.rb
@@ -0,0 +1,77 @@
+# frozen_string_literal: true
+
+#-- 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.
+#++
+
+module Admin
+ module CustomFields
+ module Hierarchy
+ class TreeViewComponent < ApplicationComponent
+ def initialize(custom_field:, active_item:)
+ super
+
+ @custom_field = custom_field
+ @active_item = active_item
+ @hierarchy_service = ::CustomFields::Hierarchy::HierarchicalItemService.new
+ end
+
+ def hierarchy_items
+ @custom_field.hierarchy_root.children
+ end
+
+ def add_sub_tree(tree, item)
+ if item.children.empty?
+ tree.with_leaf(**item_options(item))
+ else
+ tree.with_sub_tree(expanded: expanded?(item), **item_options(item)) do |sub_tree|
+ item.children.each do |sub_item|
+ add_sub_tree(sub_tree, sub_item)
+ end
+ end
+ end
+ end
+
+ def item_options(item)
+ {
+ label: item.label,
+ current: current?(item),
+ href: custom_field_item_path(@custom_field, item)
+ }
+ end
+
+ def current?(item)
+ item.id == @active_item.id
+ end
+
+ def expanded?(item)
+ @hierarchy_service.descendant_of?(item: @active_item, parent: item).success?
+ end
+ end
+ end
+ end
+end
diff --git a/app/views/admin/custom_fields/hierarchy/items/index.html.erb b/app/views/admin/custom_fields/hierarchy/items/index.html.erb
index b9b6d5af06f..9d2f8e5f76e 100644
--- a/app/views/admin/custom_fields/hierarchy/items/index.html.erb
+++ b/app/views/admin/custom_fields/hierarchy/items/index.html.erb
@@ -31,4 +31,14 @@ See COPYRIGHT and LICENSE files for more details.
<%= render(Admin::CustomFields::EditFormHeaderComponent.new(custom_field: @custom_field, selected: :items)) %>
-<%= render(Admin::CustomFields::Hierarchy::ItemsComponent.new(item: @active_item)) %>
+<%=
+ render(Primer::Alpha::Layout.new(stacking_breakpoint: :md)) do |content|
+ content.with_main do
+ render(Admin::CustomFields::Hierarchy::ItemsComponent.new(item: @active_item))
+ end
+
+ content.with_sidebar(row_placement: :start, col_placement: :start, border: true, border_radius: 2, p: 3) do
+ render(Admin::CustomFields::Hierarchy::TreeViewComponent.new(custom_field: @custom_field, active_item: @active_item))
+ end
+ end
+%>
diff --git a/spec/features/custom_fields/hierarchy_custom_field_spec.rb b/spec/features/custom_fields/hierarchy_custom_field_spec.rb
index 3a9997d6332..319a1685c3d 100644
--- a/spec/features/custom_fields/hierarchy_custom_field_spec.rb
+++ b/spec/features/custom_fields/hierarchy_custom_field_spec.rb
@@ -160,19 +160,21 @@ RSpec.describe "custom fields of type hierarchy", :js do
context "when navigating the hierarchy" do
let(:service) { CustomFields::Hierarchy::HierarchicalItemService.new }
- let(:custom_field) { create(:wp_custom_field, field_format: "hierarchy", hierarchy_root: nil) }
+ let(:custom_field) { create(:wp_custom_field, name: "Hogwarts", field_format: "hierarchy", hierarchy_root: nil) }
let!(:root) { service.generate_root(custom_field).value! }
- let!(:luke) { service.insert_item(parent: root, label: "Luke", short: "LS").value! }
- let!(:r2d2) { service.insert_item(parent: luke, label: "R2-D2", short: "R2").value! }
- let!(:mouse) { service.insert_item(parent: r2d2, label: "Mouse Droid", short: "MD").value! }
- let!(:c3po) { service.insert_item(parent: luke, label: "C-3PO", short: "3PO").value! }
- let!(:mara) { service.insert_item(parent: root, label: "Mara", short: "MJ").value! }
+ let!(:ravenclaw) { service.insert_item(parent: root, label: "Ravenclaw").value! }
+ let!(:slytherin) { service.insert_item(parent: root, label: "Slytherin").value! }
+ let!(:hufflepuff) { service.insert_item(parent: root, label: "Hufflepuff").value! }
+ let!(:gryffindor) { service.insert_item(parent: root, label: "Gryffindor").value! }
+ let!(:luna) { service.insert_item(parent: ravenclaw, label: "Luna Lovegood").value! }
+ let!(:harry) { service.insert_item(parent: gryffindor, label: "Harry Potter").value! }
+ let!(:hermione) { service.insert_item(parent: gryffindor, label: "Hermione Granger").value! }
+ let(:tree_view) { Components::TreeView.new }
before do
custom_field.reload
hierarchy_page.add_custom_field_state(custom_field)
-
- visit custom_field_item_path(root.custom_field_id, luke)
+ visit custom_field_item_path(root.custom_field_id, gryffindor)
end
it "can navigate and keep the tab selection (regression #63921)" do
@@ -181,7 +183,29 @@ RSpec.describe "custom fields of type hierarchy", :js do
hierarchy_page.expect_tab "Items"
# Navigating to an item will keep the tab nav selection
- page.find_test_selector("op-custom-fields--hierarchy-item", text: "C-3PO").click
+ page.find_test_selector("op-custom-fields--hierarchy-item", text: "Hermione Granger").click
+ hierarchy_page.expect_tab "Items"
+ end
+
+ it "can use the TreeView for navigation" do
+ expect(page).to have_test_selector("op-custom-fields--hierarchy-item", count: 2)
+
+ # Expect the current item to be selected
+ tree_view.should_have_active_item("Gryffindor")
+
+ # All other nodes are collapsed initially
+ tree_view.should_have_collapsed_node("Ravenclaw")
+
+ # Navigate to another item
+ tree_view.open_node "Ravenclaw"
+ tree_view.click_node "Luna Lovegood"
+
+ # Expect tree and page to update
+ tree_view.should_have_active_item("Luna Lovegood")
+ tree_view.should_have_open_node("Ravenclaw")
+ tree_view.should_have_collapsed_node("Gryffindor")
+
+ expect(page).to have_test_selector("op-custom-fields--hierarchy-item", count: 0)
hierarchy_page.expect_tab "Items"
end
end
diff --git a/spec/support/components/tree_view.rb b/spec/support/components/tree_view.rb
new file mode 100644
index 00000000000..341c3eced25
--- /dev/null
+++ b/spec/support/components/tree_view.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+#-- 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.
+#++
+
+module Components
+ class TreeView
+ include Capybara::DSL
+ include Capybara::RSpecMatchers
+ include RSpec::Matchers
+
+ def should_have_active_item(name)
+ expect(page).to have_css(".TreeViewItemContent", text: name, aria: { current: true })
+ end
+
+ def should_have_collapsed_node(name)
+ expect(page).to have_css(".TreeViewItemContent", text: name, aria: { expanded: false })
+ end
+
+ def should_have_open_node(name)
+ expect(page).to have_css(".TreeViewItemContent", text: name, aria: { expanded: true })
+ end
+
+ def open_node(name)
+ page.find(".TreeViewItemContent", text: name).sibling(".TreeViewItemToggle").click
+ end
+
+ def click_node(name)
+ page.find(".TreeViewItemContent", text: name).click
+ end
+ end
+end