From 5ee3dc13e20d31dd20e44a663b282b4aa74cb2c4 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 18 Mar 2022 15:10:02 +0100 Subject: [PATCH] MOBILE-3833 sitehome: Fix site home link handler --- src/core/classes/tabs.ts | 53 ++++++++++++++----- .../components/tabs-outlet/tabs-outlet.ts | 29 ++++++++-- .../sitehome/services/handlers/index-link.ts | 23 +++----- 3 files changed, 71 insertions(+), 34 deletions(-) diff --git a/src/core/classes/tabs.ts b/src/core/classes/tabs.ts index bd4cf9efe..a535e831b 100644 --- a/src/core/classes/tabs.ts +++ b/src/core/classes/tabs.ts @@ -306,13 +306,7 @@ export class CoreTabsBaseComponent implements OnInit, Aft this.calculateSlides(); }); - let selectedTab: T | undefined = this.tabs[this.selectedIndex || 0] || undefined; - - if (!selectedTab || !selectedTab.enabled) { - // The tab is not enabled or not shown. Get the first tab that is enabled. - selectedTab = this.tabs.find((tab) => tab.enabled) || undefined; - } - + const selectedTab = this.calculateInitialTab(); if (!selectedTab) { return; } @@ -329,6 +323,22 @@ export class CoreTabsBaseComponent implements OnInit, Aft this.calculateSlides(); } + /** + * Calculate the initial tab to load. + * + * @return Initial tab, undefined if no valid tab found. + */ + protected calculateInitialTab(): T | undefined { + const selectedTab: T | undefined = this.tabs[this.selectedIndex || 0] || undefined; + + if (selectedTab && selectedTab.enabled) { + return selectedTab; + } + + // The tab is not enabled or not shown. Get the first tab that is enabled. + return this.tabs.find((tab) => tab.enabled) || undefined; + } + /** * Method executed when the slides are changed. */ @@ -564,8 +574,8 @@ export class CoreTabsBaseComponent implements OnInit, Aft } const tabToSelect = this.tabs[index]; - if (!tabToSelect || !tabToSelect.enabled || tabToSelect.id == this.selected) { - // Already selected or not enabled. + if (!tabToSelect || !tabToSelect.enabled) { + // Not enabled. return; } @@ -578,17 +588,32 @@ export class CoreTabsBaseComponent implements OnInit, Aft } } + if (tabToSelect.id === this.selected) { + // Already selected. + return; + } + const ok = await this.loadTab(tabToSelect); if (ok !== false) { - this.selectHistory.push(tabToSelect.id!); - this.selected = tabToSelect.id; - this.selectedIndex = index; - - this.ionChange.emit(tabToSelect); + this.tabSelected(tabToSelect, index); } } + /** + * Update selected tab. + * + * @param tab Tab. + * @param tabIndex Tab index. + */ + protected tabSelected(tab: T, tabIndex: number): void { + this.selectHistory.push(tab.id ?? ''); + this.selected = tab.id; + this.selectedIndex = tabIndex; + + this.ionChange.emit(tab); + } + /** * Load the tab. * diff --git a/src/core/components/tabs-outlet/tabs-outlet.ts b/src/core/components/tabs-outlet/tabs-outlet.ts index e2ffef98f..4a75166a9 100644 --- a/src/core/components/tabs-outlet/tabs-outlet.ts +++ b/src/core/components/tabs-outlet/tabs-outlet.ts @@ -88,7 +88,7 @@ export class CoreTabsOutletComponent extends CoreTabsBaseComponent { super.ngAfterViewInit(); @@ -103,12 +103,20 @@ export class CoreTabsOutletComponent extends CoreTabsBaseComponent tab.page == stackEvent.enteringView.url); + const tab = tabIndex >= 0 ? this.tabs[tabIndex] : undefined; + // Add tabid to the tab content element. if (stackEvent.enteringView.element.id == '') { - const tab = this.tabs.find((tab) => tab.page == stackEvent.enteringView.url); stackEvent.enteringView.element.id = tab?.id || ''; } + if (tab && this.selected !== tab.id) { + // Tab loaded using a navigation, update the selected tab. + this.tabSelected(tab, tabIndex); + } + this.showHideNavBarButtons(stackEvent.enteringView.element.tagName); await this.listenContentScroll(stackEvent.enteringView.element, stackEvent.enteringView.id); @@ -125,7 +133,7 @@ export class CoreTabsOutletComponent extends CoreTabsBaseComponent): void { if (changes.tabs) { @@ -168,6 +176,21 @@ export class CoreTabsOutletComponent extends CoreTabsBaseComponent tab.page === currentPath); + + if (currentPathTab && currentPathTab.enabled) { + return currentPathTab; + } + + return super.calculateInitialTab(); + } + /** * Get router outlet. * diff --git a/src/core/features/sitehome/services/handlers/index-link.ts b/src/core/features/sitehome/services/handlers/index-link.ts index fbc73a2f0..fcb3b4b99 100644 --- a/src/core/features/sitehome/services/handlers/index-link.ts +++ b/src/core/features/sitehome/services/handlers/index-link.ts @@ -32,32 +32,21 @@ export class CoreSiteHomeIndexLinkHandlerService extends CoreContentLinksHandler pattern = /\/course\/view\.php.*([?&]id=\d+)/; /** - * Get the list of actions for a link (url). - * - * @param siteIds List of sites the URL belongs to. - * @param url The URL to treat. - * @param params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} - * @param courseId Course ID related to the URL. Optional but recommended. - * @return List of (or promise resolved with list of) actions. + * @inheritdoc */ getActions(): CoreContentLinksAction[] | Promise { return [{ action: (siteId: string): void => { - // @todo This should open the 'sitehome' setting as well. - CoreNavigator.navigateToSiteHome({ siteId }); + CoreNavigator.navigateToSitePath('/home/site', { + preferCurrentTab: false, + siteId, + }); }, }]; } /** - * Check if the handler is enabled for a certain site (site + user) and a URL. - * If not defined, defaults to true. - * - * @param siteId The site ID. - * @param url The URL to treat. - * @param params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} - * @param courseId Course ID related to the URL. Optional but recommended. - * @return Whether the handler is enabled for the URL and site. + * @inheritdoc */ async isEnabled(siteId: string, url: string, params: Record, courseId?: number): Promise { courseId = parseInt(params.id, 10);