2022-08-12 10:45:04 +02:00
|
|
|
class EditableChamp::EditableChampComponent < ApplicationComponent
|
2022-09-08 11:25:39 +02:00
|
|
|
def initialize(form:, champ:, seen_at: nil)
|
|
|
|
@form, @champ, @seen_at = form, champ, seen_at
|
2023-05-05 09:13:05 +02:00
|
|
|
@attribute = :value
|
2022-09-08 11:25:39 +02:00
|
|
|
end
|
|
|
|
|
2023-08-21 16:18:31 +02:00
|
|
|
def champ_component
|
|
|
|
@champ_component ||= component_class.new(form: @form, champ: @champ, seen_at: @seen_at)
|
|
|
|
end
|
|
|
|
|
2022-09-08 11:25:39 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def has_label?(champ)
|
2023-04-13 15:09:24 +02:00
|
|
|
types_without_label = [
|
|
|
|
TypeDeChamp.type_champs.fetch(:header_section),
|
|
|
|
TypeDeChamp.type_champs.fetch(:explication),
|
2023-08-17 17:19:50 +02:00
|
|
|
TypeDeChamp.type_champs.fetch(:repetition),
|
|
|
|
TypeDeChamp.type_champs.fetch(:linked_drop_down_list)
|
2023-04-13 15:09:24 +02:00
|
|
|
]
|
2022-09-08 11:25:39 +02:00
|
|
|
!types_without_label.include?(@champ.type_champ)
|
|
|
|
end
|
|
|
|
|
|
|
|
def component_class
|
|
|
|
"EditableChamp::#{@champ.type_champ.camelcase}Component".constantize
|
|
|
|
end
|
|
|
|
|
|
|
|
def html_options
|
|
|
|
{
|
2022-11-09 12:33:20 +01:00
|
|
|
class: class_names(
|
2023-05-05 09:13:05 +02:00
|
|
|
{
|
|
|
|
'editable-champ': true,
|
|
|
|
"editable-champ-#{@champ.type_champ}": true,
|
2023-08-21 16:18:31 +02:00
|
|
|
champ_component.dsfr_group_classname => true
|
|
|
|
}.merge(champ_component.input_group_error_class_names)
|
2022-11-09 12:33:20 +01:00
|
|
|
),
|
2023-05-30 11:39:53 +02:00
|
|
|
data: { controller: stimulus_controller, **data_dependent_conditions, **stimulus_values }
|
2024-04-09 16:19:54 +02:00
|
|
|
}.deep_merge(champ_component.fieldset_error_opts)
|
2022-09-08 11:25:39 +02:00
|
|
|
end
|
|
|
|
|
2023-10-09 13:02:58 +02:00
|
|
|
def fieldset_element_attributes
|
|
|
|
{
|
|
|
|
id: @champ.input_group_id,
|
|
|
|
"hidden": !@champ.visible?
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-05-30 11:39:53 +02:00
|
|
|
def stimulus_values
|
|
|
|
if @champ.fetch_external_data_pending?
|
|
|
|
{ turbo_poll_url_value: }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def turbo_poll_url_value
|
|
|
|
if @champ.private?
|
2024-03-28 12:42:29 +01:00
|
|
|
annotation_instructeur_dossier_path(@champ.dossier.procedure, @champ.dossier, @champ.stable_id, row_id: @champ.row_id, with_public_id: true)
|
2023-05-30 11:39:53 +02:00
|
|
|
else
|
2024-03-28 12:42:29 +01:00
|
|
|
champ_dossier_path(@champ.dossier, @champ.stable_id, row_id: @champ.row_id, with_public_id: true)
|
2023-05-30 11:39:53 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-08 11:25:39 +02:00
|
|
|
def stimulus_controller
|
2023-06-06 15:54:03 +02:00
|
|
|
if autosave_enabled?
|
2022-09-08 11:25:39 +02:00
|
|
|
# This is an editable champ. Lets find what controllers it might need.
|
2023-03-01 18:30:10 +01:00
|
|
|
controllers = ['autosave']
|
2022-09-08 11:25:39 +02:00
|
|
|
|
2023-05-30 11:39:53 +02:00
|
|
|
if @champ.fetch_external_data_pending?
|
|
|
|
controllers << 'turbo-poll'
|
|
|
|
end
|
|
|
|
|
2022-09-08 11:25:39 +02:00
|
|
|
controllers.join(' ')
|
|
|
|
end
|
2022-08-12 10:45:04 +02:00
|
|
|
end
|
2023-01-24 12:16:45 +01:00
|
|
|
|
|
|
|
def data_dependent_conditions
|
|
|
|
if @champ.dependent_conditions?
|
|
|
|
{ "dependent-conditions": "true" }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
2023-06-06 15:54:03 +02:00
|
|
|
|
|
|
|
def autosave_enabled?
|
|
|
|
!@champ.carte? && !@champ.block? && @champ.fillable?
|
|
|
|
end
|
2022-08-12 10:45:04 +02:00
|
|
|
end
|