2023-04-28 10:33:01 +02:00
|
|
|
class ViewableChamp::SectionComponent < ApplicationComponent
|
|
|
|
include ApplicationHelper
|
|
|
|
include TreeableConcern
|
|
|
|
|
|
|
|
def initialize(champs: nil, nodes: nil, demande_seen_at:, profile:)
|
|
|
|
@demande_seen_at, @profile, @repetition = demande_seen_at, profile
|
2023-06-22 17:17:53 +02:00
|
|
|
nodes ||= to_tree(champs:)
|
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-06-22 17:17:53 +02:00
|
|
|
if @nodes.first.is_a?(Champs::HeaderSectionChamp)
|
|
|
|
@nodes.first
|
|
|
|
end
|
2023-04-28 10:33:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def champs
|
2023-06-29 14:58:58 +02:00
|
|
|
tail.filter { _1.is_a?(Champ) }
|
2023-04-28 10:33:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def sections
|
|
|
|
tail.filter { !_1.is_a?(Champ) }
|
|
|
|
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:)
|
|
|
|
nodes.map { _1.is_a?(Array) ? ViewableChamp::SectionComponent.new(nodes: _1, demande_seen_at: @demande_seen_at, profile: @profile) : _1 }
|
|
|
|
end
|
|
|
|
end
|