{% extends "repo_master.html" %}

{% block title %}Tree - {{
    repo.namespace + '/' if repo.namespace }}{{ repo.name }}{% endblock %}
{% set tag = "home" %}

{% block header %}
<link nonce="{{ g.nonce }}" rel="stylesheet" href="{{
  url_for('static', filename='vendor/highlight.js/styles/github.css') }}?version={{ g.version}}"/>
<link nonce="{{ g.nonce }}" rel="stylesheet" href="{{
  url_for('static', filename='vendor/highlightjs-line-numbers/highlightjs-line-numbers.min.css') }}?version={{ g.version }}"/>
<style nonce="{{ g.nonce }}">
  .hljs {
    background: #fff;
  }
</style>
{% endblock %}

{% block repo %}
  <div class="row">
    <div class="col-2">
        {% block overviewtabs %}{{ super() }}{% endblock %}
    </div>
    <div class="col-10">
  <div class="row mb-1">
    <div class="col-sm-6">
    <h3>
      Files
    </h3>
    </div>

    <div class="col-sm-6">
      <div class="float-right">
        {% if branchname in g.branches %}
          <div class="btn-group">
            <a href="#" class="btn btn-outline-light border-secondary text-dark btn-sm dropdown-toggle"
                    data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <span class="fa fa-random fa-fw"></span> Branch: <span class="font-weight-bold">{{ branchname }}</span>
          </a>
            <div class="dropdown-menu dropdown-menu-right">
              {% for branch in g.branches %}
                  <a class="dropdown-item pl-1 {{'active' if branchname == branch}}" href="{{ url_for(
                      'ui_ns.view_tree',
                      repo=repo.name,
                      username=username,
                      namespace=repo.namespace,
                      identifier=branch) }}">
                      <span class="fa fa-random fa-fw"></span> <span class="{{'font-weight-bold' if branchname == branch}}">{{ branch }}</span>
                  </a>
              {% endfor %}
            </div>
          </div>
        {% else %}
        <a href="#" class="btn btn-outline-light border-secondary text-dark btn-sm"
        aria-haspopup="true" aria-expanded="false">
        Commit: <span class="font-weight-bold">{{ branchname }}</span>
        </a>
        {% endif %}
    </div>
  </div>
  </div>
    <div class="card mb-3">
      <div class="card-header">
        <ol class="breadcrumb p-0 bg-transparent mb-0">
          <li class="breadcrumb-item">
            <a href="{{ url_for('ui_ns.view_tree',
                repo=repo.name,
                username=username,
                namespace=repo.namespace,
                identifier=branchname)
          }}">
              <span class="fa fa-random">
              </span>&nbsp; {{ branchname }}
            </a>
          </li>
        {% for file in filename.split('/') %}
          {% set path = '/'.join(filename.split('/')[:loop.index]) %}
          {% if loop.index != loop.length
        %}<li class="breadcrumb-item"><a href="{{ url_for('ui_ns.view_file',
                repo=repo.name,
                username=username,
                namespace=repo.namespace,
                identifier=branchname,
                filename=path | unicode)}}">
            <span class="fa fa-folder"></span>&nbsp; {{ file }}</a>
          </li>
          {% elif file %}
          <li class="active breadcrumb-item">
            <span class="fa {% if output_type == 'tree' %}fa-folder{% else %}fa-file{% endif %}" data-glyph="">
            </span>&nbsp; {{ file }}
          </li>
          {% endif %}
        {% endfor %}
        </ol>
      </div>

