MOBILE-4470 tabs: Fix route loaded when changing main menu tabs

When using sub-routes like with core-tabs-outlet (e.g. participants), going back to the main menu tab set the main menu route as the current one instead of the sub-route and this caused problems with split view.
main
Dani Palou 2024-05-10 14:38:02 +02:00
parent e511629b9c
commit 859f94bf98
3 changed files with 36 additions and 0 deletions

View File

@ -30,6 +30,7 @@ import { StackDidChangeEvent } from '@ionic/angular/common/directives/navigation
import { CoreNavigator } from '@services/navigator'; import { CoreNavigator } from '@services/navigator';
import { CoreTabBase, CoreTabsBaseComponent } from '@classes/tabs'; import { CoreTabBase, CoreTabsBaseComponent } from '@classes/tabs';
import { CoreDirectivesRegistry } from '@singletons/directives-registry'; import { CoreDirectivesRegistry } from '@singletons/directives-registry';
import { CorePath } from '@singletons/path';
/** /**
* This component displays some top scrollable tabs that will autohide on vertical scroll. * This component displays some top scrollable tabs that will autohide on vertical scroll.
@ -143,6 +144,14 @@ export class CoreTabsOutletComponent extends CoreTabsBaseComponent<CoreTabsOutle
// After the view has entered for the first time, we can assume that it'll always be in the navigation stack // After the view has entered for the first time, we can assume that it'll always be in the navigation stack
// until it's destroyed. // until it's destroyed.
this.existsInNavigationStack = true; this.existsInNavigationStack = true;
const selectedTab = this.getSelected();
const currentPath = CoreNavigator.getCurrentPath();
if (selectedTab && CorePath.pathIsAncestor(currentPath, selectedTab.page)) {
// Current path is an ancestor of the selected path, this happens when the user changes main menu tab and comes back.
// Load the tab again so the right route is loaded. This only changes the current route, it doesn't reload the page.
this.loadTab(selectedTab);
}
} }
/** /**

View File

@ -99,4 +99,22 @@ export class CorePath {
} }
} }
/**
* Check if a certain path is the ancestor of another path.
*
* @param ancestorPath Ancestor path.
* @param path Path to check.
* @returns Whether the path is an ancestor of the other path.
*/
static pathIsAncestor(ancestorPath: string, path: string): boolean {
const ancestorSplit = CoreText.removeEndingSlash(ancestorPath).split('/');
const pathSplit = CoreText.removeEndingSlash(path).split('/');
if (ancestorSplit.length >= pathSplit.length) {
return false;
}
return !ancestorSplit.some((value, index) => value !== pathSplit[index]);
}
} }

View File

@ -52,4 +52,13 @@ describe('CorePath', () => {
expect(CorePath.concatenatePaths('foo/bar', 'baz')).toEqual('foo/bar/baz'); expect(CorePath.concatenatePaths('foo/bar', 'baz')).toEqual('foo/bar/baz');
}); });
it('checks ancestor paths', () => {
expect(CorePath.pathIsAncestor('/foo', '/foo/bar')).toEqual(true);
expect(CorePath.pathIsAncestor('/foo/', '/foo/bar')).toEqual(true);
expect(CorePath.pathIsAncestor('/foo', '/foo/bar/baz')).toEqual(true);
expect(CorePath.pathIsAncestor('/foo/baz', '/foo/bar')).toEqual(false);
expect(CorePath.pathIsAncestor('/foo/bar', '/foo/bar')).toEqual(false);
expect(CorePath.pathIsAncestor('/foo/b', '/foo/bar')).toEqual(false);
});
}); });