{% comment %}
Partial for the textbook sidebar. Renders each chapter and its sections from
_data/toc.yml .

Much of the logic here is to add active classes to the currently active
section. The currently active section / chapter should be highlighted in the
sidebar.

If a chapter or any of its sections are the current page, we should display the
chapter's sections. Otherwise, we hide the sections to keep the sidebar short.

We also prefix the sidebar entries with the chapter/section number. We assume
a 1-level nesting; we will label 1.2, but not 1.2.1.
{% endcomment %}

{% assign chapter_num = 1 %}

<nav id="js-sidebar" class="c-textbook__sidebar">
  {% if site.textbook_logo %}<a href="{{ site.textbook_logo_link }}"><img src="{{ site.textbook_logo | relative_url }}" class="textbook_logo" id="sidebar-logo" alt="textbook logo" data-turbolinks-permanent/></a>{% endif %}
  <h2 class="c-sidebar__title">{{ site.title }}</h2>
  <ul class="c-sidebar__chapters">
    {% for chapter in site.data.toc %}
      {% comment %}
      If the entry is a divider, render a divider and move to next entry.
      {% endcomment %}
      {% if chapter.divider %}
        <li class="c-sidebar__divider"></li>
        {% continue %}
      {% elsif chapter.header %}
        <li><h2 class="c-sidebar__title">{{ chapter.header }}</li>
        {% continue %}
      {% endif %}

      {% comment %}
      If the entry is the current page, assign a CSS class to highlight it.
      {% endcomment %}
      {% if page.url contains chapter.url %}
        {% assign active_class = "c-sidebar__entry--active" %}
      {% else %}
        {% assign active_class = "" %}
      {% endif %}

      {% assign topUrl = chapter.url | relative_url %}
      {% unless chapter.external == true %}
        {% assign topUrl = topUrl | append: '.html'  %}
      {% endunless %}
      <li class="c-sidebar__chapter">
        <a class="c-sidebar__entry {{ active_class }}"
          href="{{ topUrl }}"
        >
          {% unless chapter.not_numbered or site.number_toc_chapters != true %}
            {{ chapter_num }}.
          {% endunless %}
          {{ chapter.title }}
        </a>

        {% if chapter.sections %}

          {% comment %}
          By default, all sections are hidden. We show a chapter's sections
          if the chapter or any of its sections are the current page.
          {% endcomment %}
          {% assign any_section_active = 0 %}
          {% for section in chapter.sections %}
            {% if page.url contains section.url or chapter.expand_sections %}

              {% assign any_section_active = 1 %}
            {% elsif section.subsections %}
              {% for subsection in section.subsections %}
                {% if page.url contains subsection.url %}
                  {% assign any_section_active = 1 %}
                {% endif %}
              {% endfor %}
            {% elsif page.url contains chapter.url %}
              {% assign any_section_active = 1 %}
            {% endif %}
          {% endfor %}
          {% if any_section_active > 0 or site.collapse_inactive_chapters == false %}
            {% assign hide_section_class = "" %}
          {% else %}
            {% assign hide_section_class = "u-hidden-visually" %}
          {% endif %}

          {% assign section_num = 1 %}

          <ul class="c-sidebar__sections {{ hide_section_class }}">
            {% for section in chapter.sections %}
              {% comment %}
              If the entry is the current page, assign a CSS class to highlight
              it.
              {% endcomment %}
              {% if page.url contains section.url %}
                {% assign active_class = "c-sidebar__entry--active" %}
              {% else %}
                {% assign active_class = "" %}
              {% endif %}

              <li class="c-sidebar__section">
                <a class="c-sidebar__entry {{ active_class }}"
                  href="{{ section.url  | relative_url | append: '.html'}}"
                >
                  {% unless chapter.not_numbered or section.not_numbered or site.number_toc_chapters != true %}
                    {{ chapter_num }}.{{ section_num }}
                  {% endunless %}
                  {{ section.title }}
                </a>

                {% assign subsection_num = 1 %}
                {% for subsection in section.subsections %}
                  {% comment %}
                  Now add sub-sections. These will always be shown if they exist.
                  {% endcomment %}
                  {% if page.url contains subsection.url %}
                    {% assign active_class = "c-sidebar__entry--active" %}
                  {% else %}
                    {% assign active_class = "" %}
                  {% endif %}

                  <li class="c-sidebar__subsection">
                    <a class="c-sidebar__entry {{ active_class }}"
                      href="{{ subsection.url | relative_url | append: '.html'}}"
                    >
                      {% unless chapter.not_numbered or section.not_numbered or subsection.not_numbered or site.number_toc_chapters != true %}
                        {{ chapter_num }}.{{ section_num }}.{{ subsection_num }}
                        {% assign subsection_num = subsection_num | plus: 1 %}
                      {% endunless %}
                      {{ subsection.title }}
                    </a>
                {% endfor %}

              </li>
              {% unless chapter.not_numbered or section.not_numbered %}
                {% assign section_num = section_num | plus: 1 %}
              {% endunless %}
            {% endfor %}
          </ul>
        {% endif %}
      </li>

      {% unless chapter.not_numbered %}
        {% assign chapter_num = chapter_num | plus: 1 %}
      {% endunless %}
    {% endfor %}
  </ul>
  <p class="sidebar_footer">{{ site.sidebar_footer_text }}</p>
</nav>
