Skip to content

Commit 2d49e10

Browse files
committed
fix(app): restore getActiveNav api
restore getActiveNav api
1 parent ce46c24 commit 2d49e10

File tree

2 files changed

+205
-6
lines changed

2 files changed

+205
-6
lines changed

src/components/app/app.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,21 @@ export class App {
206206
}
207207

208208
/**
209-
* @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()`
209+
* @return {NavController} Returns the first Active Nav Controller from the list. This method is deprecated
210210
*/
211-
getActiveNavs(navId?: string): NavControllerBase[] {
211+
getActiveNav(): NavControllerBase {
212+
console.warn('(getActiveNav) is deprecated and will be removed in the next major release. Use getActiveNavs instead.');
213+
const navs = this.getActiveNavs();
214+
if (navs && navs.length) {
215+
return navs[0];
216+
}
217+
return null;
218+
}
219+
220+
/**
221+
* @return {NavController[]} Returns the active NavControllers. Using this method is preferred when we need access to the top-level navigation controller while on the outside views and handlers like `registerBackButtonAction()`
222+
*/
223+
getActiveNavs(rootNavId?: string): NavControllerBase[] {
212224
const portal = this._appRoot._getPortal(Constants.PORTAL_MODAL);
213225
if (portal.length() > 0) {
214226
return <NavControllerBase[]> findTopNavs(portal);
@@ -219,7 +231,16 @@ export class App {
219231
if (this._rootNavs.size === 1) {
220232
return <NavControllerBase[]> findTopNavs(this._rootNavs.values().next().value);
221233
}
222-
return <NavControllerBase[]> findTopNavs(this.getRootNavById(navId));
234+
if (rootNavId) {
235+
return <NavControllerBase[]> findTopNavs(this._rootNavs.get(rootNavId));
236+
}
237+
// fallback to just using all root names
238+
let activeNavs: NavigationContainer[] = [];
239+
this._rootNavs.forEach(nav => {
240+
const topNavs = findTopNavs(nav);
241+
activeNavs = activeNavs.concat(topNavs);
242+
});
243+
return <NavControllerBase[]> activeNavs;
223244
}
224245

225246
getRootNav(): any {

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

Lines changed: 181 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ describe('App', () => {
312312
});
313313
});
314314

315-
describe('getActiveNav', () => {
315+
describe('getActiveNavs', () => {
316316

317317
it('should get active NavController when using tabs with nested nav', () => {
318318
const nav = mockNavController();
@@ -340,6 +340,11 @@ describe('App', () => {
340340
expect(activeNavs.length).toEqual(2);
341341
expect(activeNavs[0]).toEqual(nav2);
342342
expect(activeNavs[1]).toEqual(nav3);
343+
344+
const activeNavsTwo = app.getActiveNavs();
345+
expect(activeNavsTwo.length).toEqual(2);
346+
expect(activeNavsTwo[0]).toEqual(nav2);
347+
expect(activeNavsTwo[1]).toEqual(nav3);
343348
});
344349

345350
it('should get active NavController when using tabs, nested in a root nav', () => {
@@ -355,10 +360,12 @@ describe('App', () => {
355360
tab2.setSelected(true);
356361

357362
expect(app.getActiveNavs(nav.id)[0]).toBe(tab2);
363+
expect(app.getActiveNavs()[0]).toBe(tab2);
358364

359365
tab2.setSelected(false);
360366
tab3.setSelected(true);
361367
expect(app.getActiveNavs(nav.id)[0]).toBe(tab3);
368+
expect(app.getActiveNavs()[0]).toBe(tab3);
362369
});
363370

364371
it('should get active tab NavController when using tabs, and tabs is the root', () => {
@@ -371,10 +378,12 @@ describe('App', () => {
371378
tab2.setSelected(true);
372379

373380
expect(app.getActiveNavs(tabs.id)[0]).toBe(tab2);
381+
expect(app.getActiveNavs()[0]).toBe(tab2);
374382

375383
tab2.setSelected(false);
376384
tab3.setSelected(true);
377385
expect(app.getActiveNavs(tabs.id)[0]).toBe(tab3);
386+
expect(app.getActiveNavs()[0]).toBe(tab3);
378387
});
379388

380389
it('should get active NavController when nested 3 deep', () => {
@@ -387,6 +396,8 @@ describe('App', () => {
387396
nav2.registerChildNav(nav3);
388397

389398
expect(app.getActiveNavs(nav1.id)[0]).toBe(nav3);
399+
expect(app.getActiveNavs()[0]).toBe(nav3);
400+
expect(app.getActiveNavs().length).toBe(1);
390401
});
391402

392403
it('should get active NavController when nested 2 deep', () => {
@@ -399,12 +410,15 @@ describe('App', () => {
399410
const activeNav = app.getActiveNavs(nav1.id)[0];
400411

401412
expect(activeNav).toBe(nav2);
413+
414+
expect(app.getActiveNavs()[0]).toBe(nav2);
402415
});
403416

404417
it('should get active NavController when only one nav controller', () => {
405418
const nav = mockNavController();
406419
app.registerRootNav(nav);
407420
expect(app.getActiveNavs(nav.id)[0]).toBe(nav);
421+
expect(app.getActiveNavs()[0]).toBe(nav);
408422
});
409423

410424
it('should set/get the root nav controller', () => {
@@ -414,9 +428,9 @@ describe('App', () => {
414428
});
415429

416430
it('should not get an active NavController if there is not root set', () => {
417-
const activeNav = app.getActiveNavs('');
431+
const activeNavs = app.getActiveNavs();
418432
const rootNav = app.getRootNavById('');
419-
expect(activeNav.length).toEqual(0);
433+
expect(activeNavs.length).toEqual(0);
420434
expect(rootNav).toBeFalsy();
421435
});
422436

@@ -438,6 +452,9 @@ describe('App', () => {
438452

439453
expect(activeNavOne).toBe(childNavOne);
440454
expect(activeNavTwo).toBe(childNavTwo);
455+
456+
expect(app.getActiveNavs()[0]).toBe(childNavOne);
457+
expect(app.getActiveNavs()[1]).toBe(childNavTwo);
441458
});
442459

443460
it('should get the active nav when no id is provided assuming there is one nav', () => {
@@ -451,6 +468,167 @@ describe('App', () => {
451468

452469
expect(result).toEqual(childNavOne);
453470
});
471+
472+
it('should return the all the active navs when there is not an id passed', () => {
473+
const rootNavOne = mockNavController();
474+
app.registerRootNav(rootNavOne);
475+
476+
const rootNavTwo = mockNavController();
477+
app.registerRootNav(rootNavTwo);
478+
479+
const childNavOne = mockNavController();
480+
rootNavOne.registerChildNav(childNavOne);
481+
482+
const childChildNavOne = mockNavController();
483+
childNavOne.registerChildNav(childChildNavOne);
484+
485+
const childNavTwo = mockNavController();
486+
rootNavTwo.registerChildNav(childNavTwo);
487+
488+
const childChildNavTwo = mockNavController();
489+
childNavTwo.registerChildNav(childChildNavTwo);
490+
491+
const results = app.getActiveNavs();
492+
expect(results.length).toEqual(2);
493+
expect(results[0]).toEqual(childChildNavOne);
494+
expect(results[1]).toEqual(childChildNavTwo);
495+
496+
const withIdResultsOne = app.getActiveNavs(rootNavOne.id);
497+
expect(withIdResultsOne.length).toEqual(1);
498+
expect(withIdResultsOne[0]).toEqual(childChildNavOne);
499+
500+
const withIdResultsTwo = app.getActiveNavs(rootNavTwo.id);
501+
expect(withIdResultsTwo.length).toEqual(1);
502+
expect(withIdResultsTwo[0]).toEqual(childChildNavTwo);
503+
});
504+
});
505+
506+
describe('getActiveNav', () => {
507+
it('should get active NavController when using tabs with nested nav', () => {
508+
const nav = mockNavController();
509+
app.registerRootNav(nav);
510+
511+
const tabs = mockTabs();
512+
const tab1 = mockTab(tabs);
513+
const tab2 = mockTab(tabs);
514+
nav.registerChildNav(tabs);
515+
516+
tab2.setSelected(true);
517+
const nav2 = mockNavController();
518+
nav2.name = 'nav2';
519+
const nav3 = mockNavController();
520+
nav3.name = 'nav3';
521+
const nav4 = mockNavController();
522+
nav4.name = 'nav4';
523+
524+
tab1.registerChildNav(nav4);
525+
// tab 2 registers two child navs!!
526+
tab2.registerChildNav(nav2);
527+
tab2.registerChildNav(nav3);
528+
529+
const activeNav = app.getActiveNav();
530+
expect(activeNav).toEqual(nav2);
531+
});
532+
533+
it('should get active NavController when using tabs, nested in a root nav', () => {
534+
const nav = mockNavController();
535+
app.registerRootNav(nav);
536+
537+
const tabs = mockTabs();
538+
mockTab(tabs);
539+
const tab2 = mockTab(tabs);
540+
const tab3 = mockTab(tabs);
541+
nav.registerChildNav(tabs);
542+
543+
tab2.setSelected(true);
544+
545+
expect(app.getActiveNav()).toBe(tab2);
546+
547+
tab2.setSelected(false);
548+
tab3.setSelected(true);
549+
expect(app.getActiveNav()).toBe(tab3);
550+
});
551+
552+
it('should get active tab NavController when using tabs, and tabs is the root', () => {
553+
const tabs = mockTabs();
554+
mockTab(tabs);
555+
const tab2 = mockTab(tabs);
556+
const tab3 = mockTab(tabs);
557+
app.registerRootNav(tabs);
558+
559+
tab2.setSelected(true);
560+
561+
expect(app.getActiveNav()).toBe(tab2);
562+
563+
tab2.setSelected(false);
564+
tab3.setSelected(true);
565+
expect(app.getActiveNav()).toBe(tab3);
566+
});
567+
568+
it('should get active NavController when nested 3 deep', () => {
569+
const nav1 = mockNavController();
570+
const nav2 = mockNavController();
571+
const nav3 = mockNavController();
572+
app.registerRootNav(nav1);
573+
574+
nav1.registerChildNav(nav2);
575+
nav2.registerChildNav(nav3);
576+
577+
expect(app.getActiveNav()).toBe(nav3);
578+
});
579+
580+
it('should get active NavController when nested 2 deep', () => {
581+
const nav1 = mockNavController();
582+
const nav2 = mockNavController();
583+
app.registerRootNav(nav1);
584+
585+
nav1.registerChildNav(nav2);
586+
587+
const activeNav = app.getActiveNav();
588+
589+
expect(activeNav).toBe(nav2);
590+
});
591+
592+
it('should get active NavController when only one nav controller', () => {
593+
const nav = mockNavController();
594+
app.registerRootNav(nav);
595+
expect(app.getActiveNav()).toBe(nav);
596+
});
597+
598+
it('should not get an active NavController if there is not root set', () => {
599+
const activeNav = app.getActiveNav();
600+
expect(activeNav).toBeFalsy();
601+
});
602+
603+
it('should just work when there are multiple active navs', () => {
604+
const rootNavOne = mockNavController();
605+
const rootNavTwo = mockNavController();
606+
607+
app.registerRootNav(rootNavOne);
608+
app.registerRootNav(rootNavTwo);
609+
610+
const childNavOne = mockNavController();
611+
const childNavTwo = mockNavController();
612+
613+
rootNavOne.registerChildNav(childNavOne);
614+
rootNavTwo.registerChildNav(childNavTwo);
615+
616+
const activeNavOne = app.getActiveNav();
617+
618+
expect(activeNavOne).toBe(childNavOne);
619+
});
620+
621+
it('should get the active nav when no id is provided assuming there is one nav', () => {
622+
const rootNavOne = mockNavController();
623+
app.registerRootNav(rootNavOne);
624+
625+
const childNavOne = mockNavController();
626+
rootNavOne.registerChildNav(childNavOne);
627+
628+
const result = app.getActiveNav();
629+
630+
expect(result).toEqual(childNavOne);
631+
});
454632
});
455633

456634
describe('getRootNavs', () => {

0 commit comments

Comments
 (0)