Blob Blame History Raw
{% extends "repo_master.html" %}
{% from "_formhelper.html" import render_field_in_row, show_comment, render_bootstrap_field %}

{% block title %}
  {%- if pull_request -%}
    PR#{{ requestid }}: {{ pull_request.title }} - {{ repo.name }}
  {% elif form and (repo_admin or remote_git) %}
    Create new Pull Request for {{ branch_to }} - {{ repo.name }}
  {%- else -%}
    Diff from {{ branch_from }} to {{ branch_to }} - {{ repo.name }}
  {%- endif -%}
{% endblock %}

{% set tag = "home" %}

{% block header %}
<link href="{{ url_for('static', filename='fontawesome/font-awesome.min.css') }}"
  rel="stylesheet" />
<link href="{{ url_for('static', filename='emoji/emojione.sprites.css') }}"
  rel="stylesheet" />
  <link href="{{ url_for('static', filename='selectize.bootstrap3.css') }}"
  rel="stylesheet" />
{% endblock %}

{% block repo %}
<div class="row">
  <div class="{% if pull_request %}col-md-12{% elif form and (repo_admin or remote_git) %}col-md-12{%endif%}">
{% if pull_request %}
<h2><span class="label label-default">PR#{{requestid}}</span> {{ pull_request.title | noJS(ignore="img") | safe
  }}{% if authenticated and (g.fas_user.username == pull_request.user.username
    or repo_admin) and pull_request.status == 'Open'
  %}<small>
    (<a class="edit_icon"
       href="{{ url_for('request_pull_edit', username=username,
            repo=repo.name, requestid=requestid) }}"
      title="Update title">edit</a>)</small> {%
  endif %}</h2>

<div class="header">
  <div class="btn-group" role="group" >
    {% if pull_request.status == 'Open' and repo_admin %}
     <span class="btn btn-default">
      <form action="{{ url_for('merge_request_pull', username=username,
          repo=repo.name, requestid=requestid) }}" method="POST">
        {{ mergeform.csrf_token }}
        <button type="submit" class="btn btn-success">Merge</button>
      </form>
     </span>
    {% endif %}
    {% if pull_request.status == 'Open' and authenticated and
        (repo_admin or g.fas_user.username == pull_request.user.username) %}
     <span class="btn btn-default">
      <form action="{{ url_for('cancel_request_pull', username=username,
          repo=repo.name, requestid=requestid) }}" method="POST">
        {{ mergeform.csrf_token }}
        <button type="submit" value="Close" id="cancel_pr" class="btn btn-danger">Close</button>
      </form>
     </span>
    {% endif %}
    {% if pull_request.status != 'Open' %}
     <span class="btn btn-default">{{ pull_request.status }} by
        <a href="{{ url_for('view_user', username=pull_request.closed_by.user)}} ">
          {{ pull_request.closed_by.user if pull_request.closed_by else ''}}
        </a>
     </span>
    {% endif %}
     <span class="btn btn-default">
      <a class="button patch" href="{{ url_for('request_pull_patch', username=username,
            repo=repo.name, requestid=requestid) }}">
        <button type="submit" class="btn btn-info">Patch View</button>
      </a>
     </span>
  </div>
</div>
{% elif form and (repo_admin or remote_git) %}
<h2>Create pull request</h2>
{% else %}
<h2>Diff
      (<a href="{{ url_for('view_tree', username=username,
               repo=repo.name, identifier=commitid) }}"
      >tree</a>)</h2>
{% endif %}

{% if form and (repo_admin or remote_git) %}
<section class="new_project">
  {% if remote_git %}
  <form action="{{ url_for('.new_remote_request_pull', username=username,
    repo=repo.name, confirm=True) }}" method="post">
    <input type="hidden" value="{{ branch_from }}" name="branch_from" />
    <input type="hidden" value="{{ remote_git }}" name="git_repo" />
  {% else %}
  <form action="{{ url_for('.new_request_pull', username=username,
    repo=repo.name, commitid=commitid, branch_from=branch_from,
    branch_to=branch_to) }}" method="post">
  {% endif %}
  Pull from
