2023-05-19 14:15:26 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
#-- copyright
|
|
|
|
|
# OpenProject is an open source project management software.
|
2024-07-30 13:42:36 +02:00
|
|
|
# Copyright (C) the OpenProject GmbH
|
2023-05-19 14:15:26 +03:00
|
|
|
#
|
|
|
|
|
# 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.
|
|
|
|
|
#++
|
|
|
|
|
|
2020-08-28 08:56:56 +02:00
|
|
|
module Projects
|
2023-05-19 14:15:26 +03:00
|
|
|
class TableComponent < ::TableComponent
|
2024-07-25 10:17:26 +02:00
|
|
|
include OpTurbo::Streamable
|
|
|
|
|
|
2020-08-28 08:56:56 +02:00
|
|
|
options :params # We read collapsed state from params
|
|
|
|
|
options :current_user # adds this option to those of the base class
|
2024-01-19 11:49:18 +01:00
|
|
|
options :query
|
|
|
|
|
|
2024-06-13 16:01:23 +03:00
|
|
|
def initialize(**)
|
|
|
|
|
super(rows: [], **)
|
2024-01-19 11:49:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def before_render
|
|
|
|
|
@model = projects(query)
|
|
|
|
|
super
|
|
|
|
|
end
|
2020-08-28 08:56:56 +02:00
|
|
|
|
|
|
|
|
def initial_sort
|
2021-02-11 16:02:18 +01:00
|
|
|
%i[lft asc]
|
2020-08-28 08:56:56 +02:00
|
|
|
end
|
|
|
|
|
|
2024-07-25 10:17:26 +02:00
|
|
|
def self.wrapper_key
|
|
|
|
|
"projects-table"
|
|
|
|
|
end
|
|
|
|
|
|
2020-08-28 08:56:56 +02:00
|
|
|
def table_id
|
|
|
|
|
"project-table"
|
|
|
|
|
end
|
|
|
|
|
|
2024-04-12 13:00:08 +02:00
|
|
|
def container_class
|
2024-06-13 14:31:48 +02:00
|
|
|
"generic-table--container_visible-overflow generic-table--container_height-100"
|
2024-04-12 13:00:08 +02:00
|
|
|
end
|
|
|
|
|
|
2020-08-28 08:56:56 +02:00
|
|
|
##
|
|
|
|
|
# The project sort by is handled differently
|
2024-10-17 15:05:49 +02:00
|
|
|
def quick_action_table_header(column, options)
|
2024-10-14 08:22:18 +02:00
|
|
|
helpers.projects_sort_header_tag(column, query.selects.map(&:attribute), **options, param: :json)
|
2020-08-28 08:56:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# We don't return the project row
|
|
|
|
|
# but the [project, level] array from the helper
|
|
|
|
|
def rows
|
2024-05-19 16:07:25 +02:00
|
|
|
@rows ||= begin
|
|
|
|
|
projects_enumerator = ->(model) { to_enum(:projects_with_levels_order_sensitive, model).to_a }
|
|
|
|
|
instance_exec(model, &projects_enumerator)
|
|
|
|
|
end
|
2020-08-28 08:56:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def initialize_sorted_model
|
2023-05-30 07:43:37 +03:00
|
|
|
helpers.sort_clear
|
2020-08-28 08:56:56 +02:00
|
|
|
|
2024-01-19 11:49:18 +01:00
|
|
|
orders = query.orders.select(&:valid?).map { |o| [o.attribute.to_s, o.direction.to_s] }
|
2023-05-30 07:43:37 +03:00
|
|
|
helpers.sort_init orders
|
|
|
|
|
helpers.sort_update orders.map(&:first)
|
2020-08-28 08:56:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def paginated?
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
2024-06-13 16:01:23 +03:00
|
|
|
def pagination_options
|
|
|
|
|
default_pagination_options.merge(optional_pagination_options)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def default_pagination_options
|
2024-07-25 13:15:47 +02:00
|
|
|
{
|
|
|
|
|
allowed_params: %i[query_id filters columns sortBy],
|
|
|
|
|
turbo: true
|
|
|
|
|
}
|
2024-06-13 16:01:23 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def optional_pagination_options
|
|
|
|
|
{}
|
|
|
|
|
end
|
|
|
|
|
|
2020-08-28 08:56:56 +02:00
|
|
|
def deactivate_class_on_lft_sort
|
2024-02-01 13:17:52 +01:00
|
|
|
if sorted_by_lft?
|
2022-04-27 08:02:44 +02:00
|
|
|
"spot-link_inactive"
|
2020-08-28 08:56:56 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def href_only_when_not_sort_lft
|
2024-02-01 13:17:52 +01:00
|
|
|
unless sorted_by_lft?
|
2024-05-31 16:51:54 +02:00
|
|
|
projects_path(
|
|
|
|
|
sortBy: JSON.dump([%w[lft asc]]),
|
|
|
|
|
**helpers.projects_query_params.slice(*helpers.projects_query_param_names_for_sort)
|
|
|
|
|
)
|
2020-08-28 08:56:56 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-07-25 10:53:17 +02:00
|
|
|
def order_options(select, turbo: false)
|
2024-10-17 15:05:49 +02:00
|
|
|
options = {
|
|
|
|
|
caption: select.caption,
|
|
|
|
|
sortable: sortable_column?(select)
|
|
|
|
|
}
|
2024-07-25 10:53:17 +02:00
|
|
|
|
|
|
|
|
if turbo
|
|
|
|
|
options[:data] = { "turbo-stream": true }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
options
|
2020-08-28 08:56:56 +02:00
|
|
|
end
|
|
|
|
|
|
2024-02-16 09:25:24 +01:00
|
|
|
def sortable_column?(select)
|
2024-06-13 11:22:08 +02:00
|
|
|
sortable? && query.known_order?(select.attribute)
|
2020-08-28 08:56:56 +02:00
|
|
|
end
|
|
|
|
|
|
2024-10-18 14:10:49 +02:00
|
|
|
def use_quick_action_table_headers?
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
2020-08-28 08:56:56 +02:00
|
|
|
def columns
|
2024-05-19 16:07:25 +02:00
|
|
|
@columns ||= begin
|
|
|
|
|
columns = query.selects.reject { |select| select.is_a?(::Queries::Selects::NotExistingSelect) }
|
2024-02-02 09:35:15 +01:00
|
|
|
|
2024-05-19 16:07:25 +02:00
|
|
|
index = columns.index { |column| column.attribute == :name }
|
|
|
|
|
columns.insert(index, ::Queries::Projects::Selects::Default.new(:hierarchy)) if index
|
2020-08-28 08:56:56 +02:00
|
|
|
|
2024-05-21 17:06:32 +02:00
|
|
|
columns
|
|
|
|
|
end
|
2020-08-28 08:56:56 +02:00
|
|
|
end
|
2024-01-19 11:49:18 +01:00
|
|
|
|
|
|
|
|
def projects(query)
|
2024-11-11 12:35:05 +01:00
|
|
|
scope = query.results
|
|
|
|
|
|
|
|
|
|
# The two columns associated with the
|
|
|
|
|
# * disk storage
|
|
|
|
|
# * latest activity
|
|
|
|
|
# information are only available to admins.
|
|
|
|
|
# For non admins, the performance penalty of fetching the information therefore needs never be paid.
|
|
|
|
|
if User.current.admin?
|
|
|
|
|
scope = scope
|
|
|
|
|
.with_required_storage
|
|
|
|
|
.with_latest_activity
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
scope
|
2024-01-19 11:49:18 +01:00
|
|
|
.includes(:custom_values, :enabled_modules)
|
|
|
|
|
.paginate(page: helpers.page_param(params), per_page: helpers.per_page_param(params))
|
|
|
|
|
end
|
2024-02-01 13:17:52 +01:00
|
|
|
|
|
|
|
|
def projects_with_levels_order_sensitive(projects, &)
|
|
|
|
|
if sorted_by_lft?
|
|
|
|
|
Project.project_tree(projects, &)
|
|
|
|
|
else
|
|
|
|
|
projects_with_level(projects, &)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def projects_with_level(projects, &)
|
|
|
|
|
ancestors = []
|
|
|
|
|
|
|
|
|
|
projects.each do |project|
|
|
|
|
|
while !ancestors.empty? && !project.is_descendant_of?(ancestors.last)
|
|
|
|
|
ancestors.pop
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
yield project, ancestors.count
|
|
|
|
|
|
|
|
|
|
ancestors << project
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2025-09-08 16:23:19 +01:00
|
|
|
def favorited_project_ids
|
|
|
|
|
@favorited_project_ids ||= Favorite.where(user: current_user, favorited_type: "Project").pluck(:favorited_id)
|
2024-04-04 11:30:44 +02:00
|
|
|
end
|
|
|
|
|
|
2025-03-27 15:34:34 +01:00
|
|
|
def project_phase_by_definition(definition, project)
|
|
|
|
|
@project_phases_by_definition ||= Project::Phase
|
2024-12-18 21:17:39 +02:00
|
|
|
.visible
|
2024-12-16 09:16:59 +01:00
|
|
|
.index_by { |s| [s.definition_id, s.project_id] }
|
2024-12-13 10:01:29 +01:00
|
|
|
|
2025-03-27 15:34:34 +01:00
|
|
|
@project_phases_by_definition[[definition.id, project.id]]
|
2024-12-13 10:01:29 +01:00
|
|
|
end
|
|
|
|
|
|
2024-02-01 13:17:52 +01:00
|
|
|
def sorted_by_lft?
|
2024-02-23 13:21:05 +01:00
|
|
|
query.orders.first&.attribute == :lft
|
2024-02-01 13:17:52 +01:00
|
|
|
end
|
2020-08-28 08:56:56 +02:00
|
|
|
end
|
|
|
|
|
end
|