summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/diff/gr-diff-comment-thread-group/gr-diff-comment-thread-group.js
diff options
context:
space:
mode:
Diffstat (limited to 'polygerrit-ui/app/elements/diff/gr-diff-comment-thread-group/gr-diff-comment-thread-group.js')
-rw-r--r--polygerrit-ui/app/elements/diff/gr-diff-comment-thread-group/gr-diff-comment-thread-group.js149
1 files changed, 70 insertions, 79 deletions
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-comment-thread-group/gr-diff-comment-thread-group.js b/polygerrit-ui/app/elements/diff/gr-diff-comment-thread-group/gr-diff-comment-thread-group.js
index b6af0d8064..ae45c93a73 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-comment-thread-group/gr-diff-comment-thread-group.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff-comment-thread-group/gr-diff-comment-thread-group.js
@@ -1,16 +1,19 @@
-// 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.
+/**
+ * @license
+ * 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';
@@ -19,10 +22,6 @@
properties: {
changeNum: String,
- comments: {
- type: Array,
- value() { return []; },
- },
projectName: String,
patchForNewThreads: String,
range: Object,
@@ -30,46 +29,81 @@
type: Boolean,
value: false,
},
- _threads: {
+ parentIndex: {
+ type: Number,
+ value: null,
+ },
+ threads: {
type: Array,
value() { return []; },
},
},
- observers: [
- '_commentsChanged(comments.*)',
- ],
+ get threadEls() {
+ return Polymer.dom(this.root).querySelectorAll('gr-diff-comment-thread');
+ },
- addNewThread(locationRange) {
- this.push('_threads', {
+ /**
+ * Adds a new thread. Range is optional because a comment can be
+ * added to a line without a range selected.
+ *
+ * @param {!Object} opt_range
+ */
+ addNewThread(commentSide, opt_range) {
+ this.push('threads', {
comments: [],
- locationRange,
+ commentSide,
patchNum: this.patchForNewThreads,
+ range: opt_range,
});
},
- removeThread(locationRange) {
- for (let i = 0; i < this._threads.length; i++) {
- if (this._threads[i].locationRange === locationRange) {
- this.splice('_threads', i, 1);
+ removeThread(rootId) {
+ for (let i = 0; i < this.threads.length; i++) {
+ if (this.threads[i].rootId === rootId) {
+ this.splice('threads', i, 1);
return;
}
}
},
- getThreadForRange(rangeToCheck) {
- const threads = [].filter.call(
- Polymer.dom(this.root).querySelectorAll('gr-diff-comment-thread'),
- thread => {
- return thread.locationRange === rangeToCheck;
- });
+ /**
+ * Fetch the thread group at the given range, or the range-less thread
+ * on the line if no range is provided, lineNum, and side.
+ *
+ * @param {string} side
+ * @param {!Object=} opt_range
+ * @return {!Object|undefined}
+ */
+ getThread(side, opt_range) {
+ const threads = [].filter.call(this.threadEls,
+ thread => this._rangesEqual(thread.range, opt_range))
+ .filter(thread => thread.commentSide === side);
if (threads.length === 1) {
return threads[0];
}
},
- _commentsChanged() {
- this._threads = this._getThreadGroups(this.comments);
+ _handleThreadDiscard(e) {
+ this.removeThread(e.detail.rootId);
+ },
+
+ /**
+ * Compare two ranges. Either argument may be falsy, but will only return
+ * true if both are falsy or if neither are falsy and have the same position
+ * values.
+ *
+ * @param {Object=} a range 1
+ * @param {Object=} b range 2
+ * @return {boolean}
+ */
+ _rangesEqual(a, b) {
+ if (!a && !b) { return true; }
+ if (!a || !b) { return false; }
+ return a.startLine === b.startLine &&
+ a.startChar === b.startChar &&
+ a.endLine === b.endLine &&
+ a.endChar === b.endChar;
},
_sortByDate(threadGroups) {
@@ -95,48 +129,5 @@
range.end_character + '-' +
comment.__commentSide;
},
-
- /**
- * 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.
- */
- _getPatchNum(comment) {
- return comment.patchNum || this.patchForNewThreads;
- },
-
- _getThreadGroups(comments) {
- const threadGroups = {};
-
- for (const comment of comments) {
- let locationRange;
- if (!comment.range) {
- locationRange = 'line-' + comment.__commentSide;
- } else {
- locationRange = this._calculateLocationRange(comment.range, comment);
- }
-
- if (threadGroups[locationRange]) {
- threadGroups[locationRange].comments.push(comment);
- } else {
- threadGroups[locationRange] = {
- start_datetime: comment.updated,
- comments: [comment],
- locationRange,
- commentSide: comment.__commentSide,
- patchNum: this._getPatchNum(comment),
- };
- }
- }
-
- const threadGroupArr = [];
- const threadGroupKeys = Object.keys(threadGroups);
- for (const threadGroupKey of threadGroupKeys) {
- threadGroupArr.push(threadGroups[threadGroupKey]);
- }
-
- return this._sortByDate(threadGroupArr);
- },
});
})();