blob: 72cca9e51055fb0b1f865acdebb2608c7c9a8d4e [file] [log] [blame]
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04001/**
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 Siegel3f887852017-02-02 09:09:45 -080017(function() {
18 'use strict';
19
Becky Siegelf40489e2017-06-22 14:14:07 -070020
Becky Siegel805a0af2018-03-22 17:08:40 -070021 const INTERNAL_GROUP_REGEX = /^[\da-f]{40}$/;
22
Becky Siegel3f887852017-02-02 09:09:45 -080023 Polymer({
24 is: 'gr-admin-view',
Becky Siegel3f887852017-02-02 09:09:45 -080025
26 properties: {
Becky Siegel8d92d532017-08-11 16:32:47 -070027 /** @type {?} */
Becky Siegelf40489e2017-06-22 14:14:07 -070028 params: Object,
Becky Siegel3f887852017-02-02 09:09:45 -080029 path: String,
Becky Siegelf40489e2017-06-22 14:14:07 -070030 adminView: String,
31
Becky Siegele88c3a22018-04-20 10:08:01 +020032 _breadcrumbParentName: String,
Paladox none2bd5c212017-11-16 18:54:02 +000033 _repoName: String,
Paladox nonea4b816b2017-06-18 20:21:37 +000034 _groupId: {
35 type: Number,
36 observer: '_computeGroupName',
37 },
Becky Siegel805a0af2018-03-22 17:08:40 -070038 _groupIsInternal: Boolean,
Paladox nonea4b816b2017-06-18 20:21:37 +000039 _groupName: String,
Paladox nonef7b58512017-08-05 14:52:27 +000040 _groupOwner: {
41 type: Boolean,
42 value: false,
43 },
Becky Siegelcad4bf82018-04-18 16:43:05 +020044 _subsectionLinks: Array,
Becky Siegelf40489e2017-06-22 14:14:07 -070045 _filteredLinks: Array,
46 _showDownload: {
47 type: Boolean,
48 value: false,
49 },
Paladox nonefc2ff252017-08-21 18:00:20 +000050 _isAdmin: {
51 type: Boolean,
52 value: false,
53 },
Paladox nonea4b816b2017-06-18 20:21:37 +000054 _showGroup: Boolean,
Paladox none86088282017-07-15 10:49:08 +000055 _showGroupAuditLog: Boolean,
Paladox nonea0a3b0e2017-06-18 20:04:55 +000056 _showGroupList: Boolean,
Paladox nonee28b73e2017-08-04 16:27:20 +000057 _showGroupMembers: Boolean,
Paladox none2bd5c212017-11-16 18:54:02 +000058 _showRepoAccess: Boolean,
59 _showRepoCommands: Boolean,
Becky Siegelc588ab72018-01-16 17:43:10 -080060 _showRepoDashboards: Boolean,
Paladox none2bd5c212017-11-16 18:54:02 +000061 _showRepoDetailList: Boolean,
62 _showRepoMain: Boolean,
63 _showRepoList: Boolean,
Becky Siegelf40489e2017-06-22 14:14:07 -070064 _showPluginList: Boolean,
65 },
66
67 behaviors: [
Becky Siegelcad4bf82018-04-18 16:43:05 +020068 Gerrit.AdminNavBehavior,
Becky Siegelf40489e2017-06-22 14:14:07 -070069 Gerrit.BaseUrlBehavior,
Becky Siegel3c155f8f2017-06-26 13:08:07 -070070 Gerrit.URLEncodingBehavior,
Becky Siegelf40489e2017-06-22 14:14:07 -070071 ],
72
73 observers: [
74 '_paramsChanged(params)',
75 ],
76
77 attached() {
78 this.reload();
79 },
80
81 reload() {
Wyatt Allenc1485932018-03-30 10:53:36 -070082 const promises = [
83 this.$.restAPI.getAccount(),
84 Gerrit.awaitPluginsLoaded(),
85 ];
86 return Promise.all(promises).then(result => {
87 this._account = result[0];
Becky Siegelcad4bf82018-04-18 16:43:05 +020088 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 Siegelf40489e2017-06-22 14:14:07 -070099 }
Becky Siegelcad4bf82018-04-18 16:43:05 +0200100
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 Siegele88c3a22018-04-20 10:08:01 +0200107 this._breadcrumbParentName = res.expandedSection ?
Dmitrii Filippovb82003c2019-11-05 17:02:59 +0100108 res.expandedSection.name : '';
Becky Siegelcad4bf82018-04-18 16:43:05 +0200109
110 if (!res.expandedSection) {
111 this._subsectionLinks = [];
112 return;
113 }
114 this._subsectionLinks = [res.expandedSection]
Dmitrii Filippovb82003c2019-11-05 17:02:59 +0100115 .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 Siegelcad4bf82018-04-18 16:43:05 +0200125 });
Becky Siegelf40489e2017-06-22 14:14:07 -0700126 });
127 },
128
Becky Siegele88c3a22018-04-20 10:08:01 +0200129 _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 Siegelcad4bf82018-04-18 16:43:05 +0200140 _handleSubsectionChange(e) {
141 const selected = this._subsectionLinks
142 .find(section => section.value === e.detail.value);
Wyatt Allenc1485932018-03-30 10:53:36 -0700143
Becky Siegelcad4bf82018-04-18 16:43:05 +0200144 // This is when it gets set initially.
145 if (this._selectedIsCurrentPage(selected)) {
146 return;
Becky Siegelf40489e2017-06-22 14:14:07 -0700147 }
Becky Siegelcad4bf82018-04-18 16:43:05 +0200148 Gerrit.Nav.navigateToRelativeUrl(selected.url);
Becky Siegelf40489e2017-06-22 14:14:07 -0700149 },
150
Becky Siegelf40489e2017-06-22 14:14:07 -0700151 _paramsChanged(params) {
Wyatt Allenc1727672017-10-19 15:06:54 -0700152 const isGroupView = params.view === Gerrit.Nav.View.GROUP;
Paladox none4d321972018-01-07 23:35:07 +0000153 const isRepoView = params.view === Gerrit.Nav.View.REPO;
Wyatt Allenc1727672017-10-19 15:06:54 -0700154 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 none4d321972018-01-07 23:35:07 +0000165 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 Siegelc588ab72018-01-16 17:43:10 -0800172 this.set('_showRepoDashboards', isRepoView &&
173 params.detail === Gerrit.Nav.RepoDetailView.DASHBOARDS);
Paladox none4d321972018-01-07 23:35:07 +0000174 this.set('_showRepoMain', isRepoView && !params.detail);
175
Paladox none2bd5c212017-11-16 18:54:02 +0000176 this.set('_showRepoList', isAdminView &&
177 params.adminView === 'gr-repo-list');
Paladox none4d321972018-01-07 23:35:07 +0000178
Wyatt Allenc1727672017-10-19 15:06:54 -0700179 this.set('_showPluginList', isAdminView &&
180 params.adminView === 'gr-plugin-list');
Wyatt Allenc1727672017-10-19 15:06:54 -0700181
Becky Siegele88c3a22018-04-20 10:08:01 +0200182 let needsReload = false;
Paladox none2bd5c212017-11-16 18:54:02 +0000183 if (params.repo !== this._repoName) {
184 this._repoName = params.repo || '';
Paladox nonea4b816b2017-06-18 20:21:37 +0000185 // Reloads the admin menu.
Becky Siegele88c3a22018-04-20 10:08:01 +0200186 needsReload = true;
Paladox nonea4b816b2017-06-18 20:21:37 +0000187 }
188 if (params.groupId !== this._groupId) {
189 this._groupId = params.groupId || '';
Becky Siegel3c155f8f2017-06-26 13:08:07 -0700190 // Reloads the admin menu.
Becky Siegele88c3a22018-04-20 10:08:01 +0200191 needsReload = true;
Becky Siegel3c155f8f2017-06-26 13:08:07 -0700192 }
Becky Siegele88c3a22018-04-20 10:08:01 +0200193 if (this._breadcrumbParentName && !params.groupId && !params.repo) {
194 needsReload = true;
195 }
196 if (!needsReload) { return; }
197 this.reload();
Becky Siegelf40489e2017-06-22 14:14:07 -0700198 },
199
Becky Siegelf40489e2017-06-22 14:14:07 -0700200 // 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 Siegel3c155f8f2017-06-26 13:08:07 -0700213 if (!link || typeof link.url === 'undefined') { return ''; }
Wyatt Alleneeab0ce2017-10-30 14:53:15 -0700214 if (link.target || !link.noBaseUrl) {
Becky Siegelf40489e2017-06-22 14:14:07 -0700215 return link.url;
216 }
217 return this._computeRelativeURL(link.url);
218 },
219
Becky Siegel8d92d532017-08-11 16:32:47 -0700220 /**
221 * @param {string} itemView
222 * @param {Object} params
223 * @param {string=} opt_detailType
224 */
Becky Siegel5c6a9822017-06-29 10:48:46 -0700225 _computeSelectedClass(itemView, params, opt_detailType) {
Paladox none0d7e2392019-07-29 18:37:00 +0000226 if (!params) return '';
Wyatt Allene9c8adf2017-10-30 12:07:42 -0700227 // 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 Siegele2ad9d02018-01-05 11:55:19 -0800237 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 Siegel5c6a9822017-06-29 10:48:46 -0700244 if (params.detailType && params.detailType !== opt_detailType) {
245 return '';
246 }
Becky Siegelf40489e2017-06-22 14:14:07 -0700247 return itemView === params.adminView ? 'selected' : '';
Becky Siegel3f887852017-02-02 09:09:45 -0800248 },
Paladox nonea4b816b2017-06-18 20:21:37 +0000249
250 _computeGroupName(groupId) {
251 if (!groupId) { return ''; }
Paladox none70cb10c2018-02-17 19:12:09 +0000252
Paladox nonefc2ff252017-08-21 18:00:20 +0000253 const promises = [];
Paladox nonea4b816b2017-06-18 20:21:37 +0000254 this.$.restAPI.getGroupConfig(groupId).then(group => {
Paladox none70cb10c2018-02-17 19:12:09 +0000255 if (!group || !group.name) { return; }
256
Paladox nonea4b816b2017-06-18 20:21:37 +0000257 this._groupName = group.name;
Becky Siegel805a0af2018-03-22 17:08:40 -0700258 this._groupIsInternal = !!group.id.match(INTERNAL_GROUP_REGEX);
Paladox nonea4b816b2017-06-18 20:21:37 +0000259 this.reload();
Paladox none70cb10c2018-02-17 19:12:09 +0000260
Paladox nonefc2ff252017-08-21 18:00:20 +0000261 promises.push(this.$.restAPI.getIsAdmin().then(isAdmin => {
262 this._isAdmin = isAdmin;
263 }));
Paladox none70cb10c2018-02-17 19:12:09 +0000264
Paladox nonefc2ff252017-08-21 18:00:20 +0000265 promises.push(this.$.restAPI.getIsGroupOwner(group.name).then(
Paladox nonee28b73e2017-08-04 16:27:20 +0000266 isOwner => {
Paladox nonefc2ff252017-08-21 18:00:20 +0000267 this._groupOwner = isOwner;
268 }));
Paladox none70cb10c2018-02-17 19:12:09 +0000269
Paladox nonefc2ff252017-08-21 18:00:20 +0000270 return Promise.all(promises).then(() => {
271 this.reload();
272 });
Paladox nonea4b816b2017-06-18 20:21:37 +0000273 });
274 },
275
276 _updateGroupName(e) {
277 this._groupName = e.detail.name;
278 this.reload();
279 },
Becky Siegel3f887852017-02-02 09:09:45 -0800280 });
Paladox nonea0a3b0e2017-06-18 20:04:55 +0000281})();