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

create urls and forms with all query parameters instead of relying on stimulus controller

Ivan Kuchina3e0b9a60b63
diff --git a/app/components/projects/configure_view_modal_component.html.erb b/app/components/projects/configure_view_modal_component.html.erb
index e07e57abb82..6c8ea75bfbe 100644
--- a/app/components/projects/configure_view_modal_component.html.erb
+++ b/app/components/projects/configure_view_modal_component.html.erb
@@ -10,12 +10,11 @@
<%= primer_form_with(
url: projects_path,
id: QUERY_FORM_ID,
- method: :get,
- data: {
- controller: "params-from-query",
- 'application-target': "dynamic",
- 'params-from-query-allowed-value': '["filters", "query_id", "page", "per_page"]'
- }) do |form| %>
+ method: :get
+ ) do |form| %>
+ <% helpers.projects_query_params.except(:columns, :sortBy).each do |name, value| %>
+ <%= hidden_field_tag name, value %>
+ <% end %>
<%= render(Primer::Alpha::TabPanels.new(label: "label")) do |tab_panel| %>
<% tab_panel.with_tab(selected: true, id: "tab-selects") do |tab| %>
<% tab.with_text { I18n.t("label_columns") } %>
diff --git a/app/components/projects/export_list_modal_component.html.erb b/app/components/projects/export_list_modal_component.html.erb
index 4d338159009..cb39f9c51b3 100644
--- a/app/components/projects/export_list_modal_component.html.erb
+++ b/app/components/projects/export_list_modal_component.html.erb
@@ -2,14 +2,10 @@
id: MODAL_ID)) do |d| %>
<% d.with_header(variant: :large) %>
<% d.with_body do %>
- <ul class="op-export-options"
- data-controller="params-from-query"
- data-application-target="dynamic"
- data-params-from-query-all-anchors-value="true"
- data-params-from-query-allowed-value='["query_id", "filters", "sortBy", "columns"]'>
+ <ul class="op-export-options">
<% helpers.supported_export_formats.each do |key| %>
<li class="op-export-options--option">
- <%= link_to url_for(action: 'index', format: key),
+ <%= link_to projects_path(format: key, **helpers.projects_query_params.except(:page, :per_page)),
class: 'op-export-options--option-link' do %>
<%= helpers.op_icon("icon-big icon-export-#{key}") %>
<span class="op-export-options--option-label"><%= t("export.format.#{key}") %></span>
diff --git a/app/components/projects/index_page_header_component.html.erb b/app/components/projects/index_page_header_component.html.erb
index 6f99b4163ac..39d48fa1676 100644
--- a/app/components/projects/index_page_header_component.html.erb
+++ b/app/components/projects/index_page_header_component.html.erb
@@ -9,7 +9,7 @@
header:,
message: t("lists.can_be_saved"),
label: t("button_save"),
- href: projects_query_path(query),
+ href: projects_query_path(query, projects_query_params),
method: :patch
)
elsif can_save_as?
@@ -17,7 +17,7 @@
header:,
message: t("lists.can_be_saved_as"),
label: t("button_save_as"),
- href: new_projects_query_path
+ href: new_projects_query_path(projects_query_params)
)
end
@@ -59,11 +59,20 @@
end
if can_save?
- menu_save_item(menu:, label: t('button_save'), href: projects_query_path(query), method: :patch)
+ menu_save_item(
+ menu:,
+ label: t('button_save'),
+ href: projects_query_path(query, projects_query_params),
+ method: :patch
+ )
end
if may_save_as?
- menu_save_item(menu:, label: t('button_save_as'), href: new_projects_query_path)
+ menu_save_item(
+ menu:,
+ label: t('button_save_as'),
+ href: new_projects_query_path(projects_query_params)
+ )
end
menu.with_item(
@@ -123,15 +132,15 @@
render(Primer::OpenProject::PageHeader.new) do |header|
header.with_title(data: { 'test-selector': 'project-query-name'}) do
primer_form_with(model: query,
- url: @query.new_record? ? projects_queries_path : projects_query_path(@query),
+ url: @query.new_record? ? projects_queries_path(projects_query_params) : projects_query_path(@query, projects_query_params),
scope: 'query',
- data: {
- controller: "params-from-query",
- 'application-target': "dynamic",
- 'params-from-query-allowed-value': '["filters", "columns", "query_id", "sortBy"]'
- },
id: 'project-save-form') do |f|
- render(Queries::Projects::Form.new(f, query:))
+ render(
+ Queries::Projects::Form.new(
+ f,
+ cancel_url: projects_path(**projects_query_params, **{ query_id: query.id }.compact)
+ )
+ )
end
end
header.with_breadcrumbs(breadcrumb_items)
diff --git a/app/components/projects/index_page_header_component.rb b/app/components/projects/index_page_header_component.rb
index 5cf1928edba..f75f4e4b7b5 100644
--- a/app/components/projects/index_page_header_component.rb
+++ b/app/components/projects/index_page_header_component.rb
@@ -39,6 +39,8 @@ class Projects::IndexPageHeaderComponent < ApplicationComponent
STATE_OPTIONS = %i[show edit rename].freeze
+ delegate :projects_query_params, to: :helpers
+
def initialize(current_user:, query:, params:, state: :show)
super
@@ -136,12 +138,7 @@ class Projects::IndexPageHeaderComponent < ApplicationComponent
mobile_icon: nil, # Do not show on mobile as it is already part of the menu
mobile_label: nil,
href:,
- data: {
- method:,
- controller: "params-from-query",
- "application-target": "dynamic",
- "params-from-query-allowed-value": '["filters", "columns", "sortBy", "query_id"]'
- }.compact
+ data: { method: }
) do
render(
Primer::Beta::Octicon.new(
@@ -159,12 +156,7 @@ class Projects::IndexPageHeaderComponent < ApplicationComponent
label:,
href:,
content_arguments: {
- data: {
- method:,
- controller: "params-from-query",
- "application-target": "dynamic",
- "params-from-query-allowed-value": '["filters", "columns", "sortBy", "query_id"]'
- }.compact
+ data: { method: }
}
) do |item|
item.with_leading_visual_icon(icon: :"op-save")
diff --git a/app/components/projects/table_component.html.erb b/app/components/projects/table_component.html.erb
index aaa7044986f..a9628a3a672 100644
--- a/app/components/projects/table_component.html.erb
+++ b/app/components/projects/table_component.html.erb
@@ -44,11 +44,7 @@ See COPYRIGHT and LICENSE files for more details.
<% if column.attribute == :hierarchy %>
<th id="project-table--hierarchy-header">
<div class="generic-table--sort-header-outer generic-table--sort-header-outer_no-highlighting">
- <div class="generic-table--sort-header"
- data-controller="params-from-query"
- data-application-target="dynamic"
- data-params-from-query-all-anchors-value="true"
- data-params-from-query-allowed-value='["query_id", "per_page", "filters", "columns"]'>
+ <div class="generic-table--sort-header">
<%= content_tag :a,
helpers.op_icon("icon-hierarchy"),
href: href_only_when_not_sort_lft,
@@ -102,10 +98,5 @@ See COPYRIGHT and LICENSE files for more details.
</div>
<% if paginated? %>
- <div data-controller="params-from-query"
- data-application-target="dynamic"
- data-params-from-query-all-anchors-value="true"
- data-params-from-query-allowed-value='["query_id", "columns"]'>
- <%= helpers.pagination_links_full model, { blocked_url_params: [:query_id, :columns] } %>
- </div>
+ <%= helpers.pagination_links_full model, allowed_params: %i[query_id filters columns sortBy] %>
<% end %>
diff --git a/app/components/projects/table_component.rb b/app/components/projects/table_component.rb
index 8d68f035dbc..ebb8a49c229 100644
--- a/app/components/projects/table_component.rb
+++ b/app/components/projects/table_component.rb
@@ -58,7 +58,7 @@ module Projects
##
# The project sort by is handled differently
def build_sort_header(column, options)
- helpers.projects_sort_header_tag(column, options.merge(param: :json))
+ helpers.projects_sort_header_tag(column, **options, param: :json)
end
# We don't return the project row
@@ -90,20 +90,13 @@ module Projects
def href_only_when_not_sort_lft
unless sorted_by_lft?
- projects_path(sortBy: JSON::dump([["lft", "asc"]]))
+ projects_path(sortBy: JSON.dump([%w[lft asc]]), **helpers.projects_query_params.except(:sortBy))
end
end
def order_options(select)
{
- caption: select.caption,
- data:
- {
- controller: "params-from-query",
- "application-target": "dynamic",
- "params-from-query-allowed-value": '["query_id"]',
- "params-from-query-all-anchors-value": "true"
- }
+ caption: select.caption
}
end
diff --git a/app/forms/queries/projects/form.rb b/app/forms/queries/projects/form.rb
index 4de5994d4a4..006e2d96fac 100644
--- a/app/forms/queries/projects/form.rb
+++ b/app/forms/queries/projects/form.rb
@@ -51,14 +51,13 @@ class Queries::Projects::Form < ApplicationForm
scheme: :secondary,
label: I18n.t(:button_cancel),
tag: :a,
- data: { "params-from-query-target": "anchor" },
- href: projects_path(query_id: @query)
+ href: @cancel_url
)
end
end
- def initialize(query:)
+ def initialize(cancel_url:)
super()
- @query = query
+ @cancel_url = cancel_url
end
end
diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb
index c35a073b51d..eccd5d26d59 100644
--- a/app/helpers/projects_helper.rb
+++ b/app/helpers/projects_helper.rb
@@ -32,12 +32,12 @@ module ProjectsHelper
# Just like sort_header tag but removes sorting by
# lft from the sort criteria as lft is mutually exclusive with
# the other criteria.
- def projects_sort_header_tag(*)
+ def projects_sort_header_tag(column, **)
former_criteria = @sort_criteria.criteria.dup
@sort_criteria.criteria.reject! { |a, _| a == "lft" }
- sort_header_tag(*)
+ sort_header_tag(column, **, allowed_params: %i[query_id filters columns per_page])
ensure
@sort_criteria.criteria = former_criteria
end
@@ -70,4 +70,8 @@ module ProjectsHelper
projects_columns_options
.select { |c| c[:id] == :name }
end
+
+ def projects_query_params
+ safe_query_params(%i[query_id filters columns sortBy per_page page])
+ end
end
diff --git a/frontend/src/stimulus/controllers/dynamic/params-from-query.controller.ts b/frontend/src/stimulus/controllers/dynamic/params-from-query.controller.ts
deleted file mode 100644
index 0c5598de69f..00000000000
--- a/frontend/src/stimulus/controllers/dynamic/params-from-query.controller.ts
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * -- 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.
- * ++
- */
-
-import { Controller } from '@hotwired/stimulus';
-
-export default class ParamsFromQueryController extends Controller {
- static targets = [
- 'anchor',
- 'form',
- ];
-
- declare readonly hasFormTarget:boolean;
- declare readonly hasAnchorTarget:boolean;
-
- static values = {
- allAnchors: Boolean,
- allowed: Array,
- };
-
- declare allAnchorsValue:boolean;
- declare allowedValue:string[];
-
- initialize() {
- if (this.allAnchorsValue) {
- this.element.querySelectorAll('a').forEach((target) => {
- this.appendParamsToHref(target);
- });
- }
- }
-
- connect() {
- if (this.element.tagName === 'FORM' && !this.hasFormTarget) {
- this.addFieldsToForm(this.element as HTMLFormElement);
- } else if (this.element.tagName === 'A' && !this.hasAnchorTarget) {
- this.appendParamsToHref(this.element as HTMLAnchorElement);
- }
- }
-
- anchorTargetConnected(target:HTMLAnchorElement) {
- this.appendParamsToHref(target);
- }
-
- formTargetConnected(target:HTMLFormElement) {
- this.addFieldsToForm(target);
- }
-
- appendParamsToHref(target:HTMLAnchorElement) {
- const currentHref = target.getAttribute('href') || '';
- const currentHrefParams = new URLSearchParams(currentHref.split('?')[1] || '');
-
- this.forEachMatchingUrlParam((key, value) => {
- currentHrefParams.append(key, value);
- });
-
- target.setAttribute('href', `${currentHref.split('?')[0]}?${currentHrefParams.toString()}`);
- }
-
- addFieldsToForm(form:HTMLFormElement) {
- this.forEachMatchingUrlParam((key, value) => {
- const filterElement = document.createElement('input');
- filterElement.type = 'hidden';
- filterElement.name = key;
- filterElement.value = value;
-
- form.appendChild(filterElement);
- });
-
- return true;
- }
-
- private forEachMatchingUrlParam(callback:(key:string, value:string) => void) {
- const paramsString = window.location.search.substring(1);
- const searchParams = new URLSearchParams(paramsString);
-
- searchParams.forEach((value, key) => {
- this.allowedValue.forEach((allowedKey) => {
- if (key.startsWith(allowedKey)) {
- callback(key, value);
- }
- });
- });
- }
-}
diff --git a/spec/support/pages/projects/index.rb b/spec/support/pages/projects/index.rb
index 2d5fab6566f..a3304fcaa89 100644
--- a/spec/support/pages/projects/index.rb
+++ b/spec/support/pages/projects/index.rb
@@ -90,12 +90,16 @@ module Pages
end
def expect_current_page_number(number)
- expect(page).to have_css(".op-pagination--item_current", text: number)
+ within ".op-pagination--pages" do
+ expect(page).to have_css(".op-pagination--item_current", text: number)
+ end
end
def expect_total_pages(number)
- expect(page).to have_css(".op-pagination--item", text: number)
- expect(page).to have_no_css(".op-pagination--item", text: number + 1)
+ within ".op-pagination--pages" do
+ expect(page).to have_css(".op-pagination--item", text: number)
+ expect(page).to have_no_css(".op-pagination--item", text: number + 1)
+ end
end
def set_sidebar_filter(filter_name)