changeset 5667:7834f845505a

comments: use inline comment infrastructure for general comments too
author Mads Kiilerich <madski@unity3d.com>
date Wed, 20 Jan 2016 01:47:11 +0100
parents b3ddd87f214f
children 2ed9ddab042f
files kallithea/controllers/changeset.py kallithea/public/css/style.css kallithea/public/js/base.js kallithea/templates/changeset/changeset.html kallithea/templates/changeset/changeset_file_comment.html kallithea/templates/pullrequests/pullrequest_show.html kallithea/tests/functional/test_changeset_comments.py
diffstat 7 files changed, 89 insertions(+), 99 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/changeset.py	Wed Jan 20 01:47:11 2016 +0100
+++ b/kallithea/controllers/changeset.py	Wed Jan 20 01:47:11 2016 +0100
@@ -175,6 +175,9 @@
 # Could perhaps be nice to have in the model but is too high level ...
 def create_comment(text, status, f_path, line_no, revision=None, pull_request_id=None, closing_pr=None):
     """Comment functionality shared between changesets and pullrequests"""
+    f_path = f_path or None
+    line_no = line_no or None
+
     comment = ChangesetCommentsModel().create(
         text=text,
         repo=c.db_repo.repo_id,
@@ -202,6 +205,7 @@
         c.user_groups_array = repo_model.get_user_groups_js()
 
     def _index(self, revision, method):
+        c.pull_request = None
         c.anchor_url = anchor_url
         c.ignorews_url = _ignorews_url
         c.context_url = _context_url
@@ -365,6 +369,8 @@
                                    'repository.admin')
     @jsonify
     def comment(self, repo_name, revision):
+        assert request.environ.get('HTTP_X_PARTIAL_XHR')
+
         status = request.POST.get('changeset_status')
         text = request.POST.get('text', '').strip()
 
@@ -379,9 +385,7 @@
         # get status if set !
         if status:
             # if latest status was from pull request and it's closed
-            # disallow changing status !
-            # dont_allow_on_closed_pull_request = True !
-
+            # disallow changing status ! RLY?
             try:
                 ChangesetStatusModel().set_status(
                     c.db_repo.repo_id,
@@ -389,25 +393,18 @@
                     c.authuser.user_id,
                     c.comment,
                     revision=revision,
-                    dont_allow_on_closed_pull_request=True
+                    dont_allow_on_closed_pull_request=True,
                 )
             except StatusChangeOnClosedPullRequestError:
-                log.debug(traceback.format_exc())
-                msg = _('Changing status on a changeset associated with '
-                        'a closed pull request is not allowed')
-                h.flash(msg, category='warning')
-                raise HTTPFound(location=h.url('changeset_home', repo_name=repo_name,
-                                      revision=revision))
+                log.debug('cannot change status on %s with closed pull request', revision)
+                raise HTTPBadRequest()
+
         action_logger(self.authuser,
                       'user_commented_revision:%s' % revision,
                       c.db_repo, self.ip_addr, self.sa)
 
         Session().commit()
 
-        if not request.environ.get('HTTP_X_PARTIAL_XHR'):
-            raise HTTPFound(location=h.url('changeset_home', repo_name=repo_name,
-                                  revision=revision))
-        #only ajax below
         data = {
            'target_id': h.safeid(h.safe_unicode(request.POST.get('f_path'))),
         }
--- a/kallithea/public/css/style.css	Wed Jan 20 01:47:11 2016 +0100
+++ b/kallithea/public/css/style.css	Wed Jan 20 01:47:11 2016 +0100
@@ -208,6 +208,10 @@
     color: #B9B9B9;
 }
 
