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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#67525] API Improvements to `WidgetComponent` and related components (#20350)
Alexander Brandon Coles181ba5e9b733
diff --git a/frontend/src/global_styles/content/_widget_box.sass b/frontend/src/global_styles/content/_widget_box.sassindex 68d64f5c69a..b5287dd45cb 100644--- a/frontend/src/global_styles/content/_widget_box.sass+++ b/frontend/src/global_styles/content/_widget_box.sass@@ -127,10 +127,6 @@ $widget-box--enumeration-width: 20pxpadding-bottom: 0padding-left: 0- // apply special treatment for nested boxes, e.g. Border Box- .Box- border: unset-&.-allow-inner-overflow.wikioverflow-y: auto@@ -196,6 +192,21 @@ $widget-box--enumeration-width: 20px.toolbar--editable-toolbarheight: 20px+.op-widget-box--rows+ margin: 0++.op-widget-box--row+ padding: var(--stack-padding-normal)+ list-style-type: none /* To account for applying Box component to a list */+ border-top: var(--borderWidth-thin) solid var(--borderColor-muted)++ &:first-of-type+ border-top: none++ &:last-of-type+ border-bottom-right-radius: var(--borderRadius-medium)+ border-bottom-left-radius: var(--borderRadius-medium)+.widget-box--arrow-linkslist-style: nonemargin: 0.5rem 0 1rem 0diff --git a/modules/grids/app/components/grids/widget_box_component.html.erb b/modules/grids/app/components/grids/widget_box_component.html.erbindex 8644756ed66..5ebd7aed785 100644--- a/modules/grids/app/components/grids/widget_box_component.html.erb+++ b/modules/grids/app/components/grids/widget_box_component.html.erb@@ -30,5 +30,14 @@ See COPYRIGHT and LICENSE files for more details.<%= render(Primer::BaseComponent.new(**@system_arguments)) do %><%= header %>- <%= body %>+ <%= render(Primer::ConditionalWrapper.new(condition: @turbo_enabled, **@turbo_frame_arguments)) do %>+ <%= body %>+ <% if rows.any? %>+ <%= render OpPrimer::ListComponent.new(**@list_arguments) do %>+ <% rows.each do |row| %>+ <%= row %>+ <% end %>+ <% end %>+ <% end %>+ <% end %><% end %>diff --git a/modules/grids/app/components/grids/widget_box_component.rb b/modules/grids/app/components/grids/widget_box_component.rbindex 14b804dd702..5922380b52b 100644--- a/modules/grids/app/components/grids/widget_box_component.rb+++ b/modules/grids/app/components/grids/widget_box_component.rb@@ -29,17 +29,40 @@#++module Grids+ # `WidgetBoxComponent` is a Box component with a border.class WidgetBoxComponent < ApplicationComponentattr_reader :title, :content_padding- renders_one :header, HeaderComponent- renders_one :body, BodyComponent+ renders_one :header, lambda { |title:, **system_arguments|+ system_arguments[:id] = @header_id- def initialize(title:, content_padding: BodyComponent::DEFAULT_PADDING, full_width: false, **system_arguments)+ Header.new(title:, **system_arguments)+ }++ renders_one :body, Body++ renders_many :rows, Row++ # @param key [String] The unique key of the widget.+ # @param title [String] The title that appears in the widget header.+ # @param turbo_enabled [Boolean] whether to wrap the widget content in a `turbo-frame` element.+ # @param content_padding [Symbol] <%= one_of(Grids::WidgetBox::Body::PADDING_MAPPINGS.keys) %>+ # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>+ def initialize(+ key:,+ title:,+ turbo_enabled: true,+ content_padding: Body::DEFAULT_PADDING,+ full_width: false,+ **system_arguments+ )super()+ @key = key@title = title@content_padding = content_padding+ @header_id = "#{key}-header"+@system_arguments = system_arguments@system_arguments[:tag] = :div@system_arguments[:classes] = class_names(@@ -47,14 +70,34 @@ module Grids"widget-box","widget-box_full-width" => full_width)+ @system_arguments[:id] ||= "#{key}-box"++ @turbo_enabled = turbo_enabled+ @turbo_frame_arguments = { tag: :"turbo-frame", id: key }+ @turbo_frame_arguments[:style] = "display:contents"++ @list_arguments = { tag: :ul }+ @list_arguments[:id] = "#{key}-list"+ @list_arguments[:classes] = "op-widget-box--rows"+ end++ def render?+ rows.any? || header? || body?enddef default_header- HeaderComponent.new(title:)+ Header.new(title:, id: @header_id)enddef default_body- BodyComponent.new(padding: content_padding).with_content(content) if content+ Body.new(padding: content_padding).with_content(content) if content+ end++ private++ def before_render+ return unless header+ @list_arguments[:aria] = { labelledby: @header_id }endendenddiff --git a/modules/grids/app/components/grids/widget_box_component/body_component.html.erb b/modules/grids/app/components/grids/widget_box_component/body.html.erbsimilarity index 100%rename from modules/grids/app/components/grids/widget_box_component/body_component.html.erbrename to modules/grids/app/components/grids/widget_box_component/body.html.erbdiff --git a/modules/grids/app/components/grids/widget_box_component/body_component.rb b/modules/grids/app/components/grids/widget_box_component/body.rbsimilarity index 97%rename from modules/grids/app/components/grids/widget_box_component/body_component.rbrename to modules/grids/app/components/grids/widget_box_component/body.rbindex ccb6f187000..5caa034a733 100644--- a/modules/grids/app/components/grids/widget_box_component/body_component.rb+++ b/modules/grids/app/components/grids/widget_box_component/body.rb@@ -30,7 +30,7 @@module Gridsclass WidgetBoxComponent < ApplicationComponent- class BodyComponent < ApplicationComponent+ class Body < ApplicationComponentinclude Primer::FetchOrFallbackHelperDEFAULT_PADDING = :defaultdiff --git a/modules/grids/app/components/grids/widget_box_component/header_component.html.erb b/modules/grids/app/components/grids/widget_box_component/header.html.erbsimilarity index 100%rename from modules/grids/app/components/grids/widget_box_component/header_component.html.erbrename to modules/grids/app/components/grids/widget_box_component/header.html.erbdiff --git a/modules/grids/app/components/grids/widget_box_component/header_component.rb b/modules/grids/app/components/grids/widget_box_component/header.rbsimilarity index 87%rename from modules/grids/app/components/grids/widget_box_component/header_component.rbrename to modules/grids/app/components/grids/widget_box_component/header.rbindex 0a6896245ae..5a1113e5a52 100644--- a/modules/grids/app/components/grids/widget_box_component/header_component.rb+++ b/modules/grids/app/components/grids/widget_box_component/header.rb@@ -30,19 +30,22 @@module Gridsclass WidgetBoxComponent < ApplicationComponent- class HeaderComponent < ApplicationComponent- attr_reader :title+ class Header < ApplicationComponent+ attr_reader :id, :title+ # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>def initialize(title:, **system_arguments)super()@title = title@system_arguments = system_arguments@system_arguments[:tag] = :header+ @system_arguments[:id] ||= self.class.generate_id@system_arguments[:test_selector] = "op-widget-box--header"@system_arguments[:classes] = class_names(@system_arguments[:classes],"op-widget-box--header")+ @id = @system_arguments[:id]enddef render?diff --git a/modules/grids/app/components/grids/widget_box_component/row.rb b/modules/grids/app/components/grids/widget_box_component/row.rbnew file mode 100644index 00000000000..6ce5ec4f57d--- /dev/null+++ b/modules/grids/app/components/grids/widget_box_component/row.rb@@ -0,0 +1,52 @@+# 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 Grids+ class WidgetBoxComponent < ApplicationComponent+ # Use Row to add a row with borders and maintain the same padding.+ #+ # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>+ class Row < ApplicationComponent+ def initialize(**system_arguments)+ super()++ @system_arguments = system_arguments+ @system_arguments[:classes] = class_names(+ "op-widget-box--row",+ system_arguments[:classes]+ )+ end++ def call+ render(OpPrimer::ListComponent::Item.new(**@system_arguments)) { content }+ end+ end+ end+enddiff --git a/modules/grids/app/components/grids/widget_component.rb b/modules/grids/app/components/grids/widget_component.rbindex 154685f069a..22d120a173f 100644--- a/modules/grids/app/components/grids/widget_component.rb+++ b/modules/grids/app/components/grids/widget_component.rb@@ -35,9 +35,13 @@ module Grids# @abstract Subclass and implement {#title} to create a new widget.class WidgetComponent < Primer::BaseComponentextend Dry::Initializer-include OpPrimer::ComponentHelpers- include OpTurbo::Streamable++ def self.wrapper_key+ name.underscore.tr("/", "-").tr("_", "-")+ end++ delegate :wrapper_key, to: :classoption :current_user, default: -> { User.current }@@ -48,7 +52,7 @@ module Gridsenddef widget_wrapper(**, &)- component_wrapper tag: "turbo-frame", **, &+ render(WidgetBoxComponent.new(key: wrapper_key, title:, **wrapper_arguments, **), &)enddef wrapper_argumentsdiff --git a/modules/grids/app/components/grids/widget_grid_component.html.erb b/modules/grids/app/components/grids/widget_grid_component.html.erbindex e80ba428b50..ef1b4e19117 100644--- a/modules/grids/app/components/grids/widget_grid_component.html.erb+++ b/modules/grids/app/components/grids/widget_grid_component.html.erb@@ -30,8 +30,6 @@ See COPYRIGHT and LICENSE files for more details.<%= render(Primer::BaseComponent.new(**@system_arguments)) do %><% widgets.each do |widget| %>- <%= render(Grids::WidgetBoxComponent.new(title: widget.title, **widget.wrapper_arguments)) do %>- <%= widget %>- <% end %>+ <%= widget %><% end %><% end %>diff --git a/modules/grids/spec/components/grids/widget_box_component_spec.rb b/modules/grids/spec/components/grids/widget_box_component_spec.rbindex 31b365d3feb..413bbe363d4 100644--- a/modules/grids/spec/components/grids/widget_box_component_spec.rb+++ b/modules/grids/spec/components/grids/widget_box_component_spec.rb@@ -41,4 +41,8 @@ RSpec.describe Grids::WidgetBoxComponent, type: :component doit "renders a widget box" doexpect(rendered_component).to have_css ".widget-box"end++ it "renders turbo-frame around content" do+ expect(rendered_component).to have_element :"turbo-frame", id: "cool_widget"+ endenddiff --git a/modules/overviews/app/components/overviews/widgets/project_status_component.html.erb b/modules/overviews/app/components/overviews/widgets/project_status_component.html.erbindex a060d896673..c4342f108ac 100644--- a/modules/overviews/app/components/overviews/widgets/project_status_component.html.erb+++ b/modules/overviews/app/components/overviews/widgets/project_status_component.html.erb@@ -29,28 +29,30 @@ See COPYRIGHT and LICENSE files for more details.<%=widget_wrapper do- flex_layout do |flex|- flex.with_row(mb: 3) do- render(Projects::StatusButtonComponent.new(project:, user: current_user, hide_help_text: true))- end+ component_wrapper do+ flex_layout do |flex|+ flex.with_row(mb: 3) do+ render(Projects::StatusButtonComponent.new(project:, user: current_user, hide_help_text: true))+ end- flex.with_row do- if edit_enabled?- settings_primer_form_with(- model: project,- url: project_widgets_project_status_path(project),- method: :patch,- data: { turbo: true }- ) do |f|- render(- Primer::Forms::FormList.new(- Projects::Settings::StatusForm.new(f, hide_label: true),- Projects::Settings::Submit.new(f, label: I18n.t("projects.settings.button_update_status_description"))+ flex.with_row do+ if edit_enabled?+ settings_primer_form_with(+ model: project,+ url: project_widgets_project_status_path(project),+ method: :patch,+ data: { turbo: true }+ ) do |f|+ render(+ Primer::Forms::FormList.new(+ Projects::Settings::StatusForm.new(f, hide_label: true),+ Projects::Settings::Submit.new(f, label: I18n.t("projects.settings.button_update_status_description"))+ ))- )+ end+ else+ format_text(project, :status_explanation)end- else- format_text(project, :status_explanation)endendenddiff --git a/modules/overviews/app/components/overviews/widgets/project_status_component.rb b/modules/overviews/app/components/overviews/widgets/project_status_component.rbindex d8b18bbb720..694c9bf3fd8 100644--- a/modules/overviews/app/components/overviews/widgets/project_status_component.rb+++ b/modules/overviews/app/components/overviews/widgets/project_status_component.rb@@ -32,6 +32,7 @@ module Overviewsmodule Widgetsclass ProjectStatusComponent < Grids::WidgetComponentinclude ApplicationHelper+ include OpTurbo::Streamableparam :projectdiff --git a/modules/overviews/app/components/overviews/widgets/subitems_component.html.erb b/modules/overviews/app/components/overviews/widgets/subitems_component.html.erbindex 97ed819edb9..2d24e04b52b 100644--- a/modules/overviews/app/components/overviews/widgets/subitems_component.html.erb+++ b/modules/overviews/app/components/overviews/widgets/subitems_component.html.erb@@ -29,39 +29,33 @@ See COPYRIGHT and LICENSE files for more details.++#%><%=- widget_wrapper do- render(Primer::Beta::BorderBox.new) do |container|- container.with_header(classes: "sr-only") do- title+ widget_wrapper do |container|+ if children.empty?+ container.with_row do+ render(Primer::Beta::Text.new(color: :subtle)) { I18n.t("overviews.widgets.subitems.no_visible_children") }end-- if children.empty?+ else+ children.find_each do |child|container.with_row do- render(Primer::Beta::Text.new(color: :subtle)) { I18n.t("overviews.widgets.subitems.no_visible_children") }- end- else- children.find_each do |child|- container.with_row do- flex_layout do |row|- row.with_column do- render(Primer::Beta::Text.new(color: :subtle)) do- concat render Primer::Beta::Octicon.new(- icon: helpers.workspace_icon(child),- mr: 1,- color: :subtle,- "aria-label": child.workspace_label- )- concat child.workspace_label- end+ flex_layout do |row|+ row.with_column do+ render(Primer::Beta::Text.new(color: :subtle)) do+ concat render Primer::Beta::Octicon.new(+ icon: helpers.workspace_icon(child),+ mr: 1,+ color: :subtle,+ "aria-label": child.workspace_label+ )+ concat child.workspace_labelend+ end- row.with_column(ml: 2) do- render(Primer::Beta::Link.new(href: project_path(child))) { child.name }- end+ row.with_column(ml: 2) do+ render(Primer::Beta::Link.new(href: project_path(child))) { child.name }+ end- row.with_column(ml: 2) do- render(Projects::StatusBadgeComponent.new(project: child))- end+ row.with_column(ml: 2) do+ render(Projects::StatusBadgeComponent.new(project: child))endendenddiff --git a/modules/overviews/app/components/overviews/widgets/subitems_component.rb b/modules/overviews/app/components/overviews/widgets/subitems_component.rbindex 77e30b5b87d..d1f6d046e36 100644--- a/modules/overviews/app/components/overviews/widgets/subitems_component.rb+++ b/modules/overviews/app/components/overviews/widgets/subitems_component.rb@@ -44,10 +44,6 @@ module Overviewsdef children@children ||= project.children.visibleend-- def wrapper_arguments- { content_padding: :none }- endendendend