tech(remaniement): isole la logique de rendu au champs_subtree_component

This commit is contained in:
Martin 2023-04-12 17:55:08 +02:00 committed by mfo
parent 3b5d1bd55b
commit 88abefb370
10 changed files with 137 additions and 191 deletions

View file

@ -1,41 +1,47 @@
class EditableChamp::ChampsSubtreeComponent < ApplicationComponent
include ApplicationHelper
include TreeableConcern
attr_reader :header_section, :nodes
def initialize(header_section:)
@header_section = header_section
@nodes = []
end
# a nodes can be either a champs, or a subtree
def add_node(node)
nodes.push(node)
def initialize(nodes:)
@nodes = to_fieldset(nodes:)
end
def render_within_fieldset?
header_section && !empty_section?
first_champ_is_an_header_section? && any_champ_fillable?
end
def render_header_section_only?
header_section && empty_section?
def header_section
first_champ = @nodes.first
return first_champ if first_champ.is_a?(Champs::HeaderSectionChamp)
nil
end
def empty_section?
nodes.none? { |node| node.is_a?(Champ) }
end
def champs
return @nodes if !first_champ_is_an_header_section?
_, *rest_of_champ = @nodes
def level
if header_section.parent.present?
header_section.header_section_level_value.to_i + header_section.parent.current_section_level
elsif header_section
header_section.header_section_level_value.to_i
else
0
end
rest_of_champ
end
def tag_for_depth
"h#{level + 1}"
"h#{header_section.level + 1}"
end
def fillable?
false
end
private
def to_fieldset(nodes:)
nodes.map { _1.is_a?(Array) ? EditableChamp::ChampsSubtreeComponent.new(nodes: _1) : _1 }
end
def first_champ_is_an_header_section?
header_section.present?
end
def any_champ_fillable?
champs.any? { _1&.fillable? }
end
end

View file

@ -1,17 +1,17 @@
- if render_within_fieldset?
= tag.fieldset(class: "reset-#{tag_for_depth}") do
= tag.legend do
= render EditableChamp::HeaderSectionComponent.new(champ: header_section, form: nil, level: level)
- @nodes.each do |champ_or_section|
- if champ_or_section.is_a?(Champ)
= render EditableChamp::HeaderSectionComponent.new(champ: header_section, form: nil)
- champs.each do |champ_or_section|
- if !champ_or_section.is_a?(EditableChamp::ChampsSubtreeComponent)
= render EditableChamp::FieldsForChampComponent.new(champ: champ_or_section, seen_at: nil)
- else
= render champ_or_section
- elsif render_header_section_only?
= render EditableChamp::HeaderSectionComponent.new(champ: header_section, form: nil, level: level)
- else
- @nodes.each do |champ_or_section|
- if champ_or_section.is_a?(Champ)
- if header_section
= render EditableChamp::HeaderSectionComponent.new(champ: header_section, form: nil)
- champs.each do |champ_or_section|
- if !champ_or_section.is_a?(EditableChamp::ChampsSubtreeComponent)
= render EditableChamp::FieldsForChampComponent.new(champ: champ_or_section, seen_at: nil)
- else
= render champ_or_section

View file

@ -1,13 +1,7 @@
class EditableChamp::ChampsTreeComponent < ApplicationComponent
include Champs::Treeable
attr_reader :root
include TreeableConcern
def initialize(champs:, root_depth:)
@root = to_tree(champs:, root_depth:, build_champs_subtree_component: method(:build_champs_subtree_component))
end
def build_champs_subtree_component(header_section:)
EditableChamp::ChampsSubtreeComponent.new(header_section:)
@tree = to_tree(champs:, root_depth:)
end
end

View file

@ -1 +1 @@
= render @root
= render EditableChamp::ChampsSubtreeComponent.new(nodes: @tree)

View file

@ -1,8 +1,11 @@
class EditableChamp::HeaderSectionComponent < ApplicationComponent
def initialize(form:, champ:, seen_at: nil, level: 1)
def initialize(form:, champ:, seen_at: nil)
@champ = champ
@form = form
@level = level
end
def level
@champ.level
end
def libelle
@ -10,13 +13,13 @@ class EditableChamp::HeaderSectionComponent < ApplicationComponent
end
def header_section_classnames
class_names = ["fr-h#{@level}", 'header-section']
class_names = ["fr-h#{level}", 'header-section']
class_names << 'header-section-counter' if @champ.dossier.auto_numbering_section_headers_for?(@champ)
class_names
end
def tag_for_depth
"h#{@level + 1}"
"h#{level + 1}"
end
end