<span class="pr-toplabel">{% if remote_git -%}{{ remote_git }}{%- else -%}<span class="oi" data-glyph="fork"></span> {{ repo.fullname}}{%- endif %}</span> <span class="pr-toplabel"> <span class="oi" data-glyph="random"> </span>{{ branch_from }}</span>
  into
<span class="pr-toplabel">{% if repo.is_fork -%}{{ repo.parent.fullname }}{% else %}{{ repo.fullname }}{% endif %}</span>
          <select class="pr-toplabel-select" id="branch_select" name="branch_to">
              <option value="{{ branch_to }}">{{ branch_to }}</option>
            {% for branch in branches |reverse %}
              {% if branch != branch_to %}
              <option value="{{ branch }}">{{ branch }}</option>
              {% endif %}
            {% endfor %}
          </select>
    <table>
      {{ render_bootstrap_field(form.title) }}
      <tr>
          <td>From git repo:</td>
          <td>
          {% if remote_git -%}
            {{ remote_git }}
          {%- else -%}
            {{ repo.fullname}}
          {%- endif %}
          </td>
      </tr>
      <tr>
        <td>From branch:</td>
        <td>{{ branch_from }}</td>
      </tr>
      <tr>
          <td>To git repo:</td>
          <td>

          </td>
      </tr>
      <tr>
        <td>To branch:</td>
        <td>

        </td>
      </tr>
    </table>
    <p class="buttons indent">
      <input type="submit" class="submit positive button" value="Create">
      {{ form.csrf_token }}
      <a href="{{ url_for('view_repo', username=username, repo=repo.name)}}">
        <input type="button" value="Cancel" />
      </a>
    </p>
  </form>
</section>
{% endif %}

{% if pull_request %}
<div class="row m-t-12">
  <div class="col-md-8">

    <section class="pr_info">
    <div class="card" id="pr_flags">
      <div class="card-block">
        <table class="table table-hover table-striped m-b-0">
          <thead></thead>
          <tfoot></tfoot>
          <tbody>
          <tr>
            <th>By</th>
            <td>
              <a href="{{ url_for('view_user', username=pull_request.user.user) }}">
              {{ pull_request.user.user | avatar(16) | safe }}
              {{ pull_request.user.fullname }} ({{ pull_request.user.user }})
            </a>
            </td>
          </tr>
          <tr>
            <th>From</th>
            <td>
              <a href="{{ url_for('view_repo_branch',
                username=pull_request.project_from.user.user
                  if pull_request.project_from.is_fork else None,
                repo=pull_request.project_from.name,
                branchname=pull_request.branch_from)
              }}">
                {{ pull_request.project_from.fullname or pull_request.remote_git
                  }} ({{ pull_request.branch_from }})
              </a>
            </td>
          </tr>
          <tr>
            <th>To</th>
            <td>
              <a href="{{ url_for('view_repo_branch',
                username=pull_request.project.user.user
                  if pull_request.project.is_fork else None,
                repo=pull_request.project.name,
                branchname=pull_request.branch)
              }}">
                {{ pull_request.project.fullname }} ({{ pull_request.branch }})
              </a>
            </td>
          </tr>
          <tr>
            <th>Created</th>
            <td title="{{ pull_request.date_created.strftime('%b %d %Y %H:%M:%S') }}">
              {{ pull_request.date_created |humanize }}
            </td>
          </tr>
          <tr>
            <th>Assignee</th>
            <td>
              {% if authenticated and mergeform and pull_request.status == True %}
              <form method="POST" action="{{ url_for('.set_assignee_requests',
                  username=username, repo=repo.name, requestid=requestid) }}">
                <input value="{{ pull_request.assignee.username or '' }}"
                  name="user" id="assignee" placeholder="username" >
                {{ mergeform.csrf_token }}
                <input type="submit" class="submit positive button" value="Update">
              </form>
              {% else %}
              {{ pull_request.assignee.username or '' }}
              {% endif %}
            </td>
          </tr>
        </table>
      </div>
    </div>
    </section>
  </div>

  <div class="col-md-4">
    {% if pull_request.flags %}
    <div class="card" id="pr_flags">
      <div class="card-block">
        <table class="table table-hover table-striped m-b-0">
          <thead></thead>
          <tfoot></tfoot>
          <tbody>
            {% for flag in pull_request.flags %}
              <tr style="background-color: {{ flag.percent | toRGB }}">
                <td>{{ flag.percent }}%</td>
                <td><a href="{{ flag.url }}">{{ flag.username }}</a></td>
                <td>{{ flag.comment }}</td>
                <td title="{{ flag.date_created }}" class="right">
                  {{ flag.date_created | humanize }}
                </td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    </div>
    {% endif %}

  </div>