+.inline-comments-general.show-general-status .hidden.general-only {
+    display: block !important;
+}
+
 .truncate {
        white-space: nowrap;
        overflow: hidden;
--- a/kallithea/public/js/base.js	Wed Jan 20 01:47:11 2016 +0100
+++ b/kallithea/public/js/base.js	Wed Jan 20 01:47:11 2016 +0100
@@ -673,12 +673,16 @@
 
 // append an Add button to $comment_div and hook it up to show form
 function _comment_div_append_add($comment_div, f_path, line_no) {
-    var addlabel = TRANSLATION_MAP['Add Another Comment'];
-    var $add = $('<div class="add-button-row"><span class="btn btn-mini add-button">{0}</span></div>'.format(addlabel));
-    $comment_div.append($add);
-    $add.children('.add-button').click(function(e) {
+    if (f_path && line_no) {
+        var addlabel = TRANSLATION_MAP['Add Another Comment'];
+        var $add = $('<div class="add-button-row"><span class="btn btn-mini add-button">{0}</span></div>'.format(addlabel));
+        $comment_div.append($add);
+        $add.children('.add-button').click(function(e) {
+            comment_div_state($comment_div, f_path, line_no, true);
+        });
+    } else {
         comment_div_state($comment_div, f_path, line_no, true);
-    });
+    }
 }
 
 // append a comment form to $comment_div
@@ -695,22 +699,28 @@
         e.preventDefault();
 
         var text = $textarea.val();
-        if (!text){
-            return;
+        var review_status = $form.find('input:radio[name=changeset_status]:checked').val();
+        var pr_close = $form.find('input:checkbox[name=save_close]:checked').length ? 'on' : '';
+
+        if (!text && !review_status && !pr_close) {
+            alert("Please provide a comment");
+            return false;
         }
 
         $form.find('.submitting-overlay').show();
 
+        var postData = {
+            'text': text,
+            'f_path': f_path,
+            'line': line_no,
+            'changeset_status': review_status,
+            'save_close': pr_close
+        };
         var success = function(json_data) {
             $comment_div.append(json_data['rendered_text']);
             comment_div_state($comment_div, f_path, line_no, false);
             linkInlineComments($('.firstlink'), $('.comment:first-child'));
         };
-        var postData = {
-            'text': text,
-            'f_path': f_path,
-            'line': line_no
-        };
         ajaxPOST(AJAX_COMMENT_URL, postData, success);
     });
 
--- a/kallithea/templates/changeset/changeset.html	Wed Jan 20 01:47:11 2016 +0100
+++ b/kallithea/templates/changeset/changeset.html	Wed Jan 20 01:47:11 2016 +0100
@@ -203,8 +203,7 @@
     ${comment.generate_comments()}
 
     ## main comment form and it status
-    ${comment.comments(h.url('changeset_comment', repo_name=c.repo_name, revision=c.changeset.raw_id),
-                       h.changeset_status(c.db_repo, c.changeset.raw_id))}
+    ${comment.comments()}
 
     ## FORM FOR MAKING JS ACTION AS CHANGESET COMMENTS
     <script type="text/javascript">
--- a/kallithea/templates/changeset/changeset_file_comment.html	Wed Jan 20 01:47:11 2016 +0100
+++ b/kallithea/templates/changeset/changeset_file_comment.html	Wed Jan 20 01:47:11 2016 +0100
@@ -59,6 +59,36 @@
         </div>
         <div class="mentions-container"></div>
         <textarea name="text" class="comment-block-ta yui-ac-input"></textarea>
+
+        <div id="status_block_container" class="status-block general-only hidden">
+                %if c.pull_request is None:
+                  ${_('Set changeset status')}:
+                %else:
+                  ${_('Vote for pull request status')}:
+                %endif
+                <span class="general-only cs-only">
+                </span>
+                <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:
+                    <label>
+                      <input type="radio" class="status_change_radio" name="changeset_status" id="${status}" value="${status}">
+                      ${lbl}<i class="icon-circle changeset-status-${status}" /></i>
+                    </label>
+                %endfor
+
+                %if c.pull_request is not None and ( \
+                    h.HasPermissionAny('hg.admin')() or h.HasRepoPermissionAny('repository.admin')(c.repo_name) \
+                    or c.pull_request.owner.user_id == c.authuser.user_id):
+                  <label>
+                    <input id="save_close" type="checkbox" name="save_close">
+                    ${_("Close")}
+                  </label>
+                %endif
+        </div>
+
       </div>
       <div class="comment-button">
         <div class="submitting-overlay">${_('Submitting ...')}</div>
@@ -111,7 +141,7 @@
     ${comment_count(c.inline_cnt, len(c.comments))}
   </div>
 
-      <div class="comments-list-general">
+      <div class="comments-list-chunk" data-f_path="" data-line_no="" data-target-id="general-comments">
         %for co in c.comments:
             ${comment_block(co)}
         %endfor
