
- gr-file-list recognizes local preferences (for hasRangedComments flag) - gr-file-list reacts to cursor hotkey only if there is no range selected (currently always false). - Remove dead code from GrDiffBuilderSideBySide, GrDiffBuilder, gr-diff-builder.html - Bugfix: GrDiffBuilder.prototype.getGroupsByLineRange handles one-line BOTH code sections correctly. Test updated as well. - Added utitily methods added to gr-diff-builder.html to reduce dependency on DOM structure and reduce amount of code copy-pasting: - renderLineRange, getContentByLine, etc - For gr-diff.js and gr-diff-comment-thread.js addDraft renamed to addOrEditDraft because that's what it does. - For both, addDraft method always creates a draft comment. - Added support for ranged comments in gr-diff, gr-diff-comment-thread. - Added mouseenter and mouseout events to gr-comment.js - Refactored gr-comment.js to reduce code copy-paste, unify event payload, and to eliminate need of accessing component instance for patchNum. Tests updated as well. - Refactored gr-diff.js UI data model update using gr-diff-builder.html utility methods to make code more readable. - Added support for creating ranged comments to gr-diff.js. - gr-selection-action-box now reacts to click and tap to create a comment. Change-Id: I01480a4c6f460774a8b2826915702800b3f81d25
208 lines
5.8 KiB
JavaScript
208 lines
5.8 KiB
JavaScript
// Copyright (C) 2016 The Android Open Source Project
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
(function() {
|
|
'use strict';
|
|
|
|
Polymer({
|
|
is: 'gr-diff-comment-thread',
|
|
|
|
/**
|
|
* Fired when the thread should be discarded.
|
|
*
|
|
* @event thread-discard
|
|
*/
|
|
|
|
properties: {
|
|
changeNum: String,
|
|
comments: {
|
|
type: Array,
|
|
value: function() { return []; },
|
|
},
|
|
patchNum: String,
|
|
path: String,
|
|
projectConfig: Object,
|
|
side: {
|
|
type: String,
|
|
value: 'REVISION',
|
|
},
|
|
|
|
_showActions: Boolean,
|
|
_orderedComments: Array,
|
|
},
|
|
|
|
observers: [
|
|
'_commentsChanged(comments.splices)',
|
|
],
|
|
|
|
attached: function() {
|
|
this._getLoggedIn().then(function(loggedIn) {
|
|
this._showActions = loggedIn;
|
|
}.bind(this));
|
|
},
|
|
|
|
addOrEditDraft: function(opt_lineNum) {
|
|
var lastComment = this.comments[this.comments.length - 1];
|
|
if (lastComment && lastComment.__draft) {
|
|
var commentEl = this._commentElWithDraftID(
|
|
lastComment.id || lastComment.__draftID);
|
|
commentEl.editing = true;
|
|
} else {
|
|
this.addDraft(opt_lineNum);
|
|
}
|
|
},
|
|
|
|
addDraft: function(opt_lineNum, opt_range) {
|
|
var draft = this._newDraft(opt_lineNum, opt_range);
|
|
draft.__editing = true;
|
|
this.push('comments', draft);
|
|
},
|
|
|
|
_getLoggedIn: function() {
|
|
return this.$.restAPI.getLoggedIn();
|
|
},
|
|
|
|
_commentsChanged: function(changeRecord) {
|
|
this._orderedComments = this._sortedComments(this.comments);
|
|
},
|
|
|
|
_sortedComments: function(comments) {
|
|
comments.sort(function(c1, c2) {
|
|
var c1Date = c1.__date || util.parseDate(c1.updated);
|
|
var c2Date = c2.__date || util.parseDate(c2.updated);
|
|
return c1Date - c2Date;
|
|
});
|
|
|
|
var commentIDToReplies = {};
|
|
var topLevelComments = [];
|
|
for (var i = 0; i < comments.length; i++) {
|
|
var c = comments[i];
|
|
if (c.in_reply_to) {
|
|
if (commentIDToReplies[c.in_reply_to] == null) {
|
|
commentIDToReplies[c.in_reply_to] = [];
|
|
}
|
|
commentIDToReplies[c.in_reply_to].push(c);
|
|
} else {
|
|
topLevelComments.push(c);
|
|
}
|
|
}
|
|
var results = [];
|
|
for (var i = 0; i < topLevelComments.length; i++) {
|
|
this._visitComment(topLevelComments[i], commentIDToReplies, results);
|
|
}
|
|
return results;
|
|
},
|
|
|
|
_visitComment: function(parent, commentIDToReplies, results) {
|
|
results.push(parent);
|
|
|
|
var replies = commentIDToReplies[parent.id];
|
|
if (!replies) { return; }
|
|
for (var i = 0; i < replies.length; i++) {
|
|
this._visitComment(replies[i], commentIDToReplies, results);
|
|
}
|
|
},
|
|
|
|
_handleCommentReply: function(e) {
|
|
var comment = e.detail.comment;
|
|
var quoteStr;
|
|
if (e.detail.quote) {
|
|
var msg = comment.message;
|
|
var quoteStr = msg.split('\n').map(
|
|
function(line) { return ' > ' + line; }).join('\n') + '\n\n';
|
|
}
|
|
var reply = this._newReply(comment.id, comment.line, quoteStr);
|
|
reply.__editing = true;
|
|
this.push('comments', reply);
|
|
},
|
|
|
|
_handleCommentDone: function(e) {
|
|
var comment = e.detail.comment;
|
|
var reply = this._newReply(comment.id, comment.line, 'Done');
|
|
this.push('comments', reply);
|
|
|
|
// Allow the reply to render in the dom-repeat.
|
|
this.async(function() {
|
|
var commentEl = this._commentElWithDraftID(reply.__draftID);
|
|
commentEl.save();
|
|
}.bind(this), 1);
|
|
},
|
|
|
|
_commentElWithDraftID: function(id) {
|
|
var els = Polymer.dom(this.root).querySelectorAll('gr-diff-comment');
|
|
for (var i = 0; i < els.length; i++) {
|
|
if (els[i].comment.id === id || els[i].comment.__draftID === id) {
|
|
return els[i];
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
|
|
_newReply: function(inReplyTo, opt_lineNum, opt_message) {
|
|
var d = this._newDraft(opt_lineNum);
|
|
d.in_reply_to = inReplyTo;
|
|
if (opt_message != null) {
|
|
d.message = opt_message;
|
|
}
|
|
return d;
|
|
},
|
|
|
|
_newDraft: function(opt_lineNum, opt_range) {
|
|
var d = {
|
|
__draft: true,
|
|
__draftID: Math.random().toString(36),
|
|
__date: new Date(),
|
|
path: this.path,
|
|
side: this.side,
|
|
};
|
|
if (opt_lineNum) {
|
|
d.line = opt_lineNum;
|
|
}
|
|
if (opt_range) {
|
|
d.range = {
|
|
start_line: opt_range.startLine,
|
|
start_character: opt_range.startChar,
|
|
end_line: opt_range.endLine,
|
|
end_character: opt_range.endChar,
|
|
};
|
|
}
|
|
return d;
|
|
},
|
|
|
|
_handleCommentDiscard: function(e) {
|
|
var diffCommentEl = Polymer.dom(e).rootTarget;
|
|
var comment = diffCommentEl.comment;
|
|
var idx = this._indexOf(comment, this.comments);
|
|
if (idx == -1) {
|
|
throw Error('Cannot find comment ' +
|
|
JSON.stringify(diffCommentEl.comment));
|
|
}
|
|
this.splice('comments', idx, 1);
|
|
if (this.comments.length == 0) {
|
|
this.fire('thread-discard', {lastComment: comment});
|
|
}
|
|
},
|
|
|
|
_indexOf: function(comment, arr) {
|
|
for (var i = 0; i < arr.length; i++) {
|
|
var c = arr[i];
|
|
if ((c.__draftID != null && c.__draftID == comment.__draftID) ||
|
|
(c.id != null && c.id == comment.id)) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
},
|
|
});
|
|
})();
|