2023-04-28 10:33:01 +02:00
|
|
|
class ViewableChamp::SectionComponent < ApplicationComponent
|
|
|
|
include ApplicationHelper
|
|
|
|
include TreeableConcern
|
|
|
|
|
2023-11-28 17:27:09 +01:00
|
|
|
def initialize(nodes: nil, types_de_champ: nil, row_id: nil, demande_seen_at:, profile:, champs_by_stable_id_with_row:)
|
|
|
|
@demande_seen_at, @profile, @row_id, @champs_by_stable_id_with_row = demande_seen_at, profile, row_id, champs_by_stable_id_with_row
|
|
|
|
nodes ||= to_tree(types_de_champ:)
|
2023-04-28 10:33:01 +02:00
|
|
|
@nodes = to_sections(nodes:)
|
|
|
|
end
|
|
|
|
|
|
|
|
def section_id
|
|
|
|
@section_id ||= header_section ? dom_id(header_section, :content) : SecureRandom.uuid
|
|
|
|
end
|
|
|
|
|
|
|
|
def header_section
|
2023-11-28 17:27:09 +01:00
|
|
|
maybe_header_section = @nodes.first
|
|
|
|
if maybe_header_section.is_a?(TypeDeChamp) && maybe_header_section.header_section?
|
|
|
|
champ_for_type_de_champ(maybe_header_section)
|
2023-06-22 17:17:53 +02:00
|
|
|
end
|
2023-04-28 10:33:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def champs
|
2023-11-28 17:27:09 +01:00
|
|
|
tail.filter_map { _1.is_a?(TypeDeChamp) ? champ_for_type_de_champ(_1) : nil }
|
2023-04-28 10:33:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def sections
|
2023-11-28 17:27:09 +01:00
|
|
|
tail.filter { _1.is_a?(ViewableChamp::SectionComponent) }
|
2023-04-28 10:33:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def tail
|
|
|
|
return @nodes if header_section.blank?
|
|
|
|
_, *rest_of_champ = @nodes
|
|
|
|
|
|
|
|
rest_of_champ
|
|
|
|
end
|
|
|
|
|
2023-07-04 11:22:57 +02:00
|
|
|
def reset_tag_for_depth
|
|
|
|
return if !header_section
|
|
|
|
|
|
|
|
"reset-h#{header_section.level + 1}"
|
2023-04-28 10:33:01 +02:00
|
|
|
end
|
|
|
|
|
2023-06-22 17:17:53 +02:00
|
|
|
def first_level?
|
2023-07-04 11:22:57 +02:00
|
|
|
return if header_section.nil?
|
|
|
|
|
2023-06-22 17:17:53 +02:00
|
|
|
header_section.level == 1
|
|
|
|
end
|
|
|
|
|
2023-04-28 10:33:01 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def to_sections(nodes:)
|
2024-03-08 15:53:05 +01:00
|
|
|
nodes.map { _1.is_a?(Array) ? ViewableChamp::SectionComponent.new(nodes: _1, demande_seen_at: @demande_seen_at, profile: @profile, champs_by_stable_id_with_row: @champs_by_stable_id_with_row, row_id: @row_id) : _1 }
|
2023-11-28 17:27:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def champ_for_type_de_champ(type_de_champ)
|
|
|
|
@champs_by_stable_id_with_row[[@row_id, type_de_champ.stable_id].compact]
|
2023-04-28 10:33:01 +02:00
|
|
|
end
|
|
|
|
end
|