## -*- coding: utf-8 -*-
## usage:
## <%namespace name="comment" file="/changeset/changeset_file_comment.html"/>
## ${comment.comment_block(co)}
##
<%def name="comment_block(co)">
  <div class="comment" id="comment-${co.comment_id}" line="${co.line_no}">
    <div class="comment-prev-next-links"></div>
    <div class="comment-wrapp">
      <div class="meta">
          <div style="float:left">
               ${h.gravatar(co.author.email, size=20)}
          </div>
          <div class="user">
              ${co.author.full_name_and_username}
          </div>

          <span>
              ${h.age(co.modified_at)}
              %if co.pull_request:
                ${_('on pull request')}
                <a href="${co.pull_request.url()}">"${co.pull_request.title or _("No title")}"</a>
              %else:
                ${_('on this changeset')}
              %endif
              <a class="permalink" href="${co.url()}">&para;</a>
          </span>

          %if h.HasPermissionAny('hg.admin')() or h.HasRepoPermissionAny('repository.admin')(c.repo_name) or co.author.user_id == c.authuser.user_id:
            <div onClick="confirm('${_("Delete comment?")}') && deleteComment(${co.comment_id})" class="buttons delete-comment btn btn-mini">${_('Delete')}</div>
          %endif
      </div>
      <div class="text">
        %if co.status_change:
           <div class="rst-block automatic-comment">
             <p>
               <span title="${_('Changeset status')}" class="changeset-status-lbl">${_("Status change")}: ${co.status_change[0].status_lbl}</span>
               <span class="changeset-status-ico"><i class="icon-circle changeset-status-${co.status_change[0].status}"></i></span>
             </p>
           </div>
        %endif
        %if co.text:
          ${h.rst_w_mentions(co.text)|n}
        %endif
      </div>
    </div>
  </div>
</%def>


## expanded with .format(f_path, line_no)
## TODO: don't assume line_no is globally unique ...
<%def name="comment_inline_form()">
<div id='comment-inline-form-template' style="display:none">
  <div class="ac">
  %if c.authuser.username != 'default':
    ${h.form('#', class_='inline-form')}
      <div id="edit-container_{1}" class="clearfix">
        <div class="comment-help">${_('Commenting on line {1}.')}
          ${(_('Comments parsed using %s syntax with %s support.') % (
                 ('<a href="%s">RST</a>' % h.url('rst_help')),
                   ('<span style="color:#577632" class="tooltip" title="%s">@mention</span>' % _('Use @username inside this text to notify another user'))
               )
            )|n
           }
        </div>
        <div class="mentions-container" id="mentions_container_{1}"></div>
        <textarea id="text_{1}" name="text" class="comment-block-ta yui-ac-input"></textarea>
      </div>
      <div id="preview-container_{1}" class="clearfix" style="display:none">
        <div class="comment-help">
            ${_('Comment preview')}
        </div>
        <div id="preview-box_{1}" class="preview-box"></div>
      </div>
      <div class="comment-button">
        <div class="submitting-overlay">${_('Submitting ...')}</div>
        <input type="hidden" name="f_path" value="{0}">
        <input type="hidden" name="line" value="{1}">
        ${h.submit('save', _('Comment'), class_='btn btn-small save-inline-form')}
        ${h.reset('hide-inline-form', _('Cancel'), class_='btn btn-small hide-inline-form')}
        <div id="preview-btn_{1}" class="preview-btn btn btn-small">${_('Preview')}</div>
        <div id="edit-btn_{1}" class="edit-btn btn btn-small" style="display:none">${_('Edit')}</div>
      </div>
    ${h.end_form()}
  %else:
      ${h.form('')}
      <div class="clearfix">
          <div class="comment-help">
            ${_('You need to be logged in to comment.')} <a href="${h.url('login_home', came_from=request.path_qs)}">${_('Login now')}</a>
          </div>
      </div>
      <div class="comment-button">
      ${h.reset('hide-inline-form', _('Hide'), class_='btn btn-small hide-inline-form')}
      </div>
      ${h.end_form()}
  %endif
  </div>
</div>
</%def>


## show comment count as "x comments (y inline, z general)"
<%def name="comment_count(inline_cnt, general_cnt)">
    ${'%s (%s, %s)' % (
        ungettext("%d comment", "%d comments", inline_cnt + general_cnt) % (inline_cnt + general_cnt),
        ungettext("%d inline", "%d inline", inline_cnt) % inline_cnt,
        ungettext("%d general", "%d general", general_cnt) % general_cnt
    )}
    <span class="firstlink"></span>
</%def>


