Skip to content

Commit 58e1d79

Browse files
committed
refactor(app): refactor app slightly to maintain backwards compatibility
1 parent 0480f73 commit 58e1d79

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/components/app/app.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,37 @@ export class App {
208208
/**
209209
* @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()`
210210
*/
211-
getActiveNav(navId: string): NavControllerBase {
211+
getActiveNav(navId?: string): NavControllerBase {
212212
const portal = this._appRoot._getPortal(Constants.PORTAL_MODAL);
213213
if (portal.length() > 0) {
214214
return <NavControllerBase> findTopNav(portal);
215215
}
216-
if (!this._rootNavs || !this._rootNavs.size || !this._rootNavs.has(navId)) {
216+
if (!this._rootNavs || !this._rootNavs.size) {
217217
return null;
218218
}
219+
if (this._rootNavs.size === 1) {
220+
return <NavControllerBase> findTopNav(this._rootNavs.values().next().value);
221+
}
219222
return <NavControllerBase> findTopNav(this.getRootNavById(navId));
220223
}
221224

225+
getRootNav(): any {
226+
console.warn('(getRootNav) is deprecated and will be removed in the next major release. Use getRootNavById instead.');
227+
const rootNavs = this.getRootNavs();
228+
if (rootNavs.length === 0) {
229+
return null;
230+
} else if (rootNavs.length > 1) {
231+
console.warn('(getRootNav) there are multiple root navs, use getRootNavs instead');
232+
}
233+
return rootNavs[0];
234+
}
235+
236+
getRootNavs(): any[] {
237+
const navs: NavigationContainer[] = [];
238+
this._rootNavs.forEach(nav => navs.push(nav));
239+
return navs;
240+
}
241+
222242
/**
223243
* @return {NavController} Returns the root NavController
224244
*/
@@ -233,6 +253,7 @@ export class App {
233253
this._rootNavs.set(nav.id, nav);
234254
}
235255

256+
236257
getActiveNavContainers(): NavigationContainer[] {
237258
// for each root nav container, get it's active nav
238259
const list: NavigationContainer[] = [];

src/components/app/test/app.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,49 @@ describe('App', () => {
431431
expect(activeNavOne).toBe(childNavOne);
432432
expect(activeNavTwo).toBe(childNavTwo);
433433
});
434+
435+
it('should get the active nav when no id is provided assuming there is one nav', () => {
436+
const rootNavOne = mockNavController();
437+
app.registerRootNav(rootNavOne);
438+
439+
const childNavOne = mockNavController();
440+
rootNavOne.registerChildNav(childNavOne);
441+
442+
const result = app.getActiveNav();
443+
444+
expect(result).toEqual(childNavOne);
445+
});
446+
});
447+
448+
describe('getRootNavs', () => {
449+
it('should return an array of navs', () => {
450+
const rootNavOne = mockNavController();
451+
app.registerRootNav(rootNavOne);
452+
const rootNavTwo = mockNavController();
453+
app.registerRootNav(rootNavTwo);
454+
455+
const results = app.getRootNavs();
456+
expect(results.length).toEqual(2);
457+
});
458+
});
459+
460+
describe('getRootNav', () => {
461+
it('should return the single root nav', () => {
462+
const rootNavOne = mockNavController();
463+
app.registerRootNav(rootNavOne);
464+
const result = app.getRootNav();
465+
expect(result).toEqual(rootNavOne);
466+
});
467+
468+
it('should return the first nav in the list for backwards compatibility', () => {
469+
const rootNavOne = mockNavController();
470+
app.registerRootNav(rootNavOne);
471+
const rootNavTwo = mockNavController();
472+
app.registerRootNav(rootNavTwo);
473+
474+
const result = app.getRootNav();
475+
expect(result).toEqual(rootNavOne);
476+
});
434477
});
435478

436479
describe('setEnabled', () => {

0 commit comments

Comments
 (0)