diff --git a/partials/_accordion_nav.html.erb b/partials/_accordion_nav.html.erb index 8e405cb38e..d0128c4298 100644 --- a/partials/_accordion_nav.html.erb +++ b/partials/_accordion_nav.html.erb @@ -1,13 +1,29 @@ -<% +<% def section_open?(section) (!section[:path].nil? && current_page?(section[:path])) || (!section[:open].nil? && section[:open]) || (section[:links] && section[:links].any? { |page| (!page[:path].nil? && current_page?(page[:path])) || (page[:links] && section_open?(page)) }) end + # Ties a trigger to the panel it controls via aria-controls. + def accordion_id(section, prefix) + slug = section[:title].to_s.downcase.gsub(/[^a-z0-9]+/, "-").gsub(/\A-+|-+\z/, "") + "#{prefix}-#{slug.empty? ? "section" : slug}" + end + + # The section's own index page moves into the list as "Overview" so the header + # row has exactly one job instead of being half link, half toggle. Skipped + # when the links already include that page, which most sections do. + def overview_needed?(section) + return false if section[:path].nil? + target = section[:path].to_s.chomp("/") + !Array(section[:links]).any? { |page| page[:path].to_s.chomp("/") == target } + end + nav.each { |nav| nav[:open] = section_open?(nav) unless nav[:title].nil? } - nav.each do |nav| + nav.each_with_index do |nav, nav_index| + section_id = accordion_id(nav, "nav-#{nav_index}") %> <% if nav[:links].nil? %> <%= nav_link nav[:title], nav[:path], class: "title-link" %> @@ -29,36 +45,42 @@ <% else %>