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 | |
Kasper Nilsson | de211a5 | 2017-07-11 14:50:51 -0700 | [diff] [blame] | 17 | const ERR_REVIEW_STATUS = 'Couldn’t change file review status.'; |
Wyatt Allen | ed628d7 | 2017-09-14 09:45:50 -0700 | [diff] [blame] | 18 | const MSG_LOADING_BLAME = 'Loading blame...'; |
| 19 | const MSG_LOADED_BLAME = 'Blame loaded'; |
Kasper Nilsson | de211a5 | 2017-07-11 14:50:51 -0700 | [diff] [blame] | 20 | |
Wyatt Allen | dc8782d | 2017-07-26 09:32:03 -0700 | [diff] [blame] | 21 | const PARENT = 'PARENT'; |
| 22 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 23 | const DiffSides = { |
Wyatt Allen | bdd4e34 | 2016-06-20 14:38:33 -0700 | [diff] [blame] | 24 | LEFT: 'left', |
| 25 | RIGHT: 'right', |
| 26 | }; |
| 27 | |
Wyatt Allen | 61e414c | 2018-02-06 17:02:17 -0800 | [diff] [blame] | 28 | const DiffViewMode = { |
| 29 | SIDE_BY_SIDE: 'SIDE_BY_SIDE', |
| 30 | UNIFIED: 'UNIFIED_DIFF', |
| 31 | }; |
| 32 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 33 | Polymer({ |
| 34 | is: 'gr-diff-view', |
| 35 | |
| 36 | /** |
| 37 | * Fired when the title of the page should change. |
| 38 | * |
| 39 | * @event title-change |
| 40 | */ |
| 41 | |
Kasper Nilsson | 6cff0e6 | 2017-02-06 14:50:53 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Fired when user tries to navigate away while comments are pending save. |
| 44 | * |
| 45 | * @event show-alert |
| 46 | */ |
| 47 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 48 | properties: { |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 49 | /** |
| 50 | * URL params passed from the router. |
| 51 | */ |
| 52 | params: { |
| 53 | type: Object, |
| 54 | observer: '_paramsChanged', |
| 55 | }, |
| 56 | keyEventTarget: { |
| 57 | type: Object, |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 58 | value() { return document.body; }, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 59 | }, |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 60 | /** |
| 61 | * @type {{ diffMode: (string|undefined) }} |
| 62 | */ |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 63 | changeViewState: { |
| 64 | type: Object, |
| 65 | notify: true, |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 66 | value() { return {}; }, |
Kasper Nilsson | 143a427 | 2017-07-12 12:47:14 -0700 | [diff] [blame] | 67 | observer: '_changeViewStateChanged', |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 68 | }, |
Becky Siegel | af950fd | 2017-10-12 11:35:18 -0700 | [diff] [blame] | 69 | /** @type {?} */ |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 70 | _patchRange: Object, |
Viktar Donich | a28dee06 | 2017-11-10 16:03:13 -0800 | [diff] [blame] | 71 | /** @type {?} */ |
| 72 | _commitRange: Object, |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 73 | /** |
| 74 | * @type {{ |
| 75 | * subject: string, |
| 76 | * project: string, |
| 77 | * revisions: string, |
| 78 | * }} |
| 79 | */ |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 80 | _change: Object, |
Becky Siegel | 318f2ad | 2017-11-06 15:36:10 -0800 | [diff] [blame] | 81 | /** @type {?} */ |
Becky Siegel | c7f07dc | 2017-11-02 16:07:13 -0700 | [diff] [blame] | 82 | _changeComments: Object, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 83 | _changeNum: String, |
| 84 | _diff: Object, |
Becky Siegel | fde2da6 | 2017-11-07 15:49:55 -0800 | [diff] [blame] | 85 | // An array specifically formatted to be used in a gr-dropdown-list |
| 86 | // element for selected a file to view. |
| 87 | _formattedFiles: { |
| 88 | type: Array, |
Becky Siegel | 5946f95 | 2017-11-09 11:53:33 -0800 | [diff] [blame] | 89 | computed: '_formatFilesForDropdown(_fileList, _patchRange.patchNum, ' + |
| 90 | '_changeComments)', |
Becky Siegel | fde2da6 | 2017-11-07 15:49:55 -0800 | [diff] [blame] | 91 | }, |
| 92 | // An sorted array of files, as returned by the rest API. |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 93 | _fileList: { |
| 94 | type: Array, |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 95 | value() { return []; }, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 96 | }, |
| 97 | _path: { |
| 98 | type: String, |
| 99 | observer: '_pathChanged', |
| 100 | }, |
Viktar Donich | c5069b1 | 2018-01-30 16:17:13 -0800 | [diff] [blame] | 101 | _fileNum: { |
| 102 | type: Number, |
| 103 | computed: '_computeFileNum(_path, _formattedFiles)', |
| 104 | }, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 105 | _loggedIn: { |
| 106 | type: Boolean, |
| 107 | value: false, |
| 108 | }, |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 109 | _loading: { |
| 110 | type: Boolean, |
| 111 | value: true, |
| 112 | }, |
| 113 | _prefs: Object, |
Viktar Donich | 5008b44 | 2016-06-08 12:37:31 -0700 | [diff] [blame] | 114 | _localPrefs: Object, |
Urs Wolfer | c047fcd | 2016-05-29 18:12:56 +0200 | [diff] [blame] | 115 | _projectConfig: Object, |
Wyatt Allen | f93f1c24 | 2016-05-05 16:53:38 -0700 | [diff] [blame] | 116 | _userPrefs: Object, |
| 117 | _diffMode: { |
| 118 | type: String, |
Viktar Donich | b34d1f8 | 2016-08-03 13:16:36 -0700 | [diff] [blame] | 119 | computed: '_getDiffViewMode(changeViewState.diffMode, _userPrefs)', |
Wyatt Allen | f93f1c24 | 2016-05-05 16:53:38 -0700 | [diff] [blame] | 120 | }, |
Wyatt Allen | 88678da | 2016-05-23 17:18:43 -0700 | [diff] [blame] | 121 | _isImageDiff: Boolean, |
Viktar Donich | b34d1f8 | 2016-08-03 13:16:36 -0700 | [diff] [blame] | 122 | _filesWeblinks: Object, |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 123 | |
| 124 | /** |
Kasper Nilsson | 6cff0e6 | 2017-02-06 14:50:53 -0800 | [diff] [blame] | 125 | * Map of paths in the current change and patch range that have comments |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 126 | * or drafts or robot comments. |
| 127 | */ |
| 128 | _commentMap: Object, |
| 129 | |
Wyatt Allen | dc8782d | 2017-07-26 09:32:03 -0700 | [diff] [blame] | 130 | _commentsForDiff: Object, |
| 131 | |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 132 | /** |
| 133 | * Object to contain the path of the next and previous file in the current |
| 134 | * change and patch range that has comments. |
| 135 | */ |
| 136 | _commentSkips: { |
| 137 | type: Object, |
| 138 | computed: '_computeCommentSkips(_commentMap, _fileList, _path)', |
| 139 | }, |
Viktar Donich | 168bbc9 | 2017-06-19 14:50:09 -0700 | [diff] [blame] | 140 | _panelFloatingDisabled: { |
| 141 | type: Boolean, |
| 142 | value: () => { return window.PANEL_FLOATING_DISABLED; }, |
| 143 | }, |
Kasper Nilsson | 808838e | 2018-01-23 17:15:38 -0800 | [diff] [blame] | 144 | _editMode: { |
Kasper Nilsson | 5f1f0c6 | 2017-08-08 14:04:54 -0700 | [diff] [blame] | 145 | type: Boolean, |
Kasper Nilsson | 808838e | 2018-01-23 17:15:38 -0800 | [diff] [blame] | 146 | computed: '_computeEditMode(_patchRange.*)', |
Kasper Nilsson | 5f1f0c6 | 2017-08-08 14:04:54 -0700 | [diff] [blame] | 147 | }, |
Wyatt Allen | ed628d7 | 2017-09-14 09:45:50 -0700 | [diff] [blame] | 148 | _isBlameSupported: { |
| 149 | type: Boolean, |
| 150 | value: false, |
| 151 | }, |
| 152 | _isBlameLoaded: Boolean, |
| 153 | _isBlameLoading: { |
| 154 | type: Boolean, |
| 155 | value: false, |
| 156 | }, |
Becky Siegel | fd7c71f | 2017-09-26 09:59:33 -0700 | [diff] [blame] | 157 | _allPatchSets: { |
| 158 | type: Array, |
| 159 | computed: 'computeAllPatchSets(_change, _change.revisions.*)', |
| 160 | }, |
Wyatt Allen | a744e23 | 2017-11-08 15:33:48 -0800 | [diff] [blame] | 161 | _revisionInfo: { |
| 162 | type: Object, |
| 163 | computed: '_getRevisionInfo(_change)', |
| 164 | }, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 165 | }, |
| 166 | |
| 167 | behaviors: [ |
| 168 | Gerrit.KeyboardShortcutBehavior, |
Kasper Nilsson | 9060cfd | 2017-07-21 14:07:48 -0700 | [diff] [blame] | 169 | Gerrit.PatchSetBehavior, |
Kasper Nilsson | ce67e2a | 2017-09-12 14:15:44 -0700 | [diff] [blame] | 170 | Gerrit.PathListBehavior, |
Kasper Nilsson | c59e88058 | 2016-10-21 11:01:08 -0700 | [diff] [blame] | 171 | Gerrit.RESTClientBehavior, |
Andrew Bonventre | 30344cc | 2016-04-05 16:06:57 -0400 | [diff] [blame] | 172 | ], |
| 173 | |
| 174 | observers: [ |
Andrew Bonventre | 30344cc | 2016-04-05 16:06:57 -0400 | [diff] [blame] | 175 | '_getProjectConfig(_change.project)', |
Andrew Bonventre | 91c6293 | 2016-05-11 14:31:49 -0400 | [diff] [blame] | 176 | '_getFiles(_changeNum, _patchRange.*)', |
Kasper Nilsson | f428a23 | 2018-02-07 11:37:22 -0800 | [diff] [blame] | 177 | '_setReviewedObserver(_loggedIn, params.*, _prefs)', |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 178 | ], |
| 179 | |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 180 | keyBindings: { |
| 181 | 'esc': '_handleEscKey', |
| 182 | 'shift+left': '_handleShiftLeftKey', |
| 183 | 'shift+right': '_handleShiftRightKey', |
| 184 | 'up k': '_handleUpKey', |
| 185 | 'down j': '_handleDownKey', |
| 186 | 'c': '_handleCKey', |
| 187 | '[': '_handleLeftBracketKey', |
| 188 | ']': '_handleRightBracketKey', |
| 189 | 'n shift+n': '_handleNKey', |
| 190 | 'p shift+p': '_handlePKey', |
| 191 | 'a shift+a': '_handleAKey', |
| 192 | 'u': '_handleUKey', |
| 193 | ',': '_handleCommaKey', |
Wyatt Allen | 61e414c | 2018-02-06 17:02:17 -0800 | [diff] [blame] | 194 | 'm': '_handleMKey', |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 195 | }, |
| 196 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 197 | attached() { |
| 198 | this._getLoggedIn().then(loggedIn => { |
Andrew Bonventre | cfacb80 | 2016-03-29 14:06:39 -0400 | [diff] [blame] | 199 | this._loggedIn = loggedIn; |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 200 | }); |
Wyatt Allen | 0b6fe19 | 2016-05-17 09:46:00 -0700 | [diff] [blame] | 201 | |
Wyatt Allen | ed628d7 | 2017-09-14 09:45:50 -0700 | [diff] [blame] | 202 | this.$.restAPI.getConfig().then(config => { |
| 203 | this._isBlameSupported = config.change.allow_blame; |
| 204 | }); |
| 205 | |
Wyatt Allen | 0b6fe19 | 2016-05-17 09:46:00 -0700 | [diff] [blame] | 206 | this.$.cursor.push('diffs', this.$.diff); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 207 | }, |
| 208 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 209 | _getLoggedIn() { |
Andrew Bonventre | cfacb80 | 2016-03-29 14:06:39 -0400 | [diff] [blame] | 210 | return this.$.restAPI.getLoggedIn(); |
| 211 | }, |
| 212 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 213 | _getProjectConfig(project) { |
Andrew Bonventre | 30344cc | 2016-04-05 16:06:57 -0400 | [diff] [blame] | 214 | return this.$.restAPI.getProjectConfig(project).then( |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 215 | config => { |
Andrew Bonventre | 30344cc | 2016-04-05 16:06:57 -0400 | [diff] [blame] | 216 | this._projectConfig = config; |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 217 | }); |
Andrew Bonventre | 30344cc | 2016-04-05 16:06:57 -0400 | [diff] [blame] | 218 | }, |
| 219 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 220 | _getChangeDetail(changeNum) { |
Kasper Nilsson | f89608d | 2017-07-24 15:06:07 -0700 | [diff] [blame] | 221 | return this.$.restAPI.getDiffChangeDetail(changeNum).then(change => { |
| 222 | this._change = change; |
Viktar Donich | a28dee06 | 2017-11-10 16:03:13 -0800 | [diff] [blame] | 223 | return change; |
Kasper Nilsson | f89608d | 2017-07-24 15:06:07 -0700 | [diff] [blame] | 224 | }); |
| 225 | }, |
| 226 | |
David Ostrovsky | e6a6f44 | 2017-04-25 23:48:05 +0200 | [diff] [blame] | 227 | _getChangeEdit(changeNum) { |
| 228 | return this.$.restAPI.getChangeEdit(this._changeNum); |
| 229 | }, |
| 230 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 231 | _getFiles(changeNum, patchRangeRecord) { |
| 232 | const patchRange = patchRangeRecord.base; |
Andrew Bonventre | 2c6e162 | 2016-04-14 17:52:07 -0400 | [diff] [blame] | 233 | return this.$.restAPI.getChangeFilePathsAsSpeciallySortedArray( |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 234 | changeNum, patchRange).then(files => { |
Andrew Bonventre | 2c6e162 | 2016-04-14 17:52:07 -0400 | [diff] [blame] | 235 | this._fileList = files; |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 236 | }); |
Andrew Bonventre | 30344cc | 2016-04-05 16:06:57 -0400 | [diff] [blame] | 237 | }, |
| 238 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 239 | _getDiffPreferences() { |
Andrew Bonventre | 61bcdbc | 2016-05-03 18:11:00 -0400 | [diff] [blame] | 240 | return this.$.restAPI.getDiffPreferences(); |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 241 | }, |
| 242 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 243 | _getPreferences() { |
Wyatt Allen | f93f1c24 | 2016-05-05 16:53:38 -0700 | [diff] [blame] | 244 | return this.$.restAPI.getPreferences(); |
| 245 | }, |
| 246 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 247 | _getWindowWidth() { |
Becky Siegel | 74f531c | 2016-10-07 17:22:16 -0700 | [diff] [blame] | 248 | return window.innerWidth; |
| 249 | }, |
| 250 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 251 | _handleReviewedChange(e) { |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 252 | this._setReviewed(Polymer.dom(e).rootTarget.checked); |
| 253 | }, |
| 254 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 255 | _setReviewed(reviewed) { |
Kasper Nilsson | 808838e | 2018-01-23 17:15:38 -0800 | [diff] [blame] | 256 | if (this._editMode) { return; } |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 257 | this.$.reviewed.checked = reviewed; |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 258 | this._saveReviewedState(reviewed).catch(err => { |
Kasper Nilsson | de211a5 | 2017-07-11 14:50:51 -0700 | [diff] [blame] | 259 | this.fire('show-alert', {message: ERR_REVIEW_STATUS}); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 260 | throw err; |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 261 | }); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 262 | }, |
| 263 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 264 | _saveReviewedState(reviewed) { |
Andrew Bonventre | 30344cc | 2016-04-05 16:06:57 -0400 | [diff] [blame] | 265 | return this.$.restAPI.saveFileReviewed(this._changeNum, |
| 266 | this._patchRange.patchNum, this._path, reviewed); |
| 267 | }, |
| 268 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 269 | _handleEscKey(e) { |
Kasper Nilsson | fa4b2fe | 2016-12-07 18:02:34 -0800 | [diff] [blame] | 270 | if (this.shouldSuppressKeyboardShortcut(e) || |
| 271 | this.modifierPressed(e)) { return; } |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 272 | |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 273 | e.preventDefault(); |
| 274 | this.$.diff.displayLine = false; |
| 275 | }, |
Viktar Donich | c282d7b | 2016-08-10 11:53:12 -0700 | [diff] [blame] | 276 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 277 | _handleShiftLeftKey(e) { |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 278 | if (this.shouldSuppressKeyboardShortcut(e)) { return; } |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 279 | |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 280 | e.preventDefault(); |
| 281 | this.$.cursor.moveLeft(); |
| 282 | }, |
| 283 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 284 | _handleShiftRightKey(e) { |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 285 | if (this.shouldSuppressKeyboardShortcut(e)) { return; } |
| 286 | |
| 287 | e.preventDefault(); |
| 288 | this.$.cursor.moveRight(); |
| 289 | }, |
| 290 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 291 | _handleUpKey(e) { |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 292 | if (this.shouldSuppressKeyboardShortcut(e)) { return; } |
| 293 | if (e.detail.keyboardEvent.shiftKey && |
| 294 | e.detail.keyboardEvent.keyCode === 75) { // 'K' |
| 295 | this._moveToPreviousFileWithComment(); |
| 296 | return; |
| 297 | } |
| 298 | if (this.modifierPressed(e)) { return; } |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 299 | |
| 300 | e.preventDefault(); |
| 301 | this.$.diff.displayLine = true; |
| 302 | this.$.cursor.moveUp(); |
| 303 | }, |
| 304 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 305 | _handleDownKey(e) { |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 306 | if (this.shouldSuppressKeyboardShortcut(e)) { return; } |
| 307 | if (e.detail.keyboardEvent.shiftKey && |
| 308 | e.detail.keyboardEvent.keyCode === 74) { // 'J' |
| 309 | this._moveToNextFileWithComment(); |
| 310 | return; |
| 311 | } |
| 312 | if (this.modifierPressed(e)) { return; } |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 313 | |
| 314 | e.preventDefault(); |
| 315 | this.$.diff.displayLine = true; |
| 316 | this.$.cursor.moveDown(); |
| 317 | }, |
| 318 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 319 | _moveToPreviousFileWithComment() { |
Wyatt Allen | 1b3eb9c | 2018-01-10 15:18:32 -0800 | [diff] [blame] | 320 | if (!this._commentSkips) { return; } |
| 321 | |
| 322 | // If there is no previous diff with comments, then return to the change |
| 323 | // view. |
| 324 | if (!this._commentSkips.previous) { |
| 325 | this._navToChangeView(); |
| 326 | return; |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 327 | } |
Wyatt Allen | 1b3eb9c | 2018-01-10 15:18:32 -0800 | [diff] [blame] | 328 | |
| 329 | Gerrit.Nav.navigateToDiff(this._change, this._commentSkips.previous, |
| 330 | this._patchRange.patchNum, this._patchRange.basePatchNum); |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 331 | }, |
| 332 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 333 | _moveToNextFileWithComment() { |
Wyatt Allen | 1b3eb9c | 2018-01-10 15:18:32 -0800 | [diff] [blame] | 334 | if (!this._commentSkips) { return; } |
| 335 | |
| 336 | // If there is no next diff with comments, then return to the change view. |
| 337 | if (!this._commentSkips.next) { |
| 338 | this._navToChangeView(); |
| 339 | return; |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 340 | } |
Wyatt Allen | 1b3eb9c | 2018-01-10 15:18:32 -0800 | [diff] [blame] | 341 | |
| 342 | Gerrit.Nav.navigateToDiff(this._change, this._commentSkips.next, |
| 343 | this._patchRange.patchNum, this._patchRange.basePatchNum); |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 344 | }, |
| 345 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 346 | _handleCKey(e) { |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 347 | if (this.shouldSuppressKeyboardShortcut(e)) { return; } |
| 348 | if (this.$.diff.isRangeSelected()) { return; } |
Kasper Nilsson | 1ba850e | 2016-11-28 15:16:21 -0800 | [diff] [blame] | 349 | if (this.modifierPressed(e)) { return; } |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 350 | |
| 351 | e.preventDefault(); |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 352 | const line = this.$.cursor.getTargetLineElement(); |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 353 | if (line) { |
| 354 | this.$.diff.addDraftAtLine(line); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 355 | } |
| 356 | }, |
| 357 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 358 | _handleLeftBracketKey(e) { |
Kasper Nilsson | eb07531 | 2017-05-24 13:49:02 -0700 | [diff] [blame] | 359 | // Check for meta key to avoid overriding native chrome shortcut. |
Kasper Nilsson | fa4b2fe | 2016-12-07 18:02:34 -0800 | [diff] [blame] | 360 | if (this.shouldSuppressKeyboardShortcut(e) || |
Kasper Nilsson | eb07531 | 2017-05-24 13:49:02 -0700 | [diff] [blame] | 361 | this.getKeyboardEvent(e).metaKey) { return; } |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 362 | |
| 363 | e.preventDefault(); |
| 364 | this._navToFile(this._path, this._fileList, -1); |
| 365 | }, |
| 366 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 367 | _handleRightBracketKey(e) { |
Kasper Nilsson | eb07531 | 2017-05-24 13:49:02 -0700 | [diff] [blame] | 368 | // Check for meta key to avoid overriding native chrome shortcut. |
Kasper Nilsson | fa4b2fe | 2016-12-07 18:02:34 -0800 | [diff] [blame] | 369 | if (this.shouldSuppressKeyboardShortcut(e) || |
Kasper Nilsson | eb07531 | 2017-05-24 13:49:02 -0700 | [diff] [blame] | 370 | this.getKeyboardEvent(e).metaKey) { return; } |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 371 | |
| 372 | e.preventDefault(); |
| 373 | this._navToFile(this._path, this._fileList, 1); |
| 374 | }, |
| 375 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 376 | _handleNKey(e) { |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 377 | if (this.shouldSuppressKeyboardShortcut(e)) { return; } |
| 378 | |
| 379 | e.preventDefault(); |
| 380 | if (e.detail.keyboardEvent.shiftKey) { |
| 381 | this.$.cursor.moveToNextCommentThread(); |
| 382 | } else { |
Kasper Nilsson | fa4b2fe | 2016-12-07 18:02:34 -0800 | [diff] [blame] | 383 | if (this.modifierPressed(e)) { return; } |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 384 | this.$.cursor.moveToNextChunk(); |
| 385 | } |
| 386 | }, |
| 387 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 388 | _handlePKey(e) { |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 389 | if (this.shouldSuppressKeyboardShortcut(e)) { return; } |
| 390 | |
| 391 | e.preventDefault(); |
| 392 | if (e.detail.keyboardEvent.shiftKey) { |
| 393 | this.$.cursor.moveToPreviousCommentThread(); |
| 394 | } else { |
Kasper Nilsson | fa4b2fe | 2016-12-07 18:02:34 -0800 | [diff] [blame] | 395 | if (this.modifierPressed(e)) { return; } |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 396 | this.$.cursor.moveToPreviousChunk(); |
| 397 | } |
| 398 | }, |
| 399 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 400 | _handleAKey(e) { |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 401 | if (this.shouldSuppressKeyboardShortcut(e)) { return; } |
| 402 | |
| 403 | if (e.detail.keyboardEvent.shiftKey) { // Hide left diff. |
| 404 | e.preventDefault(); |
| 405 | this.$.diff.toggleLeftDiff(); |
| 406 | return; |
| 407 | } |
| 408 | |
Kasper Nilsson | fa4b2fe | 2016-12-07 18:02:34 -0800 | [diff] [blame] | 409 | if (this.modifierPressed(e)) { return; } |
| 410 | |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 411 | if (!this._loggedIn) { return; } |
| 412 | |
| 413 | this.set('changeViewState.showReplyDialog', true); |
| 414 | e.preventDefault(); |
| 415 | this._navToChangeView(); |
| 416 | }, |
| 417 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 418 | _handleUKey(e) { |
Kasper Nilsson | fa4b2fe | 2016-12-07 18:02:34 -0800 | [diff] [blame] | 419 | if (this.shouldSuppressKeyboardShortcut(e) || |
| 420 | this.modifierPressed(e)) { return; } |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 421 | |
| 422 | e.preventDefault(); |
| 423 | this._navToChangeView(); |
| 424 | }, |
| 425 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 426 | _handleCommaKey(e) { |
Kasper Nilsson | fa4b2fe | 2016-12-07 18:02:34 -0800 | [diff] [blame] | 427 | if (this.shouldSuppressKeyboardShortcut(e) || |
| 428 | this.modifierPressed(e)) { return; } |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 429 | |
| 430 | e.preventDefault(); |
Becky Siegel | 7a45400 | 2017-04-19 12:06:43 -0700 | [diff] [blame] | 431 | this.$.diffPreferences.open(); |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 432 | }, |
| 433 | |
Wyatt Allen | 61e414c | 2018-02-06 17:02:17 -0800 | [diff] [blame] | 434 | _handleMKey(e) { |
James Putman | 376b5e9 | 2018-01-13 20:55:22 -0700 | [diff] [blame] | 435 | if (this.shouldSuppressKeyboardShortcut(e) || |
| 436 | this.modifierPressed(e)) { return; } |
| 437 | |
| 438 | e.preventDefault(); |
Wyatt Allen | 61e414c | 2018-02-06 17:02:17 -0800 | [diff] [blame] | 439 | if (this._getDiffViewMode() === DiffViewMode.SIDE_BY_SIDE) { |
Wyatt Allen | ffae724 | 2018-02-14 15:58:17 -0800 | [diff] [blame^] | 440 | this.$.modeSelect.setMode(DiffViewMode.UNIFIED); |
James Putman | 376b5e9 | 2018-01-13 20:55:22 -0700 | [diff] [blame] | 441 | } else { |
Wyatt Allen | ffae724 | 2018-02-14 15:58:17 -0800 | [diff] [blame^] | 442 | this.$.modeSelect.setMode(DiffViewMode.SIDE_BY_SIDE); |
James Putman | 376b5e9 | 2018-01-13 20:55:22 -0700 | [diff] [blame] | 443 | } |
| 444 | }, |
| 445 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 446 | _navToChangeView() { |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 447 | if (!this._changeNum || !this._patchRange.patchNum) { return; } |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 448 | this._navigateToChange( |
| 449 | this._change, |
Andrew Bonventre | 4d22c7e | 2016-11-15 17:01:15 -0800 | [diff] [blame] | 450 | this._patchRange, |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 451 | this._change && this._change.revisions); |
Paladox none | d43abe9 | 2017-03-29 16:33:29 +0000 | [diff] [blame] | 452 | }, |
| 453 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 454 | _navToFile(path, fileList, direction) { |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 455 | const newPath = this._getNavLinkPath(path, fileList, direction); |
| 456 | if (!newPath) { return; } |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 457 | |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 458 | if (newPath.up) { |
| 459 | this._navigateToChange( |
| 460 | this._change, |
| 461 | this._patchRange, |
| 462 | this._change && this._change.revisions); |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | Gerrit.Nav.navigateToDiff(this._change, newPath.path, |
| 467 | this._patchRange.patchNum, this._patchRange.basePatchNum); |
Andrew Bonventre | 04e57d6 | 2016-09-07 17:34:41 -0400 | [diff] [blame] | 468 | }, |
| 469 | |
| 470 | /** |
| 471 | * @param {?string} path The path of the current file being shown. |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 472 | * @param {!Array<string>} fileList The list of files in this change and |
Andrew Bonventre | 04e57d6 | 2016-09-07 17:34:41 -0400 | [diff] [blame] | 473 | * patch range. |
| 474 | * @param {number} direction Either 1 (next file) or -1 (prev file). |
| 475 | * @param {(number|boolean)} opt_noUp Whether to return to the change view |
| 476 | * when advancing the file goes outside the bounds of fileList. |
| 477 | * |
| 478 | * @return {?string} The next URL when proceeding in the specified |
| 479 | * direction. |
| 480 | */ |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 481 | _computeNavLinkURL(change, path, fileList, direction, opt_noUp) { |
| 482 | const newPath = this._getNavLinkPath(path, fileList, direction, opt_noUp); |
| 483 | if (!newPath) { return null; } |
| 484 | |
| 485 | if (newPath.up) { |
| 486 | return this._getChangePath( |
| 487 | this._change, |
| 488 | this._patchRange, |
| 489 | this._change && this._change.revisions); |
| 490 | } |
| 491 | return this._getDiffUrl(this._change, this._patchRange, newPath.path); |
| 492 | }, |
| 493 | |
| 494 | /** |
| 495 | * Gives an object representing the target of navigating either left or |
| 496 | * right through the change. The resulting object will have one of the |
| 497 | * following forms: |
| 498 | * * {path: "<target file path>"} - When another file path should be the |
| 499 | * result of the navigation. |
| 500 | * * {up: true} - When the result of navigating should go back to the |
| 501 | * change view. |
| 502 | * * null - When no navigation is possible for the given direction. |
| 503 | * |
| 504 | * @param {?string} path The path of the current file being shown. |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 505 | * @param {!Array<string>} fileList The list of files in this change and |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 506 | * patch range. |
| 507 | * @param {number} direction Either 1 (next file) or -1 (prev file). |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 508 | * @param {?number|boolean=} opt_noUp Whether to return to the change view |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 509 | * when advancing the file goes outside the bounds of fileList. |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 510 | * @return {?Object} |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 511 | */ |
| 512 | _getNavLinkPath(path, fileList, direction, opt_noUp) { |
Andrew Bonventre | 04e57d6 | 2016-09-07 17:34:41 -0400 | [diff] [blame] | 513 | if (!path || fileList.length === 0) { return null; } |
| 514 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 515 | let idx = fileList.indexOf(path); |
Kasper Nilsson | de8dc08 | 2017-01-06 15:31:32 -0800 | [diff] [blame] | 516 | if (idx === -1) { |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 517 | const file = direction > 0 ? |
| 518 | fileList[0] : |
| 519 | fileList[fileList.length - 1]; |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 520 | return {path: file}; |
Kasper Nilsson | de8dc08 | 2017-01-06 15:31:32 -0800 | [diff] [blame] | 521 | } |
Andrew Bonventre | 04e57d6 | 2016-09-07 17:34:41 -0400 | [diff] [blame] | 522 | |
| 523 | idx += direction; |
| 524 | // Redirect to the change view if opt_noUp isn’t truthy and idx falls |
| 525 | // outside the bounds of [0, fileList.length). |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 526 | if (idx < 0 || idx > fileList.length - 1) { |
Andrew Bonventre | 04e57d6 | 2016-09-07 17:34:41 -0400 | [diff] [blame] | 527 | if (opt_noUp) { return null; } |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 528 | return {up: true}; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 529 | } |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 530 | |
| 531 | return {path: fileList[idx]}; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 532 | }, |
| 533 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 534 | _paramsChanged(value) { |
Kasper Nilsson | 3c2e121 | 2017-07-17 14:12:16 -0700 | [diff] [blame] | 535 | if (value.view !== Gerrit.Nav.View.DIFF) { return; } |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 536 | |
Wyatt Allen | e515ff6 | 2018-01-19 17:54:50 -0800 | [diff] [blame] | 537 | this.$.diff.lineOfInterest = this._getLineOfInterest(this.params); |
Wyatt Allen | fd6a947 | 2017-08-28 16:42:40 -0700 | [diff] [blame] | 538 | this._initCursor(this.params); |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 539 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 540 | this._changeNum = value.changeNum; |
| 541 | this._patchRange = { |
| 542 | patchNum: value.patchNum, |
Wyatt Allen | dc8782d | 2017-07-26 09:32:03 -0700 | [diff] [blame] | 543 | basePatchNum: value.basePatchNum || PARENT, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 544 | }; |
| 545 | this._path = value.path; |
| 546 | |
Wyatt Allen | e9bd3e2 | 2017-07-26 09:44:27 -0700 | [diff] [blame] | 547 | // NOTE: This may be called before attachment (e.g. while parentElement is |
| 548 | // null). Fire title-change in an async so that, if attachment to the DOM |
| 549 | // has been queued, the event can bubble up to the handler in gr-app. |
Wyatt Allen | 68e1e21 | 2017-07-25 13:25:43 -0700 | [diff] [blame] | 550 | this.async(() => { |
| 551 | this.fire('title-change', |
Kasper Nilsson | ce67e2a | 2017-09-12 14:15:44 -0700 | [diff] [blame] | 552 | {title: this.computeTruncatedPath(this._path)}); |
Wyatt Allen | 68e1e21 | 2017-07-25 13:25:43 -0700 | [diff] [blame] | 553 | }); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 554 | |
| 555 | // When navigating away from the page, there is a possibility that the |
| 556 | // patch number is no longer a part of the URL (say when navigating to |
| 557 | // the top-level change info view) and therefore undefined in `params`. |
| 558 | if (!this._patchRange.patchNum) { |
| 559 | return; |
| 560 | } |
| 561 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 562 | const promises = []; |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 563 | |
Viktar Donich | b2198e8 | 2016-06-09 16:08:04 -0700 | [diff] [blame] | 564 | this._localPrefs = this.$.storage.getPreferences(); |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 565 | promises.push(this._getDiffPreferences().then(prefs => { |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 566 | this._prefs = prefs; |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 567 | })); |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 568 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 569 | promises.push(this._getPreferences().then(prefs => { |
Wyatt Allen | f93f1c24 | 2016-05-05 16:53:38 -0700 | [diff] [blame] | 570 | this._userPrefs = prefs; |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 571 | })); |
Wyatt Allen | f93f1c24 | 2016-05-05 16:53:38 -0700 | [diff] [blame] | 572 | |
Viktar Donich | a28dee06 | 2017-11-10 16:03:13 -0800 | [diff] [blame] | 573 | promises.push(this._getChangeDetail(this._changeNum).then(change => { |
| 574 | let commit; |
| 575 | let baseCommit; |
Thomas Shafer | 849c446 | 2018-02-05 12:05:38 -0800 | [diff] [blame] | 576 | for (const commitSha in change.revisions) { |
| 577 | if (!change.revisions.hasOwnProperty(commitSha)) continue; |
| 578 | const revision = change.revisions[commitSha]; |
| 579 | const patchNum = revision._number.toString(); |
Viktar Donich | a28dee06 | 2017-11-10 16:03:13 -0800 | [diff] [blame] | 580 | if (patchNum === this._patchRange.patchNum) { |
Thomas Shafer | 849c446 | 2018-02-05 12:05:38 -0800 | [diff] [blame] | 581 | commit = commitSha; |
| 582 | const commitObj = revision.commit || {}; |
| 583 | const parents = commitObj.parents || []; |
| 584 | if (this._patchRange.basePatchNum === PARENT && parents.length) { |
| 585 | baseCommit = parents[parents.length - 1].commit; |
| 586 | } |
Viktar Donich | a28dee06 | 2017-11-10 16:03:13 -0800 | [diff] [blame] | 587 | } else if (patchNum === this._patchRange.basePatchNum) { |
Thomas Shafer | 849c446 | 2018-02-05 12:05:38 -0800 | [diff] [blame] | 588 | baseCommit = commitSha; |
Viktar Donich | a28dee06 | 2017-11-10 16:03:13 -0800 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | this._commitRange = {commit, baseCommit}; |
| 592 | })); |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 593 | |
Wyatt Allen | dc8782d | 2017-07-26 09:32:03 -0700 | [diff] [blame] | 594 | promises.push(this._loadComments()); |
| 595 | |
David Ostrovsky | e6a6f44 | 2017-04-25 23:48:05 +0200 | [diff] [blame] | 596 | promises.push(this._getChangeEdit(this._changeNum)); |
| 597 | |
Kasper Nilsson | 29b2ab4 | 2017-10-09 11:36:19 -0700 | [diff] [blame] | 598 | this._loading = true; |
David Ostrovsky | e6a6f44 | 2017-04-25 23:48:05 +0200 | [diff] [blame] | 599 | Promise.all(promises).then(r => { |
| 600 | const edit = r[4]; |
| 601 | if (edit) { |
| 602 | this.set('_change.revisions.' + edit.commit.commit, { |
| 603 | _number: this.EDIT_NAME, |
| 604 | basePatchNum: edit.base_patch_set_number, |
| 605 | commit: edit.commit, |
| 606 | }); |
| 607 | } |
Wyatt Allen | 1333e30 | 2017-02-10 15:12:52 -0800 | [diff] [blame] | 608 | this._loading = false; |
Wyatt Allen | dc8782d | 2017-07-26 09:32:03 -0700 | [diff] [blame] | 609 | this.$.diff.comments = this._commentsForDiff; |
Wyatt Allen | 1333e30 | 2017-02-10 15:12:52 -0800 | [diff] [blame] | 610 | this.$.diff.reload(); |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 611 | }); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 612 | }, |
| 613 | |
Kasper Nilsson | 143a427 | 2017-07-12 12:47:14 -0700 | [diff] [blame] | 614 | _changeViewStateChanged(changeViewState) { |
Thomas Shafer | 88c1797 | 2017-06-26 15:13:03 -0700 | [diff] [blame] | 615 | if (changeViewState.diffMode === null) { |
| 616 | // If screen size is small, always default to unified view. |
| 617 | this.$.restAPI.getPreferences().then(prefs => { |
| 618 | this.set('changeViewState.diffMode', prefs.default_diff_view); |
| 619 | }); |
| 620 | } |
| 621 | }, |
| 622 | |
Kasper Nilsson | f428a23 | 2018-02-07 11:37:22 -0800 | [diff] [blame] | 623 | _setReviewedObserver(_loggedIn, paramsRecord, _prefs) { |
Kasper Nilsson | b1e2307 | 2018-01-23 10:33:44 -0800 | [diff] [blame] | 624 | const params = paramsRecord.base || {}; |
Kasper Nilsson | f428a23 | 2018-02-07 11:37:22 -0800 | [diff] [blame] | 625 | if (!_loggedIn || _prefs.manual_review) { return; } |
| 626 | |
| 627 | if (params.view === Gerrit.Nav.View.DIFF) { |
Thomas Shafer | 88c1797 | 2017-06-26 15:13:03 -0700 | [diff] [blame] | 628 | this._setReviewed(true); |
| 629 | } |
| 630 | }, |
| 631 | |
Wyatt Allen | bdd4e34 | 2016-06-20 14:38:33 -0700 | [diff] [blame] | 632 | /** |
Wyatt Allen | fd6a947 | 2017-08-28 16:42:40 -0700 | [diff] [blame] | 633 | * If the params specify a diff address then configure the diff cursor. |
Wyatt Allen | bdd4e34 | 2016-06-20 14:38:33 -0700 | [diff] [blame] | 634 | */ |
Wyatt Allen | fd6a947 | 2017-08-28 16:42:40 -0700 | [diff] [blame] | 635 | _initCursor(params) { |
| 636 | if (params.lineNum === undefined) { return; } |
| 637 | if (params.leftSide) { |
Wyatt Allen | bdd4e34 | 2016-06-20 14:38:33 -0700 | [diff] [blame] | 638 | this.$.cursor.side = DiffSides.LEFT; |
Wyatt Allen | bdd4e34 | 2016-06-20 14:38:33 -0700 | [diff] [blame] | 639 | } else { |
| 640 | this.$.cursor.side = DiffSides.RIGHT; |
| 641 | } |
Wyatt Allen | fd6a947 | 2017-08-28 16:42:40 -0700 | [diff] [blame] | 642 | this.$.cursor.initialLineNumber = params.lineNum; |
Wyatt Allen | bdd4e34 | 2016-06-20 14:38:33 -0700 | [diff] [blame] | 643 | }, |
| 644 | |
Wyatt Allen | e515ff6 | 2018-01-19 17:54:50 -0800 | [diff] [blame] | 645 | _getLineOfInterest(params) { |
| 646 | // If there is a line number specified, pass it along to the diff so that |
| 647 | // it will not get collapsed. |
| 648 | if (!params.lineNum) { return null; } |
| 649 | return {number: params.lineNum, leftSide: params.leftSide}; |
| 650 | }, |
| 651 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 652 | _pathChanged(path) { |
Thomas Shafer | 88c1797 | 2017-06-26 15:13:03 -0700 | [diff] [blame] | 653 | if (path) { |
| 654 | this.fire('title-change', |
Kasper Nilsson | ce67e2a | 2017-09-12 14:15:44 -0700 | [diff] [blame] | 655 | {title: this.computeTruncatedPath(path)}); |
Thomas Shafer | 88c1797 | 2017-06-26 15:13:03 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 658 | if (this._fileList.length == 0) { return; } |
| 659 | |
| 660 | this.set('changeViewState.selectedFileIndex', |
| 661 | this._fileList.indexOf(path)); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 662 | }, |
| 663 | |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 664 | _getDiffUrl(change, patchRange, path) { |
| 665 | return Gerrit.Nav.getUrlForDiff(change, path, patchRange.patchNum, |
| 666 | patchRange.basePatchNum); |
Andrew Bonventre | 91c6293 | 2016-05-11 14:31:49 -0400 | [diff] [blame] | 667 | }, |
| 668 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 669 | _patchRangeStr(patchRange) { |
| 670 | let patchStr = patchRange.patchNum; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 671 | if (patchRange.basePatchNum != null && |
Wyatt Allen | dc8782d | 2017-07-26 09:32:03 -0700 | [diff] [blame] | 672 | patchRange.basePatchNum != PARENT) { |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 673 | patchStr = patchRange.basePatchNum + '..' + patchRange.patchNum; |
| 674 | } |
Andrew Bonventre | 91c6293 | 2016-05-11 14:31:49 -0400 | [diff] [blame] | 675 | return patchStr; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 676 | }, |
| 677 | |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 678 | /** |
| 679 | * When the latest patch of the change is selected (and there is no base |
| 680 | * patch) then the patch range need not appear in the URL. Return a patch |
| 681 | * range object with undefined values when a range is not needed. |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 682 | * |
| 683 | * @param {!Object} patchRange |
| 684 | * @param {!Object} revisions |
| 685 | * @return {!Object} |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 686 | */ |
| 687 | _getChangeUrlRange(patchRange, revisions) { |
| 688 | let patchNum = undefined; |
| 689 | let basePatchNum = undefined; |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 690 | let latestPatchNum = -1; |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 691 | for (const rev of Object.values(revisions || {})) { |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 692 | latestPatchNum = Math.max(latestPatchNum, rev._number); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 693 | } |
Wyatt Allen | dc8782d | 2017-07-26 09:32:03 -0700 | [diff] [blame] | 694 | if (patchRange.basePatchNum !== PARENT || |
Andrew Bonventre | 91c6293 | 2016-05-11 14:31:49 -0400 | [diff] [blame] | 695 | parseInt(patchRange.patchNum, 10) !== latestPatchNum) { |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 696 | patchNum = patchRange.patchNum; |
| 697 | basePatchNum = patchRange.basePatchNum; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 698 | } |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 699 | return {patchNum, basePatchNum}; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 700 | }, |
| 701 | |
Wyatt Allen | c310f4f | 2017-07-05 21:26:33 -0700 | [diff] [blame] | 702 | _getChangePath(change, patchRange, revisions) { |
| 703 | const range = this._getChangeUrlRange(patchRange, revisions); |
| 704 | return Gerrit.Nav.getUrlForChange(change, range.patchNum, |
| 705 | range.basePatchNum); |
| 706 | }, |
| 707 | |
| 708 | _navigateToChange(change, patchRange, revisions) { |
| 709 | const range = this._getChangeUrlRange(patchRange, revisions); |
| 710 | Gerrit.Nav.navigateToChange(change, range.patchNum, range.basePatchNum); |
| 711 | }, |
| 712 | |
| 713 | _computeChangePath(change, patchRangeRecord, revisions) { |
| 714 | return this._getChangePath(change, patchRangeRecord.base, revisions); |
Andrew Bonventre | 91c6293 | 2016-05-11 14:31:49 -0400 | [diff] [blame] | 715 | }, |
| 716 | |
Becky Siegel | 5946f95 | 2017-11-09 11:53:33 -0800 | [diff] [blame] | 717 | _formatFilesForDropdown(fileList, patchNum, changeComments) { |
Becky Siegel | fde2da6 | 2017-11-07 15:49:55 -0800 | [diff] [blame] | 718 | if (!fileList) { return; } |
| 719 | const dropdownContent = []; |
| 720 | for (const path of fileList) { |
| 721 | dropdownContent.push({ |
| 722 | text: this.computeDisplayPath(path), |
| 723 | mobileText: this.computeTruncatedPath(path), |
Becky Siegel | e53ca1c | 2017-11-13 16:23:08 -0800 | [diff] [blame] | 724 | value: path, |
Becky Siegel | 5946f95 | 2017-11-09 11:53:33 -0800 | [diff] [blame] | 725 | bottomText: this._computeCommentString(changeComments, patchNum, |
| 726 | path), |
Becky Siegel | fde2da6 | 2017-11-07 15:49:55 -0800 | [diff] [blame] | 727 | }); |
| 728 | } |
| 729 | return dropdownContent; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 730 | }, |
| 731 | |
Becky Siegel | 5946f95 | 2017-11-09 11:53:33 -0800 | [diff] [blame] | 732 | _computeCommentString(changeComments, patchNum, path) { |
| 733 | const unresolvedCount = changeComments.computeUnresolvedNum(patchNum, |
| 734 | path); |
| 735 | const commentCount = changeComments.computeCommentCount(patchNum, path); |
| 736 | const commentString = GrCountStringFormatter.computePluralString( |
| 737 | commentCount, 'comment'); |
| 738 | const unresolvedString = GrCountStringFormatter.computeString( |
| 739 | unresolvedCount, 'unresolved'); |
| 740 | |
| 741 | return commentString + |
| 742 | // Add a space if both comments and unresolved |
| 743 | (commentString && unresolvedString ? ', ' : '') + |
| 744 | // Add parentheses around unresolved if it exists. |
| 745 | (unresolvedString ? `${unresolvedString}` : ''); |
| 746 | }, |
| 747 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 748 | _computePrefsButtonHidden(prefs, loggedIn) { |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 749 | return !loggedIn || !prefs; |
| 750 | }, |
| 751 | |
Becky Siegel | fde2da6 | 2017-11-07 15:49:55 -0800 | [diff] [blame] | 752 | _handleFileChange(e) { |
| 753 | // This is when it gets set initially. |
| 754 | const path = e.detail.value; |
| 755 | if (path === this._path) { |
| 756 | return; |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 757 | } |
Becky Siegel | fde2da6 | 2017-11-07 15:49:55 -0800 | [diff] [blame] | 758 | |
| 759 | Gerrit.Nav.navigateToDiff(this._change, path, this._patchRange.patchNum, |
| 760 | this._patchRange.basePatchNum); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 761 | }, |
| 762 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 763 | _handleFileTap(e) { |
Becky Siegel | 0304907 | 2017-04-06 14:24:48 -0700 | [diff] [blame] | 764 | // async is needed so that that the click event is fired before the |
| 765 | // dropdown closes (This was a bug for touch devices). |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 766 | this.async(() => { |
Becky Siegel | 0304907 | 2017-04-06 14:24:48 -0700 | [diff] [blame] | 767 | this.$.dropdown.close(); |
| 768 | }, 1); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 769 | }, |
| 770 | |
Becky Siegel | af950fd | 2017-10-12 11:35:18 -0700 | [diff] [blame] | 771 | _handlePatchChange(e) { |
| 772 | const {basePatchNum, patchNum} = e.detail; |
| 773 | if (this.patchNumEquals(basePatchNum, this._patchRange.basePatchNum) && |
| 774 | this.patchNumEquals(patchNum, this._patchRange.patchNum)) { return; } |
Becky Siegel | 89fe050 | 2017-09-12 16:54:19 -0700 | [diff] [blame] | 775 | Gerrit.Nav.navigateToDiff( |
Becky Siegel | fd7c71f | 2017-09-26 09:59:33 -0700 | [diff] [blame] | 776 | this._change, this._path, patchNum, basePatchNum); |
Becky Siegel | 89fe050 | 2017-09-12 16:54:19 -0700 | [diff] [blame] | 777 | }, |
| 778 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 779 | _handlePrefsTap(e) { |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 780 | e.preventDefault(); |
Becky Siegel | 7a45400 | 2017-04-19 12:06:43 -0700 | [diff] [blame] | 781 | this.$.diffPreferences.open(); |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 782 | }, |
| 783 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 784 | _handlePrefsSave(e) { |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 785 | e.stopPropagation(); |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 786 | const el = Polymer.dom(e).rootTarget; |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 787 | el.disabled = true; |
Viktar Donich | 5008b44 | 2016-06-08 12:37:31 -0700 | [diff] [blame] | 788 | this.$.storage.savePreferences(this._localPrefs); |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 789 | this._saveDiffPreferences().then(response => { |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 790 | el.disabled = false; |
Andrew Bonventre | 8f23107 | 2016-05-02 15:59:59 -0400 | [diff] [blame] | 791 | if (!response.ok) { return response; } |
| 792 | |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 793 | this.$.prefsOverlay.close(); |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 794 | }).catch(err => { |
Andrew Bonventre | 8f23107 | 2016-05-02 15:59:59 -0400 | [diff] [blame] | 795 | el.disabled = false; |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 796 | }); |
Andrew Bonventre | c80291c | 2016-04-23 23:18:43 -0400 | [diff] [blame] | 797 | }, |
| 798 | |
Wyatt Allen | f93f1c24 | 2016-05-05 16:53:38 -0700 | [diff] [blame] | 799 | /** |
| 800 | * _getDiffViewMode: Get the diff view (side-by-side or unified) based on |
| 801 | * the current state. |
| 802 | * |
| 803 | * The expected behavior is to use the mode specified in the user's |
Becky Siegel | 76ec596 | 2016-12-09 16:58:31 -0800 | [diff] [blame] | 804 | * preferences unless they have manually chosen the alternative view or they |
| 805 | * are on a mobile device. If the user navigates up to the change view, it |
| 806 | * should clear this choice and revert to the preference the next time a |
| 807 | * diff is viewed. |
Wyatt Allen | f93f1c24 | 2016-05-05 16:53:38 -0700 | [diff] [blame] | 808 | * |
| 809 | * Use side-by-side if the user is not logged in. |
| 810 | * |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 811 | * @return {string} |
Wyatt Allen | f93f1c24 | 2016-05-05 16:53:38 -0700 | [diff] [blame] | 812 | */ |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 813 | _getDiffViewMode() { |
Wyatt Allen | f93f1c24 | 2016-05-05 16:53:38 -0700 | [diff] [blame] | 814 | if (this.changeViewState.diffMode) { |
| 815 | return this.changeViewState.diffMode; |
Becky Siegel | 76ec596 | 2016-12-09 16:58:31 -0800 | [diff] [blame] | 816 | } else if (this._userPrefs) { |
Wyatt Allen | ffae724 | 2018-02-14 15:58:17 -0800 | [diff] [blame^] | 817 | this.set('changeViewState.diffMode', this._userPrefs.default_diff_view); |
| 818 | return this._userPrefs.default_diff_view; |
Becky Siegel | 76ec596 | 2016-12-09 16:58:31 -0800 | [diff] [blame] | 819 | } else { |
| 820 | return 'SIDE_BY_SIDE'; |
Wyatt Allen | f93f1c24 | 2016-05-05 16:53:38 -0700 | [diff] [blame] | 821 | } |
Wyatt Allen | f93f1c24 | 2016-05-05 16:53:38 -0700 | [diff] [blame] | 822 | }, |
| 823 | |
Wyatt Allen | ffae724 | 2018-02-14 15:58:17 -0800 | [diff] [blame^] | 824 | _computeModeSelectHideClass(isImageDiff) { |
| 825 | return isImageDiff ? 'hide' : ''; |
Wyatt Allen | 88678da | 2016-05-23 17:18:43 -0700 | [diff] [blame] | 826 | }, |
Wyatt Allen | a68b48e | 2016-06-20 14:02:42 -0700 | [diff] [blame] | 827 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 828 | _onLineSelected(e, detail) { |
Wyatt Allen | a68b48e | 2016-06-20 14:02:42 -0700 | [diff] [blame] | 829 | this.$.cursor.moveToLineNumber(detail.number, detail.side); |
Wyatt Allen | aea9974 | 2017-08-22 13:37:13 -0700 | [diff] [blame] | 830 | if (!this._change) { return; } |
| 831 | const cursorAddress = this.$.cursor.getAddress(); |
Wyatt Allen | 1bd466a | 2017-09-15 11:55:35 -0700 | [diff] [blame] | 832 | const number = cursorAddress ? cursorAddress.number : undefined; |
| 833 | const leftSide = cursorAddress ? cursorAddress.leftSide : undefined; |
Wyatt Allen | aea9974 | 2017-08-22 13:37:13 -0700 | [diff] [blame] | 834 | const url = Gerrit.Nav.getUrlForDiffById(this._changeNum, |
| 835 | this._change.project, this._path, this._patchRange.patchNum, |
Wyatt Allen | 1bd466a | 2017-09-15 11:55:35 -0700 | [diff] [blame] | 836 | this._patchRange.basePatchNum, number, leftSide); |
Wyatt Allen | aea9974 | 2017-08-22 13:37:13 -0700 | [diff] [blame] | 837 | history.replaceState(null, '', url); |
Wyatt Allen | a68b48e | 2016-06-20 14:02:42 -0700 | [diff] [blame] | 838 | }, |
Kasper Nilsson | 0deb7e3 | 2016-08-30 12:44:27 -0700 | [diff] [blame] | 839 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 840 | _computeDownloadLink(changeNum, patchRange, path) { |
| 841 | let url = this.changeBaseURL(changeNum, patchRange.patchNum); |
Kasper Nilsson | c59e88058 | 2016-10-21 11:01:08 -0700 | [diff] [blame] | 842 | url += '/patch?zip&path=' + encodeURIComponent(path); |
| 843 | return url; |
| 844 | }, |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 845 | |
Wyatt Allen | dc8782d | 2017-07-26 09:32:03 -0700 | [diff] [blame] | 846 | _loadComments() { |
Becky Siegel | 2033dfd | 2017-11-08 12:02:04 -0800 | [diff] [blame] | 847 | return this.$.commentAPI.loadAll(this._changeNum).then(comments => { |
| 848 | this._changeComments = comments; |
Becky Siegel | c7f07dc | 2017-11-02 16:07:13 -0700 | [diff] [blame] | 849 | this._commentMap = this._getPaths(this._patchRange); |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 850 | |
Becky Siegel | c7f07dc | 2017-11-02 16:07:13 -0700 | [diff] [blame] | 851 | this._commentsForDiff = this._getCommentsForPath(this._path, |
Wyatt Allen | dc8782d | 2017-07-26 09:32:03 -0700 | [diff] [blame] | 852 | this._patchRange, this._projectConfig); |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 853 | }); |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 854 | }, |
| 855 | |
Becky Siegel | c7f07dc | 2017-11-02 16:07:13 -0700 | [diff] [blame] | 856 | _getPaths(patchRange) { |
| 857 | return this._changeComments.getPaths(patchRange); |
| 858 | }, |
| 859 | |
| 860 | _getCommentsForPath(path, patchRange, projectConfig) { |
Becky Siegel | 57e4fb3 | 2017-11-09 13:21:11 -0800 | [diff] [blame] | 861 | return this._changeComments.getCommentsBySideForPath(path, patchRange, |
Becky Siegel | c7f07dc | 2017-11-02 16:07:13 -0700 | [diff] [blame] | 862 | projectConfig); |
| 863 | }, |
| 864 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 865 | _getDiffDrafts() { |
Wyatt Allen | c61ba25 | 2017-07-31 18:43:21 -0700 | [diff] [blame] | 866 | return this.$.restAPI.getDiffDrafts(this._changeNum); |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 867 | }, |
| 868 | |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 869 | _computeCommentSkips(commentMap, fileList, path) { |
| 870 | const skips = {previous: null, next: null}; |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 871 | if (!fileList.length) { return skips; } |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 872 | const pathIndex = fileList.indexOf(path); |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 873 | |
| 874 | // Scan backward for the previous file. |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 875 | for (let i = pathIndex - 1; i >= 0; i--) { |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 876 | if (commentMap[fileList[i]]) { |
| 877 | skips.previous = fileList[i]; |
| 878 | break; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | // Scan forward for the next file. |
Kasper Nilsson | ef51194 | 2017-05-16 14:38:48 -0700 | [diff] [blame] | 883 | for (let i = pathIndex + 1; i < fileList.length; i++) { |
Wyatt Allen | 1bc4f2f | 2017-01-18 17:10:51 -0800 | [diff] [blame] | 884 | if (commentMap[fileList[i]]) { |
| 885 | skips.next = fileList[i]; |
| 886 | break; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | return skips; |
| 891 | }, |
Viktar Donich | 168bbc9 | 2017-06-19 14:50:09 -0700 | [diff] [blame] | 892 | |
| 893 | _computeDiffClass(panelFloatingDisabled) { |
| 894 | if (panelFloatingDisabled) { |
| 895 | return 'noOverflow'; |
| 896 | } |
| 897 | }, |
Kasper Nilsson | 5f1f0c6 | 2017-08-08 14:04:54 -0700 | [diff] [blame] | 898 | |
| 899 | /** |
| 900 | * @param {!Object} patchRangeRecord |
| 901 | */ |
Kasper Nilsson | 808838e | 2018-01-23 17:15:38 -0800 | [diff] [blame] | 902 | _computeEditMode(patchRangeRecord) { |
Kasper Nilsson | 5f1f0c6 | 2017-08-08 14:04:54 -0700 | [diff] [blame] | 903 | const patchRange = patchRangeRecord.base || {}; |
| 904 | return this.patchNumEquals(patchRange.patchNum, this.EDIT_NAME); |
| 905 | }, |
| 906 | |
| 907 | /** |
Kasper Nilsson | 808838e | 2018-01-23 17:15:38 -0800 | [diff] [blame] | 908 | * @param {boolean} editMode |
Kasper Nilsson | 5f1f0c6 | 2017-08-08 14:04:54 -0700 | [diff] [blame] | 909 | */ |
Kasper Nilsson | 808838e | 2018-01-23 17:15:38 -0800 | [diff] [blame] | 910 | _computeContainerClass(editMode) { |
| 911 | return editMode ? 'editMode' : ''; |
Kasper Nilsson | 5f1f0c6 | 2017-08-08 14:04:54 -0700 | [diff] [blame] | 912 | }, |
Wyatt Allen | ed628d7 | 2017-09-14 09:45:50 -0700 | [diff] [blame] | 913 | |
| 914 | _computeBlameToggleLabel(loaded, loading) { |
| 915 | if (loaded) { return 'Hide blame'; } |
| 916 | return 'Show blame'; |
| 917 | }, |
| 918 | |
| 919 | /** |
| 920 | * Load and display blame information if it has not already been loaded. |
| 921 | * Otherwise hide it. |
| 922 | */ |
| 923 | _toggleBlame() { |
| 924 | if (this._isBlameLoaded) { |
| 925 | this.$.diff.clearBlame(); |
| 926 | return; |
| 927 | } |
| 928 | |
| 929 | this._isBlameLoading = true; |
| 930 | this.fire('show-alert', {message: MSG_LOADING_BLAME}); |
| 931 | this.$.diff.loadBlame() |
| 932 | .then(() => { |
| 933 | this._isBlameLoading = false; |
| 934 | this.fire('show-alert', {message: MSG_LOADED_BLAME}); |
| 935 | }) |
| 936 | .catch(() => { |
| 937 | this._isBlameLoading = false; |
| 938 | }); |
| 939 | }, |
| 940 | |
| 941 | _computeBlameLoaderClass(isImageDiff, supported) { |
| 942 | return !isImageDiff && supported ? 'show' : ''; |
| 943 | }, |
Wyatt Allen | a744e23 | 2017-11-08 15:33:48 -0800 | [diff] [blame] | 944 | |
| 945 | _getRevisionInfo(change) { |
| 946 | return new Gerrit.RevisionInfo(change); |
| 947 | }, |
Viktar Donich | c5069b1 | 2018-01-30 16:17:13 -0800 | [diff] [blame] | 948 | |
| 949 | _computeFileNum(file, files) { |
| 950 | return files.findIndex(({value}) => value === file) + 1; |
| 951 | }, |
| 952 | |
| 953 | _computeFileNumVisible(file, files) { |
| 954 | if (!files) { return 'hidden'; } |
| 955 | const fileNum = this._computeFileNum(file, files); |
| 956 | if (!isNaN(fileNum) && isFinite(fileNum) && fileNum > 0) { |
| 957 | return ''; |
| 958 | } else { |
| 959 | return 'hidden'; |
| 960 | } |
| 961 | }, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 962 | }); |
| 963 | })(); |