blob: 1c56c4090121b9b5cccc49b3877b5d1688db28bb [file] [log] [blame]
Andrew Bonventre78792e82016-03-04 17:48:22 -05001// Copyright (C) 2016 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14(function() {
15 'use strict';
16
17 Polymer({
18 is: 'gr-change-view',
19
20 /**
21 * Fired when the title of the page should change.
22 *
23 * @event title-change
24 */
25
Andrew Bonventre1c75ceb2016-04-05 13:22:32 -040026 /**
27 * Fired if an error occurs when fetching the change data.
28 *
29 * @event page-error
30 */
31
Andrew Bonventre78792e82016-03-04 17:48:22 -050032 properties: {
33 /**
34 * URL params passed from the router.
35 */
36 params: {
37 type: Object,
38 observer: '_paramsChanged',
39 },
40 viewState: {
41 type: Object,
42 notify: true,
43 value: function() { return {}; },
44 },
45 serverConfig: Object,
46 keyEventTarget: {
47 type: Object,
48 value: function() { return document.body; },
49 },
50
51 _comments: Object,
52 _change: {
53 type: Object,
54 observer: '_changeChanged',
55 },
56 _commitInfo: Object,
57 _changeNum: String,
Andrew Bonventre28165262016-05-19 17:24:45 -070058 _diffDrafts: {
59 type: Object,
60 value: function() { return {}; },
61 },
Andrew Bonventrecd768c52016-06-01 17:34:21 -040062 _editingCommitMessage: {
63 type: Boolean,
64 value: false,
65 },
66 _hideEditCommitMessage: {
67 type: Boolean,
68 computed: '_computeHideEditCommitMessage(_loggedIn, ' +
69 '_editingCommitMessage, _change.*, _patchRange.patchNum)',
70 },
Andrew Bonventrebf253622016-05-10 15:59:55 -040071 _patchRange: Object,
Andrew Bonventre78792e82016-03-04 17:48:22 -050072 _allPatchSets: {
73 type: Array,
74 computed: '_computeAllPatchSets(_change)',
75 },
76 _loggedIn: {
77 type: Boolean,
78 value: false,
79 },
80 _loading: Boolean,
81 _headerContainerEl: Object,
82 _headerEl: Object,
83 _projectConfig: Object,
Andrew Bonventre015b8042016-03-09 21:04:15 -050084 _replyButtonLabel: {
85 type: String,
86 value: 'Reply',
Andrew Bonventre28165262016-05-19 17:24:45 -070087 computed: '_computeReplyButtonLabel(_diffDrafts.*)',
Andrew Bonventre015b8042016-03-09 21:04:15 -050088 },
Andrew Bonventre78792e82016-03-04 17:48:22 -050089 },
90
91 behaviors: [
92 Gerrit.KeyboardShortcutBehavior,
93 Gerrit.RESTClientBehavior,
94 ],
95
Andrew Bonventre5474de82016-06-27 16:34:12 -040096 observers: [
97 '_labelsChanged(_change.labels.*)',
Wyatt Allen95cb4712016-07-28 15:37:58 -070098 '_paramsAndChangeChanged(params, _change)',
Andrew Bonventre5474de82016-06-27 16:34:12 -040099 ],
100
Andrew Bonventre78792e82016-03-04 17:48:22 -0500101 ready: function() {
Andrew Bonventre78792e82016-03-04 17:48:22 -0500102 this._headerEl = this.$$('.header');
103 },
104
105 attached: function() {
Andrew Bonventre015b8042016-03-09 21:04:15 -0500106 this._getLoggedIn().then(function(loggedIn) {
107 this._loggedIn = loggedIn;
108 }.bind(this));
109
Andrew Bonventre28165262016-05-19 17:24:45 -0700110 this.addEventListener('comment-save', this._handleCommentSave.bind(this));
111 this.addEventListener('comment-discard',
112 this._handleCommentDiscard.bind(this));
Andrew Bonventrecd768c52016-06-01 17:34:21 -0400113 this.addEventListener('editable-content-save',
114 this._handleCommitMessageSave.bind(this));
115 this.addEventListener('editable-content-cancel',
116 this._handleCommitMessageCancel.bind(this));
Andrew Bonventre28165262016-05-19 17:24:45 -0700117 this.listen(window, 'scroll', '_handleBodyScroll');
Andrew Bonventre78792e82016-03-04 17:48:22 -0500118 },
119
120 detached: function() {
Andrew Bonventre28165262016-05-19 17:24:45 -0700121 this.unlisten(window, 'scroll', '_handleBodyScroll');
Andrew Bonventre78792e82016-03-04 17:48:22 -0500122 },
123
124 _handleBodyScroll: function(e) {
125 var containerEl = this._headerContainerEl ||
126 this.$$('.headerContainer');
127
128 // Calculate where the header is relative to the window.
129 var top = containerEl.offsetTop;
130 for (var offsetParent = containerEl.offsetParent;
131 offsetParent;
132 offsetParent = offsetParent.offsetParent) {
133 top += offsetParent.offsetTop;
134 }
135 // The element may not be displayed yet, in which case do nothing.
136 if (top == 0) { return; }
137
138 this._headerEl.classList.toggle('pinned', window.scrollY >= top);
139 },
140
141 _resetHeaderEl: function() {
142 var el = this._headerEl || this.$$('.header');
143 this._headerEl = el;
144 el.classList.remove('pinned');
145 },
146
Andrew Bonventrecd768c52016-06-01 17:34:21 -0400147 _handleEditCommitMessage: function(e) {
148 this._editingCommitMessage = true;
149 this.$.commitMessageEditor.focusTextarea();
150 },
151
152 _handleCommitMessageSave: function(e) {
153 var message = e.detail.content;
154
155 this.$.commitMessageEditor.disabled = true;
156 this._saveCommitMessage(message).then(function(resp) {
157 this.$.commitMessageEditor.disabled = false;
158 if (!resp.ok) { return; }
159
160 this.set('_commitInfo.message', message);
161 this._editingCommitMessage = false;
162 this._reloadWindow();
163 }.bind(this)).catch(function(err) {
164 this.$.commitMessageEditor.disabled = false;
165 }.bind(this));
166 },
167
168 _reloadWindow: function() {
169 window.location.reload();
170 },
171
172 _handleCommitMessageCancel: function(e) {
173 this._editingCommitMessage = false;
174 },
175
176 _saveCommitMessage: function(message) {
177 return this.$.restAPI.saveChangeCommitMessageEdit(
178 this._changeNum, message).then(function(resp) {
179 if (!resp.ok) { return resp; }
180
181 return this.$.restAPI.publishChangeEdit(this._changeNum);
182 }.bind(this));
183 },
184
185 _computeHideEditCommitMessage: function(loggedIn, editing, changeRecord,
186 patchNum) {
187 if (!changeRecord || !loggedIn || editing) { return true; }
188
189 patchNum = parseInt(patchNum, 10);
190 if (isNaN(patchNum)) { return true; }
191
192 var change = changeRecord.base;
Urs Wolfer449462c2016-07-13 22:17:18 +0200193 if (!change.current_revision) { return true; }
Andrew Bonventrecd768c52016-06-01 17:34:21 -0400194 if (change.revisions[change.current_revision]._number !== patchNum) {
195 return true;
196 }
197
198 return false;
199 },
200
Andrew Bonventre28165262016-05-19 17:24:45 -0700201 _handleCommentSave: function(e) {
202 if (!e.target.comment.__draft) { return; }
203
204 var draft = e.target.comment;
205 draft.patch_set = draft.patch_set || this._patchRange.patchNum;
206
207 // The use of path-based notification helpers (set, push) can’t be used
208 // because the paths could contain dots in them. A new object must be
209 // created to satisfy Polymer’s dirty checking.
210 // https://github.com/Polymer/polymer/issues/3127
211 // TODO(andybons): Polyfill for Object.assign in IE.
212 var diffDrafts = Object.assign({}, this._diffDrafts);
213 if (!diffDrafts[draft.path]) {
214 diffDrafts[draft.path] = [draft];
215 this._diffDrafts = diffDrafts;
216 return;
217 }
218 for (var i = 0; i < this._diffDrafts[draft.path].length; i++) {
219 if (this._diffDrafts[draft.path][i].id === draft.id) {
220 diffDrafts[draft.path][i] = draft;
221 this._diffDrafts = diffDrafts;
222 return;
223 }
224 }
225 diffDrafts[draft.path].push(draft);
Andrew Bonventre270e1052016-06-09 16:32:32 -0400226 diffDrafts[draft.path].sort(function(c1, c2) {
227 // No line number means that it’s a file comment. Sort it above the
228 // others.
229 return (c1.line || -1) - (c2.line || -1);
230 });
Andrew Bonventre28165262016-05-19 17:24:45 -0700231 this._diffDrafts = diffDrafts;
232 },
233
234 _handleCommentDiscard: function(e) {
235 if (!e.target.comment.__draft) { return; }
236
237 var draft = e.target.comment;
238 if (!this._diffDrafts[draft.path]) {
239 return;
240 }
241 var index = -1;
242 for (var i = 0; i < this._diffDrafts[draft.path].length; i++) {
243 if (this._diffDrafts[draft.path][i].id === draft.id) {
244 index = i;
245 break;
246 }
247 }
248 if (index === -1) {
Andrew Bonventre861c4a22016-05-31 14:48:25 -0400249 // It may be a draft that hasn’t been added to _diffDrafts since it was
250 // never saved.
251 return;
Andrew Bonventre28165262016-05-19 17:24:45 -0700252 }
253
254 draft.patch_set = draft.patch_set || this._patchRange.patchNum;
255
256 // The use of path-based notification helpers (set, push) can’t be used
257 // because the paths could contain dots in them. A new object must be
258 // created to satisfy Polymer’s dirty checking.
259 // https://github.com/Polymer/polymer/issues/3127
260 // TODO(andybons): Polyfill for Object.assign in IE.
261 var diffDrafts = Object.assign({}, this._diffDrafts);
262 diffDrafts[draft.path].splice(index, 1);
263 if (diffDrafts[draft.path].length === 0) {
264 delete diffDrafts[draft.path];
265 }
266 this._diffDrafts = diffDrafts;
267 },
268
Andrew Bonventre78792e82016-03-04 17:48:22 -0500269 _handlePatchChange: function(e) {
270 var patchNum = e.target.value;
Urs Wolfer449462c2016-07-13 22:17:18 +0200271 var currentPatchNum;
272 if (this._change.current_revision) {
273 currentPatchNum =
274 this._change.revisions[this._change.current_revision]._number;
275 } else {
276 currentPatchNum = this._computeLatestPatchNum(this._allPatchSets);
277 }
Andrew Bonventre78792e82016-03-04 17:48:22 -0500278 if (patchNum == currentPatchNum) {
David Ostrovskye8771402016-02-13 12:23:53 +0100279 page.show(this.changePath(this._changeNum));
Andrew Bonventre78792e82016-03-04 17:48:22 -0500280 return;
281 }
David Ostrovskye8771402016-02-13 12:23:53 +0100282 page.show(this.changePath(this._changeNum) + '/' + patchNum);
Andrew Bonventre78792e82016-03-04 17:48:22 -0500283 },
284
285 _handleReplyTap: function(e) {
286 e.preventDefault();
Wyatt Allen165d5d32016-06-07 16:23:29 -0700287 this._openReplyDialog();
Andrew Bonventre78792e82016-03-04 17:48:22 -0500288 },
289
290 _handleDownloadTap: function(e) {
291 e.preventDefault();
292 this.$.downloadOverlay.open();
293 },
294
295 _handleDownloadDialogClose: function(e) {
296 this.$.downloadOverlay.close();
297 },
298
299 _handleMessageReply: function(e) {
300 var msg = e.detail.message.message;
301 var quoteStr = msg.split('\n').map(
302 function(line) { return '> ' + line; }).join('\n') + '\n\n';
303 this.$.replyDialog.draft += quoteStr;
Wyatt Allen165d5d32016-06-07 16:23:29 -0700304 this._openReplyDialog();
Andrew Bonventre78792e82016-03-04 17:48:22 -0500305 },
306
307 _handleReplyOverlayOpen: function(e) {
Andrew Bonventre78792e82016-03-04 17:48:22 -0500308 this.$.replyDialog.focus();
309 },
310
311 _handleReplySent: function(e) {
312 this.$.replyOverlay.close();
313 this._reload();
314 },
315
316 _handleReplyCancel: function(e) {
317 this.$.replyOverlay.close();
318 },
319
Logan Hanksadd2bce2016-07-20 13:22:51 -0700320 _handleShowReplyDialog: function(e) {
Logan Hanksa75fb052016-08-01 13:23:38 -0700321 var target = this.$.replyDialog.FocusTarget.REVIEWERS;
322 if (e.detail.value && e.detail.value.ccsOnly) {
323 target = this.$.replyDialog.FocusTarget.CCS;
324 }
325 this._openReplyDialog(target);
Logan Hanksadd2bce2016-07-20 13:22:51 -0700326 },
327
Andrew Bonventre78792e82016-03-04 17:48:22 -0500328 _paramsChanged: function(value) {
Andrew Bonventre6c37cf72016-05-12 16:15:53 -0400329 if (value.view !== this.tagName.toLowerCase()) { return; }
Andrew Bonventre78792e82016-03-04 17:48:22 -0500330
331 this._changeNum = value.changeNum;
Andrew Bonventrebf253622016-05-10 15:59:55 -0400332 this._patchRange = {
333 patchNum: value.patchNum,
334 basePatchNum: value.basePatchNum || 'PARENT',
335 };
336
Andrew Bonventre78792e82016-03-04 17:48:22 -0500337 this._reload().then(function() {
338 this.$.messageList.topMargin = this._headerEl.offsetHeight;
Andrew Bonventrea0f308e2016-05-15 14:41:52 -0400339 this.$.fileList.topMargin = this._headerEl.offsetHeight;
Andrew Bonventre78792e82016-03-04 17:48:22 -0500340
341 // Allow the message list to render before scrolling.
342 this.async(function() {
Andrew Bonventre6c37cf72016-05-12 16:15:53 -0400343 this._maybeScrollToMessage();
Andrew Bonventre78792e82016-03-04 17:48:22 -0500344 }.bind(this), 1);
345
Andrew Bonventre6c37cf72016-05-12 16:15:53 -0400346 this._maybeShowReplyDialog();
Andrew Bonventre4adac422016-04-19 14:33:53 -0400347
348 this.$.jsAPI.handleEvent(this.$.jsAPI.EventType.SHOW_CHANGE, {
349 change: this._change,
Andrew Bonventrebf253622016-05-10 15:59:55 -0400350 patchNum: this._patchRange.patchNum,
Andrew Bonventre4adac422016-04-19 14:33:53 -0400351 });
Andrew Bonventre78792e82016-03-04 17:48:22 -0500352 }.bind(this));
353 },
354
Wyatt Allen95cb4712016-07-28 15:37:58 -0700355 _paramsAndChangeChanged: function(value) {
356 // If the change number or patch range is different, then reset the
357 // selected file index.
358 var patchRangeState = this.viewState.patchRange;
359 if (this.viewState.changeNum !== this._changeNum ||
360 patchRangeState.basePatchNum !== this._patchRange.basePatchNum ||
361 patchRangeState.patchNum !== this._patchRange.patchNum) {
362 this._resetFileListViewState();
363 }
364 },
365
Andrew Bonventre6c37cf72016-05-12 16:15:53 -0400366 _maybeScrollToMessage: function() {
367 var msgPrefix = '#message-';
368 var hash = window.location.hash;
369 if (hash.indexOf(msgPrefix) === 0) {
370 this.$.messageList.scrollToMessage(hash.substr(msgPrefix.length));
371 }
372 },
373
374 _maybeShowReplyDialog: function() {
375 this._getLoggedIn().then(function(loggedIn) {
376 if (!loggedIn) { return; }
377
378 if (this.viewState.showReplyDialog) {
Wyatt Allen165d5d32016-06-07 16:23:29 -0700379 this._openReplyDialog();
Andrew Bonventreca00d3822016-05-20 11:25:35 -0700380 this.async(function() { this.$.replyOverlay.center(); }, 1);
Andrew Bonventre6c37cf72016-05-12 16:15:53 -0400381 this.set('viewState.showReplyDialog', false);
382 }
383 }.bind(this));
384 },
385
Andrew Bonventre91c62932016-05-11 14:31:49 -0400386 _resetFileListViewState: function() {
387 this.set('viewState.selectedFileIndex', 0);
388 this.set('viewState.changeNum', this._changeNum);
389 this.set('viewState.patchRange', this._patchRange);
Andrew Bonventrebf253622016-05-10 15:59:55 -0400390 },
391
Andrew Bonventre78792e82016-03-04 17:48:22 -0500392 _changeChanged: function(change) {
393 if (!change) { return; }
Andrew Bonventrebf253622016-05-10 15:59:55 -0400394 this.set('_patchRange.basePatchNum',
395 this._patchRange.basePatchNum || 'PARENT');
396 this.set('_patchRange.patchNum',
397 this._patchRange.patchNum ||
Urs Wolfer449462c2016-07-13 22:17:18 +0200398 this._computeLatestPatchNum(this._allPatchSets));
Andrew Bonventre78792e82016-03-04 17:48:22 -0500399
400 var title = change.subject + ' (' + change.change_id.substr(0, 9) + ')';
401 this.fire('title-change', {title: title});
402 },
403
Andrew Bonventre78792e82016-03-04 17:48:22 -0500404 _computeChangePermalink: function(changeNum) {
405 return '/' + changeNum;
406 },
407
Viktar Donich264ef462016-08-08 13:26:48 -0700408 _computeChangeStatus: function(change, patchNum) {
Viktar Donich7bb03b82016-08-08 12:41:50 -0700409 var statusString;
410 if (change.status === this.ChangeStatus.NEW) {
Andrew Bonventre78792e82016-03-04 17:48:22 -0500411 var rev = this._getRevisionNumber(change, patchNum);
Viktar Donich7bb03b82016-08-08 12:41:50 -0700412 if (rev && rev.draft === true) {
413 statusString = 'Draft';
414 }
415 } else {
416 statusString = this.changeStatusString(change);
Andrew Bonventre78792e82016-03-04 17:48:22 -0500417 }
Viktar Donich7bb03b82016-08-08 12:41:50 -0700418 return statusString ? '(' + statusString + ')' : '';
Andrew Bonventre78792e82016-03-04 17:48:22 -0500419 },
420
Urs Wolfer449462c2016-07-13 22:17:18 +0200421 _computeLatestPatchNum: function(allPatchSets) {
422 return allPatchSets[allPatchSets.length - 1];
Andrew Bonventre78792e82016-03-04 17:48:22 -0500423 },
424
425 _computeAllPatchSets: function(change) {
426 var patchNums = [];
427 for (var rev in change.revisions) {
428 patchNums.push(change.revisions[rev]._number);
429 }
430 return patchNums.sort(function(a, b) {
431 return a - b;
432 });
433 },
434
435 _getRevisionNumber: function(change, patchNum) {
436 for (var rev in change.revisions) {
437 if (change.revisions[rev]._number == patchNum) {
438 return change.revisions[rev];
439 }
440 }
441 },
442
443 _computePatchIndexIsSelected: function(index, patchNum) {
444 return this._allPatchSets[index] == patchNum;
445 },
446
447 _computeLabelNames: function(labels) {
448 return Object.keys(labels).sort();
449 },
450
451 _computeLabelValues: function(labelName, labels) {
452 var result = [];
453 var t = labels[labelName];
454 if (!t) { return result; }
455 var approvals = t.all || [];
456 approvals.forEach(function(label) {
457 if (label.value && label.value != labels[labelName].default_value) {
458 var labelClassName;
459 var labelValPrefix = '';
460 if (label.value > 0) {
461 labelValPrefix = '+';
462 labelClassName = 'approved';
463 } else if (label.value < 0) {
464 labelClassName = 'notApproved';
465 }
466 result.push({
467 value: labelValPrefix + label.value,
468 className: labelClassName,
469 account: label,
470 });
471 }
472 });
473 return result;
474 },
475
Andrew Bonventre28165262016-05-19 17:24:45 -0700476 _computeReplyButtonHighlighted: function(changeRecord) {
477 var drafts = (changeRecord && changeRecord.base) || {};
478 return Object.keys(drafts).length > 0;
Andrew Bonventre015b8042016-03-09 21:04:15 -0500479 },
480
Andrew Bonventre28165262016-05-19 17:24:45 -0700481 _computeReplyButtonLabel: function(changeRecord) {
482 var drafts = (changeRecord && changeRecord.base) || {};
Andrew Bonventre015b8042016-03-09 21:04:15 -0500483 var draftCount = Object.keys(drafts).reduce(function(count, file) {
484 return count + drafts[file].length;
485 }, 0);
486
487 var label = 'Reply';
488 if (draftCount > 0) {
489 label += ' (' + draftCount + ')';
490 }
491 return label;
492 },
493
Andrew Bonventre78792e82016-03-04 17:48:22 -0500494 _handleKey: function(e) {
495 if (this.shouldSupressKeyboardShortcut(e)) { return; }
496
497 switch (e.keyCode) {
498 case 65: // 'a'
Andrew Bonventreebf0cbc2016-03-11 13:02:16 -0500499 if (!this._loggedIn) { return; }
500
Andrew Bonventre78792e82016-03-04 17:48:22 -0500501 e.preventDefault();
Wyatt Allen165d5d32016-06-07 16:23:29 -0700502 this._openReplyDialog();
Andrew Bonventre78792e82016-03-04 17:48:22 -0500503 break;
504 case 85: // 'u'
505 e.preventDefault();
506 page.show('/');
507 break;
508 }
509 },
510
Andrew Bonventre5474de82016-06-27 16:34:12 -0400511 _labelsChanged: function(changeRecord) {
512 if (!changeRecord) { return; }
513 this.$.jsAPI.handleEvent(this.$.jsAPI.EventType.LABEL_CHANGE, {
514 change: this._change,
515 });
516 },
517
Logan Hanksadd2bce2016-07-20 13:22:51 -0700518 _openReplyDialog: function(opt_section) {
Wyatt Allen165d5d32016-06-07 16:23:29 -0700519 this.$.replyOverlay.open().then(function() {
520 this.$.replyOverlay.setFocusStops(this.$.replyDialog.getFocusStops());
Wyatt Allen4f4b3a72016-07-28 12:05:53 -0700521 this.$.replyDialog.open(opt_section);
Wyatt Allen165d5d32016-06-07 16:23:29 -0700522 }.bind(this));
523 },
524
Andrew Bonventre78792e82016-03-04 17:48:22 -0500525 _handleReloadChange: function() {
David Ostrovskye8771402016-02-13 12:23:53 +0100526 page.show(this.changePath(this._changeNum));
Andrew Bonventre78792e82016-03-04 17:48:22 -0500527 },
528
Andrew Bonventre1c75ceb2016-04-05 13:22:32 -0400529 _handleGetChangeDetailError: function(response) {
530 this.fire('page-error', {response: response});
531 },
532
Andrew Bonventre015b8042016-03-09 21:04:15 -0500533 _getDiffDrafts: function() {
534 return this.$.restAPI.getDiffDrafts(this._changeNum).then(
Andrew Bonventrec27bdb72016-04-01 20:50:21 -0400535 function(drafts) {
536 return this._diffDrafts = drafts;
537 }.bind(this));
Andrew Bonventre015b8042016-03-09 21:04:15 -0500538 },
539
540 _getLoggedIn: function() {
541 return this.$.restAPI.getLoggedIn();
542 },
543
Andrew Bonventrec27bdb72016-04-01 20:50:21 -0400544 _getProjectConfig: function() {
545 return this.$.restAPI.getProjectConfig(this._change.project).then(
546 function(config) {
547 this._projectConfig = config;
548 }.bind(this));
549 },
550
551 _getChangeDetail: function() {
Andrew Bonventre1c75ceb2016-04-05 13:22:32 -0400552 return this.$.restAPI.getChangeDetail(this._changeNum,
553 this._handleGetChangeDetailError.bind(this)).then(
554 function(change) {
Wyatt Allena3623082016-06-21 15:10:40 -0700555 // Issue 4190: Coalesce missing topics to null.
556 if (!change.topic) { change.topic = null; }
Viktar Donichae365562016-07-27 16:17:17 -0700557 if (!change.reviewer_updates) {
558 change.reviewer_updates = null;
559 }
Andrew Bonventre1c75ceb2016-04-05 13:22:32 -0400560 this._change = change;
561 }.bind(this));
Andrew Bonventrec27bdb72016-04-01 20:50:21 -0400562 },
563
564 _getComments: function() {
565 return this.$.restAPI.getDiffComments(this._changeNum).then(
566 function(comments) {
567 this._comments = comments;
568 }.bind(this));
569 },
570
571 _getCommitInfo: function() {
572 return this.$.restAPI.getChangeCommitInfo(
Andrew Bonventrebf253622016-05-10 15:59:55 -0400573 this._changeNum, this._patchRange.patchNum).then(
Andrew Bonventrec27bdb72016-04-01 20:50:21 -0400574 function(commitInfo) {
575 this._commitInfo = commitInfo;
576 }.bind(this));
577 },
578
Andrew Bonventre015b8042016-03-09 21:04:15 -0500579 _reloadDiffDrafts: function() {
580 this._diffDrafts = {};
581 this._getDiffDrafts().then(function() {
582 if (this.$.replyOverlay.opened) {
583 this.async(function() { this.$.replyOverlay.center(); }, 1);
584 }
585 }.bind(this));
586 },
587
Andrew Bonventre78792e82016-03-04 17:48:22 -0500588 _reload: function() {
Andrew Bonventrec27bdb72016-04-01 20:50:21 -0400589 this._loading = true;
590
Andrew Bonventre015b8042016-03-09 21:04:15 -0500591 this._getLoggedIn().then(function(loggedIn) {
592 if (!loggedIn) { return; }
593
594 this._reloadDiffDrafts();
595 }.bind(this));
596
Andrew Bonventrec27bdb72016-04-01 20:50:21 -0400597 var detailCompletes = this._getChangeDetail().then(function() {
598 this._loading = false;
599 }.bind(this));
600 this._getComments();
Andrew Bonventre015b8042016-03-09 21:04:15 -0500601
Andrew Bonventre78792e82016-03-04 17:48:22 -0500602 var reloadPatchNumDependentResources = function() {
603 return Promise.all([
Andrew Bonventrec27bdb72016-04-01 20:50:21 -0400604 this._getCommitInfo(),
Andrew Bonventre78792e82016-03-04 17:48:22 -0500605 this.$.actions.reload(),
606 this.$.fileList.reload(),
607 ]);
608 }.bind(this);
609 var reloadDetailDependentResources = function() {
Andrew Bonventre1c75ceb2016-04-05 13:22:32 -0400610 if (!this._change) { return Promise.resolve(); }
611
Andrew Bonventrec27bdb72016-04-01 20:50:21 -0400612 return Promise.all([
613 this.$.relatedChanges.reload(),
614 this._getProjectConfig(),
615 ]);
Andrew Bonventre78792e82016-03-04 17:48:22 -0500616 }.bind(this);
617
618 this._resetHeaderEl();
619
Andrew Bonventrebf253622016-05-10 15:59:55 -0400620 if (this._patchRange.patchNum) {
Andrew Bonventre78792e82016-03-04 17:48:22 -0500621 return reloadPatchNumDependentResources().then(function() {
622 return detailCompletes;
623 }).then(reloadDetailDependentResources);
624 } else {
625 // The patch number is reliant on the change detail request.
626 return detailCompletes.then(reloadPatchNumDependentResources).then(
627 reloadDetailDependentResources);
628 }
629 },
630 });
631})();