## generate inline comments and the main ones
<%def name="generate_comments()">
<div class="comments">
  %for f_path, lines in c.inline_comments:
    %for line_no, comments in lines.iteritems():
      <div class="comments-list-chunk" data-f_path="${f_path}" data-line_no="${line_no}" data-target-id="${h.safeid(h.safe_unicode(f_path))}_${line_no}">
        %for co in comments:
            ${comment_block(co)}
        %endfor
      </div>
    %endfor
  %endfor

  <div class="comments-number">
    ${comment_count(c.inline_cnt, len(c.comments))}
  </div>

      <div class="comments-list-general">
        %for co in c.comments:
            ${comment_block(co)}
        %endfor
      </div>
</div>
</%def>

## MAIN COMMENT FORM
<%def name="comments(post_url, cur_status, is_pr=False, change_status=True)">

<div class="comments">
    %if c.authuser.username != 'default':
    <div class="comment-form ac">
      ${h.form(post_url, id="main_form")}
        <div id="edit-container" class="clearfix">
            <div class="comment-help">
                ${(_('Comments parsed using %s syntax with %s support.') % (('<a href="%s">RST</a>' % h.url('rst_help')),
                  '<span style="color:#577632" class="tooltip" title="%s">@mention</span>' %
                  _('Use @username inside this text to notify another user.')))|n}
            </div>
            <div class="mentions-container" id="mentions_container"></div>
            ${h.textarea('text', class_="comment-block-ta")}
            %if change_status:
              <div id="status_block_container" class="status-block">
                %if is_pr:
                  ${_('Vote for pull request status')}:
                %else:
                  ${_('Set changeset status')}:
                %endif
                <input type="radio" class="status_change_radio" name="changeset_status" id="changeset_status_unchanged" value="" checked="checked" />
                <label for="changeset_status_unchanged">
                  ${_('No change')}
                </label>
                %for status,lbl in c.changeset_statuses:
                    <span>
                        <input type="radio" class="status_change_radio" name="changeset_status" id="${status}" value="${status}">
                        <label for="${status}"><i class="icon-circle changeset-status-${status}" /></i>${lbl}</label>
                    </span>
                %endfor

                %if is_pr and ( \
                    h.HasPermissionAny('hg.admin')() or h.HasRepoPermissionAny('repository.admin')(c.repo_name) \
                    or c.pull_request.owner.user_id == c.authuser.user_id):
                  <input id="save_close" type="checkbox" name="save_close">
                  <label id="save_close_label" for="save_close">${_("Close")}</label>
                %endif
              </div>
            %endif
        </div>

        <div id="preview-container" class="clearfix" style="display:none">
            <div class="comment-help">
                ${_('Comment preview')}
            </div>
            <div id="preview-box" class="preview-box"></div>
        </div>

        <div class="comment-button">
            ${h.submit('save', _('Comment'), class_="btn")}
            <div id="preview-btn" class="preview-btn btn">${_('Preview')}</div>
            <div id="edit-btn" class="edit-btn btn" style="display:none">${_('Edit')}</div>
        </div>
      ${h.end_form()}
    </div>
    %endif
</div>

<script>

$(document).ready(function () {
   MentionsAutoComplete($('#text'), $('#mentions_container'), _USERS_AC_DATA, _GROUPS_AC_DATA);

   $(window).on('beforeunload', function(){
      if($('.comment-inline-form textarea[name=text]').size() ||
         $('textarea#text').val()) {
         // this message will not be displayed on all browsers
         // (e.g. some versions of Firefox), but the user will still be warned
         return 'There are uncommitted comments.';
      }
   });

   $('form#main_form').submit(function(){
      // if no open inline forms, disable the beforeunload check - it would
      // fail in the check for the textarea we are about to submit
      if(!$('.form-open').size()){
          $(window).off('beforeunload');
      }
   });

   $('#preview-btn').click(function(){
       var _text = $('#text').val();
       if(!_text){
           return;
       }
       var post_data = {'text': _text};
       $('#preview-box').addClass('unloaded');
       $('#preview-box').html(_TM['Loading ...']);
       $('#edit-container').hide();
       $('#edit-btn').show();
       $('#preview-container').show();
       $('#preview-btn').hide();

       var url = pyroutes.url('changeset_comment_preview', {'repo_name': '${c.repo_name}'});
       ajaxPOST(url,post_data,function(html){
           $('#preview-box').html(html);
           $('#preview-box').removeClass('unloaded');
       });
   });
   $('#edit-btn').click(function(){
       $('#edit-container').show();
       $('#edit-btn').hide();
       $('#preview-container').hide();
       $('#preview-btn').show();
   });

});
</script>
</%def>
