Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright (C) 2017 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 | */ |
Becky Siegel | 3f88785 | 2017-02-02 09:09:45 -0800 | [diff] [blame] | 17 | (function() { |
| 18 | 'use strict'; |
| 19 | |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 20 | |
Becky Siegel | 805a0af | 2018-03-22 17:08:40 -0700 | [diff] [blame] | 21 | const INTERNAL_GROUP_REGEX = /^[\da-f]{40}$/; |
| 22 | |
Becky Siegel | 3f88785 | 2017-02-02 09:09:45 -0800 | [diff] [blame] | 23 | Polymer({ |
| 24 | is: 'gr-admin-view', |
Becky Siegel | 3f88785 | 2017-02-02 09:09:45 -0800 | [diff] [blame] | 25 | |
| 26 | properties: { |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 27 | /** @type {?} */ |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 28 | params: Object, |
Becky Siegel | 3f88785 | 2017-02-02 09:09:45 -0800 | [diff] [blame] | 29 | path: String, |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 30 | adminView: String, |
| 31 | |
Becky Siegel | e88c3a2 | 2018-04-20 10:08:01 +0200 | [diff] [blame] | 32 | _breadcrumbParentName: String, |
Paladox none | 2bd5c21 | 2017-11-16 18:54:02 +0000 | [diff] [blame] | 33 | _repoName: String, |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 34 | _groupId: { |
| 35 | type: Number, |
| 36 | observer: '_computeGroupName', |
| 37 | }, |
Becky Siegel | 805a0af | 2018-03-22 17:08:40 -0700 | [diff] [blame] | 38 | _groupIsInternal: Boolean, |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 39 | _groupName: String, |
Paladox none | f7b5851 | 2017-08-05 14:52:27 +0000 | [diff] [blame] | 40 | _groupOwner: { |
| 41 | type: Boolean, |
| 42 | value: false, |
| 43 | }, |
Becky Siegel | cad4bf8 | 2018-04-18 16:43:05 +0200 | [diff] [blame] | 44 | _subsectionLinks: Array, |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 45 | _filteredLinks: Array, |
| 46 | _showDownload: { |
| 47 | type: Boolean, |
| 48 | value: false, |
| 49 | }, |
Paladox none | fc2ff25 | 2017-08-21 18:00:20 +0000 | [diff] [blame] | 50 | _isAdmin: { |
| 51 | type: Boolean, |
| 52 | value: false, |
| 53 | }, |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 54 | _showGroup: Boolean, |
Paladox none | 8608828 | 2017-07-15 10:49:08 +0000 | [diff] [blame] | 55 | _showGroupAuditLog: Boolean, |
Paladox none | a0a3b0e | 2017-06-18 20:04:55 +0000 | [diff] [blame] | 56 | _showGroupList: Boolean, |
Paladox none | e28b73e | 2017-08-04 16:27:20 +0000 | [diff] [blame] | 57 | _showGroupMembers: Boolean, |
Paladox none | 2bd5c21 | 2017-11-16 18:54:02 +0000 | [diff] [blame] | 58 | _showRepoAccess: Boolean, |
| 59 | _showRepoCommands: Boolean, |
Becky Siegel | c588ab7 | 2018-01-16 17:43:10 -0800 | [diff] [blame] | 60 | _showRepoDashboards: Boolean, |
Paladox none | 2bd5c21 | 2017-11-16 18:54:02 +0000 | [diff] [blame] | 61 | _showRepoDetailList: Boolean, |
| 62 | _showRepoMain: Boolean, |
| 63 | _showRepoList: Boolean, |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 64 | _showPluginList: Boolean, |
| 65 | }, |
| 66 | |
| 67 | behaviors: [ |
Becky Siegel | cad4bf8 | 2018-04-18 16:43:05 +0200 | [diff] [blame] | 68 | Gerrit.AdminNavBehavior, |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 69 | Gerrit.BaseUrlBehavior, |
Becky Siegel | 3c155f8f | 2017-06-26 13:08:07 -0700 | [diff] [blame] | 70 | Gerrit.URLEncodingBehavior, |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 71 | ], |
| 72 | |
| 73 | observers: [ |
| 74 | '_paramsChanged(params)', |
| 75 | ], |
| 76 | |
| 77 | attached() { |
| 78 | this.reload(); |
| 79 | }, |
| 80 | |
| 81 | reload() { |
Wyatt Allen | c148593 | 2018-03-30 10:53:36 -0700 | [diff] [blame] | 82 | const promises = [ |
| 83 | this.$.restAPI.getAccount(), |
| 84 | Gerrit.awaitPluginsLoaded(), |
| 85 | ]; |
| 86 | return Promise.all(promises).then(result => { |
| 87 | this._account = result[0]; |
Becky Siegel | cad4bf8 | 2018-04-18 16:43:05 +0200 | [diff] [blame] | 88 | let options; |
| 89 | if (this._repoName) { |
| 90 | options = {repoName: this._repoName}; |
| 91 | } else if (this._groupId) { |
| 92 | options = { |
| 93 | groupId: this._groupId, |
| 94 | groupName: this._groupName, |
| 95 | groupIsInternal: this._groupIsInternal, |
| 96 | isAdmin: this._isAdmin, |
| 97 | groupOwner: this._groupOwner, |
| 98 | }; |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 99 | } |
Becky Siegel | cad4bf8 | 2018-04-18 16:43:05 +0200 | [diff] [blame] | 100 | |
| 101 | return this.getAdminLinks(this._account, |
| 102 | this.$.restAPI.getAccountCapabilities.bind(this.$.restAPI), |
| 103 | this.$.jsAPI.getAdminMenuLinks.bind(this.$.jsAPI), |
| 104 | options) |
| 105 | .then(res => { |
| 106 | this._filteredLinks = res.links; |
Becky Siegel | e88c3a2 | 2018-04-20 10:08:01 +0200 | [diff] [blame] | 107 | this._breadcrumbParentName = res.expandedSection ? |
Dmitrii Filippov | b82003c | 2019-11-05 17:02:59 +0100 | [diff] [blame] | 108 | res.expandedSection.name : ''; |
Becky Siegel | cad4bf8 | 2018-04-18 16:43:05 +0200 | [diff] [blame] | 109 | |
| 110 | if (!res.expandedSection) { |
| 111 | this._subsectionLinks = []; |
| 112 | return; |
| 113 | } |
| 114 | this._subsectionLinks = [res.expandedSection] |
Dmitrii Filippov | b82003c | 2019-11-05 17:02:59 +0100 | [diff] [blame] | 115 | .concat(res.expandedSection.children).map(section => { |
| 116 | return { |
| 117 | text: !section.detailType ? 'Home' : section.name, |
| 118 | value: section.view + (section.detailType || ''), |
| 119 | view: section.view, |
| 120 | url: section.url, |
| 121 | detailType: section.detailType, |
| 122 | parent: this._groupId || this._repoName || '', |
| 123 | }; |
| 124 | }); |
Becky Siegel | cad4bf8 | 2018-04-18 16:43:05 +0200 | [diff] [blame] | 125 | }); |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 126 | }); |
| 127 | }, |
| 128 | |
Becky Siegel | e88c3a2 | 2018-04-20 10:08:01 +0200 | [diff] [blame] | 129 | _computeSelectValue(params) { |
| 130 | if (!params || !params.view) { return; } |
| 131 | return params.view + (params.detail || ''); |
| 132 | }, |
| 133 | |
| 134 | _selectedIsCurrentPage(selected) { |
| 135 | return (selected.parent === (this._repoName || this._groupId) && |
| 136 | selected.view === this.params.view && |
| 137 | selected.detailType === this.params.detail); |
| 138 | }, |
| 139 | |
Becky Siegel | cad4bf8 | 2018-04-18 16:43:05 +0200 | [diff] [blame] | 140 | _handleSubsectionChange(e) { |
| 141 | const selected = this._subsectionLinks |
| 142 | .find(section => section.value === e.detail.value); |
Wyatt Allen | c148593 | 2018-03-30 10:53:36 -0700 | [diff] [blame] | 143 | |
Becky Siegel | cad4bf8 | 2018-04-18 16:43:05 +0200 | [diff] [blame] | 144 | // This is when it gets set initially. |
| 145 | if (this._selectedIsCurrentPage(selected)) { |
| 146 | return; |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 147 | } |
Becky Siegel | cad4bf8 | 2018-04-18 16:43:05 +0200 | [diff] [blame] | 148 | Gerrit.Nav.navigateToRelativeUrl(selected.url); |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 149 | }, |
| 150 | |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 151 | _paramsChanged(params) { |
Wyatt Allen | c172767 | 2017-10-19 15:06:54 -0700 | [diff] [blame] | 152 | const isGroupView = params.view === Gerrit.Nav.View.GROUP; |
Paladox none | 4d32197 | 2018-01-07 23:35:07 +0000 | [diff] [blame] | 153 | const isRepoView = params.view === Gerrit.Nav.View.REPO; |
Wyatt Allen | c172767 | 2017-10-19 15:06:54 -0700 | [diff] [blame] | 154 | const isAdminView = params.view === Gerrit.Nav.View.ADMIN; |
| 155 | |
| 156 | this.set('_showGroup', isGroupView && !params.detail); |
| 157 | this.set('_showGroupAuditLog', isGroupView && |
| 158 | params.detail === Gerrit.Nav.GroupDetailView.LOG); |
| 159 | this.set('_showGroupMembers', isGroupView && |
| 160 | params.detail === Gerrit.Nav.GroupDetailView.MEMBERS); |
| 161 | |
| 162 | this.set('_showGroupList', isAdminView && |
| 163 | params.adminView === 'gr-admin-group-list'); |
| 164 | |
Paladox none | 4d32197 | 2018-01-07 23:35:07 +0000 | [diff] [blame] | 165 | this.set('_showRepoAccess', isRepoView && |
| 166 | params.detail === Gerrit.Nav.RepoDetailView.ACCESS); |
| 167 | this.set('_showRepoCommands', isRepoView && |
| 168 | params.detail === Gerrit.Nav.RepoDetailView.COMMANDS); |
| 169 | this.set('_showRepoDetailList', isRepoView && |
| 170 | (params.detail === Gerrit.Nav.RepoDetailView.BRANCHES || |
| 171 | params.detail === Gerrit.Nav.RepoDetailView.TAGS)); |
Becky Siegel | c588ab7 | 2018-01-16 17:43:10 -0800 | [diff] [blame] | 172 | this.set('_showRepoDashboards', isRepoView && |
| 173 | params.detail === Gerrit.Nav.RepoDetailView.DASHBOARDS); |
Paladox none | 4d32197 | 2018-01-07 23:35:07 +0000 | [diff] [blame] | 174 | this.set('_showRepoMain', isRepoView && !params.detail); |
| 175 | |
Paladox none | 2bd5c21 | 2017-11-16 18:54:02 +0000 | [diff] [blame] | 176 | this.set('_showRepoList', isAdminView && |
| 177 | params.adminView === 'gr-repo-list'); |
Paladox none | 4d32197 | 2018-01-07 23:35:07 +0000 | [diff] [blame] | 178 | |
Wyatt Allen | c172767 | 2017-10-19 15:06:54 -0700 | [diff] [blame] | 179 | this.set('_showPluginList', isAdminView && |
| 180 | params.adminView === 'gr-plugin-list'); |
Wyatt Allen | c172767 | 2017-10-19 15:06:54 -0700 | [diff] [blame] | 181 | |
Becky Siegel | e88c3a2 | 2018-04-20 10:08:01 +0200 | [diff] [blame] | 182 | let needsReload = false; |
Paladox none | 2bd5c21 | 2017-11-16 18:54:02 +0000 | [diff] [blame] | 183 | if (params.repo !== this._repoName) { |
| 184 | this._repoName = params.repo || ''; |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 185 | // Reloads the admin menu. |
Becky Siegel | e88c3a2 | 2018-04-20 10:08:01 +0200 | [diff] [blame] | 186 | needsReload = true; |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 187 | } |
| 188 | if (params.groupId !== this._groupId) { |
| 189 | this._groupId = params.groupId || ''; |
Becky Siegel | 3c155f8f | 2017-06-26 13:08:07 -0700 | [diff] [blame] | 190 | // Reloads the admin menu. |
Becky Siegel | e88c3a2 | 2018-04-20 10:08:01 +0200 | [diff] [blame] | 191 | needsReload = true; |
Becky Siegel | 3c155f8f | 2017-06-26 13:08:07 -0700 | [diff] [blame] | 192 | } |
Becky Siegel | e88c3a2 | 2018-04-20 10:08:01 +0200 | [diff] [blame] | 193 | if (this._breadcrumbParentName && !params.groupId && !params.repo) { |
| 194 | needsReload = true; |
| 195 | } |
| 196 | if (!needsReload) { return; } |
| 197 | this.reload(); |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 198 | }, |
| 199 | |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 200 | // TODO (beckysiegel): Update these functions after router abstraction is |
| 201 | // updated. They are currently copied from gr-dropdown (and should be |
| 202 | // updated there as well once complete). |
| 203 | _computeURLHelper(host, path) { |
| 204 | return '//' + host + this.getBaseUrl() + path; |
| 205 | }, |
| 206 | |
| 207 | _computeRelativeURL(path) { |
| 208 | const host = window.location.host; |
| 209 | return this._computeURLHelper(host, path); |
| 210 | }, |
| 211 | |
| 212 | _computeLinkURL(link) { |
Becky Siegel | 3c155f8f | 2017-06-26 13:08:07 -0700 | [diff] [blame] | 213 | if (!link || typeof link.url === 'undefined') { return ''; } |
Wyatt Allen | eeab0ce | 2017-10-30 14:53:15 -0700 | [diff] [blame] | 214 | if (link.target || !link.noBaseUrl) { |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 215 | return link.url; |
| 216 | } |
| 217 | return this._computeRelativeURL(link.url); |
| 218 | }, |
| 219 | |
Becky Siegel | 8d92d53 | 2017-08-11 16:32:47 -0700 | [diff] [blame] | 220 | /** |
| 221 | * @param {string} itemView |
| 222 | * @param {Object} params |
| 223 | * @param {string=} opt_detailType |
| 224 | */ |
Becky Siegel | 5c6a982 | 2017-06-29 10:48:46 -0700 | [diff] [blame] | 225 | _computeSelectedClass(itemView, params, opt_detailType) { |
Paladox none | 0d7e239 | 2019-07-29 18:37:00 +0000 | [diff] [blame] | 226 | if (!params) return ''; |
Wyatt Allen | e9c8adf | 2017-10-30 12:07:42 -0700 | [diff] [blame] | 227 | // Group params are structured differently from admin params. Compute |
| 228 | // selected differently for groups. |
| 229 | // TODO(wyatta): Simplify this when all routes work like group params. |
| 230 | if (params.view === Gerrit.Nav.View.GROUP && |
| 231 | itemView === Gerrit.Nav.View.GROUP) { |
| 232 | if (!params.detail && !opt_detailType) { return 'selected'; } |
| 233 | if (params.detail === opt_detailType) { return 'selected'; } |
| 234 | return ''; |
| 235 | } |
| 236 | |
Becky Siegel | e2ad9d0 | 2018-01-05 11:55:19 -0800 | [diff] [blame] | 237 | if (params.view === Gerrit.Nav.View.REPO && |
| 238 | itemView === Gerrit.Nav.View.REPO) { |
| 239 | if (!params.detail && !opt_detailType) { return 'selected'; } |
| 240 | if (params.detail === opt_detailType) { return 'selected'; } |
| 241 | return ''; |
| 242 | } |
| 243 | |
Becky Siegel | 5c6a982 | 2017-06-29 10:48:46 -0700 | [diff] [blame] | 244 | if (params.detailType && params.detailType !== opt_detailType) { |
| 245 | return ''; |
| 246 | } |
Becky Siegel | f40489e | 2017-06-22 14:14:07 -0700 | [diff] [blame] | 247 | return itemView === params.adminView ? 'selected' : ''; |
Becky Siegel | 3f88785 | 2017-02-02 09:09:45 -0800 | [diff] [blame] | 248 | }, |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 249 | |
| 250 | _computeGroupName(groupId) { |
| 251 | if (!groupId) { return ''; } |
Paladox none | 70cb10c | 2018-02-17 19:12:09 +0000 | [diff] [blame] | 252 | |
Paladox none | fc2ff25 | 2017-08-21 18:00:20 +0000 | [diff] [blame] | 253 | const promises = []; |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 254 | this.$.restAPI.getGroupConfig(groupId).then(group => { |
Paladox none | 70cb10c | 2018-02-17 19:12:09 +0000 | [diff] [blame] | 255 | if (!group || !group.name) { return; } |
| 256 | |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 257 | this._groupName = group.name; |
Becky Siegel | 805a0af | 2018-03-22 17:08:40 -0700 | [diff] [blame] | 258 | this._groupIsInternal = !!group.id.match(INTERNAL_GROUP_REGEX); |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 259 | this.reload(); |
Paladox none | 70cb10c | 2018-02-17 19:12:09 +0000 | [diff] [blame] | 260 | |
Paladox none | fc2ff25 | 2017-08-21 18:00:20 +0000 | [diff] [blame] | 261 | promises.push(this.$.restAPI.getIsAdmin().then(isAdmin => { |
| 262 | this._isAdmin = isAdmin; |
| 263 | })); |
Paladox none | 70cb10c | 2018-02-17 19:12:09 +0000 | [diff] [blame] | 264 | |
Paladox none | fc2ff25 | 2017-08-21 18:00:20 +0000 | [diff] [blame] | 265 | promises.push(this.$.restAPI.getIsGroupOwner(group.name).then( |
Paladox none | e28b73e | 2017-08-04 16:27:20 +0000 | [diff] [blame] | 266 | isOwner => { |
Paladox none | fc2ff25 | 2017-08-21 18:00:20 +0000 | [diff] [blame] | 267 | this._groupOwner = isOwner; |
| 268 | })); |
Paladox none | 70cb10c | 2018-02-17 19:12:09 +0000 | [diff] [blame] | 269 | |
Paladox none | fc2ff25 | 2017-08-21 18:00:20 +0000 | [diff] [blame] | 270 | return Promise.all(promises).then(() => { |
| 271 | this.reload(); |
| 272 | }); |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 273 | }); |
| 274 | }, |
| 275 | |
| 276 | _updateGroupName(e) { |
| 277 | this._groupName = e.detail.name; |
| 278 | this.reload(); |
| 279 | }, |
Becky Siegel | 3f88785 | 2017-02-02 09:09:45 -0800 | [diff] [blame] | 280 | }); |
Paladox none | a0a3b0e | 2017-06-18 20:04:55 +0000 | [diff] [blame] | 281 | })(); |