From d8cca59ecb1061d85eaeb6848e8590f1281e6688 Mon Sep 17 00:00:00 2001 From: Francois Ferrand Date: Thu, 22 Nov 2018 15:27:44 +0100 Subject: [PATCH] Merge top menu items contributed by plugins MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each plugin contributes a list single of menus, which are expected to be merged in existing top menu entries if they already exist: as stated in the doc and already implemented in GWT: "Plugins can also add additional menu items to Gerrit’s top menu entries by defining a MenuEntry that has the same name as a Gerrit top menu entry" Change-Id: I8540b93b71e9c9c1f043e16d2076bade4de9a49a --- .../core/gr-main-header/gr-main-header.js | 26 ++-- .../gr-main-header/gr-main-header_test.html | 115 ++++++++++++++++++ 2 files changed, 134 insertions(+), 7 deletions(-) diff --git a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js index 738d29e21d..a77c66d169 100644 --- a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js +++ b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js @@ -179,11 +179,16 @@ }, _computeLinks(defaultLinks, userLinks, adminLinks, topMenus, docBaseUrl) { - const links = defaultLinks.slice(); + const links = defaultLinks.map(menu => { + return { + title: menu.title, + links: menu.links.slice(), + }; + }); if (userLinks && userLinks.length > 0) { links.push({ title: 'Your', - links: userLinks, + links: userLinks.slice(), }); } const docLinks = this._getDocLinks(docBaseUrl, DOCUMENTATION_LINKS); @@ -196,13 +201,20 @@ } links.push({ title: 'Browse', - links: adminLinks, + links: adminLinks.slice(), }); + const topMenuLinks = []; + links.forEach(link => { topMenuLinks[link.title] = link.links; }); for (const m of topMenus) { - links.push({ - title: m.name, - links: m.items.map(this._fixCustomMenuItem), - }); + const items = m.items.map(this._fixCustomMenuItem); + if (m.name in topMenuLinks) { + items.forEach(link => { topMenuLinks[m.name].push(link); }); + } else { + links.push({ + title: m.name, + links: topMenuLinks[m.name] = items, + }); + } } return links; }, diff --git a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.html b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.html index b6e64ec51a..582ca61db0 100644 --- a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.html +++ b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.html @@ -191,6 +191,121 @@ limitations under the License. }]); }); + test('merge top menus', () => { + const adminLinks = [{ + name: 'Repos', + url: '/repos', + }]; + const topMenus = [{ + name: 'Plugins', + items: [{ + name: 'Manage', + target: '_blank', + url: 'https://gerrit/plugins/plugin-manager/static/index.html', + }], + }, { + name: 'Plugins', + items: [{ + name: 'Create', + target: '_blank', + url: 'https://gerrit/plugins/plugin-manager/static/create.html', + }], + }]; + assert.deepEqual(element._computeLinks([], [], adminLinks, topMenus), [{ + title: 'Browse', + links: adminLinks, + }, { + title: 'Plugins', + links: [{ + name: 'Manage', + external: true, + url: 'https://gerrit/plugins/plugin-manager/static/index.html', + }, { + name: 'Create', + external: true, + url: 'https://gerrit/plugins/plugin-manager/static/create.html', + }], + }]); + }); + + test('merge top menus in default links', () => { + const defaultLinks = [{ + title: 'Faves', + links: [{ + name: 'Pinterest', + url: 'https://pinterest.com', + }], + }]; + const topMenus = [{ + name: 'Faves', + items: [{ + name: 'Manage', + target: '_blank', + url: 'https://gerrit/plugins/plugin-manager/static/index.html', + }], + }]; + assert.deepEqual(element._computeLinks(defaultLinks, [], [], topMenus), [{ + title: 'Faves', + links: defaultLinks[0].links.concat([{ + name: 'Manage', + external: true, + url: 'https://gerrit/plugins/plugin-manager/static/index.html', + }]), + }, { + title: 'Browse', + links: [], + }]); + }); + + test('merge top menus in user links', () => { + const userLinks = [{ + name: 'Facebook', + url: 'https://facebook.com', + }]; + const topMenus = [{ + name: 'Your', + items: [{ + name: 'Manage', + target: '_blank', + url: 'https://gerrit/plugins/plugin-manager/static/index.html', + }], + }]; + assert.deepEqual(element._computeLinks([], userLinks, [], topMenus), [{ + title: 'Your', + links: userLinks.concat([{ + name: 'Manage', + external: true, + url: 'https://gerrit/plugins/plugin-manager/static/index.html', + }]), + }, { + title: 'Browse', + links: [], + }]); + }); + + test('merge top menus in admin links', () => { + const adminLinks = [{ + name: 'Repos', + url: '/repos', + }]; + const topMenus = [{ + name: 'Browse', + items: [{ + name: 'Manage', + target: '_blank', + url: 'https://gerrit/plugins/plugin-manager/static/index.html', + }], + }]; + assert.deepEqual(element._computeLinks([], [], adminLinks, topMenus), [{ + title: 'Browse', + links: adminLinks.concat([{ + name: 'Manage', + external: true, + url: 'https://gerrit/plugins/plugin-manager/static/index.html', + }]), + }]); + }); + test('register URL', () => { const config = { auth: {