|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +module OpenProject |
| 32 | + module Common |
| 33 | + module InplaceEditFields |
| 34 | + class VersionSelectListComponent < SelectListComponent |
| 35 | + attr_reader :form, :attribute, :model |
| 36 | + |
| 37 | + def initialize(form:, attribute:, model:, **system_arguments) |
| 38 | + super |
| 39 | + |
| 40 | + unless custom_field? |
| 41 | + assign_defaults! |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + private |
| 46 | + |
| 47 | + def render_custom_field_input |
| 48 | + input_class = if custom_field.multi_value? |
| 49 | + CustomFields::Inputs::MultiVersionSelectList |
| 50 | + else |
| 51 | + CustomFields::Inputs::SingleVersionSelectList |
| 52 | + end |
| 53 | + |
| 54 | + # Use fields_for to create the proper context for custom field inputs |
| 55 | + form.fields_for(:custom_field_values) do |builder| |
| 56 | + input_class.new(builder, custom_field:, object: model) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + def render_autocompleter |
| 61 | + form.autocompleter(name: attribute, **@system_arguments) do |list| |
| 62 | + model.assignable_versions.each do |version| |
| 63 | + list.option( |
| 64 | + label: version.name, |
| 65 | + value: version.id, |
| 66 | + selected: version.id == model.version&.id |
| 67 | + ) |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + def assign_defaults! |
| 73 | + version = model.version |
| 74 | + @system_arguments[:autocomplete_options][:inputValue] = version&.id |
| 75 | + @system_arguments[:autocomplete_options][:model] = version_model |
| 76 | + @system_arguments[:autocomplete_options][:decorated] = true |
| 77 | + @system_arguments[:autocomplete_options][:closeOnSelect] = true |
| 78 | + # Override inputName to use Rails form builder naming convention |
| 79 | + @system_arguments[:autocomplete_options][:inputName] = input_name |
| 80 | + end |
| 81 | + |
| 82 | + def version_model |
| 83 | + version ? { id: version.id, name: version.name } : nil |
| 84 | + end |
| 85 | + |
| 86 | + def input_name |
| 87 | + "#{model.class.model_name.param_key}[#{attribute}]" |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + end |
| 92 | +end |
0 commit comments