{% extends "master.html" %}

{% block title %}Home{% endblock %}
{% set tag = "home" %}

{% from "_render_repo.html" import render_repos, render_user_repos%}
{% from "_browseheader.html" import browse_header %}
{% from "_projectstring.html" import projectstring, projecticon %}


{% block header %}
<link nonce="{{ g.nonce }}" rel="stylesheet" href="{{
  url_for('static', filename='vendor/selectize/selectize.bootstrap3.css') }}?version={{ g.version}}"/>
{% endblock %}

{% block content %}
<div class="bg-light border border-top-0 pt-2">
  <div class="container">
    {{ browse_header(select=select) }}
  </div>
</div>
  {% if namespace %}
  <div class="container mt-5">
    <p>
      These {{projectstring(plural=True)}} are under the `{{ namespace }}` namespace making them
      likely related to the <a href="{{ url_for('ui_ns.view_group', group=namespace)
      }}">{{ namespace }}</a>
      group.
    </p>
  </div>
  {% endif %}
  <div class="container mt-5">
    {{ render_repos(
        repos, total_page, 'page', page,
        'All '+projectstring()+'s', repos_length, 'repos', username, sorting=sorting, select=select) }}
  </div>

{% endblock %}

{% block jscripts %}
    {{ super() }}
<script type="text/javascript" nonce="{{ g.nonce }}" src="{{
  url_for('static', filename='vendor/selectize/selectize.min.js') }}?version={{ g.version}}" type="text/javascript"> </script>

<script type="text/javascript" nonce="{{ g.nonce }}">
$(document).ready(function() {

  $('#headerSearch').on('keypress keydown keyup', function(e) {
    if (e.which == 13) {
        e.preventDefault();
        return false;
    }
  });

  $('#term').selectize({
    valueField: 'fullname',
    labelField: 'fullname',
    searchField: 'fullname',
    maxItems: 1,
    create: false,
    onType: function(value){
      if (value == ""){
        this.close();
      }
    },
    onChange: function(value){
      if (value != ""){
        $('#headerSearch').submit();
      }
    },
    load: function(query, callback) {
      if (!query.length) return callback();
      $.getJSON(
        "{{ url_for('api_ns.api_projects') }}", {
          pattern: "*"+query+"*",
          short: "1",
        },
        function( data ) {
          callback( data.projects );
        }
      );
    },
    render: {
      option: function(item, escape) {
        {% set reponame = 'item.fullname' %}
        return '<div>'
          +   '<div class="projecticon-search pull-xs-left">'
          +     '<span class="fa {{projecticon()}}"></span>'
          +   '</div>'
          +   '<div class="title">'
          +     '<span class="name">'
          +       '<strong>' + escape(item.fullname) + '</strong>'
          +     '</span>'
          +   '</div>'
          +   '<div class="description">'
          +     '<small>' + escape(item.description) + '</small>'
          +   '</div>'
          + '</div>';
      }
    },
  });

  {% if username %}
    $(function(){
        $('.show_parts input[type="checkbox"]').change(function(){
            $('#' + $(this).attr('name')).toggle();
        });
    });
  {% endif %}
});
</script>

{% endblock %}