</div>
{% endif %}

<ul class="nav nav-pills nav-small">
  <li class="nav-item active" data-id="nav-diff"><a href="#request_diff">Diff</a></li>
  <li class="nav-item" data-id="nav-commits">
    <a href="#commit_list">
      <span class="hidden-sm-down">Commits&nbsp;</span>
      <span class="label label-default label-pill hidden-sm-down">
        {{ diff_commits|length }}
      </span>
    </a>
  </li>
</ul>

<div class="card" id="commit_list" style="display:none">
  <table class="table table-hover table-striped m-b-0">
    <thead>
    </thead>
    <tfoot>
    </tfoot>
    <tbody>
        {% for commit in diff_commits %}
        <tr>
          <td class="commitid">{{ commit.oid.hex|short }}</td>
          <td>
            {% if pull_request and pull_request.status and pull_request.project_from.is_fork %}
            <a href="{{ url_for('view_commit', username=pull_request.project_from.user.user,
                      repo=pull_request.project_from.name, commitid=commit.oid.hex)}}">
            {% elif pull_request and pull_request.remote %}
            <a>
            {% else %}
            <a href="{{ url_for('view_commit', username=username,
                          repo=repo.name, commitid=commit.oid.hex)}}">
            {% endif %}
              {{ commit.message.strip().split('\n')[0] }}
              <div class="commit_msg_txt commit_message_body" id="commit_msg_{{ loop.index }}">
                  {{ commit.message.strip().split('\n')[2:] | join('\n') }}
              </div>
            </a>
          </td>
          {% if commit.message.strip().split('\n') | length > 1 %}
          <td class="commit_msg_btn" data-id="{{ loop.index }}">more</td>
          {% else %}
          <td></td>
          {% endif %}
          <td class="commitdate" title="{{ commit.commit_time|format_ts }}">
            {{ commit.commit_time|humanize }}</td>
        </tr>
        {% else %}
        <tr><td class="error"> No commits found </td></tr>
        {% endfor %}
    </tbody>
  </table>
</div>

