Merge changes from topic "admin-nav-to-util"
* changes: Convert admin-nav-behaviors to admin-nav-util Rename gr-admin-nav-behavior to admin-nav-util to preserve history
This commit is contained in:
@@ -1,210 +0,0 @@
|
||||
import {GerritNav} from '../../elements/core/gr-navigation/gr-navigation.js';
|
||||
|
||||
/**
|
||||
* @license
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
const ADMIN_LINKS = [{
|
||||
name: 'Repositories',
|
||||
noBaseUrl: true,
|
||||
url: '/admin/repos',
|
||||
view: 'gr-repo-list',
|
||||
viewableToAll: true,
|
||||
}, {
|
||||
name: 'Groups',
|
||||
section: 'Groups',
|
||||
noBaseUrl: true,
|
||||
url: '/admin/groups',
|
||||
view: 'gr-admin-group-list',
|
||||
}, {
|
||||
name: 'Plugins',
|
||||
capability: 'viewPlugins',
|
||||
section: 'Plugins',
|
||||
noBaseUrl: true,
|
||||
url: '/admin/plugins',
|
||||
view: 'gr-plugin-list',
|
||||
}];
|
||||
|
||||
window.Gerrit = window.Gerrit || {};
|
||||
|
||||
/** @polymerBehavior Gerrit.AdminNavBehavior */
|
||||
export const AdminNavBehavior = {
|
||||
/**
|
||||
* @param {!Object} account
|
||||
* @param {!Function} getAccountCapabilities
|
||||
* @param {!Function} getAdminMenuLinks
|
||||
* Possible aguments in options:
|
||||
* repoName?: string
|
||||
* groupId?: string,
|
||||
* groupName?: string,
|
||||
* groupIsInternal?: boolean,
|
||||
* isAdmin?: boolean,
|
||||
* groupOwner?: boolean,
|
||||
* @param {!Object=} opt_options
|
||||
* @return {Promise<!Object>}
|
||||
*/
|
||||
getAdminLinks(account, getAccountCapabilities, getAdminMenuLinks,
|
||||
opt_options) {
|
||||
if (!account) {
|
||||
return Promise.resolve(this._filterLinks(link => link.viewableToAll,
|
||||
getAdminMenuLinks, opt_options));
|
||||
}
|
||||
return getAccountCapabilities()
|
||||
.then(capabilities => this._filterLinks(
|
||||
link => !link.capability
|
||||
|| capabilities.hasOwnProperty(link.capability),
|
||||
getAdminMenuLinks,
|
||||
opt_options));
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {!Function} filterFn
|
||||
* @param {!Function} getAdminMenuLinks
|
||||
* Possible aguments in options:
|
||||
* repoName?: string
|
||||
* groupId?: string,
|
||||
* groupName?: string,
|
||||
* groupIsInternal?: boolean,
|
||||
* isAdmin?: boolean,
|
||||
* groupOwner?: boolean,
|
||||
* @param {!Object|undefined} opt_options
|
||||
* @return {Promise<!Object>}
|
||||
*/
|
||||
_filterLinks(filterFn, getAdminMenuLinks, opt_options) {
|
||||
let links = ADMIN_LINKS.slice(0);
|
||||
let expandedSection;
|
||||
|
||||
const isExternalLink = link => link.url[0] !== '/';
|
||||
|
||||
// Append top-level links that are defined by plugins.
|
||||
links.push(...getAdminMenuLinks().map(link => {
|
||||
return {
|
||||
url: link.url,
|
||||
name: link.text,
|
||||
capability: link.capability || null,
|
||||
noBaseUrl: !isExternalLink(link),
|
||||
view: null,
|
||||
viewableToAll: !link.capability,
|
||||
target: isExternalLink(link) ? '_blank' : null,
|
||||
};
|
||||
}));
|
||||
|
||||
links = links.filter(filterFn);
|
||||
|
||||
const filteredLinks = [];
|
||||
const repoName = opt_options && opt_options.repoName;
|
||||
const groupId = opt_options && opt_options.groupId;
|
||||
const groupName = opt_options && opt_options.groupName;
|
||||
const groupIsInternal = opt_options && opt_options.groupIsInternal;
|
||||
const isAdmin = opt_options && opt_options.isAdmin;
|
||||
const groupOwner = opt_options && opt_options.groupOwner;
|
||||
|
||||
// Don't bother to get sub-navigation items if only the top level links
|
||||
// are needed. This is used by the main header dropdown.
|
||||
if (!repoName && !groupId) { return {links, expandedSection}; }
|
||||
|
||||
// Otherwise determine the full set of links and return both the full
|
||||
// set in addition to the subsection that should be displayed if it
|
||||
// exists.
|
||||
for (const link of links) {
|
||||
const linkCopy = Object.assign({}, link);
|
||||
if (linkCopy.name === 'Repositories' && repoName) {
|
||||
linkCopy.subsection = this.getRepoSubsections(repoName);
|
||||
expandedSection = linkCopy.subsection;
|
||||
} else if (linkCopy.name === 'Groups' && groupId && groupName) {
|
||||
linkCopy.subsection = this.getGroupSubsections(groupId, groupName,
|
||||
groupIsInternal, isAdmin, groupOwner);
|
||||
expandedSection = linkCopy.subsection;
|
||||
}
|
||||
filteredLinks.push(linkCopy);
|
||||
}
|
||||
return {links: filteredLinks, expandedSection};
|
||||
},
|
||||
|
||||
getGroupSubsections(groupId, groupName, groupIsInternal, isAdmin,
|
||||
groupOwner) {
|
||||
const subsection = {
|
||||
name: groupName,
|
||||
view: GerritNav.View.GROUP,
|
||||
url: GerritNav.getUrlForGroup(groupId),
|
||||
children: [],
|
||||
};
|
||||
if (groupIsInternal) {
|
||||
subsection.children.push({
|
||||
name: 'Members',
|
||||
detailType: GerritNav.GroupDetailView.MEMBERS,
|
||||
view: GerritNav.View.GROUP,
|
||||
url: GerritNav.getUrlForGroupMembers(groupId),
|
||||
});
|
||||
}
|
||||
if (groupIsInternal && (isAdmin || groupOwner)) {
|
||||
subsection.children.push(
|
||||
{
|
||||
name: 'Audit Log',
|
||||
detailType: GerritNav.GroupDetailView.LOG,
|
||||
view: GerritNav.View.GROUP,
|
||||
url: GerritNav.getUrlForGroupLog(groupId),
|
||||
}
|
||||
);
|
||||
}
|
||||
return subsection;
|
||||
},
|
||||
|
||||
getRepoSubsections(repoName) {
|
||||
return {
|
||||
name: repoName,
|
||||
view: GerritNav.View.REPO,
|
||||
url: GerritNav.getUrlForRepo(repoName),
|
||||
children: [{
|
||||
name: 'Access',
|
||||
view: GerritNav.View.REPO,
|
||||
detailType: GerritNav.RepoDetailView.ACCESS,
|
||||
url: GerritNav.getUrlForRepoAccess(repoName),
|
||||
},
|
||||
{
|
||||
name: 'Commands',
|
||||
view: GerritNav.View.REPO,
|
||||
detailType: GerritNav.RepoDetailView.COMMANDS,
|
||||
url: GerritNav.getUrlForRepoCommands(repoName),
|
||||
},
|
||||
{
|
||||
name: 'Branches',
|
||||
view: GerritNav.View.REPO,
|
||||
detailType: GerritNav.RepoDetailView.BRANCHES,
|
||||
url: GerritNav.getUrlForRepoBranches(repoName),
|
||||
},
|
||||
{
|
||||
name: 'Tags',
|
||||
view: GerritNav.View.REPO,
|
||||
detailType: GerritNav.RepoDetailView.TAGS,
|
||||
url: GerritNav.getUrlForRepoTags(repoName),
|
||||
},
|
||||
{
|
||||
name: 'Dashboards',
|
||||
view: GerritNav.View.REPO,
|
||||
detailType: GerritNav.RepoDetailView.DASHBOARDS,
|
||||
url: GerritNav.getUrlForRepoDashboards(repoName),
|
||||
}],
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
// TODO(dmfilippov) Remove the following lines with assignments
|
||||
// Plugins can use the behavior because it was accessible with
|
||||
// the global Gerrit... variable. To avoid breaking changes in plugins
|
||||
// temporary assign global variables.
|
||||
window.Gerrit = window.Gerrit || {};
|
||||
window.Gerrit.AdminNavBehavior = AdminNavBehavior;
|
||||
@@ -33,26 +33,23 @@ import '../gr-repo-commands/gr-repo-commands.js';
|
||||
import '../gr-repo-dashboards/gr-repo-dashboards.js';
|
||||
import '../gr-repo-detail-list/gr-repo-detail-list.js';
|
||||
import '../gr-repo-list/gr-repo-list.js';
|
||||
import {mixinBehaviors} from '@polymer/polymer/lib/legacy/class.js';
|
||||
import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
|
||||
import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
|
||||
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
|
||||
import {htmlTemplate} from './gr-admin-view_html.js';
|
||||
import {getBaseUrl} from '../../../utils/url-util.js';
|
||||
import {AdminNavBehavior} from '../../../behaviors/gr-admin-nav-behavior/gr-admin-nav-behavior.js';
|
||||
import {GerritNav} from '../../core/gr-navigation/gr-navigation.js';
|
||||
import {pluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader.js';
|
||||
import {getAdminLinks} from '../../../utils/admin-nav-util.js';
|
||||
|
||||
const INTERNAL_GROUP_REGEX = /^[\da-f]{40}$/;
|
||||
|
||||
/**
|
||||
* @extends PolymerElement
|
||||
*/
|
||||
class GrAdminView extends mixinBehaviors( [
|
||||
AdminNavBehavior,
|
||||
], GestureEventListeners(
|
||||
class GrAdminView extends GestureEventListeners(
|
||||
LegacyElementMixin(
|
||||
PolymerElement))) {
|
||||
PolymerElement)) {
|
||||
static get template() { return htmlTemplate; }
|
||||
|
||||
static get is() { return 'gr-admin-view'; }
|
||||
@@ -132,7 +129,7 @@ class GrAdminView extends mixinBehaviors( [
|
||||
};
|
||||
}
|
||||
|
||||
return this.getAdminLinks(this._account,
|
||||
return getAdminLinks(this._account,
|
||||
this.$.restAPI.getAccountCapabilities.bind(this.$.restAPI),
|
||||
this.$.jsAPI.getAdminMenuLinks.bind(this.$.jsAPI),
|
||||
options)
|
||||
|
||||
@@ -21,14 +21,13 @@ import '../../shared/gr-js-api-interface/gr-js-api-interface.js';
|
||||
import '../../shared/gr-rest-api-interface/gr-rest-api-interface.js';
|
||||
import '../gr-account-dropdown/gr-account-dropdown.js';
|
||||
import '../gr-smart-search/gr-smart-search.js';
|
||||
import {mixinBehaviors} from '@polymer/polymer/lib/legacy/class.js';
|
||||
import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
|
||||
import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
|
||||
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
|
||||
import {htmlTemplate} from './gr-main-header_html.js';
|
||||
import {getBaseUrl, getDocsBaseUrl} from '../../../utils/url-util.js';
|
||||
import {AdminNavBehavior} from '../../../behaviors/gr-admin-nav-behavior/gr-admin-nav-behavior.js';
|
||||
import {pluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader.js';
|
||||
import {getAdminLinks} from '../../../utils/admin-nav-util.js';
|
||||
|
||||
const DEFAULT_LINKS = [{
|
||||
title: 'Changes',
|
||||
@@ -85,11 +84,9 @@ const AUTH_TYPES_WITH_REGISTER_URL = new Set([
|
||||
/**
|
||||
* @extends PolymerElement
|
||||
*/
|
||||
class GrMainHeader extends mixinBehaviors( [
|
||||
AdminNavBehavior,
|
||||
], GestureEventListeners(
|
||||
class GrMainHeader extends GestureEventListeners(
|
||||
LegacyElementMixin(
|
||||
PolymerElement))) {
|
||||
PolymerElement)) {
|
||||
static get template() { return htmlTemplate; }
|
||||
|
||||
static get is() { return 'gr-main-header'; }
|
||||
@@ -272,7 +269,7 @@ class GrMainHeader extends mixinBehaviors( [
|
||||
this.loading = false;
|
||||
this._topMenus = result[1];
|
||||
|
||||
return this.getAdminLinks(account,
|
||||
return getAdminLinks(account,
|
||||
this.$.restAPI.getAccountCapabilities.bind(this.$.restAPI),
|
||||
this.$.jsAPI.getAdminMenuLinks.bind(this.$.jsAPI))
|
||||
.then(res => {
|
||||
|
||||
197
polygerrit-ui/app/utils/admin-nav-util.js
Normal file
197
polygerrit-ui/app/utils/admin-nav-util.js
Normal file
@@ -0,0 +1,197 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import {GerritNav} from '../elements/core/gr-navigation/gr-navigation.js';
|
||||
|
||||
const ADMIN_LINKS = [{
|
||||
name: 'Repositories',
|
||||
noBaseUrl: true,
|
||||
url: '/admin/repos',
|
||||
view: 'gr-repo-list',
|
||||
viewableToAll: true,
|
||||
}, {
|
||||
name: 'Groups',
|
||||
section: 'Groups',
|
||||
noBaseUrl: true,
|
||||
url: '/admin/groups',
|
||||
view: 'gr-admin-group-list',
|
||||
}, {
|
||||
name: 'Plugins',
|
||||
capability: 'viewPlugins',
|
||||
section: 'Plugins',
|
||||
noBaseUrl: true,
|
||||
url: '/admin/plugins',
|
||||
view: 'gr-plugin-list',
|
||||
}];
|
||||
|
||||
/**
|
||||
* @param {!Object} account
|
||||
* @param {!Function} getAccountCapabilities
|
||||
* @param {!Function} getAdminMenuLinks
|
||||
* Possible aguments in options:
|
||||
* repoName?: string
|
||||
* groupId?: string,
|
||||
* groupName?: string,
|
||||
* groupIsInternal?: boolean,
|
||||
* isAdmin?: boolean,
|
||||
* groupOwner?: boolean,
|
||||
* @param {!Object=} opt_options
|
||||
* @return {Promise<!Object>}
|
||||
*/
|
||||
export function getAdminLinks(account, getAccountCapabilities,
|
||||
getAdminMenuLinks, opt_options) {
|
||||
if (!account) {
|
||||
return Promise.resolve(_filterLinks(link => link.viewableToAll,
|
||||
getAdminMenuLinks, opt_options));
|
||||
}
|
||||
return getAccountCapabilities()
|
||||
.then(capabilities => _filterLinks(
|
||||
link => !link.capability
|
||||
|| capabilities.hasOwnProperty(link.capability),
|
||||
getAdminMenuLinks,
|
||||
opt_options));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Function} filterFn
|
||||
* @param {!Function} getAdminMenuLinks
|
||||
* Possible aguments in options:
|
||||
* repoName?: string
|
||||
* groupId?: string,
|
||||
* groupName?: string,
|
||||
* groupIsInternal?: boolean,
|
||||
* isAdmin?: boolean,
|
||||
* groupOwner?: boolean,
|
||||
* @param {!Object|undefined} opt_options
|
||||
* @return {Promise<!Object>}
|
||||
*/
|
||||
function _filterLinks(filterFn, getAdminMenuLinks, opt_options) {
|
||||
let links = ADMIN_LINKS.slice(0);
|
||||
let expandedSection;
|
||||
|
||||
const isExternalLink = link => link.url[0] !== '/';
|
||||
|
||||
// Append top-level links that are defined by plugins.
|
||||
links.push(...getAdminMenuLinks().map(link => {
|
||||
return {
|
||||
url: link.url,
|
||||
name: link.text,
|
||||
capability: link.capability || null,
|
||||
noBaseUrl: !isExternalLink(link),
|
||||
view: null,
|
||||
viewableToAll: !link.capability,
|
||||
target: isExternalLink(link) ? '_blank' : null,
|
||||
};
|
||||
}));
|
||||
|
||||
links = links.filter(filterFn);
|
||||
|
||||
const filteredLinks = [];
|
||||
const repoName = opt_options && opt_options.repoName;
|
||||
const groupId = opt_options && opt_options.groupId;
|
||||
const groupName = opt_options && opt_options.groupName;
|
||||
const groupIsInternal = opt_options && opt_options.groupIsInternal;
|
||||
const isAdmin = opt_options && opt_options.isAdmin;
|
||||
const groupOwner = opt_options && opt_options.groupOwner;
|
||||
|
||||
// Don't bother to get sub-navigation items if only the top level links
|
||||
// are needed. This is used by the main header dropdown.
|
||||
if (!repoName && !groupId) { return {links, expandedSection}; }
|
||||
|
||||
// Otherwise determine the full set of links and return both the full
|
||||
// set in addition to the subsection that should be displayed if it
|
||||
// exists.
|
||||
for (const link of links) {
|
||||
const linkCopy = Object.assign({}, link);
|
||||
if (linkCopy.name === 'Repositories' && repoName) {
|
||||
linkCopy.subsection = getRepoSubsections(repoName);
|
||||
expandedSection = linkCopy.subsection;
|
||||
} else if (linkCopy.name === 'Groups' && groupId && groupName) {
|
||||
linkCopy.subsection = getGroupSubsections(groupId, groupName,
|
||||
groupIsInternal, isAdmin, groupOwner);
|
||||
expandedSection = linkCopy.subsection;
|
||||
}
|
||||
filteredLinks.push(linkCopy);
|
||||
}
|
||||
return {links: filteredLinks, expandedSection};
|
||||
}
|
||||
|
||||
export function getGroupSubsections(groupId, groupName, groupIsInternal,
|
||||
isAdmin, groupOwner) {
|
||||
const subsection = {
|
||||
name: groupName,
|
||||
view: GerritNav.View.GROUP,
|
||||
url: GerritNav.getUrlForGroup(groupId),
|
||||
children: [],
|
||||
};
|
||||
if (groupIsInternal) {
|
||||
subsection.children.push({
|
||||
name: 'Members',
|
||||
detailType: GerritNav.GroupDetailView.MEMBERS,
|
||||
view: GerritNav.View.GROUP,
|
||||
url: GerritNav.getUrlForGroupMembers(groupId),
|
||||
});
|
||||
}
|
||||
if (groupIsInternal && (isAdmin || groupOwner)) {
|
||||
subsection.children.push(
|
||||
{
|
||||
name: 'Audit Log',
|
||||
detailType: GerritNav.GroupDetailView.LOG,
|
||||
view: GerritNav.View.GROUP,
|
||||
url: GerritNav.getUrlForGroupLog(groupId),
|
||||
}
|
||||
);
|
||||
}
|
||||
return subsection;
|
||||
}
|
||||
|
||||
export function getRepoSubsections(repoName) {
|
||||
return {
|
||||
name: repoName,
|
||||
view: GerritNav.View.REPO,
|
||||
url: GerritNav.getUrlForRepo(repoName),
|
||||
children: [{
|
||||
name: 'Access',
|
||||
view: GerritNav.View.REPO,
|
||||
detailType: GerritNav.RepoDetailView.ACCESS,
|
||||
url: GerritNav.getUrlForRepoAccess(repoName),
|
||||
},
|
||||
{
|
||||
name: 'Commands',
|
||||
view: GerritNav.View.REPO,
|
||||
detailType: GerritNav.RepoDetailView.COMMANDS,
|
||||
url: GerritNav.getUrlForRepoCommands(repoName),
|
||||
},
|
||||
{
|
||||
name: 'Branches',
|
||||
view: GerritNav.View.REPO,
|
||||
detailType: GerritNav.RepoDetailView.BRANCHES,
|
||||
url: GerritNav.getUrlForRepoBranches(repoName),
|
||||
},
|
||||
{
|
||||
name: 'Tags',
|
||||
view: GerritNav.View.REPO,
|
||||
detailType: GerritNav.RepoDetailView.TAGS,
|
||||
url: GerritNav.getUrlForRepoTags(repoName),
|
||||
},
|
||||
{
|
||||
name: 'Dashboards',
|
||||
view: GerritNav.View.REPO,
|
||||
detailType: GerritNav.RepoDetailView.DASHBOARDS,
|
||||
url: GerritNav.getUrlForRepoDashboards(repoName),
|
||||
}],
|
||||
};
|
||||
}
|
||||
@@ -15,35 +15,20 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import '../../test/common-test-setup-karma.js';
|
||||
import {Polymer} from '@polymer/polymer/lib/legacy/polymer-fn.js';
|
||||
import {AdminNavBehavior} from './gr-admin-nav-behavior.js';
|
||||
|
||||
const basicFixture = fixtureFromElement('gr-admin-nav-behavior-test-element');
|
||||
import '../test/common-test-setup-karma.js';
|
||||
import {getAdminLinks} from './admin-nav-util.js';
|
||||
|
||||
suite('gr-admin-nav-behavior tests', () => {
|
||||
let element;
|
||||
let capabilityStub;
|
||||
let menuLinkStub;
|
||||
|
||||
suiteSetup(() => {
|
||||
// Define a Polymer element that uses this behavior.
|
||||
Polymer({
|
||||
is: 'gr-admin-nav-behavior-test-element',
|
||||
behaviors: [
|
||||
AdminNavBehavior,
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
setup(() => {
|
||||
element = basicFixture.instantiate();
|
||||
capabilityStub = sinon.stub();
|
||||
menuLinkStub = sinon.stub().returns([]);
|
||||
});
|
||||
|
||||
const testAdminLinks = (account, options, expected, done) => {
|
||||
element.getAdminLinks(account,
|
||||
getAdminLinks(account,
|
||||
capabilityStub,
|
||||
menuLinkStub,
|
||||
options)
|
||||
Reference in New Issue
Block a user