@@ -120,76 +150,32 @@
 </%def>
 
 ## MAIN COMMENT FORM
-<%def name="comments(post_url, cur_status, is_pr=False, change_status=True)">
+<%def name="comments(change_status=True)">
+
+## global, shared for all edit boxes
+<div class="mentions-container" id="mentions_container"></div>
 
-<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">
-              <span style="color:#577632" class="tooltip">${_('Comments are in plain text. Use @username inside this text to send notification to another local user.')|n}</span>
-            </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 class="comment-button">
-            ${h.submit('save', _('Comment'), class_="btn")}
-        </div>
-      ${h.end_form()}
-    </div>
-    %endif
+<div class="inline-comments inline-comments-general
+            ${'show-general-status' if change_status else ''}">
+  <div id="comments-general-comments" class="">
+  ## comment_div for general comments
+  </div>
 </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()) {
+      var $textareas = $('.comment-inline-form textarea[name=text]');
+      if($textareas.size() > 1 ||
+         $textareas.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');
-      }
-   });
-
 });
 </script>
 </%def>
--- a/kallithea/templates/pullrequests/pullrequest_show.html	Wed Jan 20 01:47:11 2016 +0100
+++ b/kallithea/templates/pullrequests/pullrequest_show.html	Wed Jan 20 01:47:11 2016 +0100
@@ -369,10 +369,7 @@
     ${comment.generate_comments()}
 
     ## main comment form and it status
-    ${comment.comments(h.url('pullrequest_comment', repo_name=c.repo_name,
-                              pull_request_id=c.pull_request.pull_request_id),
-                       c.current_voting_result,
-                       is_pr=True, change_status=c.allowed_to_change_status)}
+    ${comment.comments(change_status=c.allowed_to_change_status)}
 
     <script type="text/javascript">
       $(document).ready(function(){
--- a/kallithea/tests/functional/test_changeset_comments.py	Wed Jan 20 01:47:11 2016 +0100
+++ b/kallithea/tests/functional/test_changeset_comments.py	Wed Jan 20 01:47:11 2016 +0100
@@ -21,10 +21,9 @@
         params = {'text': text, '_authentication_token': self.authentication_token()}
         response = self.app.post(url(controller='changeset', action='comment',
                                      repo_name=HG_REPO, revision=rev),
-                                     params=params)
+                                     params=params, extra_environ={'HTTP_X_PARTIAL_XHR': '1'})
         # Test response...
-        self.assertEqual(response.status, '302 Found')
-        response.follow()
+        self.assertEqual(response.status, '200 OK')
 
         response = self.app.get(url(controller='changeset', action='index',
                                 repo_name=HG_REPO, revision=rev))
@@ -58,10 +57,9 @@
         params = {'text': text, 'f_path': f_path, 'line': line, '_authentication_token': self.authentication_token()}
         response = self.app.post(url(controller='changeset', action='comment',
                                      repo_name=HG_REPO, revision=rev),
-                                     params=params)
+                                     params=params, extra_environ={'HTTP_X_PARTIAL_XHR': '1'})
         # Test response...
-        self.assertEqual(response.status, '302 Found')
-        response.follow()
+        self.assertEqual(response.status, '200 OK')
 
         response = self.app.get(url(controller='changeset', action='index',
                                 repo_name=HG_REPO, revision=rev))
@@ -98,10 +96,9 @@
         params = {'text': text, '_authentication_token': self.authentication_token()}
         response = self.app.post(url(controller='changeset', action='comment',
                                      repo_name=HG_REPO, revision=rev),
-                                     params=params)
+                                     params=params, extra_environ={'HTTP_X_PARTIAL_XHR': '1'})
         # Test response...
-        self.assertEqual(response.status, '302 Found')
-        response.follow()
+        self.assertEqual(response.status, '200 OK')
 
         response = self.app.get(url(controller='changeset', action='index',
                                 repo_name=HG_REPO, revision=rev))
@@ -126,7 +123,7 @@
         params = {'text': text, '_authentication_token': self.authentication_token()}
         response = self.app.post(url(controller='changeset', action='comment',
                                      repo_name=HG_REPO, revision=rev),
-                                     params=params)
+                                     params=params, extra_environ={'HTTP_X_PARTIAL_XHR': '1'})
 
         comments = ChangesetComment.query().all()
         self.assertEqual(len(comments), 1)