MOBILE-3833 sitehome: Fix site home link handler

main
Dani Palou 2022-03-18 15:10:02 +01:00
parent da11a08c07
commit 5ee3dc13e2
3 changed files with 71 additions and 34 deletions

View File

@ -306,13 +306,7 @@ export class CoreTabsBaseComponent<T extends CoreTabBase> implements OnInit, Aft
this.calculateSlides(); this.calculateSlides();
}); });
let selectedTab: T | undefined = this.tabs[this.selectedIndex || 0] || undefined; const selectedTab = this.calculateInitialTab();
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;
}
if (!selectedTab) { if (!selectedTab) {
return; return;
} }
@ -329,6 +323,22 @@ export class CoreTabsBaseComponent<T extends CoreTabBase> implements OnInit, Aft
this.calculateSlides(); 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. * Method executed when the slides are changed.
*/ */
@ -564,8 +574,8 @@ export class CoreTabsBaseComponent<T extends CoreTabBase> implements OnInit, Aft
} }
const tabToSelect = this.tabs[index]; const tabToSelect = this.tabs[index];
if (!tabToSelect || !tabToSelect.enabled || tabToSelect.id == this.selected) { if (!tabToSelect || !tabToSelect.enabled) {
// Already selected or not enabled. // Not enabled.
return; return;
} }
@ -578,17 +588,32 @@ export class CoreTabsBaseComponent<T extends CoreTabBase> implements OnInit, Aft
} }
} }
if (tabToSelect.id === this.selected) {
// Already selected.
return;
}
const ok = await this.loadTab(tabToSelect); const ok = await this.loadTab(tabToSelect);
if (ok !== false) { if (ok !== false) {
this.selectHistory.push(tabToSelect.id!); this.tabSelected(tabToSelect, index);
this.selected = tabToSelect.id;
this.selectedIndex = index;
this.ionChange.emit(tabToSelect);
} }
} }
/**
* 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. * Load the tab.
* *

View File

@ -88,7 +88,7 @@ export class CoreTabsOutletComponent extends CoreTabsBaseComponent<CoreTabsOutle
} }
/** /**
* View has been initialized. * @inheritdoc
*/ */
async ngAfterViewInit(): Promise<void> { async ngAfterViewInit(): Promise<void> {
super.ngAfterViewInit(); super.ngAfterViewInit();
@ -103,12 +103,20 @@ export class CoreTabsOutletComponent extends CoreTabsBaseComponent<CoreTabsOutle
return; return;
} }
// Search the tab loaded.
const tabIndex = this.tabs.findIndex((tab) => tab.page == stackEvent.enteringView.url);
const tab = tabIndex >= 0 ? this.tabs[tabIndex] : undefined;
// Add tabid to the tab content element. // Add tabid to the tab content element.
if (stackEvent.enteringView.element.id == '') { if (stackEvent.enteringView.element.id == '') {
const tab = this.tabs.find((tab) => tab.page == stackEvent.enteringView.url);
stackEvent.enteringView.element.id = tab?.id || ''; 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); this.showHideNavBarButtons(stackEvent.enteringView.element.tagName);
await this.listenContentScroll(stackEvent.enteringView.element, stackEvent.enteringView.id); await this.listenContentScroll(stackEvent.enteringView.element, stackEvent.enteringView.id);
@ -125,7 +133,7 @@ export class CoreTabsOutletComponent extends CoreTabsBaseComponent<CoreTabsOutle
} }
/** /**
* Detect changes on input properties. * @inheritdoc
*/ */
ngOnChanges(changes: Record<string, SimpleChange>): void { ngOnChanges(changes: Record<string, SimpleChange>): void {
if (changes.tabs) { if (changes.tabs) {
@ -168,6 +176,21 @@ export class CoreTabsOutletComponent extends CoreTabsBaseComponent<CoreTabsOutle
this.lastActiveComponent?.ionViewDidLeave?.(); this.lastActiveComponent?.ionViewDidLeave?.();
} }
/**
* @inheritdoc
*/
protected calculateInitialTab(): CoreTabsOutletTab | undefined {
// Check if a tab should be selected because it was loaded by path.
const currentPath = CoreNavigator.getCurrentPath();
const currentPathTab = this.tabs.find(tab => tab.page === currentPath);
if (currentPathTab && currentPathTab.enabled) {
return currentPathTab;
}
return super.calculateInitialTab();
}
/** /**
* Get router outlet. * Get router outlet.
* *

View File

@ -32,32 +32,21 @@ export class CoreSiteHomeIndexLinkHandlerService extends CoreContentLinksHandler
pattern = /\/course\/view\.php.*([?&]id=\d+)/; pattern = /\/course\/view\.php.*([?&]id=\d+)/;
/** /**
* Get the list of actions for a link (url). * @inheritdoc
*
* @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.
*/ */
getActions(): CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> { getActions(): CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> {
return [{ return [{
action: (siteId: string): void => { action: (siteId: string): void => {
// @todo This should open the 'sitehome' setting as well. CoreNavigator.navigateToSitePath('/home/site', {
CoreNavigator.navigateToSiteHome({ siteId }); preferCurrentTab: false,
siteId,
});
}, },
}]; }];
} }
/** /**
* Check if the handler is enabled for a certain site (site + user) and a URL. * @inheritdoc
* 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.
*/ */
async isEnabled(siteId: string, url: string, params: Record<string, string>, courseId?: number): Promise<boolean> { async isEnabled(siteId: string, url: string, params: Record<string, string>, courseId?: number): Promise<boolean> {
courseId = parseInt(params.id, 10); courseId = parseInt(params.id, 10);