diff --git a/lib/primer/open_project/forms/dsl/filterable_tree_view_input.rb b/lib/primer/open_project/forms/dsl/filterable_tree_view_input.rb new file mode 100644 index 00000000000..46334d2685d --- /dev/null +++ b/lib/primer/open_project/forms/dsl/filterable_tree_view_input.rb @@ -0,0 +1,66 @@ +# 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 Primer + module OpenProject + module Forms + module Dsl + # :nodoc: + class FilterableTreeViewInput < Primer::Forms::Dsl::Input + attr_reader :name, :label, :block + + def initialize(name:, label: nil, **system_arguments, &block) + @name = name + @label = label + @block = block + + super(**system_arguments) + end + + def to_component + FilterableTreeView.new(input: self) + end + + # :nocov: + def type + :filterable_tree_view + end + # :nocov: + + # :nocov: + def focusable? + true + end + # :nocov: + end + end + end + end +end diff --git a/lib/primer/open_project/forms/dsl/input_methods.rb b/lib/primer/open_project/forms/dsl/input_methods.rb index 79650763ba1..3efc5ae272f 100644 --- a/lib/primer/open_project/forms/dsl/input_methods.rb +++ b/lib/primer/open_project/forms/dsl/input_methods.rb @@ -84,6 +84,10 @@ module Primer add_input SelectPanelInput.new(builder:, form:, **decorate_options(**), &) end + def filterable_tree_view(**, &) + add_input FilterableTreeViewInput.new(builder:, form:, **decorate_options(**), &) + end + def decorate_options(include_help_text: true, help_text_options: {}, **options) if include_help_text && supports_help_texts?(form.model) attribute_name = help_text_options[:attribute_name] || options[:name] diff --git a/lib/primer/open_project/forms/filterable_tree_view.html.erb b/lib/primer/open_project/forms/filterable_tree_view.html.erb new file mode 100644 index 00000000000..bb222c38920 --- /dev/null +++ b/lib/primer/open_project/forms/filterable_tree_view.html.erb @@ -0,0 +1,7 @@ +<%= + render(FormControl.new(input: @input)) do + render(Primer::OpenProject::FilterableTreeView.new(**@input.input_arguments)) do |tree_view| + @input.block&.call(tree_view) + end + end +%> diff --git a/lib/primer/open_project/forms/filterable_tree_view.rb b/lib/primer/open_project/forms/filterable_tree_view.rb new file mode 100644 index 00000000000..c6b43a0cbb9 --- /dev/null +++ b/lib/primer/open_project/forms/filterable_tree_view.rb @@ -0,0 +1,51 @@ +# 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 Primer + module OpenProject + module Forms + # :nodoc: + class FilterableTreeView < Primer::Forms::BaseComponent + delegate :builder, :form, to: :@input + + def initialize(input:) + super() + + @input = input + + @input.input_arguments[:form_arguments] = { + name: @input.name, + builder: builder + } + end + end + end + end +end diff --git a/modules/wikis/app/forms/wikis/link_existing_wiki_page_form.rb b/modules/wikis/app/forms/wikis/link_existing_wiki_page_form.rb index acb68eaf236..cd39eca96a4 100644 --- a/modules/wikis/app/forms/wikis/link_existing_wiki_page_form.rb +++ b/modules/wikis/app/forms/wikis/link_existing_wiki_page_form.rb @@ -35,37 +35,24 @@ module Wikis f.hidden(name: :linkable_type) f.hidden(name: :linkable_id) - f.html_content do - render( - Primer::OpenProject::FilterableTreeView.new( - src: helpers.search_wiki_pages_path(provider_id: model.provider_id, name: "wiki_page_selection"), - form_arguments: { builder: rails_builder(f), name: "wiki_page_selection" }, - filter_mode_control_arguments: { hidden: true }, - filter_input_arguments: { - placeholder: I18n.t("wikis.link_existing_wiki_page_form.placeholder"), - # every other property is just refilling the default values, - # as those are not merged into custom arguments - name: :filter, - label: I18n.t(:button_filter), - type: :search, - leading_visual: { icon: :search }, - visually_hide_label: true, - show_clear_button: true - }, - include_sub_items_check_box_arguments: { hidden: true }, - no_results_node_arguments: { label: I18n.t("wikis.link_existing_wiki_page_form.no_results") } - ) - ) - end - end - - private - - # Primer's FormObject stores the underlying ActionView/Primer form builder - # as @builder. FilterableTreeView requires an ActionView::FormBuilder to - # generate its hidden form inputs via hidden_field. - def rails_builder(form) - form.instance_variable_get(:@builder) + f.filterable_tree_view( + name: "wiki_page_selection", + src: helpers.search_wiki_pages_path(provider_id: model.provider_id, name: "wiki_page_selection"), + filter_mode_control_arguments: { hidden: true }, + filter_input_arguments: { + placeholder: I18n.t("wikis.link_existing_wiki_page_form.placeholder"), + # every other property is just refilling the default values, + # as those are not merged into custom arguments + name: :filter, + label: I18n.t(:button_filter), + type: :search, + leading_visual: { icon: :search }, + visually_hide_label: true, + show_clear_button: true + }, + include_sub_items_check_box_arguments: { hidden: true }, + no_results_node_arguments: { label: I18n.t("wikis.link_existing_wiki_page_form.no_results") } + ) end end end