{% macro build_document_crumbs(document) %}
  <nav class="crumbs" role="navigation"><ol xmlns:v="http://rdf.data-vocabulary.org/#" aria-label="{{ _('breadcrumbs') }}">
    <li typeof="v:Breadcrumb"><a href="/{{ request.locale }}" rel="v:url" property="v:title">MDN</a></li>
    {% for doc in document.parents %}
      <li class="crumb" typeof="v:Breadcrumb"><a href="{{ url('wiki.document', doc.full_path) }}" rel="v:url" property="v:title">{{ doc.title }}</a></li>
    {% endfor %}
    <li class="crumb" typeof="v:Breadcrumb" property="v:title">{{ document.title }}</li>
  </ol></nav>
{%- endmacro %}

{% macro get_document_buttons(document, edit_link) %}

  {% if document.parent %}
    {# If there is a parent doc, use it for translating. #}
    {% set translate_url = url('wiki.select_locale', document_path=document.parent.full_path) %}
  {% else %}
    {% set translate_url = url('wiki.select_locale', document_path=document.full_path, locale=document.locale) %}
  {% endif %}

  {% if not edit_link %}
    {% set edit_link = url('wiki.edit_document', document.full_path, locale=document.locale) %}
  {% endif %}


  <ul class="page-buttons" data-sticky="false">{% if not document.is_template %}<li><button id="languages-menu" class="transparent" aria-haspopup="true" aria-owns="languages-menu-submenu" aria-expanded="false"><span>{{ _('Languages') }}</span><i aria-hidden="true" class="icon-globe"></i></button>

        <div class="submenu" id="languages-menu-submenu">
          <div class="submenu-column">
            <ul id="translations">
              {% if document.other_translations.exists() %}
                {% for translation in document.other_translations %}
                  <li><bdi><a rel="internal" href="{{ url('wiki.document', translation.full_path, locale=translation.locale) }}" title="{{ translation.title }}">{{ translation.language }}</a></bdi></li>
                {% endfor %}
              {% else %}
                <li class="smaller">{{ _('No translations exist for this document.') }}</li>
              {% endif %}

              {% if document.is_localizable %}
                <li><a href="{{ translate_url }}">{{ _('Add a translation') }}</a></li>
              {% endif %}
            </ul>
          </div>
        </div></li>{% endif %}<li class="page-buttons-edit"><a href="{{ edit_link }}" class="button" data-optimizely-hook="button-edit-doc" id="edit-button">{{ _('Edit') }}<i aria-hidden="true" class="icon-pencil"></i></a></li><li><button id="advanced-menu" class="only-icon" aria-haspopup="true" aria-owns="advanced-menu-submenu" aria-expanded="false"><span>{{ _('Advanced') }}</span><i aria-hidden="true" class="icon-cog"></i></button>

        <div class="submenu" id="advanced-menu-submenu">
          <!-- this page -->
          <div class="submenu-column">
            <div class="title">{{ _('Advanced') }}</div>
            <ul>
                <li><a href="{{ url('wiki.document_revisions', document.full_path) }}">{{_('History')}}</a></li>
                {% if user.is_authenticated() and document %}
                  <li class="page-watch">

                  	<form action="{{ url('wiki.subscribe_document', document.slug) }}" method="post">
                  		{{ csrf() }}
	                  	<a href="#" data-subscribe-text="{{ _('Subscribe') }}" data-unsubscribe-text="{{ _('Unsubscribe') }}">
	                  	{% if document.is_watched_by(user) %}{{ _('Unsubscribe') }}{% else %}{{ _('Subscribe') }}{% endif %}
	                  	</a>
                  	</form>

                </li>{% endif %}
                {% if user.is_authenticated() and not document.is_template %}
                  <li><a href="{{ url('wiki.new_document') }}?parent={{ document.id }}">{{ _('New sub-page') }}</a></li>
                  <li><a href="{{ url('wiki.new_document') }}?clone={{ document.id }}{% if document.parent_topic %}&parent={{ document.parent_topic.id }}{% endif %}">{{ _('Clone this page') }}</a></li>
                {% endif %}
                {% if user.is_authenticated() and user.has_perm('wiki.move_tree') %}
                  <li><a href="{{ url('wiki.move', document.slug, locale=document.locale) }}">{{ _('Move this page') }}</a></li>
                {% endif %}
                {% if user.is_superuser %}
                  <li><a href="{{ url('admin:wiki_document_change', document.id) }}">{{ _('View in admin') }}</a></li>
                {% endif %}

{#
    # HACK: https://bugzil.la/972545 - Don't delete pages that have children
    # TODO: https://bugzil.la/972541 -  Deleting a page that has subpages
#}
                {% if user.has_perm('wiki.delete_document') and not document.has_children() %}
                  <li><a href="{{ url('wiki.delete_document', document.slug) }}">{{ _('Delete this page') }}</a></li>
                {% endif %}

                {% set policy_links = build_policy_admin_links(request.user, document) %}
                {% if policy_links['change_one'] %}
                  <li><a target="_blank" href="{{ policy_links['change_one'] }}">{{ _('Manage access policy') }}</a></li>
                {% elif policy_links['change_list'] %}
                  <li><a target="_blank" href="{{ policy_links['change_list'] }}">{{ _('Manage access policies') }}</a></li>
                {% elif policy_links['add'] %}
                  <li><a target="_blank" href="{{ policy_links['add'] }}">{{ _('Add access policy') }}</a></li>
                {% endif %}

                {% set zone_links = document_zone_management_links(request.user, document) %}
                {% if zone_links['change'] %}
                  <li><a target="_blank" href="{{ zone_links['change'] }}">{{ _('Manage content zone') }}</a></li>
                {% endif %}
                {% if zone_links['add'] and not document.is_template %}
                  <li><a target="_blank" href="{{ zone_links['add'] }}">{{ _('Convert to content zone') }}</a></li>
                {% endif %}
                <li class="page-print"><a href="#" onclick="return window.print();">{{ _('Print this page') }}</a></li>
            </ul>
          </div>
        </div>
  </li></ul>
{%- endmacro %}

{% macro get_document_subnav(html) %}
  <nav class="zone-subnav-container" role="navigation">
    <div class="subnav" id="Subnav">
      {{ html|safe }}
    </div>
  </nav>&nbsp;
{%- endmacro %}

{% macro get_document_quick_links(html) %}
  <div class="quick-links" id="quick-links">
    <div class="title see-also">{{ _('See also') }}</div>
    {{ html|safe }}
  </div>
{%- endmacro %}

{% macro get_approvals_html(document, user) %}
  {% if user.is_authenticated() and document.current_revision and document.allows_revision_by(user) %}
    {% if document.current_revision.needs_technical_review() or document.current_revision.needs_editorial_review() %}
    <section class="page-meta reviews">
      <p>{{ _('The following reviews have been requested:') }}</p>

      <form method="post" action="{{ url('wiki.quick_review', document.full_path) }}">
        {{ csrf() }}
        <ul>
        {% if document.current_revision.needs_technical_review() %}
          <li>
            <input type="checkbox" value="approve_technical" id="id_approve_technical" name="approve_technical">
            <label for="id_approve_technical">{{ _('Technical') }}</label>
          </li>
        {% endif %}
        {% if document.current_revision.needs_editorial_review() %}
          <li>
            <input type="checkbox" value="approve_editorial" id="id_approve_editorial" name="approve_editorial">
            <label for="id_approve_editorial">{{ _('Editorial') }}</label>
          </li>
        {% endif %}
        </ul>
        <input type="hidden" value="{{ document.current_revision.id }}" name="revision_id" />
        <button type="submit">{{ _('Submit Review') }}</button>
      </form>
    </section>
    {% endif %}
  {% endif %}
{%- endmacro %}
