Skip to content

Commit 0b5f4d8

Browse files
committed
Add RowView
Represents a single row and here we will keep all the operations related to it: extracting file/line data, manipulating cells.
1 parent 7ece9fe commit 0b5f4d8

1 file changed

Lines changed: 35 additions & 20 deletions

File tree

code_comments/htdocs/code-comments.js

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,8 @@
148148
view.appendTo( this.$( "ul.comments" ) );
149149
},
150150
showAddCommentDialog: function() {
151-
var $parentRow = $( this.el ).prev()[0],
152-
$th = ( $( 'th', $parentRow ).length) ? $( 'th', $parentRow ) : $parentRow,
153-
$item = $th.last(),
154-
file = $item.parents( 'li' ).find( 'h2>a:first' ).text(),
155-
line = $.inArray( $parentRow, CodeComments.$tableRows ) + 1,
156-
displayLine = $item.text().trim() || $th.first().text() + ' (deleted)';
157-
158-
AddCommentDialog.open( LineComments, this.line, file, displayLine );
151+
row = new RowView( { el: $( this.el ).prev().get( 0 ) } );
152+
AddCommentDialog.open( LineComments, this.line, row.getFile(), row.getDisplayLine() );
159153
}
160154
});
161155

@@ -224,15 +218,11 @@
224218
window.LineCommentBubblesView = Backbone.View.extend({
225219
render: function() {
226220
var callbackMouseover = function( event ) {
227-
var $th = ( $( 'th', this ).length) ? $( 'th', this ) : $( this ),
228-
$item = $th.last(),
229-
file = $item.parents( 'li' ).find( 'h2>a:first' ).text(),
230-
line = $.inArray( this, CodeComments.$tableRows ) + 1,
231-
displayLine = $item.text().trim() || $th.first().text() + ' (deleted)';
232-
233-
$item.children().css( 'display', 'none' );
234-
235-
$item.prepend( '<a title="Comment on this line" href="#L' + line + '" class="bubble"><span class="ui-icon ui-icon-comment"></span></a>' );
221+
var row = new RowView( { el: this } ),
222+
file = row.getFile(),
223+
line = row.getLineNumber(),
224+
displayLine = row.getDisplayLine();
225+
row.replaceLineNumberCellContent( '<a title="Comment on this line" href="#L' + line + '" class="bubble"><span class="ui-icon ui-icon-comment"></span></a>' );
236226

237227
$( 'a.bubble' ).click( function( e ) {
238228
e.preventDefault();
@@ -241,9 +231,10 @@
241231
};
242232

243233
var callbackMouseout = function( event ) {
244-
var $th = $( 'th', this ).length ? $( 'th', this ) : $( this );
245-
$( 'a.bubble', $th ).remove();
246-
$th.children().css( 'display', '' );
234+
var tr = $( 'th', this ).length? this : $( this ).parent().get( 0 ),
235+
row = new RowView( { el: tr } );
236+
$( 'a.bubble', tr ).remove();
237+
row.bringBackOriginalLineNumberCellContent();
247238
};
248239

249240
Rows.hover( callbackMouseover, callbackMouseout );
@@ -278,6 +269,30 @@
278269
}
279270
} );
280271

272+
window.RowView = Backbone.View.extend( {
273+
initialize: function( atts ) {
274+
this.$th = this.$( 'th' );
275+
this.$lineNumberCell = this.$th.last();
276+
this.$el = $( this.el );
277+
},
278+
replaceLineNumberCellContent: function( html ) {
279+
this.$lineNumberCell.children().css( 'display', 'none' );
280+
this.$lineNumberCell.prepend( html );
281+
},
282+
bringBackOriginalLineNumberCellContent: function() {
283+
this.$lineNumberCell.children().css( 'display', '' );
284+
},
285+
getFile: function() {
286+
return this.$el.parents( 'li' ).find( 'h2>a:first' ).text();
287+
},
288+
getLineNumber: function() {
289+
return Rows.getLineByTR( this.el );
290+
},
291+
getDisplayLine: function() {
292+
return this.$lineNumberCell.text().trim() || this.$th.first().text() + ' (deleted)';
293+
}
294+
} );
295+
281296
window.TopComments = new CommentsList();
282297
window.LineComments = new CommentsList();
283298
window.TopCommentsBlock = new TopCommentsView();

0 commit comments

Comments
 (0)