<!--!  Copyright (C) 2010-2014 Edgewall Software

  This software is licensed as described in the file COPYING, which
  you should have received as part of this distribution. The terms
  are also available at http://trac.edgewall.com/license.html.

  This software consists of voluntary contributions made by many
  individuals. For the exact contribution history, see the revision
  history and logs, available at http://trac.edgewall.org/.

Render a ticket comment.

Arguments:
 - change: the change data
 - hide_buttons=False: hide all buttons (Edit, Reply)
 - cnum_edit=None: the comment number being edited
 - edited_comment: the current value of the comment editor
 - cnum_hist=None: the comment number for which to show a historical content
 - can_append=False: True if the user is allowed to append to tickets
 - has_edit_comment=False: True if the user is allowed to edit all comments
 - preview=False: True if rendering a change preview
-->
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude"
      xmlns:i18n="http://genshi.edgewall.org/i18n"
      py:with="cnum = change.get('cnum'); hide_buttons = value_of('hide_buttons', False);
               cnum_edit = value_of('cnum_edit'); cnum_hist = value_of('cnum_hist');
               can_append = value_of('can_append', False); has_edit_comment = value_of('has_edit_comment', False);
               preview = value_of('preview', False);
               can_edit_comment = has_edit_comment or (authname and authname != 'anonymous'
                                                       and authname == change.author);
               show_editor = can_edit_comment and str(cnum) == cnum_edit;
               show_history = str(cnum) == cnum_hist;
               max_version = max(change.comment_history) if change.comment_history else 0;
               comment_version = as_int(cversion, max_version, 0, max_version) if show_history else max_version;
               show_buttons = not hide_buttons and not show_editor and comment_version == max_version"
      py:strip="">
  <py:def function="commentref(prefix, cnum, cls=None)">
    <a href="#comment:$cnum" class="$cls">$prefix$cnum</a>
  </py:def>
  <h3 class="change" id="${'comment:%s' % cnum if 'cnum' in change else None}">
    <span class="threading"
          py:with="change_replies = replies.get(str(cnum), []) if 'cnum' in change else []">
      <span py:if="'cnum' in change" class="cnum">${commentref('comment:', cnum)}</span>
      <span py:if="'replyto' in change" class="trac-in-reply-to">
        in reply to: ${commentref('&nbsp;', change.replyto, 'in-reply-to')}
        <py:if test="change_replies">; </py:if>
      </span>
      <span py:if="change_replies" class="trac-follow-ups">
        <i18n:choose numeral="len(change_replies)">
          <span i18n:singular="">follow-up:</span>
          <span i18n:plural="">follow-ups:</span>
        </i18n:choose>
        <py:for each="reply in change_replies">
          ${commentref('&nbsp;', reply, 'follow-up')}
        </py:for>
      </span>
    </span>
    <py:choose>
      <py:when test="'date' in change">
        <i18n:msg params="date, author">Changed ${pretty_dateinfo(change.date)} by ${authorinfo(change.author)}</i18n:msg>
      </py:when>
      <py:otherwise>
        <i18n:msg params="author">Changed by ${authorinfo(change.author)}</i18n:msg>
      </py:otherwise>
    </py:choose>
    <span py:if="preview or show_editor" class="trac-loading"/>
  </h3>

  <div class="trac-change-panel">

    <!--! Per-change controls -->
    <div py:if="show_buttons" class="trac-ticket-buttons">
      <form py:if="'cnum' in change and can_append" id="reply-to-comment-${cnum}"
            method="get" action="#comment">
        <div class="inlinebuttons">
          <input type="hidden" name="replyto" value="${cnum}"/>
          <input type="submit" value="${captioned_button('↳', _('Reply'))}"
                 title="${_('Reply to comment %(cnum)s', cnum=cnum)}" />
        </div>
      </form>
      <form py:if="'cnum' in change and can_edit_comment" id="edit-comment-${cnum}"
            method="get" action="#comment:${cnum}">
        <div class="inlinebuttons">
          <input type="hidden" name="cnum_edit" value="${cnum}"/>
          <input type="submit" value="${captioned_button('✎', _('Edit'))}"
                 title="${_('Edit comment %(cnum)s', cnum=cnum)}" />
        </div>
      </form>
    </div>

    <!--! Property changes -->
    <table py:if="change.fields" class="changes">
      <tr py:for="field_name, field in sorted(change.fields.iteritems(), key=lambda item: item[1].label.lower())"
          class="trac-field-${field_name}${' trac-conflict' if preview and field_name in conflicts else None}">
        <th class="trac-field-${field_name}" i18n:msg="field">${field.label}:</th>
        <td py:choose="">
          <py:when test="field_name == 'attachment'"><i18n:msg params="name">
            <a href="${href.attachment('ticket', ticket.id, field.new)}"><em>${field.new
              }</em></a><a href="${href('raw-attachment', 'ticket', ticket.id, field.new)}"
                           title="Download" class="trac-rawlink">&#8203;</a>
            added
          </i18n:msg></py:when>
          <py:when test="'rendered' in field">${field.rendered}</py:when>
          <py:when test="field.old and field.new">
            <span class="trac-field-old">${field.old}</span> &rarr;
            <span class="trac-field-new">${field.new}</span>
          </py:when>
          <py:when test="not field.old and field.new">
            &rarr; <span class="trac-field-new">${field.new}</span>
          </py:when>
          <py:otherwise>
            <span class="trac-field-deleted">${field.old}</span>
          </py:otherwise>
          <py:if test="preview and field.by == 'user'">
            (<button py:with="old = ticket.get_default(field_name) if field.old is empty else field.old"
                     type="submit" name="revert_$field_name" class="trac-revert"
                     title="Revert this change">revert<div id="revert-$field_name">${
              '0' if 'cc_update' in field else old}</div></button>)
          </py:if>
        </td>
      </tr>
    </table>

  </div>

  <!--! Comment editor -->
  <form py:if="show_editor" id="trac-comment-editor" method="post"
        action="${href.ticket(ticket.id) + '#comment:%d' % cnum}">
    <div>
      <textarea name="edited_comment" class="wikitext trac-fullwidth trac-resizable trac-autofocus" rows="10" cols="78">
