Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright (C) 2016 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 17 | (function() { |
| 18 | 'use strict'; |
| 19 | |
Logan Hanks | c34e0b6 | 2017-10-03 01:40:54 -0700 | [diff] [blame] | 20 | const PROJECT_PLACEHOLDER_PATTERN = /\$\{project\}/g; |
Kasper Nilsson | 8be6f1a | 2017-05-30 12:49:01 -0700 | [diff] [blame] | 21 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 22 | Polymer({ |
| 23 | is: 'gr-dashboard-view', |
| 24 | |
| 25 | /** |
| 26 | * Fired when the title of the page should change. |
| 27 | * |
| 28 | * @event title-change |
| 29 | */ |
| 30 | |
| 31 | properties: { |
Paladox none | ebc64ef | 2018-04-06 13:57:21 +0000 | [diff] [blame] | 32 | account: { |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 33 | type: Object, |
Wyatt Allen | 24caf0a | 2018-03-02 16:16:54 -0800 | [diff] [blame] | 34 | value: null, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 35 | }, |
Kasper Nilsson | 8e6a64d | 2018-04-04 15:33:19 -0700 | [diff] [blame] | 36 | preferences: Object, |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 37 | /** @type {{ selectedChangeIndex: number }} */ |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 38 | viewState: Object, |
Wyatt Allen | 809fef2 | 2017-10-03 10:41:32 +0100 | [diff] [blame] | 39 | |
Logan Hanks | 894eebd | 2018-11-13 11:54:30 -0800 | [diff] [blame] | 40 | /** @type {{ project: string, user: string }} */ |
Becky Siegel | 2c94476 | 2017-01-11 14:18:56 -0800 | [diff] [blame] | 41 | params: { |
| 42 | type: Object, |
Becky Siegel | 2c94476 | 2017-01-11 14:18:56 -0800 | [diff] [blame] | 43 | }, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 44 | |
Kasper Nilsson | 57c1a28 | 2018-10-04 13:38:36 -0700 | [diff] [blame] | 45 | createChangeTap: { |
| 46 | type: Function, |
| 47 | value() { |
| 48 | return this._createChangeTap.bind(this); |
| 49 | }, |
| 50 | }, |
| 51 | |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 52 | _results: Array, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * For showing a "loading..." string during ajax requests. |
| 56 | */ |
| 57 | _loading: { |
| 58 | type: Boolean, |
| 59 | value: true, |
| 60 | }, |
Kasper Nilsson | 0dac13a | 2018-09-20 15:36:08 -0700 | [diff] [blame] | 61 | |
| 62 | _showDraftsBanner: { |
| 63 | type: Boolean, |
| 64 | value: false, |
| 65 | }, |
Logan Hanks | 911b9b3 | 2018-09-20 08:29:32 -0700 | [diff] [blame] | 66 | |
| 67 | _showNewUserHelp: { |
| 68 | type: Boolean, |
| 69 | value: false, |
| 70 | }, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 71 | }, |
| 72 | |
Logan Hanks | 9ccf9d0 | 2017-08-11 14:17:36 -0700 | [diff] [blame] | 73 | observers: [ |
Logan Hanks | aa501d0 | 2017-09-30 04:07:34 -0700 | [diff] [blame] | 74 | '_paramsChanged(params.*)', |
Logan Hanks | 9ccf9d0 | 2017-08-11 14:17:36 -0700 | [diff] [blame] | 75 | ], |
| 76 | |
Kasper Nilsson | 8be6f1a | 2017-05-30 12:49:01 -0700 | [diff] [blame] | 77 | behaviors: [ |
Paladox none | 44b3a7c | 2019-08-06 14:36:29 +0000 | [diff] [blame] | 78 | Gerrit.FireBehavior, |
Kasper Nilsson | 8be6f1a | 2017-05-30 12:49:01 -0700 | [diff] [blame] | 79 | Gerrit.RESTClientBehavior, |
| 80 | ], |
| 81 | |
| 82 | get options() { |
| 83 | return this.listChangesOptionsToHex( |
| 84 | this.ListChangesOption.LABELS, |
| 85 | this.ListChangesOption.DETAILED_ACCOUNTS, |
| 86 | this.ListChangesOption.REVIEWED |
| 87 | ); |
| 88 | }, |
| 89 | |
Kasper Nilsson | 8e6a64d | 2018-04-04 15:33:19 -0700 | [diff] [blame] | 90 | attached() { |
| 91 | this._loadPreferences(); |
| 92 | }, |
| 93 | |
| 94 | _loadPreferences() { |
| 95 | return this.$.restAPI.getLoggedIn().then(loggedIn => { |
| 96 | if (loggedIn) { |
| 97 | this.$.restAPI.getPreferences().then(preferences => { |
| 98 | this.preferences = preferences; |
| 99 | }); |
| 100 | } else { |
| 101 | this.preferences = {}; |
| 102 | } |
| 103 | }); |
| 104 | }, |
| 105 | |
Logan Hanks | c34e0b6 | 2017-10-03 01:40:54 -0700 | [diff] [blame] | 106 | _getProjectDashboard(project, dashboard) { |
| 107 | const errFn = response => { |
| 108 | this.fire('page-error', {response}); |
| 109 | }; |
| 110 | return this.$.restAPI.getDashboard( |
| 111 | project, dashboard, errFn).then(response => { |
| 112 | if (!response) { |
| 113 | return; |
| 114 | } |
| 115 | return { |
| 116 | title: response.title, |
| 117 | sections: response.sections.map(section => { |
| 118 | const suffix = response.foreach ? ' ' + response.foreach : ''; |
| 119 | return { |
| 120 | name: section.name, |
Paladox none | abbd26d | 2019-09-25 15:41:36 +0000 | [diff] [blame] | 121 | query: (section.query + suffix).replace( |
| 122 | PROJECT_PLACEHOLDER_PATTERN, project), |
Logan Hanks | c34e0b6 | 2017-10-03 01:40:54 -0700 | [diff] [blame] | 123 | }; |
| 124 | }), |
| 125 | }; |
| 126 | }); |
| 127 | }, |
| 128 | |
Logan Hanks | e6458a2 | 2017-08-11 14:41:57 -0700 | [diff] [blame] | 129 | _computeTitle(user) { |
Logan Hanks | c34e0b6 | 2017-10-03 01:40:54 -0700 | [diff] [blame] | 130 | if (!user || user === 'self') { |
Logan Hanks | e6458a2 | 2017-08-11 14:41:57 -0700 | [diff] [blame] | 131 | return 'My Reviews'; |
| 132 | } |
| 133 | return 'Dashboard for ' + user; |
Becky Siegel | 2c94476 | 2017-01-11 14:18:56 -0800 | [diff] [blame] | 134 | }, |
Andrew Bonventre | 0c76ebb | 2016-05-03 22:24:25 -0400 | [diff] [blame] | 135 | |
Logan Hanks | c34e0b6 | 2017-10-03 01:40:54 -0700 | [diff] [blame] | 136 | _isViewActive(params) { |
| 137 | return params.view === Gerrit.Nav.View.DASHBOARD; |
| 138 | }, |
| 139 | |
Logan Hanks | aa501d0 | 2017-09-30 04:07:34 -0700 | [diff] [blame] | 140 | _paramsChanged(paramsChangeRecord) { |
| 141 | const params = paramsChangeRecord.base; |
| 142 | |
Logan Hanks | c34e0b6 | 2017-10-03 01:40:54 -0700 | [diff] [blame] | 143 | if (!this._isViewActive(params)) { |
| 144 | return Promise.resolve(); |
Logan Hanks | aa501d0 | 2017-09-30 04:07:34 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Kasper Nilsson | f5136ec | 2018-08-17 14:11:09 -0700 | [diff] [blame] | 147 | return this._reload(); |
| 148 | }, |
Logan Hanks | aa501d0 | 2017-09-30 04:07:34 -0700 | [diff] [blame] | 149 | |
Kasper Nilsson | f5136ec | 2018-08-17 14:11:09 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Reloads the element. |
| 152 | * |
| 153 | * @return {Promise<!Object>} |
| 154 | */ |
| 155 | _reload() { |
Andrew Bonventre | 0c76ebb | 2016-05-03 22:24:25 -0400 | [diff] [blame] | 156 | this._loading = true; |
Kasper Nilsson | f5136ec | 2018-08-17 14:11:09 -0700 | [diff] [blame] | 157 | const {project, dashboard, title, user, sections} = this.params; |
| 158 | const dashboardPromise = project ? |
| 159 | this._getProjectDashboard(project, dashboard) : |
Kasper Nilsson | 6750cf9 | 2018-09-17 13:43:12 -0700 | [diff] [blame] | 160 | Promise.resolve(Gerrit.Nav.getUserDashboard( |
| 161 | user, |
| 162 | sections, |
| 163 | title || this._computeTitle(user))); |
Logan Hanks | c34e0b6 | 2017-10-03 01:40:54 -0700 | [diff] [blame] | 164 | |
Logan Hanks | 911b9b3 | 2018-09-20 08:29:32 -0700 | [diff] [blame] | 165 | const checkForNewUser = !project && user === 'self'; |
| 166 | return dashboardPromise |
Tao Zhou | 02ac639 | 2019-10-01 13:39:17 +0200 | [diff] [blame] | 167 | .then(res => { |
| 168 | if (res && res.title) { |
| 169 | this.fire('title-change', {title: res.title}); |
| 170 | } |
| 171 | return this._fetchDashboardChanges(res, checkForNewUser); |
| 172 | }) |
Kasper Nilsson | f5136ec | 2018-08-17 14:11:09 -0700 | [diff] [blame] | 173 | .then(() => { |
Kasper Nilsson | 0dac13a | 2018-09-20 15:36:08 -0700 | [diff] [blame] | 174 | this._maybeShowDraftsBanner(); |
Kasper Nilsson | f5136ec | 2018-08-17 14:11:09 -0700 | [diff] [blame] | 175 | this.$.reporting.dashboardDisplayed(); |
| 176 | }).catch(err => { |
Tao Zhou | 02ac639 | 2019-10-01 13:39:17 +0200 | [diff] [blame] | 177 | this.fire('title-change', { |
| 178 | title: title || this._computeTitle(user), |
| 179 | }); |
Kasper Nilsson | f5136ec | 2018-08-17 14:11:09 -0700 | [diff] [blame] | 180 | console.warn(err); |
Wyatt Allen | 4ec5615 | 2018-08-28 11:47:13 -0700 | [diff] [blame] | 181 | }).then(() => { this._loading = false; }); |
Kasper Nilsson | f5136ec | 2018-08-17 14:11:09 -0700 | [diff] [blame] | 182 | }, |
| 183 | |
| 184 | /** |
| 185 | * Fetches the changes for each dashboard section and sets this._results |
| 186 | * with the response. |
| 187 | * |
| 188 | * @param {!Object} res |
Logan Hanks | 911b9b3 | 2018-09-20 08:29:32 -0700 | [diff] [blame] | 189 | * @param {boolean} checkForNewUser |
Kasper Nilsson | f5136ec | 2018-08-17 14:11:09 -0700 | [diff] [blame] | 190 | * @return {Promise} |
| 191 | */ |
Logan Hanks | 911b9b3 | 2018-09-20 08:29:32 -0700 | [diff] [blame] | 192 | _fetchDashboardChanges(res, checkForNewUser) { |
Kasper Nilsson | f5136ec | 2018-08-17 14:11:09 -0700 | [diff] [blame] | 193 | if (!res) { return Promise.resolve(); } |
Logan Hanks | 911b9b3 | 2018-09-20 08:29:32 -0700 | [diff] [blame] | 194 | |
| 195 | const queries = res.sections |
| 196 | .map(section => section.suffixForDashboard ? |
| 197 | section.query + ' ' + section.suffixForDashboard : |
| 198 | section.query); |
| 199 | |
| 200 | if (checkForNewUser) { |
Milutin Kristofic | 5f31b76 | 2019-10-07 15:17:31 +0000 | [diff] [blame] | 201 | queries.push('owner:self limit:1'); |
Logan Hanks | 911b9b3 | 2018-09-20 08:29:32 -0700 | [diff] [blame] | 202 | } |
Kasper Nilsson | f5136ec | 2018-08-17 14:11:09 -0700 | [diff] [blame] | 203 | |
| 204 | return this.$.restAPI.getChanges(null, queries, null, this.options) |
| 205 | .then(changes => { |
Logan Hanks | 911b9b3 | 2018-09-20 08:29:32 -0700 | [diff] [blame] | 206 | if (checkForNewUser) { |
| 207 | // Last set of results is not meant for dashboard display. |
| 208 | const lastResultSet = changes.pop(); |
| 209 | this._showNewUserHelp = lastResultSet.length == 0; |
| 210 | } |
Kasper Nilsson | f5136ec | 2018-08-17 14:11:09 -0700 | [diff] [blame] | 211 | this._results = changes.map((results, i) => ({ |
Milutin Kristofic | 497380f | 2019-10-09 16:19:54 +0200 | [diff] [blame] | 212 | name: this._computeSectionName(res.sections[i].name, results), |
Kasper Nilsson | f5136ec | 2018-08-17 14:11:09 -0700 | [diff] [blame] | 213 | query: res.sections[i].query, |
| 214 | results, |
Logan Hanks | 1d3dc2a | 2018-09-19 13:18:59 -0700 | [diff] [blame] | 215 | isOutgoing: res.sections[i].isOutgoing, |
Logan Hanks | 911b9b3 | 2018-09-20 08:29:32 -0700 | [diff] [blame] | 216 | })).filter((section, i) => i < res.sections.length && ( |
| 217 | !res.sections[i].hideIfEmpty || |
| 218 | section.results.length)); |
Kasper Nilsson | f5136ec | 2018-08-17 14:11:09 -0700 | [diff] [blame] | 219 | }); |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 220 | }, |
Logan Hanks | 2eaf855 | 2017-07-17 10:15:39 -0700 | [diff] [blame] | 221 | |
Milutin Kristofic | 497380f | 2019-10-09 16:19:54 +0200 | [diff] [blame] | 222 | _computeSectionName(name, changes) { |
| 223 | if (!changes || !changes.length || changes.length == 0) { |
| 224 | return name; |
| 225 | } |
| 226 | const more = changes[changes.length - 1]._more_changes; |
| 227 | const numChanges = changes.length; |
| 228 | const andMore = more ? ' and more' : ''; |
| 229 | const changeLabel = `change${numChanges > 1 ? 's' : ''}`; |
| 230 | return `${name} (${numChanges} ${changeLabel}${andMore})`; |
| 231 | }, |
| 232 | |
Logan Hanks | 894eebd | 2018-11-13 11:54:30 -0800 | [diff] [blame] | 233 | _computeUserHeaderClass(params) { |
| 234 | if (!params || !!params.project || !params.user |
| 235 | || params.user === 'self') { |
| 236 | return 'hide'; |
| 237 | } |
| 238 | return ''; |
Wyatt Allen | 809fef2 | 2017-10-03 10:41:32 +0100 | [diff] [blame] | 239 | }, |
Kasper Nilsson | 08ab3aa | 2018-07-12 11:15:41 -0700 | [diff] [blame] | 240 | |
| 241 | _handleToggleStar(e) { |
| 242 | this.$.restAPI.saveChangeStarred(e.detail.change._number, |
| 243 | e.detail.starred); |
| 244 | }, |
Sean Egan | 3278d7b | 2018-07-06 11:02:31 -0400 | [diff] [blame] | 245 | |
| 246 | _handleToggleReviewed(e) { |
| 247 | this.$.restAPI.saveChangeReviewed(e.detail.change._number, |
| 248 | e.detail.reviewed); |
| 249 | }, |
Kasper Nilsson | 0dac13a | 2018-09-20 15:36:08 -0700 | [diff] [blame] | 250 | |
| 251 | /** |
| 252 | * Banner is shown if a user is on their own dashboard and they have draft |
| 253 | * comments on closed changes. |
| 254 | */ |
| 255 | _maybeShowDraftsBanner() { |
| 256 | this._showDraftsBanner = false; |
| 257 | if (!(this.params.user === 'self')) { return; } |
| 258 | |
| 259 | const draftSection = this._results |
| 260 | .find(section => section.query === 'has:draft'); |
| 261 | if (!draftSection || !draftSection.results.length) { return; } |
| 262 | |
| 263 | const closedChanges = draftSection.results |
Paladox none | 7dca128 | 2019-07-09 22:45:44 +0000 | [diff] [blame] | 264 | .filter(change => !this.changeIsOpen(change)); |
Kasper Nilsson | 0dac13a | 2018-09-20 15:36:08 -0700 | [diff] [blame] | 265 | if (!closedChanges.length) { return; } |
| 266 | |
| 267 | this._showDraftsBanner = true; |
| 268 | }, |
| 269 | |
| 270 | _computeBannerClass(show) { |
| 271 | return show ? '' : 'hide'; |
| 272 | }, |
| 273 | |
| 274 | _handleOpenDeleteDialog() { |
| 275 | this.$.confirmDeleteOverlay.open(); |
| 276 | }, |
| 277 | |
| 278 | _handleConfirmDelete() { |
| 279 | this.$.confirmDeleteDialog.disabled = true; |
| 280 | return this.$.restAPI.deleteDraftComments('-is:open').then(() => { |
| 281 | this._closeConfirmDeleteOverlay(); |
| 282 | this._reload(); |
| 283 | }); |
| 284 | }, |
| 285 | |
| 286 | _closeConfirmDeleteOverlay() { |
| 287 | this.$.confirmDeleteOverlay.close(); |
| 288 | }, |
Kasper Nilsson | 4fa21ad | 2018-09-26 10:25:27 -0700 | [diff] [blame] | 289 | |
| 290 | _computeDraftsLink() { |
| 291 | return Gerrit.Nav.getUrlForSearchQuery('has:draft -is:open'); |
| 292 | }, |
Logan Hanks | 1d3dc2a | 2018-09-19 13:18:59 -0700 | [diff] [blame] | 293 | |
Kasper Nilsson | 57c1a28 | 2018-10-04 13:38:36 -0700 | [diff] [blame] | 294 | _createChangeTap(e) { |
Logan Hanks | 1d3dc2a | 2018-09-19 13:18:59 -0700 | [diff] [blame] | 295 | this.$.destinationDialog.open(); |
| 296 | }, |
| 297 | |
| 298 | _handleDestinationConfirm(e) { |
| 299 | this.$.commandsDialog.branch = e.detail.branch; |
| 300 | this.$.commandsDialog.open(); |
| 301 | }, |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 302 | }); |
| 303 | })(); |