2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-04-28 10:33:01 +02:00
|
|
|
class ViewableChamp::SectionComponent < ApplicationComponent
|
|
|
|
include ApplicationHelper
|
|
|
|
include TreeableConcern
|
|
|
|
|
2024-03-14 15:13:13 +01:00
|
|
|
def initialize(dossier:, nodes: nil, types_de_champ: nil, row_id: nil, demande_seen_at:, profile:)
|
|
|
|
@dossier, @demande_seen_at, @profile, @row_id = dossier, demande_seen_at, profile, row_id
|
2023-11-28 17:27:09 +01:00
|
|
|
nodes ||= to_tree(types_de_champ:)
|
2023-04-28 10:33:01 +02:00
|
|
|
@nodes = to_sections(nodes:)
|
|
|
|
end
|
|
|
|
|
2024-03-14 15:13:13 +01:00
|
|
|
private
|
|
|
|
|
2023-04-28 10:33:01 +02:00
|
|
|
def section_id
|
|
|
|
@section_id ||= header_section ? dom_id(header_section, :content) : SecureRandom.uuid
|
|
|
|
end
|
|
|
|
|
|
|
|
def header_section
|
2024-03-14 15:13:13 +01:00
|
|
|
node = @nodes.first
|
2024-12-09 09:36:27 +01:00
|
|
|
@dossier.project_champ(node, row_id: @row_id) if node.is_a?(TypeDeChamp) && node.header_section?
|
2023-04-28 10:33:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def champs
|
2024-12-09 09:36:27 +01:00
|
|
|
tail.filter_map { _1.is_a?(TypeDeChamp) ? @dossier.project_champ(_1, row_id: @row_id) : 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
|
2024-03-14 15:13:13 +01:00
|
|
|
return @nodes if header_section.nil?
|
2023-04-28 10:33:01 +02:00
|
|
|
_, *rest_of_champ = @nodes
|
|
|
|
|
|
|
|
rest_of_champ
|
|
|
|
end
|
|
|
|
|
2023-07-04 11:22:57 +02:00
|
|
|
def reset_tag_for_depth
|
2024-03-14 15:13:13 +01:00
|
|
|
return if header_section.nil?
|
2023-07-04 11:22:57 +02:00
|
|
|
|
|
|
|
"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-14 15:13:13 +01:00
|
|
|
nodes.map { _1.is_a?(Array) ? ViewableChamp::SectionComponent.new(dossier: @dossier, nodes: _1, demande_seen_at: @demande_seen_at, profile: @profile, row_id: @row_id) : _1 }
|
2023-04-28 10:33:01 +02:00
|
|
|
end
|
|
|
|
end
|