{% 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}}"/>

<style nonce="{{ g.nonce }}">
  .hljs {
    background: #fff;
  }
</style>
{% endblock %}

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

    <div class="col-sm-6">
      <div class="float-right">
      {% if branchname %}
        <div class="btn-group">
          <button type="button" 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>
          </button>
          <div class="dropdown-menu dropdown-menu-right">
            {% for branch in g.branches %}
              {% if origin == 'view_tree' %}
                <a class="dropdown-item" href="{{ url_for(
                    'ui_ns.view_tree',
                    repo=repo.name,
                    username=username,
                    namespace=repo.namespace,
                    identifier=branch) }}">
                  {{ branch }}
                </a>
              {% elif origin == 'view_file' %}
                <a class="dropdown-item" href="{{ url_for(
                    'ui_ns.view_file',
                    repo=repo.name,
                    username=username,
                    namespace=repo.namespace,
                    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 p-0 bg-transparent mb-0">
          <li>
            <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>
        {% set path = '' %}
        {% 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('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">
            <span class="fa {% if output_type == 'tree' %}fa-folder{% else %}fa-file{% endif %}">
            </span>&nbsp; {{ file }}
          </li>
          {% endif %}
        {% endfor %}
        </ol>
      </div>

{% if content %}
  <div class="card-block p-a-0">
    <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') %}
      <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">
            Fork and Edit
        </button>
        {{ 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_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>

    {% autoescape false %}
    {{ content | blame_loc(repo, username, blame) }}
    {% endautoescape %}
  </div>
{% 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-block">
   {{ readme | noJS | safe }}
 </div>
 </div>
 {% endif %}

{% 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/highlight.js/spec.js') }}?version={{ g.version}}"></script>

<script type="text/javascript" nonce="{{ g.nonce }}">
  hljs.initHighlightingOnLoad();
</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 [];
}
$(document).ready(function () {
  $('.fork_project_btn').click($("[name=fork_project]").submit);

  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 %}
