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

[#64620] introduce work package type projects tab controller

Eric Schubert59f56edc4d0f
diff --git a/app/components/work_package_types/projects_component.html.erb b/app/components/work_package_types/projects_component.html.erb
new file mode 100644
index 00000000000..75fb3eee67e
--- /dev/null
+++ b/app/components/work_package_types/projects_component.html.erb
@@ -0,0 +1,61 @@
+<%#-- 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.
+
+++#%>
+
+<%=
+ settings_primer_form_with(**form_options) do |form|
+ component_wrapper do
+ flex_layout do |container|
+ container.with_row(mb: 3) do
+ render(
+ Primer::OpenProject::TreeView.new(
+ expanded: true,
+ data: {
+ "admin--work-package-type-projects-target": "treeView",
+ action: "click->admin--work-package-type-projects#updateSelectedProjects"
+ },
+ select_variant: :multiple
+ )
+ ) do |tree_view|
+ build_project_tree(tree_view)
+ end
+ end
+
+ container.with_row do
+ form.hidden_field :project_ids,
+ value: "[]",
+ data: { "admin--work-package-type-projects-target": "selectedProjects" }
+ end
+
+ container.with_row do
+ render(Primer::Beta::Button.new(scheme: :primary, type: :submit)) { t(:button_save) }
+ end
+ end
+ end
+ end
+%>
diff --git a/app/components/work_package_types/projects_component.rb b/app/components/work_package_types/projects_component.rb
new file mode 100644
index 00000000000..8c34a544fe9
--- /dev/null
+++ b/app/components/work_package_types/projects_component.rb
@@ -0,0 +1,87 @@
+# 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 WorkPackageTypes
+ class ProjectsComponent < ApplicationComponent
+ include ApplicationHelper
+ include OpPrimer::ComponentHelpers
+ include OpTurbo::Streamable
+
+ def form_options
+ {
+ url: type_projects_path(type_id: model.id),
+ method: :put,
+ model:,
+ data: {
+ controller: "admin--work-package-type-projects",
+ "admin--work-package-type-projects-initially-selected-projects-value": model.projects.pluck(:id).join(",")
+ }
+ }
+ end
+
+ def projects = options[:projects]
+
+ def build_project_tree(tree)
+ nested_project_list = Project.build_projects_hierarchy(projects)
+
+ tree.with_sub_tree(expanded: true,
+ select_variant: :multiple,
+ select_strategy: :self,
+ label: "All Projects") do |sub_tree|
+ add_sub_tree(sub_tree, nested_project_list)
+ end
+ end
+
+ private
+
+ def add_sub_tree(tree, project_list)
+ project_list.each do |project_hash|
+ if project_hash[:children].empty?
+ tree.with_leaf(**item_options(project_hash[:project]))
+ else
+ tree.with_sub_tree(expanded: true,
+ select_strategy: :self,
+ **item_options(project_hash[:project])) do |sub_tree|
+ add_sub_tree(sub_tree, project_hash[:children])
+ end
+ end
+ end
+ end
+
+ def item_options(item)
+ {
+ select_variant: :multiple,
+ label: item.name,
+ data: { project_id: item.id },
+ checked: model.projects.include?(item)
+ }
+ end
+ end
+end
diff --git a/app/controllers/work_package_types/projects_tab_controller.rb b/app/controllers/work_package_types/projects_tab_controller.rb
new file mode 100644
index 00000000000..a5646b519f9
--- /dev/null
+++ b/app/controllers/work_package_types/projects_tab_controller.rb
@@ -0,0 +1,65 @@
+# 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 WorkPackageTypes
+ class ProjectsTabController < ApplicationController
+ layout "admin"
+
+ before_action :require_admin
+ before_action :find_type
+
+ def edit
+ @projects = Project.all
+ end
+
+ def update
+ result = UpdateService.new(user: current_user, model: @type, contract_class: UpdateProjectsContract)
+ .call(permitted_project_params)
+
+ if result.success?
+ redirect_to edit_type_projects_path(type_id: @type.id), notice: I18n.t(:notice_successful_update)
+ else
+ render :edit, status: :unprocessable_entity
+ end
+ end
+
+ private
+
+ def find_type
+ @type = ::Type.find(params[:type_id])
+ end
+
+ def permitted_project_params
+ # TODO: once the input is correctly delivered just return: params.expect(type: [:project_ids])
+
+ { project_ids: JSON.parse(params.expect(type: [:project_ids])[:project_ids]) }
+ end
+ end
+end
diff --git a/app/helpers/types_helper.rb b/app/helpers/types_helper.rb
index 16513a1a6bb..84855815ddc 100644
--- a/app/helpers/types_helper.rb
+++ b/app/helpers/types_helper.rb
@@ -53,8 +53,8 @@ module ::TypesHelper
},
{
name: "projects",
- partial: "types/form/projects",
- path: edit_tab_type_path(id: @type.id, tab: :projects),
+ path: edit_type_projects_path(type_id: @type.id),
+ view_component: WorkPackageTypes::ProjectsComponent,
label: I18n.t("types.edit.projects.tab")
},
{
diff --git a/app/views/types/form/_projects.html.erb b/app/views/types/form/_projects.html.erb
deleted file mode 100644
index cc291e9c497..00000000000
--- a/app/views/types/form/_projects.html.erb
+++ /dev/null
@@ -1,62 +0,0 @@
-<%#-- 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.
-
-++#%>
-
-<section class="form--section">
- <div class="grid-block wrap">
- <div class="grid-content small-12 large-6">
- <% if @projects.any? %>
- <fieldset class="form--fieldset" id="type_project_ids">
- <legend class="form--fieldset-legend">
- <%= t("types.edit.projects.enabled_projects") %>
- </legend>
- <div class="form--toolbar">
- <span class="form--toolbar-item">
- (<%= check_all_links "type_project_ids" %>)
- </span>
- </div>
- <%= project_nested_ul(@projects) do |p|
- content_tag(
- "label",
- check_box_tag("type[project_ids][]", p.id, @type.projects.include?(p), id: nil) +
- " " + h(p), class: "form--label-with-check-box"
- )
- end %>
- <%= hidden_field_tag("type[project_ids][]", "", id: nil) %>
- </fieldset>
- <% end %>
- </div>
- </div>
-</section>
-
-<div class="grid-block">
- <div class="generic-table--action-buttons">
- <%= styled_button_tag t(@type.new_record? ? :button_create : :button_save),
- class: "-primary -with-icon icon-checkmark" %>
- </div>
-</div>
diff --git a/app/views/work_package_types/projects_tab/edit.html.erb b/app/views/work_package_types/projects_tab/edit.html.erb
new file mode 100644
index 00000000000..bf5bf95e7f8
--- /dev/null
+++ b/app/views/work_package_types/projects_tab/edit.html.erb
@@ -0,0 +1,34 @@
+<%#-- 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.
+
+++#%>
+
+<% html_title t(:label_administration), "#{t(:label_edit)} #{t(:label_work_package_types)} #{h @type.name}" %>
+
+<%= render ::Types::EditPageHeaderComponent.new(type: @type, tabs: types_tabs) %>
+
+<%= render WorkPackageTypes::ProjectsComponent.new(@type, projects: @projects) %>
diff --git a/config/routes.rb b/config/routes.rb
index 363e9a1fe5a..edf55acfa57 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -138,6 +138,8 @@ Rails.application.routes.draw do
get "/roles/workflow/:id/:role_id/:type_id" => "roles#workflow"
resources :types do
+ resource :projects, controller: "work_package_types/projects_tab", only: %i[update edit]
+
member do
get "edit/:tab" => "types#edit", as: "edit_tab"
match "update/:tab" => "types#update", as: "update_tab", via: %i[post patch]
diff --git a/frontend/src/stimulus/controllers/dynamic/admin/work-package-type-projects.controller.ts b/frontend/src/stimulus/controllers/dynamic/admin/work-package-type-projects.controller.ts
new file mode 100644
index 00000000000..d5e67c76869
--- /dev/null
+++ b/frontend/src/stimulus/controllers/dynamic/admin/work-package-type-projects.controller.ts
@@ -0,0 +1,102 @@
+/*
+ * -- 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';
+
+export default class WorkPackageTypeProjectsController extends Controller {
+ static targets = [
+ 'selectedProjects',
+ 'treeView',
+ ];
+
+ static values = {
+ initiallySelectedProjects: String,
+ };
+
+ declare initiallySelectedProjectsValue:string;
+
+ declare readonly treeViewTarget:HTMLElement;
+
+ declare readonly selectedProjectsTarget:HTMLInputElement;
+
+ connect():void {
+ this.addProjectIds(this.initiallySelectedProjectsValue.split(','));
+ }
+
+ updateSelectedProjects(ev:PointerEvent):void {
+ const target = ev.target;
+ if (!this.isHTMLElement(target)) {
+ return;
+ }
+
+ const projectItem = target.closest('.TreeViewItemContent');
+ if (!this.isHTMLElement(projectItem)) {
+ return;
+ }
+
+ const projectId = projectItem.dataset.projectId;
+ const checked = projectItem.ariaChecked;
+
+ if (!projectId || !checked) {
+ return;
+ }
+
+ if (checked === 'false') {
+ // 'false' means it was now changed to true -> selected
+ this.addProjectIds([projectId]);
+ } else {
+ this.removeProjectIds([projectId]);
+ }
+ }
+
+ private addProjectIds(ids:string[]):void {
+ if (!this.selectedProjectsTarget) {
+ return;
+ }
+
+ const currentIds = JSON.parse(this.selectedProjectsTarget.value) as string[];
+ const distinctIds = [...new Set(currentIds), ...new Set(ids)].filter((id) => id.length > 0);
+ this.selectedProjectsTarget.value = JSON.stringify(distinctIds);
+ }
+
+ private removeProjectIds(ids:string[]):void {
+ if (!this.selectedProjectsTarget) {
+ return;
+ }
+
+ const currentIds = JSON.parse(this.selectedProjectsTarget.value) as string[];
+ const newIds = currentIds.filter((id) => !ids.includes(id));
+ this.selectedProjectsTarget.value = JSON.stringify(newIds);
+ }
+
+ private isHTMLElement(obj:unknown):obj is HTMLElement {
+ return obj instanceof HTMLElement;
+ }
+}
diff --git a/lookbook/previews/open_project/common/tree_view_preview.rb b/lookbook/previews/open_project/common/tree_view_preview.rb
index 01c68e141a4..441dfe4e05b 100644
--- a/lookbook/previews/open_project/common/tree_view_preview.rb
+++ b/lookbook/previews/open_project/common/tree_view_preview.rb
@@ -28,7 +28,7 @@ module OpenProject
sub_tree.with_sub_tree(label: "OpenProject GmbH",
expanded: expanded,
- elect_variant: select_variant,
+ select_variant: select_variant,
select_strategy: select_strategy) do |sub_tree2|
sub_tree2.with_leaf(label: "HR", select_variant: select_variant)
sub_tree2.with_leaf(label: "Development", current: true, select_variant: select_variant)