diff --git a/src/addons/calendar/calendar-lazy.module.ts b/src/addons/calendar/calendar-lazy.module.ts index c8058bd01..941f0449d 100644 --- a/src/addons/calendar/calendar-lazy.module.ts +++ b/src/addons/calendar/calendar-lazy.module.ts @@ -32,10 +32,16 @@ function buildRoutes(injector: Injector): Routes { return [ { path: 'index', + data: { + isMainMenuRoot: true, + }, loadChildren: () => import('@/addons/calendar/pages/index/index.module').then(m => m.AddonCalendarIndexPageModule), }, { path: 'list', + data: { + isMainMenuRoot: true, + }, loadChildren: () => import('@/addons/calendar/pages/list/list.module').then(m => m.AddonCalendarListPageModule), }, { diff --git a/src/addons/messages/pages/discussions-35/discussions.module.ts b/src/addons/messages/pages/discussions-35/discussions.module.ts index 2406cee87..e5b0b56b1 100644 --- a/src/addons/messages/pages/discussions-35/discussions.module.ts +++ b/src/addons/messages/pages/discussions-35/discussions.module.ts @@ -26,6 +26,9 @@ import { AddonMessagesDiscussions35Page } from './discussions.page'; const mobileRoutes: Routes = [ { path: '', + data: { + isMainMenuRoot: true, + }, component: AddonMessagesDiscussions35Page, }, AddonMessagesDiscussionRoute, @@ -34,6 +37,9 @@ const mobileRoutes: Routes = [ const tabletRoutes: Routes = [ { path: '', + data: { + isMainMenuRoot: true, + }, component: AddonMessagesDiscussions35Page, children: [ AddonMessagesDiscussionRoute, diff --git a/src/addons/messages/pages/group-conversations/group-conversations.module.ts b/src/addons/messages/pages/group-conversations/group-conversations.module.ts index 33f437be7..57b851b41 100644 --- a/src/addons/messages/pages/group-conversations/group-conversations.module.ts +++ b/src/addons/messages/pages/group-conversations/group-conversations.module.ts @@ -25,6 +25,9 @@ import { AddonMessagesGroupConversationsPage } from './group-conversations.page' const mobileRoutes: Routes = [ { path: '', + data: { + isMainMenuRoot: true, + }, component: AddonMessagesGroupConversationsPage, }, AddonMessagesDiscussionRoute, @@ -33,6 +36,9 @@ const mobileRoutes: Routes = [ const tabletRoutes: Routes = [ { path: '', + data: { + isMainMenuRoot: true, + }, component: AddonMessagesGroupConversationsPage, children: [ AddonMessagesDiscussionRoute, diff --git a/src/addons/notifications/notifications-lazy.module.ts b/src/addons/notifications/notifications-lazy.module.ts index 890e8df7c..eac71a80f 100644 --- a/src/addons/notifications/notifications-lazy.module.ts +++ b/src/addons/notifications/notifications-lazy.module.ts @@ -21,6 +21,9 @@ function buildRoutes(injector: Injector): Routes { return [ { path: 'list', + data: { + isMainMenuRoot: true, + }, loadChildren: () => import('./pages/list/list.module').then(m => m.AddonNotificationsListPageModule), }, ...buildTabMainRoutes(injector, { diff --git a/src/addons/privatefiles/privatefiles-lazy.module.ts b/src/addons/privatefiles/privatefiles-lazy.module.ts index 405d9ff9d..79e5096b3 100644 --- a/src/addons/privatefiles/privatefiles-lazy.module.ts +++ b/src/addons/privatefiles/privatefiles-lazy.module.ts @@ -19,12 +19,19 @@ import { buildTabMainRoutes } from '@features/mainmenu/mainmenu-tab-routing.modu function buildRoutes(injector: Injector): Routes { return [ + { + path: 'root', + data: { + isMainMenuRoot: true, + }, + loadChildren: () => import('./pages/index/index.module').then(m => m.AddonPrivateFilesIndexPageModule), + }, { path: ':hash', loadChildren: () => import('./pages/index/index.module').then(m => m.AddonPrivateFilesIndexPageModule), }, ...buildTabMainRoutes(injector, { - redirectTo: 'root', // Fake "hash". + redirectTo: 'root', pathMatch: 'full', }), ]; diff --git a/src/addons/privatefiles/services/handlers/mainmenu.ts b/src/addons/privatefiles/services/handlers/mainmenu.ts index 5b8015217..0ec18da4c 100644 --- a/src/addons/privatefiles/services/handlers/mainmenu.ts +++ b/src/addons/privatefiles/services/handlers/mainmenu.ts @@ -48,7 +48,6 @@ export class AddonPrivateFilesMainMenuHandlerService implements CoreMainMenuHand icon: 'fas-folder', title: 'addon.privatefiles.files', page: AddonPrivateFilesMainMenuHandlerService.PAGE_NAME, - subPage: 'root', class: 'addon-privatefiles-handler', }; } diff --git a/src/core/features/mainmenu/pages/home/home.module.ts b/src/core/features/mainmenu/pages/home/home.module.ts index 2160d8709..2ad028009 100644 --- a/src/core/features/mainmenu/pages/home/home.module.ts +++ b/src/core/features/mainmenu/pages/home/home.module.ts @@ -28,6 +28,9 @@ function buildRoutes(injector: Injector): Routes { return [ ...buildTabMainRoutes(injector, { path: '', + data: { + isMainMenuRoot: true, + }, component: CoreMainMenuHomePage, children: routes.children, }), diff --git a/src/core/features/mainmenu/pages/menu/menu.ts b/src/core/features/mainmenu/pages/menu/menu.ts index 48c0f45f9..233749b89 100644 --- a/src/core/features/mainmenu/pages/menu/menu.ts +++ b/src/core/features/mainmenu/pages/menu/menu.ts @@ -13,13 +13,12 @@ // limitations under the License. import { Component, OnInit, OnDestroy, ViewChild, ChangeDetectorRef } from '@angular/core'; -import { ActivatedRoute, Router } from '@angular/router'; +import { ActivatedRoute } from '@angular/router'; import { IonTabs } from '@ionic/angular'; import { BackButtonEvent } from '@ionic/core'; import { Subscription } from 'rxjs'; import { CoreApp } from '@services/app'; -import { CoreTextUtils } from '@services/utils/text'; import { CoreEvents, CoreEventObserver } from '@singletons/events'; import { CoreMainMenu, CoreMainMenuProvider } from '../../services/mainmenu'; import { CoreMainMenuDelegate, CoreMainMenuHandlerToDisplay } from '../../services/mainmenu-delegate'; @@ -27,6 +26,7 @@ import { CoreDomUtils } from '@services/utils/dom'; import { Translate } from '@singletons'; import { CoreUtils } from '@services/utils/utils'; import { CoreAriaRoleTab, CoreAriaRoleTabFindable } from '@classes/aria-role-tab'; +import { CoreNavigator } from '@services/navigator'; /** * Page that displays the main menu of the app. @@ -61,7 +61,6 @@ export class CoreMainMenuPage implements OnInit, OnDestroy { constructor( protected route: ActivatedRoute, protected changeDetector: ChangeDetectorRef, - protected router: Router, ) { this.resizeFunction = this.initHandlers.bind(this); this.backButtonFunction = this.backButtonClicked.bind(this); @@ -172,29 +171,26 @@ export class CoreMainMenuPage implements OnInit, OnDestroy { return; } - const trimmedUrl = CoreTextUtils.trimCharacter(this.router.url, '/'); - - // Current tab was clicked. Check if user is already at root level. - if (trimmedUrl == CoreTextUtils.trimCharacter(page, '/')) { - // Already at root level, nothing to do. - return; - } - - // Ask the user if he wants to go back to the root page of the tab. e.preventDefault(); e.stopPropagation(); e.stopImmediatePropagation(); + // Current tab was clicked. Check if user is already at root level. + const mainMenuRootRoute = CoreNavigator.getCurrentRoute({ routeData: { isMainMenuRoot: true } }); + if (mainMenuRootRoute) { + return; // Already at root level, nothing to do. + } + + // Current route doesn't define isMainMenuRoot. Check if the current path is the tab one. + const currentPath = CoreNavigator.getCurrentPath(); + if (currentPath == `/main/${page}`) { + return; // Already at root level, nothing to do. + } + + // Ask the user if he wants to go back to the root page of the tab. try { const tab = this.tabs.find((tab) => tab.page == page); - // Use tab's subPage to check if user is already at root level. - if (tab?.subPage && trimmedUrl == - CoreTextUtils.trimCharacter(CoreTextUtils.concatenatePaths(tab.page, tab.subPage), '/')) { - // Already at root level, nothing to do. - return; - } - if (tab?.title) { await CoreDomUtils.showConfirm(Translate.instant('core.confirmgotabroot', { name: Translate.instant(tab.title), diff --git a/src/core/features/mainmenu/pages/more/more.ts b/src/core/features/mainmenu/pages/more/more.ts index 4e48698dc..42dafbdfb 100644 --- a/src/core/features/mainmenu/pages/more/more.ts +++ b/src/core/features/mainmenu/pages/more/more.ts @@ -136,7 +136,6 @@ export class CoreMainMenuMorePage implements OnInit, OnDestroy { * Open a handler. * * @param handler Handler to open. - * @todo: use subPage? */ openHandler(handler: CoreMainMenuHandlerData): void { const params = handler.pageParams; diff --git a/src/core/features/mainmenu/services/handlers/mainmenu.ts b/src/core/features/mainmenu/services/handlers/mainmenu.ts index aff6cdb9d..45c6bd8ba 100644 --- a/src/core/features/mainmenu/services/handlers/mainmenu.ts +++ b/src/core/features/mainmenu/services/handlers/mainmenu.ts @@ -42,7 +42,6 @@ export class CoreMainMenuHomeHandlerService implements CoreMainMenuHandler { icon: 'fas-tachometer-alt', title: 'core.mainmenu.home', page: CoreMainMenuHomeHandlerService.PAGE_NAME, - // @todo: subPage? The page can change due to core-tabs. class: 'core-home-handler', }; } diff --git a/src/core/features/mainmenu/services/mainmenu-delegate.ts b/src/core/features/mainmenu/services/mainmenu-delegate.ts index 862bb5b55..62f15b039 100644 --- a/src/core/features/mainmenu/services/mainmenu-delegate.ts +++ b/src/core/features/mainmenu/services/mainmenu-delegate.ts @@ -33,13 +33,6 @@ export interface CoreMainMenuHandlerData { */ page: string; - /** - * Sub page loaded when the handler page is loaded. - * If your module performs a redirect when it's opened you need to specify the sub page in here. - * E.g. if page is 'foo' but it redirects to 'foo/bar' when opened, this value must be 'bar'. - */ - subPage?: string; - /** * Title to display for the handler. */ diff --git a/src/core/features/tag/tag-lazy.module.ts b/src/core/features/tag/tag-lazy.module.ts index dc6ed32c1..3156dc8c6 100644 --- a/src/core/features/tag/tag-lazy.module.ts +++ b/src/core/features/tag/tag-lazy.module.ts @@ -31,6 +31,9 @@ function buildRoutes(injector: Injector): Routes { }, { path: 'search', + data: { + isMainMenuRoot: true, + }, loadChildren: () => import('@features/tag//pages/search/search.page.module').then(m => m.CoreTagSearchPageModule), }, CoreTagIndexAreaRoute, diff --git a/src/core/services/navigator.ts b/src/core/services/navigator.ts index d543aac00..da03518ff 100644 --- a/src/core/services/navigator.ts +++ b/src/core/services/navigator.ts @@ -519,6 +519,31 @@ export class CoreNavigatorService { return path; } + /** + * Get the full path of a certain route, including parent routes paths. + * + * @param route Route. + * @return Path. + */ + getRouteFullPath(route: ActivatedRoute | null): string { + if (!route) { + return ''; + } + + const parentPath = this.getRouteFullPath(route.parent); + const routePath = route.snapshot.url.join('/'); + + if (!parentPath && !routePath) { + return ''; + } else if (parentPath && !routePath) { + return parentPath; + } else if (!parentPath && routePath) { + return '/' + routePath; + } else { + return parentPath + '/' + routePath; + } + } + } export const CoreNavigator = makeSingleton(CoreNavigatorService);