{% if content is not none %}
  {% if output_type in ('file','binary','image','markup') %}
  <div class="card-body p-0">
        {% if content is not none %}
          {% if output_type in ('file','binary','image','markup') %}
            <div class="bg-light border text-right pr-2">
                {% if output_type in ('file','markup') and g.repo_admin %}
                <a class="btn btn-sm btn-secondary" href="{{ url_for(
                    'ui_ns.edit_file',
                    repo=repo.name,
                    username=username,
                    namespace=repo.namespace,
                    branchname=branchname,
                    filename=filename | unicode) }}" title="Edit file">Edit</a>
                {% endif %}
                {% if output_type in ('file','markup')
                    and (
                        not g.authenticated
                        or (
                            not repo.is_fork
                            or (
                                g.authenticated
                                and repo.user.user != g.fas_user.username
                            )
                        )
                    )
                %}
                <form class="btn btn-sm" method="POST" name="fork_project"
                    action="{{ url_for('ui_ns.fork_edit_file',
                        repo=repo.name,
                        username=username,
                        namespace=repo.namespace,
                        branchname=branchname, filename=filename) }}">
                    <button class="btn btn-sm btn-secondary fork_project_btn">
                            {% if g.repo_forked %}
                            Edit in your fork
                            {% else %}
                            Fork and Edit
                            {% endif %}
                    </button>
                    {{ g.forkbuttonform.csrf_token }}
                </form>
                {% endif %}

                {% if output_type == 'markup'  %}
                <a class="btn btn-sm btn-secondary" href="{{ url_for(
                    'ui_ns.view_file', username=username, namespace=repo.namespace,
                    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(
                    'ui_ns.view_file',
                    repo=repo.name,
                    username=username,
                    namespace=repo.namespace,
                    identifier=branchname,
                    filename=filename | unicode) }}" title="View as blob">Blob</a>
                {% endif %}

                <a class="btn btn-secondary btn-sm" href="{{ url_for(
                    'ui_ns.view_blame_file',
                    repo=repo.name,
                    username=username,
                    namespace=repo.namespace,
                    identifier=branchname,
                    filename=filename) | unicode }}" title="View git blame">Blame</a>

                <a class="btn btn-secondary btn-sm" href="{{ url_for(
                    'ui_ns.view_history_file',
                    repo=repo.name,
                    username=username,
                    namespace=repo.namespace,
                    identifier=branchname,
                    filename=filename) | unicode }}" title="View git log for this file">History</a>

                <a class="btn btn-secondary btn-sm" href="{{ url_for(
                    'ui_ns.view_raw_file',
                    repo=repo.name,
                    username=username,
                    namespace=repo.namespace,
                    identifier=branchname,
                    filename=filename) | unicode }}" title="View as raw">Raw</a>
            </div>
          {% endif %}
        {% endif %}

    {% if output_type=='file' %}
        <pre class="syntaxhighlightblock"><code class="{{filename|syntax_alias}}">{{ content }}</code></pre>
    {% elif output_type == 'markup' %}
        <div class="m-2">
        {% autoescape false %}
        {{ content | noJS | safe }}
        {% endautoescape %}
      </div>
    {% elif output_type == 'image' %}
        <img src="{{ url_for('ui_ns.view_raw_file',
                    repo=repo.name,
                    username=username,
                    namespace=repo.namespace,
                    identifier=branchname,
                    filename=filename | unicode) }}"/>
    {% elif output_type == 'binary' %}
        <p class="noresult">
          Binary files cannot be rendered.<br/>
          Please
          <a href="{{ url_for('ui_ns.view_raw_file',
                    repo=repo.name,
                    username=username,
                    namespace=repo.namespace,
                    identifier=branchname,
                    filename=filename | unicode) }}">view the raw version
          </a>
        </p>
    {% endif %}
  </div>
  {% else %}
      <div class="card-body p-0">
          <table class="table table-sm mb-0">
        <tbody>
          {% for entry in content %}
            <tr>
                <td class="pagure-table-icon">
                {% if entry.filemode == 16384 %}
                    <span class="fa fa-folder text-muted"></span>
                {% elif entry.filemode == 40960 %}
                    <span class="fa fa-chain-broken text-muted"></span>
                {% elif entry.filemode == 57344 %}
                    <span class="fa fa-clone text-muted"></span>
                {% else %}
                    <span class="fa fa-file text-muted"></span>
                {% endif %}
            </td>
            <td class="pagure-table-filename table-expand-col">
                {% if entry.filemode == 16384 %}<strong>{% endif -%}
                <a href="{{ url_for(
                    'ui_ns.view_file',
                    repo=repo.name,
                    username=username,
                    namespace=repo.namespace,
                    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-->

 {% if readme %}
 <div class="card">
   <div class="card-header">
     <strong>README{{readme_ext}}</strong>
   </div>
   <div class="card-body">
   {{ readme | noJS | safe }}
 </div>
 </div>
 {% endif %}
</div>
</div>
{% endblock %}

{% block jscripts %}
{{ super() }}
<script type="text/javascript" nonce="{{ g.nonce }}" src="{{
  url_for('static', filename='vendor/highlight.js/highlight.pack.js') }}?version={{ g.version}}"></script>
<script type="text/javascript" nonce="{{ g.nonce }}" src="{{
  url_for('static', filename='vendor/highlightjs-line-numbers/highlightjs-line-numbers.min.js') }}?version={{ g.version}}"></script>
<script type="text/javascript" nonce="{{ g.nonce }}" src="{{
  url_for('static', filename='vendor/highlight.js/spec.js') }}?version={{ g.version}}"></script>

<script type="text/javascript" nonce="{{ g.nonce }}">
  $(document).ready(function() {
  $('.fork_project_btn').click($("[name=fork_project]").submit);

  $('pre.syntaxhighlightblock code').each(function(i, block) {
    hljs.highlightBlock(block);
    hljs.lineNumbersBlock(block);
  });

  var cls = "highlighted-line";
  var lines = location.hash.substr(2).split('-').map(function (x) { return parseInt(x, 10) });
  if (! isNaN(lines[0]))
  {
    for (var i = lines[lines.length - 1]; i >= lines[0]; i--) {
      $('#_' + i).parent().parent().addClass(cls);
    }
    setTimeout(function(){
      $("#_" + lines[0]).get(0).scrollIntoView({behavior: "instant", block: "start", inline: "nearest"});
    }, 50);
  }
});
</script>

<script type="text/javascript" nonce="{{ g.nonce }}">

  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 [];
  }
  $(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 %}
