{% extends "repo_master.html" %}

{% block title %}Tree - {{ repo.name }}{% endblock %}
{% set tag = "home" %}


{% block repo %}
  <div class="row m-b-1">
    <div class="col-sm-6">
    <h3>
      Files
    </h3>
    </div>

    <div class="col-sm-6">
      <div class="pull-xs-right">
      <div class="button-label button-label-default">
        {% if repo.is_fork %}
        <span class="oi" data-glyph="fork"></span>
        {{ repo.user.user }}/{{ repo.name }}
        {% else %}
        <span class="oi" data-glyph="document"></span> {{ repo.name }}
        {% endif %}

      </div>
      {% if branchname %}
        <div class="btn-group">
          <button type="button" class="btn btn-secondary btn-sm dropdown-toggle"
                  data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  <span class="oi" data-glyph="random"> </span> {{ branchname }}
          </button>
          <div class="dropdown-menu dropdown-menu-right">
            {% for branch in branches %}
              {% if origin == 'view_tree' %}
                <a class="dropdown-item" href="{{ url_for(
                    'view_tree',
                    username=username, repo=repo.name, identifier=branch) }}">
                  {{ branch }}
                </a>
              {% elif origin == 'view_file' %}
                <a class="dropdown-item" href="{{ url_for(
                    'view_file',
                    username=username, repo=repo.name, identifier=branch,
                    filename=filename | unicode ) }}">
                  {{ branch }}
                </a>
              {% endif %}
            {% endfor %}
          </div>
        </div>
      {% endif %}
    </div>
  </div>
  </div>
    <div class="card">
      <div class="card-header">
        <ol class="breadcrumb">
          <li>
            <a href="{{ url_for('view_tree', username=username,
                      repo=repo.name, identifier=branchname)
          }}">
              <span class="oi" data-glyph="random">
              </span>&nbsp; {{ branchname }}
            </a>
          </li>
        {% for file in filename.split('/') %}
          {% if loop.first %}
          {% set path = file %}
          {% else %}
          {% set path = path + '/' + file %}
          {% endif %}
          {% if loop.index != loop.length %}<li><a
          href="{{ url_for('view_file', username=username,
                  repo=repo.name, identifier=branchname,
                  filename=path | unicode)}}">
            <span class="oi" data-glyph="folder"></span>&nbsp; {{ file }}</a>
          </li>
          {% elif file %}
          <li class="active">
            <span class="oi" data-glyph="{% if output_type == 'tree' %}folder{% else %}file{% endif %}">
            </span>&nbsp; {{ file }}
          </li>
          {% endif %}
        {% endfor %}
        </ol>
      </div>

{% if content %}
  {% if output_type in ('file','binary','image','markup') %}
  <div class="card-block p-a-0">
        {% if content %}
          {% if output_type in ('file','binary','image','markup') %}
            <div class="file-button-bar">
                {% if output_type in ('file','markup') and repo_admin %}
                <a class="btn btn-sm btn-secondary" href="{{ url_for(
                    'edit_file', username=username,
                    repo=repo.name, branchname=branchname,
                    filename=filename | unicode) }}" title="Edit file">Edit</a>
                {% endif %}
                {% if output_type == 'markup'  %}
                <a class="btn btn-sm btn-secondary" href="{{ url_for(
                    'view_file', username=username,
                    repo=repo.name, identifier=branchname,
                    filename=filename | unicode, text=True) }}"
                    title="View as text">Text</a>
                {% else %}
                <a class="btn btn-secondary btn-sm" href="{{ url_for(
                    'view_file', username=username,
                    repo=repo.name, identifier=branchname,
                    filename=filename | unicode) }}" title="View as blob">Blob</a>
                {% endif %}
                <a class="btn btn-secondary btn-sm" href="{{ url_for(
                    'view_raw_file', username=username,
                    repo=repo.name, identifier=branchname,
                    filename=filename) | unicode }}" title="View as raw">Raw</a>
            </div>
          {% endif %}
        {% endif %}

    {% if output_type=='file' %}
        {% autoescape false %}
        {{ content | format_loc }}
        {% endautoescape %}
    {% elif output_type == 'markup' %}
        {% autoescape false %}
        {{ content | noJS }}
        {% endautoescape %}
    {% elif output_type == 'image' %}
        <img src="{{ url_for('view_raw_file', username=username,
                    repo=repo.name, identifier=branchname,
                    filename=filename | unicode) }}"/>
    {% elif output_type == 'binary' %}
        <p class="noresult">
          Binary files cannot be rendered.<br/>
          Please
          <a href="{{ url_for('view_raw_file', username=username,
                    repo=repo.name, identifier=branchname,
                    filename=filename | unicode) }}">view the raw version
          </a>
        </p>
    {% endif %}
  </div>
  {% else %}
      <div class="card-block p-a-0">
          <table class="table table-sm">
        <tbody>
          {% for entry in content %}
            <tr>
                <td class="pagure-table-icon">
                {% if entry.filemode == 16384 %}
                    <span class="oi text-muted" data-glyph="folder"></span>
                {% elif entry.filemode == 40960 %}
                    <span class="oi text-muted" data-glyph="link-broken"></span>
                {% elif entry.filemode == 57344 %}
                    <span class="oi text-muted" data-glyph="layers"></span>
                {% else %}
                    <span class="oi text-muted" data-glyph="file"></span>
                {% endif %}
            </td>
            <td class="pagure-table-filename table-expand-col">
                {% if entry.filemode == 16384 %}<strong>{% endif -%}
                <a href="{{ url_for(
                    'view_file', username=username,repo=repo.name,
                    identifier=branchname,
                    filename=filename | unicode + '/' +
                    entry.name | unicode if filename else entry.name) }}">
                   {{ entry.name | unicode }}
                </a>{% if entry.filemode == 16384 %}</strong>{% endif%}
            </td>
          {% endfor %}
        </tbody>
      </table>
      </div>
  {% endif %}
{% else %}
No content found in this repository
{% endif %}
 </div> <!-- end .card-->

{% endblock %}

{% block jscripts %}
{{ super() }}
<script type="text/javascript">
function updateHighlight() {
  var cls = "highlighted-line";
  $('.' + cls).removeClass(cls)
  if (location.hash !== '') {
    var lines = location.hash.substr(2).split('-').map(function (x) { return parseInt(x, 10) });
    for (var i = lines[lines.length - 1]; i >= lines[0]; i--) {
      $('[data-line-number=' + i + ']').closest('tr').addClass(cls);
    }
    return lines;
  }
  return [];
}
$(document).ready(function () {
  var lines = updateHighlight();
  if (lines) {
    var offset = $('[data-line-number=' + lines[0] + ']').offset().top;
    $('html, body').animate({scrollTop: offset}, 200);
  }
});
$(window).on('hashchange', updateHighlight);
var selected = [];
$("[data-line-number]").click(function (ev) {
  var line = $(this).attr('data-line-number');
  if (ev.shiftKey) {
    selected = selected.slice(-1).concat(line);
  } else {
    selected = [line];
  }

  var hash = '_' + selected[0];
  if (selected.length === 2) {
    hash = '_' + Math.min(selected[0], selected[1]) + '-' + Math.max(selected[0], selected[1]);
  }
  window.location.hash = hash;
  return false;
});
</script>
{% endblock %}