<div class="request_diff" id="request_diff">

  {% if authenticated and pull_request %}
  <form action="{{ url_for('.pull_request_drop_comment', username=username,
      repo=repo.name, requestid=requestid) }}" method="post" class="icon"
      onsubmit="return try_async_comment(this, null)" >
  {% endif %}

    {% if diff %}
    {% for patch in diff %}
    <section class="commit_diff">
    <div class="card clearfix">
      <div class="card-header">
          <h3>
            {%- if patch | hasattr('new_file_path') -%}
              {%- if patch.new_file_path == patch.old_file_path -%}
              {{ patch.new_file_path }}
              {%- else -%}
              {{ patch.new_file_path }} (Renamed from "{{ patch.old_file_path }}")
              {%- endif -%}
            {%- elif patch | hasattr('delta') -%}
              {%- if patch.delta.new_file.path == patch.delta.old_file.path -%}
              {{ patch.delta.new_file.path }}
              {%- else -%}
              {{ patch.delta.new_file.path }} (Renamed from "{{ patch.delta.old_file.path }}")
              {%- endif -%}
            {%- endif -%}
          </h3>
            {% if patch | hasattr('additions') %}
            {# Version of pygit2 -0.21.4 -- F21/EL7 #}
            {% if (patch.additions + patch.deletions) %}
              <span style="width: {{ (100.0 * patch.additions / (patch.additions + patch.deletions))|round|int }}%">
                {% if patch.additions > 0 %}+{{ patch.additions }}{% endif %}
                {% if patch.deletions > 0 %}-{{ patch.deletions }}{% endif %}
              </span>
            {% endif %}
            {% else %}
            {# Version of pygit2 -0.23.0 -- F23 #}
            {% if (patch.line_stats[1] + patch.line_stats[2]) %}
              <span style="width: {{ (100.0 * patch.line_stats[1] / (patch.line_stats[1] + patch.line_stats[2]))|round|int }}%">
                {% if patch.line_stats[1] > 0 %}+{{ patch.line_stats[1] }}{% endif %}
                {% if patch.line_stats[2] > 0 %}-{{ patch.line_stats[2] }}{% endif %}
              </span>
            {% endif %}
            {% endif %}

            {% if pull_request %}
            <a class="button view headerlink pull-xs-right" href="{{
                url_for(
                    'view_file',
                    username=pull_request.project_from.user.username
                        if pull_request.project_from.is_fork else None,
                    repo=pull_request.project_from.name,
                    identifier=pull_request.branch_from,
                    filename=patch.new_file_path) }}"
            {% else %}
            <a class="button view headerlink pull-xs-right" href="{{
                url_for(
                    'view_file',
                    username=username,
                    repo=repo.name,
                    identifier=branch_from,
                    filename=patch.new_file_path) }}"
            {% endif %}
            {% if patch |hasattr('new_id') %}
            title="View file as of {{ patch.new_id|short }}">View</a>
            {% elif patch |hasattr('delta') %}
            title="View file as of {{ patch.delta.new_file.id|short }}">View</a>
            {% else %}
            title="View file as of {{ patch.new_oid|short }}">View</a>
            {% endif %}
      </div>
      <div class="card-block">
    {% autoescape false %}
        {{ patch | patch_to_diff | html_diff | format_loc(
                filename=patch.new_file_path,
                commit=patch.new_id or patch.new_oid,
                prequest=pull_request,
                index=loop.index)}}
    {% endautoescape %}
      </div>
    </div>
    </section>
    {% endfor %}
    {% endif %}

  {% if authenticated and pull_request %}
  {{ mergeform.csrf_token }}
  </form>
  {% endif %}


  {% if pull_request %}
  <section class="request_comment" id="request_comment">
    {% if pull_request.discussion %}
      <form action="{{ url_for('.pull_request_drop_comment', username=username,
          repo=repo.name, requestid=requestid) }}" method="post"
          onsubmit="return try_async_comment(this, null)">

      {% for comment in pull_request.discussion %}
        {% if not comment.commit_id %}
          {{ show_comment(comment, comment.id, repo, username,
                          requestid, form, repo_admin) }}
        {% endif %}
      {% endfor %}
      {{ mergeform.csrf_token }}
      </form>
    {% endif %}
    </section>
  {% endif %}

  <div class="card">
    <div class="card-header">

      <section class="request_comment add_comment">
      {% if authenticated and mergeform and pull_request %}


        <form action="{{ url_for(
          'pull_request_add_comment', username=username, repo=repo.name,
          requestid=requestid) }}"
          method="post" onsubmit="return try_async_comment(this, false)">
        {{ mergeform.csrf_token }}

        <fieldset class="form-group">
          <label for="comment"><strong>Add new comment</strong></label>
          <small class="text-muted pull-xs-right">
            <span class="btn btn-sm btn-secondary inactive"
              aria-pressed="false" id="previewinmarkdown">Preview
            </span>
          </small>
          <textarea class="form-control" rows=8 id="comment" name="comment"
          placeholder="Enter your comment here"></textarea>

          <div id="preview">
          </div>
        </fieldset>

          <div>
            <input type="submit" class="btn btn-primary" value="Update Issue">
            <input type="button" class="btn btn-secondary" value="Clear" id="clear_comment" />
          </div>
      </form>
      {% else %}
        <p><a href="{{ url_for('auth_login', next=request.url) }}">Login</a> to comment on this ticket.</p>
      {% endif %}
      </section>

    </div>
  </div>
