MOBILE-3629 core: Add route options to getRouteParams on CoreNavigator

main
Pau Ferrer Ocaña 2021-03-10 13:55:54 +01:00
parent cbdcb8bd8f
commit 5aaf4acea5
6 changed files with 50 additions and 49 deletions

View File

@ -164,9 +164,9 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView
this.route.queryParams.subscribe(async (params) => { this.route.queryParams.subscribe(async (params) => {
this.loaded = false; this.loaded = false;
this.conversationId = CoreNavigator.getRouteNumberParam('conversationId', params) || undefined; this.conversationId = CoreNavigator.getRouteNumberParam('conversationId', { params }) || undefined;
this.userId = CoreNavigator.getRouteNumberParam('userId', params) || undefined; this.userId = CoreNavigator.getRouteNumberParam('userId', { params }) || undefined;
this.showKeyboard = CoreNavigator.getRouteBooleanParam('showKeyboard', params) || false; this.showKeyboard = CoreNavigator.getRouteBooleanParam('showKeyboard', { params }) || false;
await this.fetchData(); await this.fetchData();

View File

@ -138,8 +138,8 @@ export class AddonMessagesDiscussions35Page implements OnInit, OnDestroy {
*/ */
ngOnInit(): void { ngOnInit(): void {
this.route.queryParams.subscribe(async (params) => { this.route.queryParams.subscribe(async (params) => {
const discussionUserId = CoreNavigator.getRouteNumberParam('discussionUserId', params) || const discussionUserId = CoreNavigator.getRouteNumberParam('discussionUserId', { params }) ||
CoreNavigator.getRouteNumberParam('userId', params) || undefined; CoreNavigator.getRouteNumberParam('userId', { params }) || undefined;
if (this.loaded && this.discussionUserId == discussionUserId) { if (this.loaded && this.discussionUserId == discussionUserId) {
return; return;

View File

@ -273,9 +273,9 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
ngOnInit(): void { ngOnInit(): void {
this.route.queryParams.subscribe(async (params) => { this.route.queryParams.subscribe(async (params) => {
// Conversation to load. // Conversation to load.
this.conversationId = CoreNavigator.getRouteNumberParam('conversationId', params) || undefined; this.conversationId = CoreNavigator.getRouteNumberParam('conversationId', { params }) || undefined;
if (!this.conversationId) { if (!this.conversationId) {
this.discussionUserId = CoreNavigator.getRouteNumberParam('discussionUserId', params) || undefined; this.discussionUserId = CoreNavigator.getRouteNumberParam('discussionUserId', { params }) || undefined;
} }
if (this.conversationId || this.discussionUserId) { if (this.conversationId || this.discussionUserId) {

View File

@ -58,7 +58,7 @@ export class AddonModAssignSubmissionReviewPage implements OnInit, CanLeave {
this.moduleId = CoreNavigator.getRouteNumberParam('cmId')!; this.moduleId = CoreNavigator.getRouteNumberParam('cmId')!;
this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; this.courseId = CoreNavigator.getRouteNumberParam('courseId')!;
this.submitId = CoreNavigator.getRouteNumberParam('submitId') || 0; this.submitId = CoreNavigator.getRouteNumberParam('submitId') || 0;
this.blindId = CoreNavigator.getRouteNumberParam('blindId', params); this.blindId = CoreNavigator.getRouteNumberParam('blindId', { params });
this.fetchSubmission().finally(() => { this.fetchSubmission().finally(() => {
this.loaded = true; this.loaded = true;

View File

@ -16,11 +16,10 @@ import { AfterViewInit, Component, OnDestroy, ViewChild } from '@angular/core';
import { ActivatedRouteSnapshot } from '@angular/router'; import { ActivatedRouteSnapshot } from '@angular/router';
import { CorePageItemsListManager } from '@classes/page-items-list-manager'; import { CorePageItemsListManager } from '@classes/page-items-list-manager';
import { CoreSplitViewComponent, CoreSplitViewMode } from '@components/split-view/split-view'; import { CoreSplitViewComponent } from '@components/split-view/split-view';
import { CoreGrades } from '@features/grades/services/grades'; import { CoreGrades } from '@features/grades/services/grades';
import { CoreGradesGradeOverviewWithCourseData, CoreGradesHelper } from '@features/grades/services/grades-helper'; import { CoreGradesGradeOverviewWithCourseData, CoreGradesHelper } from '@features/grades/services/grades-helper';
import { IonRefresher } from '@ionic/angular'; import { IonRefresher } from '@ionic/angular';
import { CoreNavigator } from '@services/navigator';
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
@ -34,27 +33,13 @@ import { CoreUtils } from '@services/utils/utils';
export class CoreGradesCoursesPage implements OnDestroy, AfterViewInit { export class CoreGradesCoursesPage implements OnDestroy, AfterViewInit {
courses: CoreGradesCoursesManager = new CoreGradesCoursesManager(CoreGradesCoursesPage); courses: CoreGradesCoursesManager = new CoreGradesCoursesManager(CoreGradesCoursesPage);
splitViewMode?: CoreSplitViewMode;
@ViewChild(CoreSplitViewComponent) splitView!: CoreSplitViewComponent; @ViewChild(CoreSplitViewComponent) splitView!: CoreSplitViewComponent;
constructor() {
const userId = CoreNavigator.getRouteNumberParam('userId');
const courseId = CoreNavigator.getRouteNumberParam('courseId');
// If courseId and userId is set, show only the content page.
this.splitViewMode = courseId && userId ? undefined : CoreSplitViewMode.ContentOnly;
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
async ngAfterViewInit(): Promise<void> { async ngAfterViewInit(): Promise<void> {
if (this.splitViewMode) {
// Won't be shown, do nothing.
return;
}
await this.fetchInitialCourses(); await this.fetchInitialCourses();
this.courses.start(this.splitView); this.courses.start(this.splitView);

View File

@ -52,10 +52,11 @@ export type CoreNavigationOptions = {
}; };
/** /**
* Options for CoreNavigatorService#getCurrentRoute method. * Route options to get route or params values.
*/ */
type GetCurrentRouteOptions = Partial<{ export type CoreNavigatorCurrentRouteOptions = Partial<{
parentRoute: ActivatedRoute; params: Params; // Params to get the value from.
route: ActivatedRoute; // Current Route.
pageComponent: unknown; pageComponent: unknown;
}>; }>;
@ -246,8 +247,8 @@ export class CoreNavigatorService {
/** /**
* Iterately get the params checking parent routes. * Iterately get the params checking parent routes.
* *
* @param route Current route.
* @param name Name of the parameter. * @param name Name of the parameter.
* @param route Current route.
* @return Value of the parameter, undefined if not found. * @return Value of the parameter, undefined if not found.
*/ */
protected getRouteSnapshotParam<T = unknown>(name: string, route?: ActivatedRoute): T | undefined { protected getRouteSnapshotParam<T = unknown>(name: string, route?: ActivatedRoute): T | undefined {
@ -270,18 +271,21 @@ export class CoreNavigatorService {
* unless there's a new navigation to the page. * unless there's a new navigation to the page.
* *
* @param name Name of the parameter. * @param name Name of the parameter.
* @param params Optional params to get the value from. If missing, it will autodetect. * @param routeOptions Optional routeOptions to get the params or route value from. If missing, it will autodetect.
* @return Value of the parameter, undefined if not found. * @return Value of the parameter, undefined if not found.
*/ */
getRouteParam<T = unknown>(name: string, params?: Params): T | undefined { getRouteParam<T = unknown>(name: string, routeOptions: CoreNavigatorCurrentRouteOptions = {}): T | undefined {
let value: any; let value: any;
if (!params) { if (!routeOptions.params) {
const route = this.getCurrentRoute(); let route = this.getCurrentRoute();
if (!route?.snapshot && routeOptions.route) {
route = routeOptions.route;
}
value = this.getRouteSnapshotParam(name, route); value = this.getRouteSnapshotParam(name, route);
} else { } else {
value = params[name]; value = routeOptions.params[name];
} }
if (typeof value == 'undefined') { if (typeof value == 'undefined') {
@ -309,11 +313,11 @@ export class CoreNavigatorService {
* Angular router automatically converts numbers to string, this function automatically converts it back to number. * Angular router automatically converts numbers to string, this function automatically converts it back to number.
* *
* @param name Name of the parameter. * @param name Name of the parameter.
* @param params Optional params to get the value from. If missing, it will autodetect. * @param routeOptions Optional routeOptions to get the params or route value from. If missing, it will autodetect.
* @return Value of the parameter, undefined if not found. * @return Value of the parameter, undefined if not found.
*/ */
getRouteNumberParam(name: string, params?: Params): number | undefined { getRouteNumberParam(name: string, routeOptions: CoreNavigatorCurrentRouteOptions = {}): number | undefined {
const value = this.getRouteParam<string>(name, params); const value = this.getRouteParam<string>(name, routeOptions);
return value !== undefined ? Number(value) : value; return value !== undefined ? Number(value) : value;
} }
@ -323,13 +327,25 @@ export class CoreNavigatorService {
* Angular router automatically converts booleans to string, this function automatically converts it back to boolean. * Angular router automatically converts booleans to string, this function automatically converts it back to boolean.
* *
* @param name Name of the parameter. * @param name Name of the parameter.
* @param params Optional params to get the value from. If missing, it will autodetect. * @param routeOptions Optional routeOptions to get the params or route value from. If missing, it will autodetect.
* @return Value of the parameter, undefined if not found. * @return Value of the parameter, undefined if not found.
*/ */
getRouteBooleanParam(name: string, params?: Params): boolean | undefined { getRouteBooleanParam(name: string, routeOptions: CoreNavigatorCurrentRouteOptions = {}): boolean | undefined {
const value = this.getRouteParam<string>(name, params); const value = this.getRouteParam<string>(name, routeOptions);
return value !== undefined ? Boolean(value) : value; if (typeof value == 'undefined') {
return value;
}
if (CoreUtils.isTrueOrOne(value)) {
return true;
}
if (CoreUtils.isFalseOrZero(value)) {
return false;
}
return Boolean(value);
} }
/** /**
@ -345,25 +361,25 @@ export class CoreNavigatorService {
* Get current activated route. * Get current activated route.
* *
* @param options * @param options
* - parent: Parent route, if this isn't provided the current active route will be used. * - route: Parent route, if this isn't provided the current active route will be used.
* - pageComponent: Page component of the route to find, if this isn't provided the deepest route in the hierarchy * - pageComponent: Page component of the route to find, if this isn't provided the deepest route in the hierarchy
* will be returned. * will be returned.
* @return Current activated route. * @return Current activated route.
*/ */
getCurrentRoute(): ActivatedRoute; getCurrentRoute(): ActivatedRoute;
getCurrentRoute(options: GetCurrentRouteOptions): ActivatedRoute | null; getCurrentRoute(options: CoreNavigatorCurrentRouteOptions): ActivatedRoute | null;
getCurrentRoute({ parentRoute, pageComponent }: GetCurrentRouteOptions = {}): ActivatedRoute | null { getCurrentRoute({ route, pageComponent }: CoreNavigatorCurrentRouteOptions = {}): ActivatedRoute | null {
parentRoute = parentRoute ?? Router.routerState.root; route = route ?? Router.routerState.root;
if (pageComponent && parentRoute.component === pageComponent) { if (pageComponent && route.component === pageComponent) {
return parentRoute; return route;
} }
if (parentRoute.firstChild) { if (route.firstChild) {
return this.getCurrentRoute({ parentRoute: parentRoute.firstChild, pageComponent }); return this.getCurrentRoute({ route: route.firstChild, pageComponent });
} }
return pageComponent ? null : parentRoute; return pageComponent ? null : route;
} }
/** /**