2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-04-14 10:55:36 +02:00
|
|
|
class EditableChamp::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)
|
2023-11-28 17:27:09 +01:00
|
|
|
nodes ||= to_tree(types_de_champ:)
|
2024-03-14 15:13:13 +01:00
|
|
|
@dossier = dossier
|
2023-11-28 17:27:09 +01:00
|
|
|
@row_id = row_id
|
2023-04-14 10:55:36 +02:00
|
|
|
@nodes = to_fieldset(nodes:)
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_within_fieldset?
|
2023-09-27 18:43:45 +02:00
|
|
|
first_champ_is_an_header_section?
|
2023-04-14 10:55:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def header_section
|
2023-11-28 17:27:09 +01:00
|
|
|
node = @nodes.first
|
2024-03-14 15:13:13 +01:00
|
|
|
@dossier.project_champ(node, @row_id) if node.is_a?(TypeDeChamp) && node.header_section?
|
2023-04-14 10:55:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def splitted_tail
|
|
|
|
tail.map { split_section_champ(_1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def tail
|
|
|
|
return @nodes if !first_champ_is_an_header_section?
|
|
|
|
_, *rest_of_champ = @nodes
|
|
|
|
|
|
|
|
rest_of_champ
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag_for_depth
|
|
|
|
"h#{header_section.level + 1}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def split_section_champ(node)
|
|
|
|
case node
|
|
|
|
when EditableChamp::SectionComponent
|
|
|
|
[node, nil]
|
|
|
|
else
|
2024-03-14 15:13:13 +01:00
|
|
|
[nil, @dossier.project_champ(node, @row_id)]
|
2023-04-14 10:55:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def to_fieldset(nodes:)
|
2024-03-14 15:13:13 +01:00
|
|
|
nodes.map { _1.is_a?(Array) ? EditableChamp::SectionComponent.new(dossier: @dossier, nodes: _1, row_id: @row_id) : _1 }
|
2023-04-14 10:55:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def first_champ_is_an_header_section?
|
|
|
|
header_section.present?
|
|
|
|
end
|
|
|
|
end
|