</div>
</div>

</div>
{% endblock %}

{% block jscripts %}
{{ super() }}
<script type="text/javascript"
    src="{{ url_for('static', filename='emoji/jquery.textcomplete.min.js') }}">
</script>
<script type="text/javascript"
    src="{{ url_for('static', filename='emoji/emojione.min.js') }}">
</script>
<script src="{{ url_for('static', filename='selectize.min.js') }}" type="text/javascript"> </script>

<script type="text/javascript">
function cancel_edit_btn() {
  $(".cancel").unbind();
  $( ".cancel" ).click(
    function() {
      item = $(this).closest('section');
      $(item.parent().find('.issue_comment')).show();
      $(item.parent().find('.issue_actions')).show();
      $(item.parent().find('.edit_comment')).remove();
      return false;
    }
  );
};

function setup_edit_btns() {
  $(".edit_btn").unbind();
  $(".edit_btn").click(function() {
    var commentid = $( this ).attr('data-comment');
    var _url = '{{ request.base_url }}' + '/comment/' + commentid + '/edit';
    $.ajax({
        url: _url + '?js=1',
        type: 'GET',
        dataType: 'html',
        success: function(res) {
          var el = $('#comment-' + commentid);
          var sec = el.parent().find('.issue_comment');
          $(sec).hide();
          el.parent().find('.issue_actions').hide();
          $(sec).after(res);
          cancel_edit_btn();
        },
        error: function() {
          alert('Could not make edit work');
        }
    });
    return false;
  });
};

function setup_reply_btns() {
  $(".reply").unbind();
  $( ".reply" ).click(
    function() {
      var _section = $(this).closest('.card-block');
      var _comment = _section.find('.comment_body');
      var _text = _comment.text().split("\n");
      var _output = new Array();
      for (var cnt=0; cnt < _text.length; cnt++) {
          _output[cnt] = '> ' + _text[cnt];
      }
      $( "#comment" ).val(_output.join("\n"));
    }
  );
};

 $(document).ready(function() {

  $( ".commit_msg_txt" ).hide();
  $( ".commit_msg_btn" ).click(function() {
    var msgid = $( this ).attr('data-id');
    $( '#commit_msg_' + msgid).toggle();
  });

  var folder = '{{url_for("static", filename="emoji/png/") }}';
  var json_url = '{{ url_for("static", filename="emoji/emoji_strategy.json") }}';


  var branchselect = $('#branch_select').selectize({
      create: false,
      sortField: 'text',
      allowEmptyOption: false,
      onChange:     function(value) {
        if (value != ""){
        var sel = $('#branch_select');
        var final_url = "{{ url_for('.new_request_pull', username=username,
          repo=repo.name, branch_from=branch_from,
          branch_to='--') }}";
        final_url = final_url.replace('--', sel.val());
        window.location.href = final_url;
        }
    }
  });

  $( ".tabs" ).tabs({
    activate: function( event, ui ) {
      var _title = ui.newPanel.attr('id');
      if ($.inArray( _title, [ "request_diff", "commit_list" ] ) >= 0) {
        window.location.hash = _title;
      }
    }
  });

