
Formerly, if a formatted text component tried to render without the project config (used by inner linked text components) it would temporarily fall-back to rendering the unformatted (and un-linkified) text via `.textContent` -- mirroring the behavior of gr-linked-text. The result is formatted text elements (when rendered without a project config) appear as one long line of text. Unlike linkification, however, text can be accurately formatted with or without the project config -- so this disruptive, poor UX is unnecessary. The formatted text component is updated to format text when the project config has not provided, and to re-render when the config has been provided. In order to propagate project config loads to the formatted text components hosted by diff comments, the REST calls must be made by the diff thread component. To make this call, the thread must have the project's name, so gr-diff-builder is updated to provide this name to thread components. Bug: Issue 6686 Change-Id: I8d09c740930500e99cb5f87b92f4d72f3f50a9ce
143 lines
4.0 KiB
JavaScript
143 lines
4.0 KiB
JavaScript
// 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,
|
|
value() { return []; },
|
|
},
|
|
projectName: String,
|
|
patchForNewThreads: String,
|
|
range: Object,
|
|
isOnParent: {
|
|
type: Boolean,
|
|
value: false,
|
|
},
|
|
_threads: {
|
|
type: Array,
|
|
value() { return []; },
|
|
},
|
|
},
|
|
|
|
observers: [
|
|
'_commentsChanged(comments.*)',
|
|
],
|
|
|
|
addNewThread(locationRange) {
|
|
this.push('_threads', {
|
|
comments: [],
|
|
locationRange,
|
|
patchNum: this.patchForNewThreads,
|
|
});
|
|
},
|
|
|
|
removeThread(locationRange) {
|
|
for (let i = 0; i < this._threads.length; i++) {
|
|
if (this._threads[i].locationRange === locationRange) {
|
|
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;
|
|
});
|
|
if (threads.length === 1) {
|
|
return threads[0];
|
|
}
|
|
},
|
|
|
|
_commentsChanged() {
|
|
this._threads = this._getThreadGroups(this.comments);
|
|
},
|
|
|
|
_sortByDate(threadGroups) {
|
|
if (!threadGroups.length) { return; }
|
|
return threadGroups.sort((a, b) => {
|
|
// 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;
|
|
}
|
|
return util.parseDate(a.start_datetime) -
|
|
util.parseDate(b.start_datetime);
|
|
});
|
|
},
|
|
|
|
_calculateLocationRange(range, comment) {
|
|
return 'range-' + range.start_line + '-' +
|
|
range.start_character + '-' +
|
|
range.end_line + '-' +
|
|
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);
|
|
},
|
|
});
|
|
})();
|