{# -*- engine: jinja -*- #}
{% extends "base.html" %}

{% block summary %}
<div id="summary" class="summary">
  {% include "directory_page.summary.html" | indent(2) %}
</div>

{% endblock %}

{% block content %}
{% if info.single_page == "js-enabled" %}
<main id="{{ FUNCTIONS_FNAME }}" class="js-enabled-hidden js-disabled-hidden">
  <details>
    {# If javascript is enabled this details content is moved one level up and the details is removed #}
    <summary>Overall list of functions</summary>
    <nav class="function_page">
      {%  include "functions_page.content.html" %}
    </nav>
  </details>
  <hr class="js-enabled-hidden"/>
</main>
{% endif %}

{%  for directory in directories %}
<main id="{{ directory.dirname }}" class="js-enabled-hidden{% if not loop.first %} js-disabled-hidden{% endif %}">
  <nav>
    {% include "directory_page.navigation.html" | indent(2) %}
  </nav>
  {% set entries = directory.entries %}
  {% include "directory_page.content.html" | indent(2) %}
  <hr class="js-enabled-hidden"/>
</main>
{%  endfor %}

{%  for file in files %}
<main id="{{ file.link }}" class="js-enabled-hidden">
  <hr class="js-enabled-hidden"/>
  <div id="summary-{{ file.link }}" class="summary js-enabled-hidden">
    {# If javascript is enabled this summary is moved to the summary block at the top #}
    {% set filename = file.filename %}
    {% set html_filename = file.html_filename %}
    {% set lines = file.lines %}
    {% set functions = file.functions %}
    {% set branches = file.branches %}
    {% set conditions = file.conditions %}
    {% set decisions = file.decisions %}
    {% set calls = file.calls %}
    {% include "source_page.summary.html" | indent(2) %}
  </div>
  <nav>
    {%  set function_list = file.function_list %}
    {%  set source_lines = file.source_lines %}
    {% include "source_page.navigation.html" | indent(2) %}
  </nav>
  {%  include "source_page.content.html" | indent(2) %}
  {% if not loop.last %}
  <hr class="js-enabled-hidden"/>
  {%  endif %}
</main>
{%  endfor %}

{% if info.single_page == "js-enabled" %}
<script>
  document.body.classList.add("js-enabled")
  document.body.classList.remove("js-disabled")

  var global_summary = document.getElementById("summary")
  var global_content = document.getElementById("{{ directories[-1].dirname }}")
  var function_list = document.getElementById("{{ FUNCTIONS_FNAME }}")

  // Move the summaries in the tree
  summaries = document.body.querySelectorAll(".summary")
  for (var i = 0; i < summaries.length; i++) {
    if (summaries[i].id != summary) {
      global_summary.parentNode.insertBefore(summaries[i], global_summary)
    }
  }

  // Remove the details element of the function list
  function_list_details = function_list.querySelector("details")
  function_list_details.parentNode.insertBefore(function_list.querySelector("nav"), function_list_details)
  function_list_details.parentNode.removeChild(function_list_details)

  var old_hash = null
  var enabled_elements = [global_summary, global_content]
  function activateElement() {
    if (old_hash != location.hash) {
      old_hash = location.hash
      hash_parts =
        (location.hash == "")
        ? ["{{ directories[-1].dirname }}"]
        : decodeURIComponent(location.hash.substring(1)).split("|")

      for (var i = 0; i < enabled_elements.length; i++) {
        enabled_elements[i].classList.add("js-enabled-hidden")
      }

      enabled_elements = []
      element = document.getElementById(hash_parts[0])
      if (element == null) {
        enabled_elements.push(global_summary)
        enabled_elements.push(global_content)
      }
      else
      {
        enabled_elements.push(element)
        var summary = document.getElementById("summary-" + hash_parts[0])
        if (summary == null) {
          enabled_elements.push(global_summary)
        }
        else {
          enabled_elements.push(summary)
        }
      }
      for (var i = 0; i < enabled_elements.length; i++) {
        enabled_elements[i].classList.remove("js-enabled-hidden")
      }
      window.scrollTo(0, 0)

      // We need to scroll to the element
      if (hash_parts.length > 1) {
        document.getElementById(hash_parts[0]).scrollIntoView()
      }
    }
  }
  activateElement()
  window.addEventListener("hashchange", activateElement)
</script>
{% endif %}

{% endblock %}
