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
|
|
|
|
end
|
|
|
|
|
|
|
|
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),
|
|
|
|
TypeDeChamp.type_champs.fetch(:repetition)
|
|
|
|
]
|
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(
|
|
|
|
"editable-champ-#{@champ.type_champ}": true,
|
|
|
|
"hidden": !@champ.visible?
|
|
|
|
),
|
2022-09-08 11:25:39 +02:00
|
|
|
id: @champ.input_group_id,
|
2023-01-24 12:16:45 +01:00
|
|
|
data: { controller: stimulus_controller, **data_dependent_conditions }
|
2022-09-08 11:25:39 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|