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
Generalize status button component for use in meetings and work packages
Oliver Günther2dd107d3c9c7
diff --git a/app/components/op_primer/status_button_component.html.erb b/app/components/op_primer/status_button_component.html.erbnew file mode 100644index 00000000000..1bb30932659--- /dev/null+++ b/app/components/op_primer/status_button_component.html.erb@@ -0,0 +1,31 @@+<%=+ if @readonly || @items.empty?+ render(Primer::Beta::Button.new(**button_arguments)) do |button|+ button_content(button)+ end+ else+ render(Primer::Alpha::ActionMenu.new(**@menu_arguments)) do |menu|+ menu.with_show_button(**button_arguments) do |button|+ button_content(button)+ end++ @items.each do |option|+ menu.with_item(**option.item_arguments) do |item|+ item.with_leading_visual_icon(icon: option.icon) if option.icon++ flex_layout(align_items: :center) do |flex|+ if option.icon.nil? && option.color+ flex.with_column(mr: 1) do+ helpers.icon_for_color(option.color, style: "display: block")+ end+ end++ flex.with_column do+ render(Primer::Beta::Text.new) { option.name }+ end+ end+ end+ end+ end+ end+%>diff --git a/app/components/op_primer/status_button_component.rb b/app/components/op_primer/status_button_component.rbnew file mode 100644index 00000000000..a514605bf8f--- /dev/null+++ b/app/components/op_primer/status_button_component.rb@@ -0,0 +1,78 @@+# 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 OpPrimer+ class StatusButtonComponent < ApplicationComponent+ include OpPrimer::ComponentHelpers++ def initialize(current_status:, items:, readonly: false, disabled: false, button_arguments: {}, menu_arguments: {})+ super++ @current_status = current_status+ @items = items+ @readonly = readonly+ @disabled = disabled+ @menu_arguments = menu_arguments+ @button_arguments = button_arguments+ end++ def default_button_title+ raise NotImplementedError+ end++ def disabled?+ @disabled+ end++ def readonly?+ @readonly+ end++ def button_content(button)+ button.with_leading_visual_icon(icon: @current_status.icon) if @current_status.icon+ button.with_trailing_action_icon(icon: "triangle-down") if !readonly? && @items.any?++ @current_status.name+ end++ def button_arguments+ title = @button_arguments.fetch(:title) { default_button_title }++ {+ title:,+ disabled: disabled?,+ aria: {+ label: title+ },+ style: @current_status.color&.color_styles_css+ }.compact.deep_merge(@button_arguments)+ end+ end+enddiff --git a/app/components/op_primer/status_button_option.rb b/app/components/op_primer/status_button_option.rbnew file mode 100644index 00000000000..5e15c105d58--- /dev/null+++ b/app/components/op_primer/status_button_option.rb@@ -0,0 +1,46 @@+# 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 OpPrimer+ class StatusButtonOption # rubocop:disable OpenProject/AddPreviewForViewComponent+ attr_reader :name, :color, :icon, :item_arguments++ def initialize(name:, color: nil, icon: nil, **item_arguments)+ @name = name+ @color = color+ @icon = icon+ @item_arguments = item_arguments+ end++ def to_s+ name+ end+ end+enddiff --git a/app/components/work_packages/status_button_component.html.erb b/app/components/work_packages/status_button_component.html.erbdeleted file mode 100644index 3550328d880..00000000000--- a/app/components/work_packages/status_button_component.html.erb+++ /dev/null@@ -1,26 +0,0 @@-<%=- if @readonly- render(Primer::Beta::Button.new(**button_arguments)) do |button|- button.with_leading_visual_icon(icon: "lock") if readonly?- @status.name- end- else- render(Primer::Alpha::ActionMenu.new(**@menu_arguments)) do |menu|- menu.with_show_button(**button_arguments) do |button|- button.with_trailing_action_icon(icon: "triangle-down")- button.with_leading_visual_icon(icon: "lock") if readonly?- @status.name- end-- @items.each do |item|- menu.with_item(- label: item.name,- content_arguments: { classes: "__hl_inline_status_#{item.id}",- align_items: :center }- ) do |menu_item|- menu_item.with_trailing_visual_icon(icon: :lock) if item.is_readonly?- end- end- end- end-%>diff --git a/app/components/work_packages/status_button_component.rb b/app/components/work_packages/status_button_component.rbindex 61b19cb887b..9cff3a784b8 100644--- a/app/components/work_packages/status_button_component.rb+++ b/app/components/work_packages/status_button_component.rb@@ -1,45 +1,45 @@# frozen_string_literal: true-class WorkPackages::StatusButtonComponent < ApplicationComponent- include OpPrimer::ComponentHelpers+class WorkPackages::StatusButtonComponent < OpPrimer::StatusButtonComponent+ include Primer::ClassNameHelper- def initialize(work_package:, user:, readonly: false, button_arguments: {}, menu_arguments: {})- super+ attr_reader :work_package, :user+ def initialize(work_package:, user:, readonly: false, button_arguments: {}, menu_arguments: {})@work_package = work_package@user = user- @status = work_package.status- @project = work_package.project-- @readonly = readonly- @menu_arguments = menu_arguments- @button_arguments = button_arguments.merge({ classes: "__hl_background_status_#{@status.id}" })- @items = available_statusses+ button_arguments[:classes] = class_names(+ button_arguments[:classes],+ "__hl_background_status_#{work_package.status.id}"+ )++ super(+ current_status: map_status(work_package.status),+ items: available_statuses,+ readonly:,+ button_arguments:,+ menu_arguments:+ )end- def button_title+ def default_button_titleI18n.t("js.label_edit_status")enddef disabled?- [email protected]_in_project?(:edit_work_packages, @project)- end-- def readonly?- @status.is_readonly?+ !user.allowed_in_project?(:edit_work_packages, work_package.project)end- def button_arguments- { title: button_title,- disabled: disabled?,- aria: {- label: button_title- } }.deep_merge(@button_arguments)+ def available_statuses+ WorkPackages::UpdateContract+ .new(work_package, user)+ .assignable_statuses+ .map { |status| map_status(status) }end- def available_statusses- WorkPackages::UpdateContract.new(@work_package, @user)- .assignable_statuses+ def map_status(status)+ icon = status.is_readonly? ? :lock : nil+ OpPrimer::StatusButtonOption.new(name: status.name, color: status.color, icon:)endenddiff --git a/app/helpers/colors_helper.rb b/app/helpers/colors_helper.rbindex f3989c4e506..8fb9da6e0be 100644--- a/app/helpers/colors_helper.rb+++ b/app/helpers/colors_helper.rb@@ -170,11 +170,11 @@ module ColorsHelperbackground: rgb(var(--color-r), var(--color-g), var(--color-b)) !important;"mode = User.current.pref.theme- if mode == "light_high_contrast"- style += "border: 1px solid hsla(var(--color-h), calc(var(--color-s) * 1%), calc((var(--color-l) - 75) * 1%), 1) !important;"- else- style += "border: 1px solid hsl(var(--color-h), calc(var(--color-s) * 1%), calc((var(--color-l) - 15) * 1%)) !important;"- end+ style += if mode == "light_high_contrast"+ "border: 1px solid hsla(var(--color-h), calc(var(--color-s) * 1%), calc((var(--color-l) - 75) * 1%), 1) !important;"+ else+ "border: 1px solid hsl(var(--color-h), calc(var(--color-s) * 1%), calc((var(--color-l) - 15) * 1%)) !important;"+ endstyleenddiff --git a/app/helpers/mail_notification_helper.rb b/app/helpers/mail_notification_helper.rbindex 7219b8b3507..4ad335ede1c 100644--- a/app/helpers/mail_notification_helper.rb+++ b/app/helpers/mail_notification_helper.rb@@ -47,6 +47,6 @@ module MailNotificationHelperdef status_colors(status)color_id = selected_color(status)- Color.find(color_id).color_styles.map { |k, v| "#{k}:#{v};" }.join(" ") if color_id+ Color.find(color_id).color_styles_css if color_idendenddiff --git a/app/models/colors/hex_color.rb b/app/models/colors/hex_color.rbindex 7825f306f1b..30baf4b3d34 100644--- a/app/models/colors/hex_color.rb+++ b/app/models/colors/hex_color.rb@@ -8,6 +8,10 @@ module Colors{ color: contrasting_font_color(light: light_color, dark: dark_color), "background-color": hexcode }end+ def color_styles_css+ color_styles.map { |k, v| "#{k}: #{v};" }.join(" ")+ end+def contrasting_font_color(light: "#FFFFFF", dark: "#333333")bright? ? dark : lightenddiff --git a/lookbook/previews/op_primer/status_button_component_preview.rb b/lookbook/previews/op_primer/status_button_component_preview.rbnew file mode 100644index 00000000000..d2f4b1ce6e8--- /dev/null+++ b/lookbook/previews/op_primer/status_button_component_preview.rb@@ -0,0 +1,48 @@+# frozen_string_literal: true++module OpPrimer+ # @logical_path OpenProject/Primer+ class StatusButtonComponentPreview < ViewComponent::Preview+ # See the [component documentation](/lookbook/pages/components/status_button) for more details.+ # @display min_height 400px+ # @param readonly [Boolean]+ # @param disabled [Boolean]+ # @param size [Symbol] select [small, medium, large]+ def playground(readonly: false, disabled: false, size: :medium)+ status = OpPrimer::StatusButtonOption.new(name: "Open",+ color: FactoryBot.build(:color_maroon),+ tag: :a,+ href: "/some/test")+ items = [+ status,+ OpPrimer::StatusButtonOption.new(name: "Closed",+ color: FactoryBot.build(:color_green),+ tag: :a,+ href: "/some/other/action")+ ]+ component = OpPrimer::StatusButtonComponent.new(current_status: status,+ items:,+ readonly:,+ disabled:,+ button_arguments: { title: "Edit", size: })++ render(component)+ end++ def with_icon(size: :medium)+ status = OpPrimer::StatusButtonOption.new(name: "Open", icon: :unlock, color: Color.new(hexcode: "#FF0000"))++ items = [+ status,+ OpPrimer::StatusButtonOption.new(name: "Closed", icon: :lock, color: Color.new(hexcode: "#00FF00"))+ ]++ component = OpPrimer::StatusButtonComponent.new(current_status: status,+ items: items,+ readonly: false,+ button_arguments: { size:, title: "foo" })++ render(component)+ end+ end+enddiff --git a/spec/components/op_primer/status_button_component_spec.rb b/spec/components/op_primer/status_button_component_spec.rbnew file mode 100644index 00000000000..41a3d975ac4--- /dev/null+++ b/spec/components/op_primer/status_button_component_spec.rb@@ -0,0 +1,136 @@+# 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.+#++++require "rails_helper"++RSpec.describe OpPrimer::StatusButtonComponent, type: :component do+ let(:current_status) do+ OpPrimer::StatusButtonOption.new(+ name: "In Progress",+ icon: "clock",+ color: nil+ )+ end++ let(:items) { [] }+ let(:readonly) { false }+ let(:title) { "foo" }++ subject(:component) do+ described_class.new(+ current_status:,+ items:,+ readonly:,+ button_arguments: { title: }.compact+ )+ end++ context "when rendering with an icon status" do+ it "renders the status with an icon" do+ render_inline(component)++ expect(page).to have_css(".octicon-clock")+ expect(page).to have_text("In Progress")+ end+ end++ context "when rendering with items" do+ let(:items) do+ [+ OpPrimer::StatusButtonOption.new(name: "Todo", icon: "circle", color: nil),+ OpPrimer::StatusButtonOption.new(name: "Done", icon: "check", color: nil)+ ]+ end++ it "renders a dropdown button" do+ render_inline(component)++ expect(page).to have_css(".octicon-triangle-down")+ end++ it "renders menu items with icons" do+ render_inline(component)++ expect(page).to have_css(".octicon-circle")+ expect(page).to have_css(".octicon-check")+ expect(page).to have_text("Todo")+ expect(page).to have_text("Done")+ end+ end++ context "when rendering with color status" do+ let(:current_status) do+ OpPrimer::StatusButtonOption.new(+ name: "High Priority",+ icon: nil,+ color: Color.new(hexcode: "#FF0000")+ )+ end++ let(:items) do+ [+ OpPrimer::StatusButtonOption.new(+ name: "Low Priority",+ icon: nil,+ color: Color.new(hexcode: "#00FF00")+ )+ ]+ end++ it "renders the status with color styles" do+ render_inline(component)++ expect(page).to have_css("[style*='background-color: #FF0000']")+ expect(page).to have_text("High Priority")++ expect(page).to have_css("[style*='background-color: #00FF00']", visible: :all)+ expect(page).to have_text("Low Priority")+ end+ end++ context "when readonly" do+ let(:readonly) { true }++ it "does not render the dropdown or icon" do+ render_inline(component)++ expect(page).to have_no_css(".octicon-triangle-down")+ expect(page).to have_no_css(".ActionMenu")+ end+ end++ context "when title not provided" do+ let(:title) { nil }++ it "raises an exception" do+ expect { render_inline(component) }.to raise_error(NotImplementedError)+ end+ end+end