{% comment %}
Copyright (C) 2017-2024  The Software Heritage developers
See the AUTHORS file at the top-level directory of this distribution
License: GNU Affero General Public License version 3, or any later version
See top-level LICENSE file for more information
{% endcomment %}

{% load swh_templatetags %}

{% if swhids_info %}
  <div id="swh-identifiers">
    <a id="swhids-handle"
       class="handle ui-slideouttab-handle ui-slideouttab-handle-rounded"
       href="#"
       role="button"
       aria-expanded="false"
       aria-label="Expand permalinks tab">
      <i class="mdi mdi-link-variant mdi-fw" aria-hidden="true"></i>
      {% if swhids_info|length > 1 %}
        Permalinks
      {% else %}
        Permalink
      {% endif %}
    </a>
    <div id="swh-identifiers-content" style="display: none;">
      <p class="mb-0">
        To
        <a target="_blank"
           rel="noopener noreferrer"
           href="https://www.softwareheritage.org/howto-archive-and-reference-your-code/">
          reference or cite
        </a>
        the objects present in the Software Heritage archive, permalinks based on
        <a target="_blank"
           rel="noopener noreferrer"
           href="https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html">
          SoftWare Hash IDentifiers (SWHIDs)
        </a>
        must be used.
        <br />
        Select below a type of object currently browsed in order to display its
        associated SWHID and permalink.
      </p>
      <ul id="swhid-object-types" class="nav nav-pills ml-auto p-2">
        {% for swhid_info in swhids_info %}
          {% if forloop.first %}
            <li class="nav-item">
              <a class="nav-link active"
                 href="#swhid-tab-{{ swhid_info.object_type.name.lower }}"
                 data-toggle="tab"
                 onclick="swh.browse.swhIdObjectTypeToggled(event)">
                <i class="{{ swh_object_icons|key_value:swhid_info.object_type.name.lower }} mdi-fw"
                   aria-hidden="true"></i>{{ swhid_info.object_type.name.lower }}
              </a>
            </li>
          {% else %}
            <li class="nav-item">
              <a class="nav-link"
                 href="#swhid-tab-{{ swhid_info.object_type.name.lower }}"
                 data-toggle="tab"
                 onclick="swh.browse.swhIdObjectTypeToggled(event)">
                <i class="{{ swh_object_icons|key_value:swhid_info.object_type.name.lower }} mdi-fw"
                   aria-hidden="true"></i>{{ swhid_info.object_type.name.lower }}
              </a>
            </li>
          {% endif %}
        {% endfor %}
      </ul>
      <div class="tab-content">
        {% for swhid_info in swhids_info %}
          <div class="tab-pane {% if forloop.first %}active{% endif %}"
               id="swhid-tab-{{ swhid_info.object_type.name.lower }}">
            <div class="card">
              <div class="card-body swhid-ui">
                {% if not iframe_mode %}
                  <div class="row swh-badges-iframe">
                    {% if "swh.web.badges" in SWH_DJANGO_APPS %}
                      <div class="swh-badges">
                        {% if snapshot_context and snapshot_context.origin_info %}
                          <img class="swh-badge swh-badge-origin" alt="origin badge" src="{% url 'swh-badge' 'origin' snapshot_context.origin_info.url %}" onclick="swh.badges.showBadgeInfoModal('origin', '{{ snapshot_context.origin_info.url|urlencode:"/?:@&" }}')" title="Click to display badge integration info" />
                        {% endif %}
                        {% if swhid_info.object_id %}
                          <img class="swh-badge swh-badge-{{ swhid_info.object_type.name.lower }}"
                               alt="{{ swhid_info.object_type.name.lower }} badge"
                               src="{% url 'swh-badge' swhid_info.object_type.name.lower swhid_info.object_id %}"
                               onclick="swh.badges.showBadgeInfoModal('{{ swhid_info.object_type.name.lower }}', $(this).closest('.swhid-ui').find('.swhid').text())"
                               title="Click to display badge integration info" />
                        {% endif %}
                      </div>
                    {% endif %}
                    {% if swhid_info.object_type.name.lower == "content" or swhid_info.object_type.name.lower == "directory" %}
                      <a class="ml-auto float-right"
                         style="cursor: pointer"
                         role="button"
                         onclick="swh.browse.showIframeInfoModal('{{ swhid_info.object_type.name.lower }}', $(this).closest('.swhid-ui').find('.swhid').text())">
                        Iframe embedding
                      </a>
                    {% endif %}
                  </div>
                {% endif %}
                {% if swhid_info.object_id %}
                  <pre><a class="swhid" target="_blank" rel="noopener noreferrer" id="{{ swhid_info.swhid }}" href="{{ swhid_info.swhid_url }}">{{ swhid_info.swhid }}</a></pre>
                {% endif %}
                {% if swhid_info.swhid_with_context is not None %}
                  <div class="float-left">
                    <form id="swhid-options-{{ swhid_info.object_type.name.lower }}"
                          aria-label="Options for {{ swhid_info.object_type.name.lower }} SWHID">
                      <div class="custom-control custom-checkbox swhid-option">
                        <input class="custom-control-input swhid-context-option"
                               value="option-origin"
                               type="checkbox"
                               id="swhid-context-option-{{ swhid_info.object_type.name.lower }}"
                               data-swhid-with-context="{{ swhid_info.swhid_with_context }}"
                               data-swhid-with-context-url="{{ swhid_info.swhid_with_context_url }}"
                               onclick="swh.browse.swhIdContextOptionToggled(event)"
                               checked />
                        <label class="custom-control-label font-weight-normal"
                               for="swhid-context-option-{{ swhid_info.object_type.name.lower }}">
                          Add contextual information
                        </label>
                      </div>
                    </form>
                  </div>
                {% endif %}
                <div id="swhid-copy-buttons" class="float-right">
                  <button type="button"
                          class="btn btn-default btn-sm btn-swhid-copy"
                          title="Copy SWHID to clipboard">
                    <i class="mdi mdi-content-copy mdi-fw" aria-hidden="true"></i>Copy identifier
                  </button>
                  <button type="button"
                          class="btn btn-default btn-sm btn-swhid-url-copy"
                          title="Copy url resolving the SWHID to clipboard">
                    <i class="mdi mdi-content-copy mdi-fw" aria-hidden="true"></i>Copy permalink
                  </button>
                </div>
              </div>
            </div>
          </div>
        {% endfor %}
      </div>
    </div>
  </div>
  <script>swh.webapp.setSwhIdsContext({{ swhids_info|jsonify }});</script>
{% endif %}
