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

Extract all sorts into a main compoonent so we can bind the controller

Klaus Zanders009bd7c794af
diff --git a/app/components/projects/configure_view_modal_component.html.erb b/app/components/projects/configure_view_modal_component.html.erb
index 64aa23ac467..922c6f870db 100644
--- a/app/components/projects/configure_view_modal_component.html.erb
+++ b/app/components/projects/configure_view_modal_component.html.erb
@@ -14,7 +14,7 @@
data: {
controller: "params-from-query",
'application-target': "dynamic",
- 'params-from-query-allowed-value': '["filters", "query_id", "page", "per_page", "sortBy"]'
+ 'params-from-query-allowed-value': '["filters", "query_id", "page", "per_page"]'
}) do |form| %>
<%= render(Primer::Alpha::TabPanels.new(label: "label")) do |tab_panel| %>
<% tab_panel.with_tab(selected: true, id: "tab-selects") do |tab| %>
@@ -39,13 +39,7 @@
<% tab_panel.with_tab(id: "tab-selects") do |tab| %>
<% tab.with_text { "Sort By" }%>
<% tab.with_panel do %>
- <%= render(Primer::OpenProject::FlexLayout.new) do |layout| %>
- <% 3.times do |i| %>
- <% layout.with_row do %>
- <%= render(Queries::SortByComponent.new(order: query.orders[i], available_orders:)) %>
- <% end %>
- <% end %>
- <% end %>
+ <%= render(Queries::SortByComponent.new(query: query)) %>
<% end%>
<% end %>
<% end %>
diff --git a/app/components/projects/configure_view_modal_component.rb b/app/components/projects/configure_view_modal_component.rb
index 71c2ca14107..2b0d92552cb 100644
--- a/app/components/projects/configure_view_modal_component.rb
+++ b/app/components/projects/configure_view_modal_component.rb
@@ -40,16 +40,4 @@ class Projects::ConfigureViewModalComponent < ApplicationComponent
.selects
.map { |c| { id: c.attribute, name: c.caption } }
end
-
- def available_orders
- @available_orders ||= begin
- all_selectable_columns = helpers.projects_columns_options
- all_order_keys = ::Queries::Register.orders[query.class]&.map(&:key)
-
- # Keys from the order can be symbols, strings or regexes
- all_selectable_columns.select do |column_option|
- all_order_keys.any? { |order_key| order_key === column_option[:id] }
- end
- end
- end
end
diff --git a/app/components/queries/sort_by_component.html.erb b/app/components/queries/sort_by_component.html.erb
index e2371c097d8..99aedaadca5 100644
--- a/app/components/queries/sort_by_component.html.erb
+++ b/app/components/queries/sort_by_component.html.erb
@@ -1,11 +1,10 @@
-<%= render(Primer::OpenProject::GridLayout.new(css_class: "op-configure-query-sort-form", tag: :div)) do |grid| %>
- <% grid.with_area("field") do %>
- <%= select_tag 'sort_field', select_options, include_blank: true %>
- <% end %>
- <% grid.with_area("direction") do %>
- <%= render(Primer::Alpha::SegmentedControl.new("aria-label": "Sort order", hide_labels: true)) do |sort_buttons| %>
- <% sort_buttons.with_item(icon: "sort-asc", label: "sort ascending", selected: order_asc?) %>
- <% sort_buttons.with_item(icon: "sort-desc", label: "sort descending", selected: order_desc?) %>
+<div data-controller="sort-by-config" data-application-target="dynamic">
+ <%= hidden_field_tag :sortBy, current_orders, data: { "sort-by-config-target" => "sortByField"} %>
+ <%= render(Primer::OpenProject::FlexLayout.new) do |layout| %>
+ <% order_limit.times do |i| %>
+ <% layout.with_row do %>
+ <%= render(Queries::SortByFieldComponent.new(order: query.orders[i], available_orders:)) %>
+ <% end %>
<% end %>
<% end %>
-<% end %>
+</div>
diff --git a/app/components/queries/sort_by_component.rb b/app/components/queries/sort_by_component.rb
index 2c16f469ac4..51531cdf18e 100644
--- a/app/components/queries/sort_by_component.rb
+++ b/app/components/queries/sort_by_component.rb
@@ -29,21 +29,25 @@
# ++
class Queries::SortByComponent < ApplicationComponent
- options :order,
- :available_orders
+ options :query
- def select_options
- options_for_select(
- available_orders.map { |order| [order[:name], order[:id]] },
- order&.attribute
- )
+ def current_orders
+ JSON.dump(query.orders.map { |order| [order.attribute, order.direction] })
end
- def order_asc?
- order&.direction == :asc
+ def order_limit
+ 3
end
- def order_desc?
- order&.direction == :desc
+ def available_orders
+ @available_orders ||= begin
+ all_selectable_columns = helpers.projects_columns_options
+ all_order_keys = ::Queries::Register.orders[query.class]&.map(&:key)
+
+ # Keys from the order can be symbols, strings or regexes
+ all_selectable_columns.select do |column_option|
+ all_order_keys.any? { |order_key| order_key === column_option[:id] }
+ end
+ end
end
end
diff --git a/app/components/queries/sort_by_field_component.html.erb b/app/components/queries/sort_by_field_component.html.erb
new file mode 100644
index 00000000000..324ac75d3a7
--- /dev/null
+++ b/app/components/queries/sort_by_field_component.html.erb
@@ -0,0 +1,11 @@
+<%= render(Primer::OpenProject::GridLayout.new(css_class: "op-configure-query-sort-form", tag: :div)) do |grid| %>
+ <% grid.with_area("field") do %>
+ <%= select_tag 'sort_field', select_options, include_blank: true, form: "none" %>
+ <% end %>
+ <% grid.with_area("direction") do %>
+ <%= render(Primer::Alpha::SegmentedControl.new("aria-label": "Sort order", hide_labels: true)) do |sort_buttons| %>
+ <% sort_buttons.with_item(icon: "sort-asc", label: "sort ascending", selected: order_asc?) %>
+ <% sort_buttons.with_item(icon: "sort-desc", label: "sort descending", selected: order_desc?) %>
+ <% end %>
+ <% end %>
+<% end %>
diff --git a/app/components/queries/sort_by_field_component.rb b/app/components/queries/sort_by_field_component.rb
new file mode 100644
index 00000000000..d0b67432169
--- /dev/null
+++ b/app/components/queries/sort_by_field_component.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2024 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.
+# ++
+
+class Queries::SortByFieldComponent < ApplicationComponent
+ options :order,
+ :available_orders
+
+ def select_options
+ options_for_select(
+ available_orders.map { |order| [order[:name], order[:id]] },
+ order&.attribute
+ )
+ end
+
+ def order_asc?
+ order&.direction == :asc
+ end
+
+ def order_desc?
+ order&.direction == :desc
+ end
+end
diff --git a/frontend/src/stimulus/controllers/dynamic/sort-by-config.controller.ts b/frontend/src/stimulus/controllers/dynamic/sort-by-config.controller.ts
new file mode 100644
index 00000000000..bde4f086f96
--- /dev/null
+++ b/frontend/src/stimulus/controllers/dynamic/sort-by-config.controller.ts
@@ -0,0 +1,44 @@
+/*
+ * -- copyright
+ * OpenProject is an open source project management software.
+ * Copyright (C) 2023 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 SortByConfigController extends Controller {
+ static targets = [
+ 'sortByField',
+ ];
+
+ declare readonly sortByFieldTarget:HTMLInputElement;
+
+ connect(): void {
+ console.log("Hello, we are connected!")
+ console.log(`Current value of the field: ${this.sortByFieldTarget.value}`)
+ }
+}