Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 1 | // 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 Bonventre | 1c75ceb | 2016-04-05 13:22:32 -0400 | [diff] [blame] | 26 | /** |
| 27 | * Fired if an error occurs when fetching the change data. |
| 28 | * |
| 29 | * @event page-error |
| 30 | */ |
| 31 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 32 | 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 Bonventre | 2816526 | 2016-05-19 17:24:45 -0700 | [diff] [blame] | 58 | _diffDrafts: { |
| 59 | type: Object, |
| 60 | value: function() { return {}; }, |
| 61 | }, |
Andrew Bonventre | cd768c5 | 2016-06-01 17:34:21 -0400 | [diff] [blame] | 62 | _editingCommitMessage: { |
| 63 | type: Boolean, |
| 64 | value: false, |
| 65 | }, |
| 66 | _hideEditCommitMessage: { |
| 67 | type: Boolean, |
| 68 | computed: '_computeHideEditCommitMessage(_loggedIn, ' + |
| 69 | '_editingCommitMessage, _change.*, _patchRange.patchNum)', |
| 70 | }, |
Andrew Bonventre | bf25362 | 2016-05-10 15:59:55 -0400 | [diff] [blame] | 71 | _patchRange: Object, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 72 | _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 Bonventre | 015b804 | 2016-03-09 21:04:15 -0500 | [diff] [blame] | 84 | _replyButtonLabel: { |
| 85 | type: String, |
| 86 | value: 'Reply', |
Andrew Bonventre | 2816526 | 2016-05-19 17:24:45 -0700 | [diff] [blame] | 87 | computed: '_computeReplyButtonLabel(_diffDrafts.*)', |
Andrew Bonventre | 015b804 | 2016-03-09 21:04:15 -0500 | [diff] [blame] | 88 | }, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 89 | }, |
| 90 | |
| 91 | behaviors: [ |
| 92 | Gerrit.KeyboardShortcutBehavior, |
| 93 | Gerrit.RESTClientBehavior, |
| 94 | ], |
| 95 | |
Andrew Bonventre | 5474de8 | 2016-06-27 16:34:12 -0400 | [diff] [blame] | 96 | observers: [ |
| 97 | '_labelsChanged(_change.labels.*)', |
Wyatt Allen | 95cb471 | 2016-07-28 15:37:58 -0700 | [diff] [blame] | 98 | '_paramsAndChangeChanged(params, _change)', |
Andrew Bonventre | 5474de8 | 2016-06-27 16:34:12 -0400 | [diff] [blame] | 99 | ], |
| 100 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 101 | ready: function() { |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 102 | this._headerEl = this.$$('.header'); |
| 103 | }, |
| 104 | |
| 105 | attached: function() { |
Andrew Bonventre | 015b804 | 2016-03-09 21:04:15 -0500 | [diff] [blame] | 106 | this._getLoggedIn().then(function(loggedIn) { |
| 107 | this._loggedIn = loggedIn; |
| 108 | }.bind(this)); |
| 109 | |
Andrew Bonventre | 2816526 | 2016-05-19 17:24:45 -0700 | [diff] [blame] | 110 | this.addEventListener('comment-save', this._handleCommentSave.bind(this)); |
| 111 | this.addEventListener('comment-discard', |
| 112 | this._handleCommentDiscard.bind(this)); |
Andrew Bonventre | cd768c5 | 2016-06-01 17:34:21 -0400 | [diff] [blame] | 113 | this.addEventListener('editable-content-save', |
| 114 | this._handleCommitMessageSave.bind(this)); |
| 115 | this.addEventListener('editable-content-cancel', |
| 116 | this._handleCommitMessageCancel.bind(this)); |
Andrew Bonventre | 2816526 | 2016-05-19 17:24:45 -0700 | [diff] [blame] | 117 | this.listen(window, 'scroll', '_handleBodyScroll'); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 118 | }, |
| 119 | |
| 120 | detached: function() { |
Andrew Bonventre | 2816526 | 2016-05-19 17:24:45 -0700 | [diff] [blame] | 121 | this.unlisten(window, 'scroll', '_handleBodyScroll'); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 122 | }, |
| 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 Bonventre | cd768c5 | 2016-06-01 17:34:21 -0400 | [diff] [blame] | 147 | _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 Wolfer | 449462c | 2016-07-13 22:17:18 +0200 | [diff] [blame] | 193 | if (!change.current_revision) { return true; } |
Andrew Bonventre | cd768c5 | 2016-06-01 17:34:21 -0400 | [diff] [blame] | 194 | if (change.revisions[change.current_revision]._number !== patchNum) { |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | return false; |
| 199 | }, |
| 200 | |
Andrew Bonventre | 2816526 | 2016-05-19 17:24:45 -0700 | [diff] [blame] | 201 | _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 Bonventre | 270e105 | 2016-06-09 16:32:32 -0400 | [diff] [blame] | 226 | 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 Bonventre | 2816526 | 2016-05-19 17:24:45 -0700 | [diff] [blame] | 231 | 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 Bonventre | 861c4a2 | 2016-05-31 14:48:25 -0400 | [diff] [blame] | 249 | // It may be a draft that hasn’t been added to _diffDrafts since it was |
| 250 | // never saved. |
| 251 | return; |
Andrew Bonventre | 2816526 | 2016-05-19 17:24:45 -0700 | [diff] [blame] | 252 | } |
| 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 Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 269 | _handlePatchChange: function(e) { |
| 270 | var patchNum = e.target.value; |
Urs Wolfer | 449462c | 2016-07-13 22:17:18 +0200 | [diff] [blame] | 271 | 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 Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 278 | if (patchNum == currentPatchNum) { |
David Ostrovsky | e877140 | 2016-02-13 12:23:53 +0100 | [diff] [blame] | 279 | page.show(this.changePath(this._changeNum)); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 280 | return; |
| 281 | } |
David Ostrovsky | e877140 | 2016-02-13 12:23:53 +0100 | [diff] [blame] | 282 | page.show(this.changePath(this._changeNum) + '/' + patchNum); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 283 | }, |
| 284 | |
| 285 | _handleReplyTap: function(e) { |
| 286 | e.preventDefault(); |
Wyatt Allen | 165d5d3 | 2016-06-07 16:23:29 -0700 | [diff] [blame] | 287 | this._openReplyDialog(); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 288 | }, |
| 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 Allen | 165d5d3 | 2016-06-07 16:23:29 -0700 | [diff] [blame] | 304 | this._openReplyDialog(); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 305 | }, |
| 306 | |
| 307 | _handleReplyOverlayOpen: function(e) { |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 308 | 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 Hanks | 1cc8f03 | 2016-08-12 12:09:47 -0700 | [diff] [blame^] | 320 | _handleReplyAutogrow: function(e) { |
| 321 | this.$.replyOverlay.refit(); |
| 322 | }, |
| 323 | |
Logan Hanks | add2bce | 2016-07-20 13:22:51 -0700 | [diff] [blame] | 324 | _handleShowReplyDialog: function(e) { |
Logan Hanks | a75fb05 | 2016-08-01 13:23:38 -0700 | [diff] [blame] | 325 | var target = this.$.replyDialog.FocusTarget.REVIEWERS; |
| 326 | if (e.detail.value && e.detail.value.ccsOnly) { |
| 327 | target = this.$.replyDialog.FocusTarget.CCS; |
| 328 | } |
| 329 | this._openReplyDialog(target); |
Logan Hanks | add2bce | 2016-07-20 13:22:51 -0700 | [diff] [blame] | 330 | }, |
| 331 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 332 | _paramsChanged: function(value) { |
Andrew Bonventre | 6c37cf7 | 2016-05-12 16:15:53 -0400 | [diff] [blame] | 333 | if (value.view !== this.tagName.toLowerCase()) { return; } |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 334 | |
| 335 | this._changeNum = value.changeNum; |
Andrew Bonventre | bf25362 | 2016-05-10 15:59:55 -0400 | [diff] [blame] | 336 | this._patchRange = { |
| 337 | patchNum: value.patchNum, |
| 338 | basePatchNum: value.basePatchNum || 'PARENT', |
| 339 | }; |
| 340 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 341 | this._reload().then(function() { |
| 342 | this.$.messageList.topMargin = this._headerEl.offsetHeight; |
Andrew Bonventre | a0f308e | 2016-05-15 14:41:52 -0400 | [diff] [blame] | 343 | this.$.fileList.topMargin = this._headerEl.offsetHeight; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 344 | |
| 345 | // Allow the message list to render before scrolling. |
| 346 | this.async(function() { |
Andrew Bonventre | 6c37cf7 | 2016-05-12 16:15:53 -0400 | [diff] [blame] | 347 | this._maybeScrollToMessage(); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 348 | }.bind(this), 1); |
| 349 | |
Andrew Bonventre | 6c37cf7 | 2016-05-12 16:15:53 -0400 | [diff] [blame] | 350 | this._maybeShowReplyDialog(); |
Andrew Bonventre | 4adac42 | 2016-04-19 14:33:53 -0400 | [diff] [blame] | 351 | |
| 352 | this.$.jsAPI.handleEvent(this.$.jsAPI.EventType.SHOW_CHANGE, { |
| 353 | change: this._change, |
Andrew Bonventre | bf25362 | 2016-05-10 15:59:55 -0400 | [diff] [blame] | 354 | patchNum: this._patchRange.patchNum, |
Andrew Bonventre | 4adac42 | 2016-04-19 14:33:53 -0400 | [diff] [blame] | 355 | }); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 356 | }.bind(this)); |
| 357 | }, |
| 358 | |
Wyatt Allen | 95cb471 | 2016-07-28 15:37:58 -0700 | [diff] [blame] | 359 | _paramsAndChangeChanged: function(value) { |
| 360 | // If the change number or patch range is different, then reset the |
| 361 | // selected file index. |
| 362 | var patchRangeState = this.viewState.patchRange; |
| 363 | if (this.viewState.changeNum !== this._changeNum || |
| 364 | patchRangeState.basePatchNum !== this._patchRange.basePatchNum || |
| 365 | patchRangeState.patchNum !== this._patchRange.patchNum) { |
| 366 | this._resetFileListViewState(); |
| 367 | } |
| 368 | }, |
| 369 | |
Andrew Bonventre | 6c37cf7 | 2016-05-12 16:15:53 -0400 | [diff] [blame] | 370 | _maybeScrollToMessage: function() { |
| 371 | var msgPrefix = '#message-'; |
| 372 | var hash = window.location.hash; |
| 373 | if (hash.indexOf(msgPrefix) === 0) { |
| 374 | this.$.messageList.scrollToMessage(hash.substr(msgPrefix.length)); |
| 375 | } |
| 376 | }, |
| 377 | |
| 378 | _maybeShowReplyDialog: function() { |
| 379 | this._getLoggedIn().then(function(loggedIn) { |
| 380 | if (!loggedIn) { return; } |
| 381 | |
| 382 | if (this.viewState.showReplyDialog) { |
Wyatt Allen | 165d5d3 | 2016-06-07 16:23:29 -0700 | [diff] [blame] | 383 | this._openReplyDialog(); |
Andrew Bonventre | ca00d382 | 2016-05-20 11:25:35 -0700 | [diff] [blame] | 384 | this.async(function() { this.$.replyOverlay.center(); }, 1); |
Andrew Bonventre | 6c37cf7 | 2016-05-12 16:15:53 -0400 | [diff] [blame] | 385 | this.set('viewState.showReplyDialog', false); |
| 386 | } |
| 387 | }.bind(this)); |
| 388 | }, |
| 389 | |
Andrew Bonventre | 91c6293 | 2016-05-11 14:31:49 -0400 | [diff] [blame] | 390 | _resetFileListViewState: function() { |
| 391 | this.set('viewState.selectedFileIndex', 0); |
| 392 | this.set('viewState.changeNum', this._changeNum); |
| 393 | this.set('viewState.patchRange', this._patchRange); |
Andrew Bonventre | bf25362 | 2016-05-10 15:59:55 -0400 | [diff] [blame] | 394 | }, |
| 395 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 396 | _changeChanged: function(change) { |
| 397 | if (!change) { return; } |
Andrew Bonventre | bf25362 | 2016-05-10 15:59:55 -0400 | [diff] [blame] | 398 | this.set('_patchRange.basePatchNum', |
| 399 | this._patchRange.basePatchNum || 'PARENT'); |
| 400 | this.set('_patchRange.patchNum', |
| 401 | this._patchRange.patchNum || |
Urs Wolfer | 449462c | 2016-07-13 22:17:18 +0200 | [diff] [blame] | 402 | this._computeLatestPatchNum(this._allPatchSets)); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 403 | |
| 404 | var title = change.subject + ' (' + change.change_id.substr(0, 9) + ')'; |
| 405 | this.fire('title-change', {title: title}); |
| 406 | }, |
| 407 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 408 | _computeChangePermalink: function(changeNum) { |
| 409 | return '/' + changeNum; |
| 410 | }, |
| 411 | |
Viktar Donich | 264ef46 | 2016-08-08 13:26:48 -0700 | [diff] [blame] | 412 | _computeChangeStatus: function(change, patchNum) { |
Viktar Donich | 7bb03b8 | 2016-08-08 12:41:50 -0700 | [diff] [blame] | 413 | var statusString; |
| 414 | if (change.status === this.ChangeStatus.NEW) { |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 415 | var rev = this._getRevisionNumber(change, patchNum); |
Viktar Donich | 7bb03b8 | 2016-08-08 12:41:50 -0700 | [diff] [blame] | 416 | if (rev && rev.draft === true) { |
| 417 | statusString = 'Draft'; |
| 418 | } |
| 419 | } else { |
| 420 | statusString = this.changeStatusString(change); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 421 | } |
Viktar Donich | 7bb03b8 | 2016-08-08 12:41:50 -0700 | [diff] [blame] | 422 | return statusString ? '(' + statusString + ')' : ''; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 423 | }, |
| 424 | |
Urs Wolfer | 449462c | 2016-07-13 22:17:18 +0200 | [diff] [blame] | 425 | _computeLatestPatchNum: function(allPatchSets) { |
| 426 | return allPatchSets[allPatchSets.length - 1]; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 427 | }, |
| 428 | |
| 429 | _computeAllPatchSets: function(change) { |
| 430 | var patchNums = []; |
| 431 | for (var rev in change.revisions) { |
| 432 | patchNums.push(change.revisions[rev]._number); |
| 433 | } |
| 434 | return patchNums.sort(function(a, b) { |
| 435 | return a - b; |
| 436 | }); |
| 437 | }, |
| 438 | |
| 439 | _getRevisionNumber: function(change, patchNum) { |
| 440 | for (var rev in change.revisions) { |
| 441 | if (change.revisions[rev]._number == patchNum) { |
| 442 | return change.revisions[rev]; |
| 443 | } |
| 444 | } |
| 445 | }, |
| 446 | |
| 447 | _computePatchIndexIsSelected: function(index, patchNum) { |
| 448 | return this._allPatchSets[index] == patchNum; |
| 449 | }, |
| 450 | |
| 451 | _computeLabelNames: function(labels) { |
| 452 | return Object.keys(labels).sort(); |
| 453 | }, |
| 454 | |
| 455 | _computeLabelValues: function(labelName, labels) { |
| 456 | var result = []; |
| 457 | var t = labels[labelName]; |
| 458 | if (!t) { return result; } |
| 459 | var approvals = t.all || []; |
| 460 | approvals.forEach(function(label) { |
| 461 | if (label.value && label.value != labels[labelName].default_value) { |
| 462 | var labelClassName; |
| 463 | var labelValPrefix = ''; |
| 464 | if (label.value > 0) { |
| 465 | labelValPrefix = '+'; |
| 466 | labelClassName = 'approved'; |
| 467 | } else if (label.value < 0) { |
| 468 | labelClassName = 'notApproved'; |
| 469 | } |
| 470 | result.push({ |
| 471 | value: labelValPrefix + label.value, |
| 472 | className: labelClassName, |
| 473 | account: label, |
| 474 | }); |
| 475 | } |
| 476 | }); |
| 477 | return result; |
| 478 | }, |
| 479 | |
Andrew Bonventre | 2816526 | 2016-05-19 17:24:45 -0700 | [diff] [blame] | 480 | _computeReplyButtonHighlighted: function(changeRecord) { |
| 481 | var drafts = (changeRecord && changeRecord.base) || {}; |
| 482 | return Object.keys(drafts).length > 0; |
Andrew Bonventre | 015b804 | 2016-03-09 21:04:15 -0500 | [diff] [blame] | 483 | }, |
| 484 | |
Andrew Bonventre | 2816526 | 2016-05-19 17:24:45 -0700 | [diff] [blame] | 485 | _computeReplyButtonLabel: function(changeRecord) { |
| 486 | var drafts = (changeRecord && changeRecord.base) || {}; |
Andrew Bonventre | 015b804 | 2016-03-09 21:04:15 -0500 | [diff] [blame] | 487 | var draftCount = Object.keys(drafts).reduce(function(count, file) { |
| 488 | return count + drafts[file].length; |
| 489 | }, 0); |
| 490 | |
| 491 | var label = 'Reply'; |
| 492 | if (draftCount > 0) { |
| 493 | label += ' (' + draftCount + ')'; |
| 494 | } |
| 495 | return label; |
| 496 | }, |
| 497 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 498 | _handleKey: function(e) { |
| 499 | if (this.shouldSupressKeyboardShortcut(e)) { return; } |
| 500 | |
| 501 | switch (e.keyCode) { |
| 502 | case 65: // 'a' |
Viktar Donich | c282d7b | 2016-08-10 11:53:12 -0700 | [diff] [blame] | 503 | if (this._loggedIn && !e.shiftKey) { |
| 504 | e.preventDefault(); |
| 505 | this._openReplyDialog(); |
| 506 | } |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 507 | break; |
| 508 | case 85: // 'u' |
| 509 | e.preventDefault(); |
| 510 | page.show('/'); |
| 511 | break; |
| 512 | } |
| 513 | }, |
| 514 | |
Andrew Bonventre | 5474de8 | 2016-06-27 16:34:12 -0400 | [diff] [blame] | 515 | _labelsChanged: function(changeRecord) { |
| 516 | if (!changeRecord) { return; } |
| 517 | this.$.jsAPI.handleEvent(this.$.jsAPI.EventType.LABEL_CHANGE, { |
| 518 | change: this._change, |
| 519 | }); |
| 520 | }, |
| 521 | |
Logan Hanks | add2bce | 2016-07-20 13:22:51 -0700 | [diff] [blame] | 522 | _openReplyDialog: function(opt_section) { |
Wyatt Allen | 165d5d3 | 2016-06-07 16:23:29 -0700 | [diff] [blame] | 523 | this.$.replyOverlay.open().then(function() { |
| 524 | this.$.replyOverlay.setFocusStops(this.$.replyDialog.getFocusStops()); |
Wyatt Allen | 4f4b3a7 | 2016-07-28 12:05:53 -0700 | [diff] [blame] | 525 | this.$.replyDialog.open(opt_section); |
Wyatt Allen | 165d5d3 | 2016-06-07 16:23:29 -0700 | [diff] [blame] | 526 | }.bind(this)); |
| 527 | }, |
| 528 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 529 | _handleReloadChange: function() { |
David Ostrovsky | e877140 | 2016-02-13 12:23:53 +0100 | [diff] [blame] | 530 | page.show(this.changePath(this._changeNum)); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 531 | }, |
| 532 | |
Andrew Bonventre | 1c75ceb | 2016-04-05 13:22:32 -0400 | [diff] [blame] | 533 | _handleGetChangeDetailError: function(response) { |
| 534 | this.fire('page-error', {response: response}); |
| 535 | }, |
| 536 | |
Andrew Bonventre | 015b804 | 2016-03-09 21:04:15 -0500 | [diff] [blame] | 537 | _getDiffDrafts: function() { |
| 538 | return this.$.restAPI.getDiffDrafts(this._changeNum).then( |
Andrew Bonventre | c27bdb7 | 2016-04-01 20:50:21 -0400 | [diff] [blame] | 539 | function(drafts) { |
| 540 | return this._diffDrafts = drafts; |
| 541 | }.bind(this)); |
Andrew Bonventre | 015b804 | 2016-03-09 21:04:15 -0500 | [diff] [blame] | 542 | }, |
| 543 | |
| 544 | _getLoggedIn: function() { |
| 545 | return this.$.restAPI.getLoggedIn(); |
| 546 | }, |
| 547 | |
Andrew Bonventre | c27bdb7 | 2016-04-01 20:50:21 -0400 | [diff] [blame] | 548 | _getProjectConfig: function() { |
| 549 | return this.$.restAPI.getProjectConfig(this._change.project).then( |
| 550 | function(config) { |
| 551 | this._projectConfig = config; |
| 552 | }.bind(this)); |
| 553 | }, |
| 554 | |
| 555 | _getChangeDetail: function() { |
Andrew Bonventre | 1c75ceb | 2016-04-05 13:22:32 -0400 | [diff] [blame] | 556 | return this.$.restAPI.getChangeDetail(this._changeNum, |
| 557 | this._handleGetChangeDetailError.bind(this)).then( |
| 558 | function(change) { |
Wyatt Allen | a362308 | 2016-06-21 15:10:40 -0700 | [diff] [blame] | 559 | // Issue 4190: Coalesce missing topics to null. |
| 560 | if (!change.topic) { change.topic = null; } |
Viktar Donich | ae36556 | 2016-07-27 16:17:17 -0700 | [diff] [blame] | 561 | if (!change.reviewer_updates) { |
| 562 | change.reviewer_updates = null; |
| 563 | } |
Andrew Bonventre | 1c75ceb | 2016-04-05 13:22:32 -0400 | [diff] [blame] | 564 | this._change = change; |
| 565 | }.bind(this)); |
Andrew Bonventre | c27bdb7 | 2016-04-01 20:50:21 -0400 | [diff] [blame] | 566 | }, |
| 567 | |
| 568 | _getComments: function() { |
| 569 | return this.$.restAPI.getDiffComments(this._changeNum).then( |
| 570 | function(comments) { |
| 571 | this._comments = comments; |
| 572 | }.bind(this)); |
| 573 | }, |
| 574 | |
| 575 | _getCommitInfo: function() { |
| 576 | return this.$.restAPI.getChangeCommitInfo( |
Andrew Bonventre | bf25362 | 2016-05-10 15:59:55 -0400 | [diff] [blame] | 577 | this._changeNum, this._patchRange.patchNum).then( |
Andrew Bonventre | c27bdb7 | 2016-04-01 20:50:21 -0400 | [diff] [blame] | 578 | function(commitInfo) { |
| 579 | this._commitInfo = commitInfo; |
| 580 | }.bind(this)); |
| 581 | }, |
| 582 | |
Andrew Bonventre | 015b804 | 2016-03-09 21:04:15 -0500 | [diff] [blame] | 583 | _reloadDiffDrafts: function() { |
| 584 | this._diffDrafts = {}; |
| 585 | this._getDiffDrafts().then(function() { |
| 586 | if (this.$.replyOverlay.opened) { |
| 587 | this.async(function() { this.$.replyOverlay.center(); }, 1); |
| 588 | } |
| 589 | }.bind(this)); |
| 590 | }, |
| 591 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 592 | _reload: function() { |
Andrew Bonventre | c27bdb7 | 2016-04-01 20:50:21 -0400 | [diff] [blame] | 593 | this._loading = true; |
| 594 | |
Andrew Bonventre | 015b804 | 2016-03-09 21:04:15 -0500 | [diff] [blame] | 595 | this._getLoggedIn().then(function(loggedIn) { |
| 596 | if (!loggedIn) { return; } |
| 597 | |
| 598 | this._reloadDiffDrafts(); |
| 599 | }.bind(this)); |
| 600 | |
Andrew Bonventre | c27bdb7 | 2016-04-01 20:50:21 -0400 | [diff] [blame] | 601 | var detailCompletes = this._getChangeDetail().then(function() { |
| 602 | this._loading = false; |
| 603 | }.bind(this)); |
| 604 | this._getComments(); |
Andrew Bonventre | 015b804 | 2016-03-09 21:04:15 -0500 | [diff] [blame] | 605 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 606 | var reloadPatchNumDependentResources = function() { |
| 607 | return Promise.all([ |
Andrew Bonventre | c27bdb7 | 2016-04-01 20:50:21 -0400 | [diff] [blame] | 608 | this._getCommitInfo(), |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 609 | this.$.actions.reload(), |
| 610 | this.$.fileList.reload(), |
| 611 | ]); |
| 612 | }.bind(this); |
| 613 | var reloadDetailDependentResources = function() { |
Andrew Bonventre | 1c75ceb | 2016-04-05 13:22:32 -0400 | [diff] [blame] | 614 | if (!this._change) { return Promise.resolve(); } |
| 615 | |
Andrew Bonventre | c27bdb7 | 2016-04-01 20:50:21 -0400 | [diff] [blame] | 616 | return Promise.all([ |
| 617 | this.$.relatedChanges.reload(), |
| 618 | this._getProjectConfig(), |
| 619 | ]); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 620 | }.bind(this); |
| 621 | |
| 622 | this._resetHeaderEl(); |
| 623 | |
Andrew Bonventre | bf25362 | 2016-05-10 15:59:55 -0400 | [diff] [blame] | 624 | if (this._patchRange.patchNum) { |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 625 | return reloadPatchNumDependentResources().then(function() { |
| 626 | return detailCompletes; |
| 627 | }).then(reloadDetailDependentResources); |
| 628 | } else { |
| 629 | // The patch number is reliant on the change detail request. |
| 630 | return detailCompletes.then(reloadPatchNumDependentResources).then( |
| 631 | reloadDetailDependentResources); |
| 632 | } |
| 633 | }, |
| 634 | }); |
| 635 | })(); |