{% if pull_request %}
{# These lines are only for existing pull-requests, not new ones #}

  emoji_complete(json_url, folder);

  $('#cancel_pr').click(function(){
    return window.confirm("Are you sure you want to close this requested pull?");
  });

  $( ".code_table tr" ).hover(
    function() {
      $( this ).find( ".prc_img" ).show().width(13);
    }, function() {
      $( this ).find( ".prc_img" ).hide();
    }
  );

  $( ".prc" ).click(
    function() {
      var row = $( this ).attr('data-row');
      var commit = $( this ).attr('data-commit');
      var filename = $( this ).attr('data-filename');
      var url = "{{ url_for(
        'pull_request_add_comment', username=username, repo=repo.name,
        requestid=requestid, commit='', filename='', row='') }}".slice(0, -2);
      url = url + commit + '/' + filename + '/' + row;
      var rowid = $(this).prev().find('a').attr('id');
      var table = $( this ).parent().parent();
      var nextid = rowid.replace('_' + row, '_' + (Number(row) + 1));
      var next_row = table.find('#' + nextid).parent().parent();
      {# If we're at the last row, we won't be able to find the next_row
         therefore we need to add it manually #}
      if (next_row.length == 0) {
        table.find("tr:last").after(
            '<tr><td><a id="' + nextid + '"></a></td></tr>');
        next_row = table.find('#' + nextid).parent().parent();
      }
      if (next_row.prev().find('.pr_comment_form').length == 0){
        $.get( url , function( data ) {
          next_row.before(
            '<tr><td></td><td colspan="2" class="pr_comment_form">' + data + '</td></tr>' );
          cancel_edit_btn();
          emoji_complete(json_url, folder);
        });
      } else {
        next_row.prev().find('.pr_comment_form').parent().remove();
      }
    }
  );

  $( ".nav-item" ).click(
    function() {
      var header = $( this ).attr('data-id');
      if (header == 'nav-diff') {
        $($(this).parent().children()[1]).removeClass('active');
        $(this).addClass('active');
        $('#commit_list').hide();
        $('.commit_diff').show();
      } else {
        $($(this).parent().children()[0]).removeClass('active');
        $(this).addClass('active');
        $('.commit_diff').hide();
        $('#commit_list').show();
      }
    }
  );

  $( "#clear_comment").click(
    function() {
      $( "#comment" ).val("");
    }
  );

  $( "#preview" ).hide();
  $( "#previewinmarkdown" ).click(
    function(event, ui) {
        if ($( "#previewinmarkdown" ).hasClass("inactive")){
          var _text = $( "#comment" ).val();
          var _url = "{{ url_for('markdown_preview') }}";
            $.ajax({
                url: _url ,
                type: 'POST',
                data: {
                  content: _text,
                  csrf_token: "{{ mergeform.csrf_token.current_token }}",
                },
                dataType: 'html',
                success: function(res) {
                    $( "#preview" ).html(res);
                    $( "#previewinmarkdown" ).removeClass("inactive");
                    $( "#previewinmarkdown" ).addClass("active");
                    $( "#comment" ).hide();
                    $( "#preview" ).show();
                },
                error: function() {
                    alert('Unable to generate preview!');
                }
            });
            return false;
          } else if ($( "#previewinmarkdown" ).hasClass("active")){
            $( "#previewinmarkdown" ).addClass("inactive");
            $( "#previewinmarkdown" ).removeClass("active");
            $( "#comment" ).show();
            $( "#preview" ).hide();
          }
    }
  );

  setup_edit_btns();
  setup_reply_btns();

  var cur_hash = null;

  highlight_comment = function() {
    var _hash = window.location.hash;
    console.log(_hash);
    if (_hash == '#commit_list' || _hash == '#request_diff') {
        return false;
    }
    if (_hash != cur_hash) {
      $( cur_hash ).css(
        "background", "linear-gradient(to bottom, #ededed 0%, #fff 100%)"
      );
    };
    cur_hash = _hash;
    if ( _hash ) {
      $( _hash ).css(
        "background", "linear-gradient(to bottom, #eded98 0%, #fff 100%)"
      );
    };
    return false;
  };

  $(window.onload=highlight_comment());
  $(window).on('hashchange', highlight_comment);

  $(".comment_body").each(function(ind, obj) {
    var source = $(obj).html();
    var preview = emojione.toImage(source);
    $(obj).html(preview);
  });
  $(".pr_comment").each(function(ind, obj) {
    var source = $(obj).html();
    var preview = emojione.toImage(source);
    $(obj).html(preview);
  });

  {% if pull_request.status == 'Open' %}
  $(function(){
    $.ajax({
      url: '{{ url_for("internal_ns.mergeable_request_pull") }}' ,
      type: 'POST',
      data: {
        requestid: "{{ pull_request.uid }}",
        csrf_token: "{{ mergeform.csrf_token.current_token }}",
      },
      dataType: 'json',
      success: function(res) {
        var _obj = $('#pr_flags').find('tbody');
        _obj.append($('<tr><th>PR Status:</th><td><span class="'+res.code+'" title="'
          +res.message+'"></span>'+res.short_code+'</td></tr>'));
      },
      error: function(res) {
        var _obj = $('#pr_flags').find('tbody');
        if (res.responseJSON.message) {
          _obj.append(
            $('<tr><th>PR Status:</th><td><span class="'+ res.responseJSON.code +'" title="'
            + res.responseJSON.message +'"></span>'+ res.responseJSON.message +'</td></tr>'));
        } else {
          _obj.append(
            $('<tr><th>PR Status:</th><td><span class="'+ res.responseJSON.code +'"></span>'
              + res.responseJSON.message +'</td></tr>'));
        }
      }
    });
    return false;
  });
  {% endif %}

