MOBILE-3922 menu: Fix select initial main menu page

main
Pau Ferrer Ocaña 2021-11-08 16:19:08 +01:00 committed by Dani Palou
parent 2dea93a60f
commit f53b783fc9
6 changed files with 55 additions and 14 deletions

View File

@ -35,7 +35,6 @@ function buildRoutes(injector: Injector): Routes {
{
path: '',
pathMatch: 'full',
redirectTo: CoreMainMenuHomeHandlerService.PAGE_NAME,
},
{
path: CoreMainMenuHomeHandlerService.PAGE_NAME,

View File

@ -26,6 +26,7 @@ import { CoreCourseHelper } from '@features/course/services/course-helper';
import { CoreCourse } from '@features/course/services/course';
import { CoreContentLinksDelegate } from '@features/contentlinks/services/contentlinks-delegate';
import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper';
import { CoreMainMenuHomeHandlerService } from '@features/mainmenu/services/handlers/mainmenu';
/**
* Page that displays the Home.
@ -96,7 +97,7 @@ export class CoreMainMenuHomePage implements OnInit {
}
return {
page: `/main/home/${handler.page}`,
page: `/main/${CoreMainMenuHomeHandlerService.PAGE_NAME}/${handler.page}`,
pageParams: handler.pageParams,
title: handler.title,
class: handler.class,

View File

@ -173,6 +173,10 @@ export class CoreMainMenuPage implements OnInit, OnDestroy {
this.loaded = CoreMainMenuDelegate.areHandlersLoaded();
if (this.loaded && this.tabs[0] && !CoreNavigator.getCurrentMainMenuTab()) {
// No tab selected, select the first one.
this.mainTabs?.select(this.tabs[0].page);
}
}
/**

View File

@ -14,6 +14,7 @@
import { Injectable } from '@angular/core';
import { CanActivate, CanLoad, UrlTree } from '@angular/router';
import { CoreMainMenuHomeHandlerService } from '@features/mainmenu/services/handlers/mainmenu';
import { CoreApp } from '@services/app';
import { CoreRedirectPayload } from '@services/navigator';
import { CoreSites } from '@services/sites';
@ -59,7 +60,7 @@ export class CoreRedirectGuard implements CanLoad, CanActivate {
redirect.page,
redirect.options,
);
const route = Router.parseUrl('/main/home');
const route = Router.parseUrl(`/main/${CoreMainMenuHomeHandlerService.PAGE_NAME}`);
route.queryParams = {
redirectPath: redirect.page,

View File

@ -20,7 +20,6 @@ import { NavigationOptions } from '@ionic/angular/providers/nav-controller';
import { CoreConstants } from '@/core/constants';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreMainMenu } from '@features/mainmenu/services/mainmenu';
import { CoreMainMenuHomeHandlerService } from '@features/mainmenu/services/handlers/mainmenu';
import { CoreObject } from '@singletons/object';
import { CoreSites } from '@services/sites';
import { CoreUtils } from '@services/utils/utils';
@ -31,8 +30,8 @@ import { CoreScreen } from './screen';
import { CoreApp } from './app';
import { CoreSitePlugins } from '@features/siteplugins/services/siteplugins';
import { CoreError } from '@classes/errors/error';
const DEFAULT_MAIN_MENU_TAB = CoreMainMenuHomeHandlerService.PAGE_NAME;
import { CoreMainMenuDelegate } from '@features/mainmenu/services/mainmenu-delegate';
import { CoreMainMenuHomeHandlerService } from '@features/mainmenu/services/handlers/mainmenu';
/**
* Redirect payload.
@ -184,9 +183,12 @@ export class CoreNavigatorService {
* @return Whether navigation suceeded.
*/
async navigateToSiteHome(options: Omit<CoreNavigationOptions, 'reset'> & { siteId?: string } = {}): Promise<boolean> {
return this.navigateToSitePath(DEFAULT_MAIN_MENU_TAB, {
const landingPagePath = this.getLandingTabPage();
return this.navigateToSitePath(landingPagePath, {
...options,
reset: true,
preferCurrentTab: false,
});
}
@ -535,8 +537,13 @@ export class CoreNavigatorService {
path = path.replace(/^(\.|\/main)?\//, '');
const pathRoot = /^[^/]+/.exec(path)?.[0] ?? '';
if (!pathRoot) {
// No path root, going to the site home.
return this.navigate('/main', options);
}
const currentMainMenuTab = this.getCurrentMainMenuTab();
const isMainMenuTab = pathRoot === currentMainMenuTab || (!currentMainMenuTab && path === DEFAULT_MAIN_MENU_TAB) ||
const isMainMenuTab = pathRoot === currentMainMenuTab || (!currentMainMenuTab && path === this.getLandingTabPage()) ||
await CoreUtils.ignoreErrors(CoreMainMenu.isMainMenuTab(pathRoot), false);
if (!options.preferCurrentTab && isMainMenuTab) {
@ -553,16 +560,32 @@ export class CoreNavigatorService {
return this.navigate(`/main/${path}`, options);
}
// Open the path within the default main tab.
return this.navigate(`/main/${DEFAULT_MAIN_MENU_TAB}`, {
// Open the path within the home tab.
return this.navigate(`/main/${CoreMainMenuHomeHandlerService.PAGE_NAME}`, {
...options,
params: {
redirectPath: `/main/${DEFAULT_MAIN_MENU_TAB}/${path}`,
redirectPath: `/main/${CoreMainMenuHomeHandlerService.PAGE_NAME}/${path}`,
redirectOptions: options.params || options.nextNavigation ? options : undefined,
} as CoreRedirectPayload,
});
}
/**
* Get the first page path using priority.
*
* @return Landing page path.
*/
protected getLandingTabPage(): string {
if (!CoreMainMenuDelegate.areHandlersLoaded()) {
// Handlers not loaded yet, landing page is the root page.
return '';
}
const handlers = CoreMainMenuDelegate.getHandlers();
return handlers[0]?.page || '';
}
/**
* Replace all objects in query params with an ID that can be used to retrieve the object later.
*

View File

@ -21,6 +21,7 @@ import { NavController, Router } from '@singletons';
import { ActivatedRoute, RouterState } from '@angular/router';
import { CoreSites } from '@services/sites';
import { CoreMainMenu } from '@features/mainmenu/services/mainmenu';
import { CoreMainMenuDelegate } from '@features/mainmenu/services/mainmenu-delegate';
describe('CoreNavigator', () => {
@ -118,7 +119,7 @@ describe('CoreNavigator', () => {
});
});
it('navigates to site paths ussing different path formats', async () => {
it('navigates to site paths using different path formats', async () => {
currentMainMenuHandlers.push('users');
const assertNavigation = async (currentPath, sitePath, expectedPath) => {
@ -136,11 +137,23 @@ describe('CoreNavigator', () => {
await assertNavigation('/main/home', '/users/user/42', '/main/users/user/42');
});
it('navigates to site home', async () => {
it('navigates to site home: no handlers loaded', async () => {
const success = await navigator.navigateToSiteHome();
expect(success).toBe(true);
expect(navControllerMock.navigateRoot).toHaveBeenCalledWith(['/main/home'], {});
expect(navControllerMock.navigateRoot).toHaveBeenCalledWith(['/main'], {});
});
it('navigates to site home: handlers loaded', async () => {
mockSingleton(CoreMainMenuDelegate, {
areHandlersLoaded: () => true,
getHandlers: () => [{ title: 'Test', page: 'initialpage', icon: '' }],
});
const success = await navigator.navigateToSiteHome();
expect(success).toBe(true);
expect(navControllerMock.navigateRoot).toHaveBeenCalledWith(['/main/initialpage'], {});
});
it.todo('navigates to a different site');