MOBILE-3320 navigation: Fix home tabs

main
Noel De Martin 2021-03-03 16:46:47 +01:00
parent 3dc1834a6b
commit 80aaa02b11
1 changed files with 26 additions and 14 deletions

View File

@ -19,6 +19,7 @@ import { CoreSites } from '@services/sites';
import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { CoreTabsOutletComponent, CoreTabsOutletTab } from '@components/tabs-outlet/tabs-outlet'; import { CoreTabsOutletComponent, CoreTabsOutletTab } from '@components/tabs-outlet/tabs-outlet';
import { CoreMainMenuHomeDelegate, CoreMainMenuHomeHandlerToDisplay } from '../../services/home-delegate'; import { CoreMainMenuHomeDelegate, CoreMainMenuHomeHandlerToDisplay } from '../../services/home-delegate';
import { CoreUtils } from '@services/utils/utils';
/** /**
* Page that displays the Home. * Page that displays the Home.
@ -60,31 +61,42 @@ export class CoreMainMenuHomePage implements OnInit {
* Init handlers on change (size or handlers). * Init handlers on change (size or handlers).
*/ */
initHandlers(handlers: CoreMainMenuHomeHandlerToDisplay[]): void { initHandlers(handlers: CoreMainMenuHomeHandlerToDisplay[]): void {
// Re-build the list of tabs. If a handler is already in the list, use existing object to prevent re-creating the tab. // Re-build the list of tabs.
const newTabs: CoreMainMenuHomeHandlerToDisplay[] = handlers.map((handler) => { const handlersMap = CoreUtils.arrayToObject(handlers, 'title');
handler.page = '/main/home/' + handler.page; const newTabs = handlers.map((handler): CoreTabsOutletTab => {
const tab = this.tabs.find(tab => tab.title == handler.title);
// Check if the handler is already in the tabs list. If so, use it. // If a handler is already in the list, use existing object to prevent re-creating the tab.
const tab = this.tabs.find((tab) => tab.title == handler.title); if (tab) {
return tab;
}
return tab || handler; return {
page: `/main/home/${handler.page}`,
pageParams: handler.pageParams,
title: handler.title,
class: handler.class,
icon: handler.icon,
badge: handler.badge,
};
}); });
// Sort them by priority so new handlers are in the right position. // Sort them by priority so new handlers are in the right position.
newTabs.sort((a, b) => (b.priority || 0) - (a.priority || 0)); newTabs.sort((a, b) => (handlersMap[b.title].priority || 0) - (handlersMap[a.title].priority || 0));
if (typeof this.selectedTab == 'undefined' && newTabs.length > 0) { if (typeof this.selectedTab == 'undefined' && newTabs.length > 0) {
let maxPriority = 0; let maxPriority = 0;
let maxIndex = 0;
newTabs.forEach((tab, index) => { this.selectedTab = Object.entries(newTabs).reduce((maxIndex, [index, tab]) => {
if ((tab.selectPriority || 0) > maxPriority) { const selectPriority = handlersMap[tab.title].selectPriority ?? 0;
maxPriority = tab.selectPriority || 0;
maxIndex = index; if (selectPriority > maxPriority) {
maxPriority = selectPriority;
maxIndex = Number(index);
} }
});
this.selectedTab = maxIndex; return maxIndex;
}, 0);
} }
this.tabs = newTabs; this.tabs = newTabs;