From df0059e74dbce0ff6464980237c300dad534a88b Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Wed, 17 Mar 2021 13:20:15 +0100 Subject: [PATCH] MOBILE-3320 navigation: Simplify items manager --- .../pages/user-badges/user-badges.page.ts | 9 +-- .../pages/competencies/competencies.ts | 9 +-- .../competency/pages/planlist/planlist.ts | 8 --- .../submission-list/submission-list.page.ts | 9 +-- .../mod/forum/components/index/index.ts | 17 +----- src/core/classes/page-items-list-manager.ts | 55 ++++++++++--------- .../grades/pages/course/course.page.ts | 9 +-- .../grades/pages/courses/courses.page.ts | 10 ---- .../features/settings/pages/index/index.ts | 10 +--- src/core/features/settings/pages/site/site.ts | 9 +-- .../settings/services/settings-helper.ts | 26 --------- .../pages/participants/participants.page.ts | 8 --- 12 files changed, 37 insertions(+), 142 deletions(-) diff --git a/src/addons/badges/pages/user-badges/user-badges.page.ts b/src/addons/badges/pages/user-badges/user-badges.page.ts index 4a145fb4b..1dabb74fe 100644 --- a/src/addons/badges/pages/user-badges/user-badges.page.ts +++ b/src/addons/badges/pages/user-badges/user-badges.page.ts @@ -20,7 +20,7 @@ import { CoreDomUtils } from '@services/utils/dom'; import { CoreSites } from '@services/sites'; import { CoreUtils } from '@services/utils/utils'; import { CorePageItemsListManager } from '@classes/page-items-list-manager'; -import { ActivatedRouteSnapshot, Params } from '@angular/router'; +import { Params } from '@angular/router'; import { CoreSplitViewComponent } from '@components/split-view/split-view'; import { CoreNavigator } from '@services/navigator'; @@ -131,11 +131,4 @@ class AddonBadgesUserBadgesManager extends CorePageItemsListManager { // Calculate current selected item. const route = CoreNavigator.getCurrentRoute({ pageComponent: this.pageComponent }); - if (route !== null && route.firstChild) { - this.updateSelectedItem(route.firstChild.snapshot); - } + this.updateSelectedItem(route?.snapshot ?? null); // Select default item if none is selected on a non-mobile layout. if (!CoreScreen.isMobile && this.selectedItem === null) { @@ -94,9 +92,11 @@ export abstract class CorePageItemsListManager { */ watchSplitViewOutlet(splitView: CoreSplitViewComponent): void { this.splitView = splitView; - this.splitViewOutletSubscription = splitView.outletRouteObservable.subscribe(route => this.updateSelectedItem(route)); + this.splitViewOutletSubscription = splitView.outletRouteObservable.subscribe( + route => this.updateSelectedItem(this.getPageRouteFromSplitViewOutlet(route)), + ); - this.updateSelectedItem(splitView.outletRoute); + this.updateSelectedItem(this.getPageRouteFromSplitViewOutlet(splitView.outletRoute) ?? null); } /** @@ -135,9 +135,8 @@ export abstract class CorePageItemsListManager { } // If this item is already selected, do nothing. - const itemRoute = this.getItemRoute(route); const itemPath = this.getItemPath(item); - const selectedItemPath = itemRoute?.snapshot ? this.getSelectedItemPath(itemRoute.snapshot) : null; + const selectedItemPath = this.getSelectedItemPath(route.snapshot); if (selectedItemPath === itemPath) { return; @@ -168,7 +167,7 @@ export abstract class CorePageItemsListManager { return map; }, {}); - this.updateSelectedItem(this.splitView?.outletRoute); + this.updateSelectedItem(this.getPageRouteFromSplitViewOutlet(this.splitView?.outletRoute ?? null)); } /** @@ -183,8 +182,8 @@ export abstract class CorePageItemsListManager { * * @param route Current route. */ - protected updateSelectedItem(route?: ActivatedRouteSnapshot | null): void { - const selectedItemPath = route ? this.getSelectedItemPath(route) : null; + protected updateSelectedItem(route: ActivatedRouteSnapshot | null): void { + const selectedItemPath = this.getSelectedItemPath(route); this.selectedItem = selectedItemPath ? this.itemsMap?.[selectedItemPath] ?? null @@ -220,25 +219,31 @@ export abstract class CorePageItemsListManager { /** * Get the path of the selected item given the current route. * - * @param route Current route. + * @param route Page route. * @return Path of the selected item in the given route. */ - protected abstract getSelectedItemPath(route: ActivatedRouteSnapshot): string | null; + protected getSelectedItemPath(route?: ActivatedRouteSnapshot | null): string | null { + const segments: UrlSegment[] = []; - /** - * Get the active item route, if any. - * - * @param pageRoute Page route. - * @return Item route. - */ - private getItemRoute(pageRoute: ActivatedRoute): ActivatedRoute | null { - let itemRoute = pageRoute.firstChild; - - while (itemRoute && !itemRoute.component) { - itemRoute = itemRoute.firstChild; + while ((route = route?.firstChild)) { + segments.push(...route.url); } - return itemRoute; + return segments.map(segment => segment.path).join('/').replace(/\/+/, '/').trim() || null; + } + + /** + * Get the page route given a child route on the splitview outlet. + * + * @param route Child route. + * @return Page route. + */ + private getPageRouteFromSplitViewOutlet(route: ActivatedRouteSnapshot | null): ActivatedRouteSnapshot | null { + while (route && route.component !== this.pageComponent) { + route = route.parent; + } + + return route; } } diff --git a/src/core/features/grades/pages/course/course.page.ts b/src/core/features/grades/pages/course/course.page.ts index 540beed21..2c93e5aae 100644 --- a/src/core/features/grades/pages/course/course.page.ts +++ b/src/core/features/grades/pages/course/course.page.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { ActivatedRoute, ActivatedRouteSnapshot, Params } from '@angular/router'; +import { ActivatedRoute, Params } from '@angular/router'; import { AfterViewInit, Component, OnDestroy, ViewChild } from '@angular/core'; import { IonRefresher } from '@ionic/angular'; @@ -176,13 +176,6 @@ class CoreGradesCourseManager extends CorePageItemsListManager