{% endif %}

 });

{% if authenticated and pull_request %}
$('#assignee').selectize({
  valueField: 'user',
  labelField: 'user',
  searchField: 'user',
  maxItems: 1,
  create: false,
  load: function(query, callback) {
    if (!query.length) return callback();
    $.getJSON(
      "{{ url_for('api_ns.api_users') }}", {
        pattern: query.term
      },
      function( data ) {
        callback( data.users.map(function(x) { return { user: x }; }) );
      }
    );
  }
});

function try_async_comment(form, inline) {
  $(form).off('submit');
  var _data = $(form).serialize();
  var btn = $(document.activeElement);
  if (btn[0].name == 'drop_comment'){
    _data += '&drop_comment=' + btn[0].value;
    return true;
  }
  if (!sse) {
    console.log('no sse, submitting');
    form.submit();
    return false;
  }
  $.post( form.action + "?js=1", $(form).serialize() )
    .done(function(data) {
      if(data == 'ok') {
        $('#comment').val('');
        /* We have submitted the comment correctly */
        var item = $('#comment_block').closest('section');
        if (!$(item.parent().children()[1]).is(':visible')){
          $(item.parent().children()[1]).show()
        }
        item.remove();
      } else {
        // Make the browser submit the form sync
        form.submit();
      }
    })
    .fail(function() {
      // Make the browser submit the form sync
      form.submit();
    })
  $(form).on('submit');
  return false;
};
{% endif %}

</script>


{% if config['EVENTSOURCE_SOURCE'] and pull_request %}
<script type="text/javascript"
  src="{{ url_for('static', filename='request_ev.js') }}"></script>

<script type="text/javascript">
var source = null;
var sse = true;
if (!!window.EventSource) {
  source = new EventSource('{{ config["EVENTSOURCE_SOURCE"]
    + request.script_root + request.path }}');
  source.addEventListener('error', function(e) {
    sse = false;
  }, false);
}

window.onbeforeunload = function() {
    source.close()
};

source.addEventListener('message', function(e) {
  console.log(e.data);
  var data = $.parseJSON(e.data);
  process_event(data, "{{ request.uid }}");
  setup_edit_btns();
  setup_reply_btns();
  }, false);
</script>
{% endif %}

{% endblock %}