diff --git a/src/components/app/app.ts b/src/components/app/app.ts index ccfc8f4168..da7a9b1517 100644 --- a/src/components/app/app.ts +++ b/src/components/app/app.ts @@ -208,17 +208,37 @@ export class App { /** * @return {NavController} Returns the active NavController. Using this method is preferred when we need access to the top-level navigation controller while on the outside views and handlers like `registerBackButtonAction()` */ - getActiveNav(navId: string): NavControllerBase { + getActiveNav(navId?: string): NavControllerBase { const portal = this._appRoot._getPortal(Constants.PORTAL_MODAL); if (portal.length() > 0) { return findTopNav(portal); } - if (!this._rootNavs || !this._rootNavs.size || !this._rootNavs.has(navId)) { + if (!this._rootNavs || !this._rootNavs.size) { return null; } + if (this._rootNavs.size === 1) { + return findTopNav(this._rootNavs.values().next().value); + } return findTopNav(this.getRootNavById(navId)); } + getRootNav(): any { + console.warn('(getRootNav) is deprecated and will be removed in the next major release. Use getRootNavById instead.'); + const rootNavs = this.getRootNavs(); + if (rootNavs.length === 0) { + return null; + } else if (rootNavs.length > 1) { + console.warn('(getRootNav) there are multiple root navs, use getRootNavs instead'); + } + return rootNavs[0]; + } + + getRootNavs(): any[] { + const navs: NavigationContainer[] = []; + this._rootNavs.forEach(nav => navs.push(nav)); + return navs; + } + /** * @return {NavController} Returns the root NavController */ @@ -233,6 +253,7 @@ export class App { this._rootNavs.set(nav.id, nav); } + getActiveNavContainers(): NavigationContainer[] { // for each root nav container, get it's active nav const list: NavigationContainer[] = []; diff --git a/src/components/app/test/app.spec.ts b/src/components/app/test/app.spec.ts index a6813e7039..9b3355b92b 100644 --- a/src/components/app/test/app.spec.ts +++ b/src/components/app/test/app.spec.ts @@ -431,6 +431,49 @@ describe('App', () => { expect(activeNavOne).toBe(childNavOne); expect(activeNavTwo).toBe(childNavTwo); }); + + it('should get the active nav when no id is provided assuming there is one nav', () => { + const rootNavOne = mockNavController(); + app.registerRootNav(rootNavOne); + + const childNavOne = mockNavController(); + rootNavOne.registerChildNav(childNavOne); + + const result = app.getActiveNav(); + + expect(result).toEqual(childNavOne); + }); + }); + + describe('getRootNavs', () => { + it('should return an array of navs', () => { + const rootNavOne = mockNavController(); + app.registerRootNav(rootNavOne); + const rootNavTwo = mockNavController(); + app.registerRootNav(rootNavTwo); + + const results = app.getRootNavs(); + expect(results.length).toEqual(2); + }); + }); + + describe('getRootNav', () => { + it('should return the single root nav', () => { + const rootNavOne = mockNavController(); + app.registerRootNav(rootNavOne); + const result = app.getRootNav(); + expect(result).toEqual(rootNavOne); + }); + + it('should return the first nav in the list for backwards compatibility', () => { + const rootNavOne = mockNavController(); + app.registerRootNav(rootNavOne); + const rootNavTwo = mockNavController(); + app.registerRootNav(rootNavTwo); + + const result = app.getRootNav(); + expect(result).toEqual(rootNavOne); + }); }); describe('setEnabled', () => {