2017-01-27 15:33:08 -08:00
|
|
|
// Copyright (C) 2017 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-group',
|
|
|
|
|
|
|
|
properties: {
|
|
|
|
changeNum: String,
|
|
|
|
comments: {
|
|
|
|
type: Array,
|
2017-05-16 14:01:18 -07:00
|
|
|
value() { return []; },
|
2017-01-27 15:33:08 -08:00
|
|
|
},
|
2017-07-31 14:33:16 -07:00
|
|
|
projectName: String,
|
2017-02-09 07:34:23 -08:00
|
|
|
patchForNewThreads: String,
|
2017-01-27 15:33:08 -08:00
|
|
|
range: Object,
|
2017-03-13 16:40:01 -07:00
|
|
|
isOnParent: {
|
|
|
|
type: Boolean,
|
|
|
|
value: false,
|
2017-01-27 15:33:08 -08:00
|
|
|
},
|
2017-02-09 07:34:23 -08:00
|
|
|
_threads: {
|
2017-01-27 15:33:08 -08:00
|
|
|
type: Array,
|
2017-05-16 14:01:18 -07:00
|
|
|
value() { return []; },
|
2017-01-27 15:33:08 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
observers: [
|
|
|
|
'_commentsChanged(comments.*)',
|
|
|
|
],
|
|
|
|
|
2017-05-16 14:01:18 -07:00
|
|
|
addNewThread(locationRange) {
|
2017-02-09 07:34:23 -08:00
|
|
|
this.push('_threads', {
|
2017-01-27 15:33:08 -08:00
|
|
|
comments: [],
|
2017-05-16 14:01:18 -07:00
|
|
|
locationRange,
|
2017-02-09 07:34:23 -08:00
|
|
|
patchNum: this.patchForNewThreads,
|
2017-01-27 15:33:08 -08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-05-16 14:01:18 -07:00
|
|
|
removeThread(locationRange) {
|
|
|
|
for (let i = 0; i < this._threads.length; i++) {
|
2017-02-09 07:34:23 -08:00
|
|
|
if (this._threads[i].locationRange === locationRange) {
|
|
|
|
this.splice('_threads', i, 1);
|
2017-01-27 15:33:08 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-16 14:01:18 -07:00
|
|
|
getThreadForRange(rangeToCheck) {
|
|
|
|
const threads = [].filter.call(
|
2017-01-27 15:33:08 -08:00
|
|
|
Polymer.dom(this.root).querySelectorAll('gr-diff-comment-thread'),
|
2017-05-16 14:01:18 -07:00
|
|
|
thread => {
|
2017-01-27 15:33:08 -08:00
|
|
|
return thread.locationRange === rangeToCheck;
|
|
|
|
});
|
|
|
|
if (threads.length === 1) {
|
|
|
|
return threads[0];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-16 14:01:18 -07:00
|
|
|
_commentsChanged() {
|
2017-02-09 07:34:23 -08:00
|
|
|
this._threads = this._getThreadGroups(this.comments);
|
2017-01-27 15:33:08 -08:00
|
|
|
},
|
|
|
|
|
2017-05-16 14:01:18 -07:00
|
|
|
_sortByDate(threadGroups) {
|
2017-01-27 15:33:08 -08:00
|
|
|
if (!threadGroups.length) { return; }
|
2017-05-16 14:01:18 -07:00
|
|
|
return threadGroups.sort((a, b) => {
|
Move reply buttons to comment thread
Move all buttons that generate a reply of some sort (done, ack, reply,
quote) to the comment thread instead of the comment [1].
When there is a draft for a particular comment thread, all reply buttons
are hidden [2]. For example, if you click reply, you cannot click done
on the same thread, unless you remove the draft.
Each thread can have up to 1 draft. It's also worth noting that if a
thread has a draft, and the user clicks on the line or 'c' at the same
range, the existing draft will switch to 'editing' form.
[1] With the exception of "please fix" for robot comments.
[2] In this case, The please fix button will be disabled when other
reply buttons are hidden.
Feature: Issue 5410
Change-Id: Id847ee0cba0d0ce4e5b6476f58141866d41ffdad
2017-02-09 16:07:32 -08:00
|
|
|
// If a comment is a draft, it doesn't have a start_datetime yet.
|
|
|
|
// Assume it is newer than the comment it is being compared to.
|
|
|
|
if (!a.start_datetime) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!b.start_datetime) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-02-06 18:15:14 -08:00
|
|
|
return util.parseDate(a.start_datetime) -
|
|
|
|
util.parseDate(b.start_datetime);
|
2017-01-27 15:33:08 -08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-05-16 14:01:18 -07:00
|
|
|
_calculateLocationRange(range, comment) {
|
2017-01-27 15:33:08 -08:00
|
|
|
return 'range-' + range.start_line + '-' +
|
|
|
|
range.start_character + '-' +
|
|
|
|
range.end_line + '-' +
|
2017-01-30 12:03:13 -08:00
|
|
|
range.end_character + '-' +
|
|
|
|
comment.__commentSide;
|
2017-01-27 15:33:08 -08:00
|
|
|
},
|
|
|
|
|
2017-02-09 07:34:23 -08:00
|
|
|
/**
|
|
|
|
* Determines what the patchNum of a thread should be. Use patchNum from
|
|
|
|
* comment if it exists, otherwise the property of the thread group.
|
|
|
|
* This is needed for switching between side-by-side and unified views when
|
|
|
|
* there are unsaved drafts.
|
|
|
|
*/
|
2017-05-16 14:01:18 -07:00
|
|
|
_getPatchNum(comment) {
|
2017-02-09 07:34:23 -08:00
|
|
|
return comment.patchNum || this.patchForNewThreads;
|
|
|
|
},
|
|
|
|
|
2017-05-16 14:01:18 -07:00
|
|
|
_getThreadGroups(comments) {
|
|
|
|
const threadGroups = {};
|
2017-01-27 15:33:08 -08:00
|
|
|
|
2017-05-16 14:01:18 -07:00
|
|
|
for (const comment of comments) {
|
|
|
|
let locationRange;
|
2017-01-27 15:33:08 -08:00
|
|
|
if (!comment.range) {
|
2017-01-30 12:03:13 -08:00
|
|
|
locationRange = 'line-' + comment.__commentSide;
|
2017-01-27 15:33:08 -08:00
|
|
|
} else {
|
2017-01-30 12:03:13 -08:00
|
|
|
locationRange = this._calculateLocationRange(comment.range, comment);
|
2017-01-27 15:33:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (threadGroups[locationRange]) {
|
|
|
|
threadGroups[locationRange].comments.push(comment);
|
|
|
|
} else {
|
|
|
|
threadGroups[locationRange] = {
|
|
|
|
start_datetime: comment.updated,
|
|
|
|
comments: [comment],
|
2017-05-16 14:01:18 -07:00
|
|
|
locationRange,
|
2017-01-30 12:03:13 -08:00
|
|
|
commentSide: comment.__commentSide,
|
2017-02-09 07:34:23 -08:00
|
|
|
patchNum: this._getPatchNum(comment),
|
2017-01-27 15:33:08 -08:00
|
|
|
};
|
|
|
|
}
|
2017-05-16 14:01:18 -07:00
|
|
|
}
|
2017-01-27 15:33:08 -08:00
|
|
|
|
2017-05-16 14:01:18 -07:00
|
|
|
const threadGroupArr = [];
|
|
|
|
const threadGroupKeys = Object.keys(threadGroups);
|
|
|
|
for (const threadGroupKey of threadGroupKeys) {
|
2017-01-27 15:33:08 -08:00
|
|
|
threadGroupArr.push(threadGroups[threadGroupKey]);
|
2017-05-16 14:01:18 -07:00
|
|
|
}
|
2017-01-27 15:33:08 -08:00
|
|
|
|
|
|
|
return this._sortByDate(threadGroupArr);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})();
|