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

Add three default queries for the Gantt module

Henriette Darge58ae8b7ebb75
diff --git a/app/components/open_project/common/submenu_component.html.erb b/app/components/open_project/common/submenu_component.html.erb
index 97fa34d2fa2..8cc4ee5b056 100644
--- a/app/components/open_project/common/submenu_component.html.erb
+++ b/app/components/open_project/common/submenu_component.html.erb
@@ -20,32 +20,34 @@
<% nested_sidebar_menu_items = @sidebar_menu_items.filter { |menu_item| menu_item.header.present? } %>
<% if nested_sidebar_menu_items.any? %>
<% nested_sidebar_menu_items.each do |menu_item| %>
- <div class="op-sidemenu"
- data-controller="menus--expandable-sidemenu"
- data-application-target="dynamic">
+ <% if menu_item.children.any? %>
+ <div class="op-sidemenu"
+ data-controller="menus--expandable-sidemenu"
+ data-application-target="dynamic">
- <button class="op-sidemenu--title"
- type="button"
- data-action="click->menus--expandable-sidemenu#toggleContainer">
- <%= menu_item.header %>
- <span class="icon-small icon-arrow-up1"
- aria-hidden="true"
- data-menus--expandable-sidemenu-target="indicator">
- </span>
- </button>
+ <button class="op-sidemenu--title"
+ type="button"
+ data-action="click->menus--expandable-sidemenu#toggleContainer">
+ <%= menu_item.header %>
+ <span class="icon-small icon-arrow-up1"
+ aria-hidden="true"
+ data-menus--expandable-sidemenu-target="indicator">
+ </span>
+ </button>
- <ul class="op-sidemenu--items"
- data-menus--expandable-sidemenu-target="container">
- <% menu_item.children.each do |child_item| %>
- <li class="op-sidemenu--item">
- <% selected = child_item.selected ? 'selected' : '' %>
- <a class="op-sidemenu--item-action <%= selected %>" href="<%= child_item.href %>">
- <span class="op-sidemenu--item-title"><%= child_item.title %></span>
- </a>
- </li>
- <% end %>
- </ul>
- </div>
+ <ul class="op-sidemenu--items"
+ data-menus--expandable-sidemenu-target="container">
+ <% menu_item.children.each do |child_item| %>
+ <li class="op-sidemenu--item">
+ <% selected = child_item.selected ? 'selected' : '' %>
+ <a class="op-sidemenu--item-action <%= selected %>" href="<%= child_item.href %>">
+ <span class="op-sidemenu--item-title"><%= child_item.title %></span>
+ </a>
+ </li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
<% end %>
<% end %>
</div>
diff --git a/modules/gantt/app/controllers/gantt/gantt_controller.rb b/modules/gantt/app/controllers/gantt/gantt_controller.rb
index a779c17e5d5..2950e0d8c3f 100644
--- a/modules/gantt/app/controllers/gantt/gantt_controller.rb
+++ b/modules/gantt/app/controllers/gantt/gantt_controller.rb
@@ -11,6 +11,24 @@ module ::Gantt
menu_item :gantt
def index
+ # If there are no query_props given, redirect to the default query
+ if params[:query_props].nil? && params[:query_id].nil?
+ if @project.present?
+ return redirect_to(
+ project_gantt_index_path(
+ @project,
+ query_props: ::Gantt::DefaultQueryGeneratorService.new(with_project_context: true).call
+ )
+ )
+ else
+ return redirect_to(
+ gantt_index_path(
+ query_props: ::Gantt::DefaultQueryGeneratorService.new(with_project_context: false).call
+ )
+ )
+ end
+ end
+
respond_to do |format|
format.html do
render :index,
diff --git a/modules/gantt/app/controllers/gantt/menus_controller.rb b/modules/gantt/app/controllers/gantt/menus_controller.rb
index 3ea9ea412cb..9e86583fffa 100644
--- a/modules/gantt/app/controllers/gantt/menus_controller.rb
+++ b/modules/gantt/app/controllers/gantt/menus_controller.rb
@@ -25,25 +25,21 @@
#
# See COPYRIGHT and LICENSE files for more details.
# ++
-module ::Gantt
+module Gantt
class MenusController < ApplicationController
before_action :find_optional_project
def show
- @sidebar_menu_items = first_level_menu_items + nested_menu_items
+ @sidebar_menu_items = menu_items
render layout: nil
end
private
- def first_level_menu_items
- []
- end
-
- def nested_menu_items
+ def menu_items
[
OpenProject::Menu::MenuGroup.new(header: I18n.t('js.label_starred_queries'), children: starred_queries),
- OpenProject::Menu::MenuGroup.new(header: I18n.t('js.label_default_queries'), children: []),
+ OpenProject::Menu::MenuGroup.new(header: I18n.t('js.label_default_queries'), children: default_queries),
OpenProject::Menu::MenuGroup.new(header: I18n.t('js.label_global_queries'), children: global_queries),
OpenProject::Menu::MenuGroup.new(header: I18n.t('js.label_custom_queries'), children: custom_queries)
]
@@ -56,6 +52,20 @@ module ::Gantt
.map { |id, name| menu_item(:query_id, id, name) }
end
+ def default_queries
+ query_generator = Gantt::DefaultQueryGeneratorService.new(with_project_context: @project.present?)
+ queries = []
+ Gantt::DefaultQueryGeneratorService::QUERY_OPTIONS.each do |query_key|
+ queries << menu_item(
+ :query_props,
+ query_generator.call(query_key:),
+ Gantt::DefaultQueryGeneratorService::QUERY_MAPPINGS[query_key]
+ )
+ end
+
+ queries
+ end
+
def global_queries
base_query
.where('starred' => 'f')
@@ -85,24 +95,22 @@ module ::Gantt
base_query
end
- def menu_item(filter_key, id, name)
+ def menu_item(filter_key, filter_val, name)
OpenProject::Menu::MenuItem.new(title: name,
- href: gantt_path(filter_key, id),
- selected: selected?(filter_key, id))
+ href: gantt_path(filter_key, filter_val),
+ selected: selected?(filter_key, filter_val))
end
def selected?(filter_key, value)
- return false if active_filter_count > 1
-
params[filter_key] == value.to_s
end
- def active_filter_count
- @active_filter_count ||= params[:filters].present? ? params[:filters].count : 0
- end
-
def gantt_path(filter_key, id)
- @project.present? ? project_gantt_index_path(@project, filter_key => id) : gantt_index_path(filter_key => id)
+ if @project.present?
+ project_gantt_index_path(@project, params.permit(filter_key).merge!(filter_key => id))
+ else
+ gantt_index_path(params.permit(filter_key).merge!(filter_key => id))
+ end
end
end
end
diff --git a/modules/gantt/app/services/gantt/default_query_generator_service.rb b/modules/gantt/app/services/gantt/default_query_generator_service.rb
new file mode 100644
index 00000000000..4056ebdeb5e
--- /dev/null
+++ b/modules/gantt/app/services/gantt/default_query_generator_service.rb
@@ -0,0 +1,124 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-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.
+#++
+
+module ::Gantt
+ class DefaultQueryGeneratorService
+ DEFAULT_QUERY = :all_open
+ QUERY_MAPPINGS = {
+ DEFAULT_QUERY => I18n.t('queries.all_open'),
+ :timeline => I18n.t('queries.timeline'),
+ :milestones => I18n.t('queries.milestones')
+ }.freeze
+ QUERY_OPTIONS = QUERY_MAPPINGS.keys
+
+ PROJECT_DEFAULT_COLUMNS = %w[id type subject status startDate dueDate duration].freeze
+ GLOBAL_DEFAULT_COLUMNS = %w[id type subject status startDate dueDate duration project].freeze
+
+ DEFAULT_PARAMS =
+ {
+ tll: '{"left":"startDate","right":"dueDate","farRight":"subject"}',
+ tzl: "auto",
+ tv: true,
+ hi: true,
+ t: 'start_date:asc'
+ }.to_json.freeze
+
+ attr_reader :with_project_context
+
+ def initialize(with_project_context:)
+ @with_project_context = with_project_context
+ end
+
+ def call(query_key: DEFAULT_QUERY)
+ case query_key
+ when DEFAULT_QUERY
+ params = self.class.all_open_query(@with_project_context)
+ when :timeline
+ params = self.class.timeline_query(@with_project_context)
+ when :milestones
+ params = self.class.milestones_query(@with_project_context)
+ else
+ return
+ end
+
+ params.to_json
+ end
+
+ class << self
+ def all_open_query(with_project_context)
+ default_with_filter = add_columns(with_project_context)
+
+ default_with_filter['f'] = [{ 'n' => 'status', 'o' => 'o', 'v' => [] }]
+
+ default_with_filter
+ end
+
+ def timeline_query(with_project_context)
+ default_with_filter = add_columns(with_project_context)
+
+ milestones = milestone_ids
+ phase = ::Type.where(name: 'Phase').pluck(:id).map(&:to_s)
+
+ type_filter_values = milestones.concat(phase)
+ if type_filter_values.any?
+ default_with_filter['f'] = [{ 'n' => 'type', 'o' => '=', 'v' => type_filter_values }]
+ end
+
+ default_with_filter
+ end
+
+ def milestones_query(with_project_context)
+ default_with_filter = add_columns(with_project_context)
+
+ milestones = milestone_ids
+ if milestone_ids.any?
+ default_with_filter['f'] = [{ 'n' => 'type', 'o' => '=', 'v' => milestones }]
+ end
+
+ default_with_filter
+ end
+
+ def add_columns(with_project_context)
+ default_with_filter = JSON
+ .parse(Gantt::DefaultQueryGeneratorService::DEFAULT_PARAMS)
+
+ default_with_filter['c'] = if with_project_context
+ Gantt::DefaultQueryGeneratorService::PROJECT_DEFAULT_COLUMNS
+ else
+ Gantt::DefaultQueryGeneratorService::GLOBAL_DEFAULT_COLUMNS
+ end
+
+ default_with_filter
+ end
+
+ def milestone_ids
+ ::Type.milestone.pluck(:id).map(&:to_s)
+ end
+ end
+ end
+end
diff --git a/modules/gantt/app/views/gantt/menus/_menu.html.erb b/modules/gantt/app/views/gantt/menus/_menu.html.erb
index 2a2c60d350b..929fd074c6a 100644
--- a/modules/gantt/app/views/gantt/menus/_menu.html.erb
+++ b/modules/gantt/app/views/gantt/menus/_menu.html.erb
@@ -1,5 +1,5 @@
<%= turbo_frame_tag "gantt_menu",
- src: @project ? menu_project_gantt_index_path(@project, params.permit(:filters, :query_id)) : gantt_menu_path(params.permit(:filters, :query_id)),
+ src: @project ? menu_project_gantt_index_path(@project, params.permit(:query_props, :query_id)) : gantt_menu_path(params.permit(:query_props, :query_id)),
target: '_top',
data: { turbo: false },
loading: :lazy %>
diff --git a/modules/gantt/config/locales/en.yml b/modules/gantt/config/locales/en.yml
index 13d8dabe726..ea1a8eb5fb2 100644
--- a/modules/gantt/config/locales/en.yml
+++ b/modules/gantt/config/locales/en.yml
@@ -1,3 +1,7 @@
# English strings go here
en:
project_module_gantt: "Gantt"
+ queries:
+ all_open: 'All open'
+ timeline: 'Timeline'
+ milestones: 'Milestones'
diff --git a/modules/gantt/spec/features/timeline/timeline_navigation_spec.rb b/modules/gantt/spec/features/timeline/timeline_navigation_spec.rb
index e0639178d4b..2e9f39f2f17 100644
--- a/modules/gantt/spec/features/timeline/timeline_navigation_spec.rb
+++ b/modules/gantt/spec/features/timeline/timeline_navigation_spec.rb
@@ -155,6 +155,35 @@ RSpec.describe 'Work package timeline navigation',
end
end
+ describe 'with the default query' do
+ let(:closed_status) { create(:closed_status, name: 'Closed') }
+ let(:project) { create(:project, types: [milestone_type], enabled_module_names:) }
+
+ let!(:wp_milestone) do
+ create(:work_package,
+ project:,
+ type: milestone_type)
+ end
+ let!(:wp_closed) do
+ create(:work_package,
+ project:,
+ status: closed_status)
+ end
+
+ it 'shows the timeline open initially' do
+ wp_timeline.visit!
+ loading_indicator_saveguard
+
+ # The timeline is open per default
+ wp_timeline.expect_timeline!(open: true)
+
+ # Further, there is an active filter on open work packages
+ wp_timeline.expect_work_package_listed(wp_milestone)
+ wp_timeline.expect_work_package_listed(work_package)
+ wp_timeline.expect_work_package_not_listed(wp_closed)
+ end
+ end
+
it 'can save the open state and zoom of timeline' do
wp_timeline.visit_query query
wp_timeline.expect_work_package_listed(work_package)
diff --git a/spec/support/pages/work_packages/work_packages_timeline.rb b/spec/support/pages/work_packages/work_packages_timeline.rb
index 2b75104bff1..0bbdc7acc3e 100644
--- a/spec/support/pages/work_packages/work_packages_timeline.rb
+++ b/spec/support/pages/work_packages/work_packages_timeline.rb
@@ -60,8 +60,6 @@ module Pages
end
def expect_work_package_not_listed(*work_packages)
- super(*work_packages)
-
within(timeline_container) do
work_packages.each do |wp|
expect(page).to have_no_css(".wp-row-#{wp.id}-timeline", visible: true)