From db08f21da84ae249b3129b4b3805314f9faf9b7e Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Sat, 16 Jul 2016 04:30:05 -0600 Subject: [PATCH] Adding method removeBasePath --- src/ui/public/chrome/api/__tests__/nav.js | 13 +++++++++++++ src/ui/public/chrome/api/nav.js | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/src/ui/public/chrome/api/__tests__/nav.js b/src/ui/public/chrome/api/__tests__/nav.js index 735cde4683987..18de366430712 100644 --- a/src/ui/public/chrome/api/__tests__/nav.js +++ b/src/ui/public/chrome/api/__tests__/nav.js @@ -45,6 +45,19 @@ describe('chrome nav apis', function () { }); }); + describe('#removeBasePath', () => { + it ('returns the given URL as-is when no basepath is set', () => { + const basePath = ''; + const { chrome } = init({ basePath }); + expect(chrome.removeBasePath('/app/kibana?a=b')).to.be('/app/kibana?a=b'); + }); + + it ('returns the given URL with the basepath stripped out when basepath is set', () => { + const { chrome } = init(); + expect(chrome.removeBasePath(`${basePath}/app/kibana?a=b`)).to.be('/app/kibana?a=b'); + }); + }); + describe('#getNavLinkById', () => { it ('retrieves the correct nav link, given its ID', () => { const appUrlStore = new StubBrowserStorage(); diff --git a/src/ui/public/chrome/api/nav.js b/src/ui/public/chrome/api/nav.js index f78ea3a364162..66e27445c088a 100644 --- a/src/ui/public/chrome/api/nav.js +++ b/src/ui/public/chrome/api/nav.js @@ -38,6 +38,15 @@ export default function (chrome, internals) { }); }; + chrome.removeBasePath = function (url) { + if (!internals.basePath) { + return url; + } + + const basePathRegExp = new RegExp(`^${internals.basePath}`); + return url.replace(basePathRegExp, ''); + }; + function lastSubUrlKey(link) { return `lastSubUrl:${link.url}`; }