From ad2184f1ee71d68886a82cd40952fec48e2af007 Mon Sep 17 00:00:00 2001 From: Sebastian Dietrich Date: Wed, 6 Dec 2017 17:01:13 +0100 Subject: [PATCH] Comment is placed on line of file instead of line of diff. --- code_comments/comments.py | 3 ++ code_comments/htdocs/code-comments.js | 45 ++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/code_comments/comments.py b/code_comments/comments.py index f027e48..50bab44 100644 --- a/code_comments/comments.py +++ b/code_comments/comments.py @@ -87,6 +87,9 @@ def get_condition_str_and_corresponding_values(self, args): elif name.endswith('__lt'): name = name.replace('__lt', '') conditions.append(name + ' < %s') + elif name.endswith('__ne'): + name = name.replace('__ne', '') + conditions.append(name + ' != %s') elif name.endswith('__prefix'): values.append(args[name].replace('%', '\\%').replace('_', '\\_') + '%') name = name.replace('__prefix', '') diff --git a/code_comments/htdocs/code-comments.js b/code_comments/htdocs/code-comments.js index 55cbc01..c0ae3d2 100644 --- a/code_comments/htdocs/code-comments.js +++ b/code_comments/htdocs/code-comments.js @@ -35,7 +35,7 @@ var underscore = _.noConflict(); return this.fetch( { data: _.extend( { line: 0 }, this.defaultFetchParams ) } ); }, fetchLineComments: function() { - return this.fetch( { data: _.extend( { line__gt: 0 }, this.defaultFetchParams ) } ); + return this.fetch( { data: _.extend( { line__ne: 0 }, this.defaultFetchParams ) } ); } }); @@ -111,13 +111,15 @@ var underscore = _.noConflict(); }, addOne: function(comment) { var line = comment.get('line'); - if (!this.viewPerLine[line]) { - this.viewPerLine[line] = new CommentsForALineView( { line: line } ); + var file = comment.get('path'); + var key = 'file_' + file + ':' + line; + if (!this.viewPerLine[key]) { + this.viewPerLine[key] = new CommentsForALineView( { file: file, line: line } ); - var $tr = $( Rows.getTrByLineNumberInDiff( line ) ); - $tr.after(this.viewPerLine[line].render().el).addClass('with-comments'); + var $tr = $( Rows.getTrByFileAndLineNumberInFile( file, line ) ); + $tr.after(this.viewPerLine[key].render().el).addClass('with-comments'); } - this.viewPerLine[line].addOne(comment); + this.viewPerLine[key].addOne(comment); }, addAll: function() { var view = this; @@ -133,6 +135,7 @@ var underscore = _.noConflict(); template: _.template(CodeComments.templates.comments_for_a_line), initialize: function(attrs) { this.line = attrs.line; + this.file = attrs.file; }, events: { 'click button': 'showAddCommentDialog' @@ -235,7 +238,7 @@ var underscore = _.noConflict(); var callbackMouseover = function( event ) { var row = new RowView( { el: this } ), file = row.getFile(), - line = row.getLineNumberInDiff(), + line = row.getLineNumberInFile(), displayLine = row.getLineNumberInFile(), displayLineText = displayLine; if (displayLine < 0) { @@ -274,6 +277,34 @@ var underscore = _.noConflict(); getTrByLineNumberInDiff: function( line ) { return this.$rows[line - 1]; }, + getTrByFileAndLineNumberInFile: function ( file, line) { + var col; + var container; + if (CodeComments.page == "browser" && CodeComments.path == file) { + container = $( 'thead', 'table.code' ); + col = 0; + } else { + if (line < 0) { + line = -line; + col = 0; + } else { + col = 1 + } + var containers = $( 'thead', 'table.code, table.trac-diff' ); + for ( var i = 0; (container = containers[i]) != null; i++ ) { + if ($(container).parents( 'li' ).find( 'h2>a:first' ).text() == file) { + break; + } + } + } + var trs = $(container).parents( 'table' ).find( 'tbody>tr' ).not('.comments'); + for ( var i = 0, tr; (tr = trs[i]) != null; i++ ) { + if ($(tr).find( 'th>span' )[col].textContent == line) { + return tr; + } + } + return null; + }, wrapTHsInSpans: function() { $( 'th', this.$rows ).each( function( i, elem ) { elem.innerHTML = '' + elem.innerHTML + '';