<!--!  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">
    <span class="threading"
          py:with="change_replies = replies.get(str(cnum), []) if 'cnum' in change else []">
      <span py:if="'cnum' in change" id="comment:$cnum" class="cnum">${commentref('comment:', cnum)}</span>
      <py:if test="'replyto' in change">
        in reply to: ${commentref('&uarr;&nbsp;', change.replyto)}
        <py:if test="change_replies">; </py:if>
      </py:if>
      <py:if test="change_replies">
        <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('&darr;&nbsp;', reply, 'follow-up')}
        </py:for>
      </py:if>
    </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 py:if="show_buttons" class="trac-ticket-buttons">
    <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>
    <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>
  </div>
  <ul py:if="change.fields" class="changes">
    <li 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}">
      <strong class="trac-field-${field_name}">${field.label}</strong>
      <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"><i18n:msg params="old, new">
          changed from <em>${field.old}</em> to <em>${field.new}</em>
        </i18n:msg></py:when>
        <py:when test="not field.old and field.new"><i18n:msg params="value">
          set to <em>${field.new}</em>
        </i18n:msg></py:when>
        <py:otherwise><i18n:msg params="value">
          <em>${field.old}</em> deleted
        </i18n:msg></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>
      </py:choose>
    </li>
  </ul>
  <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" 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>
  <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>