${edited_comment if edited_comment is not None else change.comment}</textarea>
      <input type="hidden" name="cnum_edit" value="${cnum}"/>
    </div>
    <div class="buttons">
      <input type="submit" name="preview_comment" value="${_('Preview')}"
             title="${_('Preview changes to comment %(cnum)s', cnum=cnum)}"/>
      <input type="submit" name="edit_comment" value="${_('Submit changes')}"
             title="${_('Submit changes to comment %(cnum)s', cnum=cnum)}"
             class="trac-disable-on-submit" />
      <input type="submit" name="cancel_comment" value="${_('Cancel')}"
             title="Cancel comment edit"/>
    </div>
  </form>

  <!--! Comment display -->
  <py:choose>
    <div py:when="str(cnum) == cnum_edit"
         py:with="text = edited_comment if edited_comment is not None else change.comment"
         class="comment searchable ticketdraft" style="${'display: none' if not text else None}" xml:space="preserve">
      ${wiki_to_html(context, text, escape_newlines=preserve_newlines)}
    </div>
    <div py:when="show_history" class="comment searchable" xml:space="preserve">
      ${wiki_to_html(context, change.comment_history[comment_version].comment, escape_newlines=preserve_newlines)}
    </div>
    <div py:when="change.comment" class="comment searchable" xml:space="preserve">
      ${wiki_to_html(context, change.comment, escape_newlines=preserve_newlines)}
    </div>
  </py:choose>
  <div py:if="not show_editor and len(change.comment_history) > 1" py:choose=""
       class="trac-lastedit ${'trac-shade' if comment_version != max_version else None}">
    <i18n:msg params="version, date, author" py:when="comment_version != max_version">
        Version ${comment_version}, edited ${pretty_dateinfo(change.comment_history[comment_version].date)}
        by ${authorinfo(change.comment_history[comment_version].author)}
    </i18n:msg>
    <i18n:msg params="date, author" py:otherwise="">
        Last edited ${pretty_dateinfo(change.comment_history[comment_version].date)}
        by ${authorinfo(change.comment_history[comment_version].author)}
    </i18n:msg>
    <py:if test="comment_version > 0">
      (<a href="${href.ticket(ticket.id, cnum_hist=cnum, cversion=comment_version - 1)
                 }#comment:${cnum}">previous</a>)
    </py:if>
    <py:if test="comment_version &lt; max_version">
      (<a href="${href.ticket(ticket.id, cnum_hist=cnum, cversion=comment_version + 1)
                 }#comment:${cnum}">next</a>)
    </py:if>
    <py:if test="comment_version > 0">
      (<a href="${href.ticket(ticket.id, action='comment-diff', cnum=cnum,
                              version=comment_version)}">diff</a>)
    </py:if>
  </div>
</html>
