MOBILE-3218 links: Adapt links to new handler functions

main
Pau Ferrer Ocaña 2019-11-29 11:39:36 +01:00
parent 30e6feab24
commit ce602321ff
4 changed files with 21 additions and 72 deletions

View File

@ -15,7 +15,6 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CoreContentLinksModuleIndexHandler } from '@core/contentlinks/classes/module-index-handler'; import { CoreContentLinksModuleIndexHandler } from '@core/contentlinks/classes/module-index-handler';
import { CoreCourseHelperProvider } from '@core/course/providers/helper'; import { CoreCourseHelperProvider } from '@core/course/providers/helper';
import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate';
import { AddonModBookProvider } from './book'; import { AddonModBookProvider } from './book';
/** /**
@ -31,26 +30,15 @@ export class AddonModBookLinkHandler extends CoreContentLinksModuleIndexHandler
} }
/** /**
* Get the list of actions for a link (url). * Get the mod params necessary to open an activity.
* *
* @param siteIds List of sites the URL belongs to.
* @param url The URL to treat. * @param url The URL to treat.
* @param params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} * @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. * @param courseId Course ID related to the URL. Optional but recommended.
* @return List of (or promise resolved with list of) actions. * @return List of params to pass to navigateToModule / navigateToModuleByInstance.
*/ */
getActions(siteIds: string[], url: string, params: any, courseId?: number): getPageParams(url: string, params: any, courseId?: number): any {
CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> { return params.chapterid ? {chapterId: parseInt(params.chapterid, 10)} : undefined;
const modParams = params.chapterid ? {chapterId: params.chapterid} : undefined;
courseId = courseId || params.courseid || params.cid;
return [{
action: (siteId, navCtrl?): void => {
this.courseHelper.navigateToModule(parseInt(params.id, 10), siteId, courseId, undefined,
this.useModNameToGetModule ? this.modName : undefined, modParams, navCtrl);
}
}];
} }
/** /**

View File

@ -15,9 +15,6 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CoreContentLinksModuleIndexHandler } from '@core/contentlinks/classes/module-index-handler'; import { CoreContentLinksModuleIndexHandler } from '@core/contentlinks/classes/module-index-handler';
import { CoreCourseHelperProvider } from '@core/course/providers/helper'; import { CoreCourseHelperProvider } from '@core/course/providers/helper';
import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate';
import { CoreCourseProvider } from '@core/course/providers/course';
import { CoreDomUtilsProvider } from '@providers/utils/dom';
/** /**
* Handler to treat links to forum index. * Handler to treat links to forum index.
@ -26,43 +23,7 @@ import { CoreDomUtilsProvider } from '@providers/utils/dom';
export class AddonModForumIndexLinkHandler extends CoreContentLinksModuleIndexHandler { export class AddonModForumIndexLinkHandler extends CoreContentLinksModuleIndexHandler {
name = 'AddonModForumIndexLinkHandler'; name = 'AddonModForumIndexLinkHandler';
constructor(courseHelper: CoreCourseHelperProvider, constructor(courseHelper: CoreCourseHelperProvider) {
private courseProvider: CoreCourseProvider, private domUtils: CoreDomUtilsProvider) { super(courseHelper, 'AddonModForum', 'forum', 'f');
super(courseHelper, 'AddonModForum', 'forum');
// Match the view.php URL with an id param.
this.pattern = new RegExp('\/mod\/forum\/view\.php.*([\&\?](f|id)=\\d+)');
}
/**
* Get the list of actions for a link (url).
*
* @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(siteIds: string[], url: string, params: any, courseId?: number):
CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> {
if (typeof params.f != 'undefined') {
return [{
action: (siteId, navCtrl?): void => {
const modal = this.domUtils.showModalLoading(),
forumId = parseInt(params.f, 10);
this.courseProvider.getModuleBasicInfoByInstance(forumId, 'forum', siteId).then((module) => {
this.courseHelper.navigateToModule(parseInt(module.id, 10), siteId, module.course, undefined,
undefined, undefined, navCtrl);
}).finally(() => {
// Just in case. In fact we need to dismiss the modal before showing a toast or error message.
modal.dismiss();
});
}
}];
}
return super.getActions(siteIds, url, params, courseId);
} }
} }

View File

@ -58,7 +58,7 @@ export class CoreContentLinksModuleIndexHandler extends CoreContentLinksHandlerB
* @param courseId Course ID related to the URL. Optional but recommended. * @param courseId Course ID related to the URL. Optional but recommended.
* @return List of params to pass to navigateToModule / navigateToModuleByInstance. * @return List of params to pass to navigateToModule / navigateToModuleByInstance.
*/ */
getModParams(url: string, params: any, courseId?: number): any { getPageParams(url: string, params: any, courseId?: number): any {
return undefined; return undefined;
} }
@ -75,7 +75,7 @@ export class CoreContentLinksModuleIndexHandler extends CoreContentLinksHandlerB
CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> { CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> {
courseId = courseId || params.courseid || params.cid; courseId = courseId || params.courseid || params.cid;
const modParams = this.getModParams(url, params, courseId); const pageParams = this.getPageParams(url, params, courseId);
if (this.instanceIdParam && typeof params[this.instanceIdParam] != 'undefined') { if (this.instanceIdParam && typeof params[this.instanceIdParam] != 'undefined') {
const instanceId = parseInt(params[this.instanceIdParam], 10); const instanceId = parseInt(params[this.instanceIdParam], 10);
@ -83,7 +83,7 @@ export class CoreContentLinksModuleIndexHandler extends CoreContentLinksHandlerB
return [{ return [{
action: (siteId, navCtrl?): void => { action: (siteId, navCtrl?): void => {
this.courseHelper.navigateToModuleByInstance(instanceId, this.modName, siteId, courseId, undefined, this.courseHelper.navigateToModuleByInstance(instanceId, this.modName, siteId, courseId, undefined,
this.useModNameToGetModule, modParams, navCtrl); this.useModNameToGetModule, pageParams, navCtrl);
} }
}]; }];
} }
@ -91,7 +91,7 @@ export class CoreContentLinksModuleIndexHandler extends CoreContentLinksHandlerB
return [{ return [{
action: (siteId, navCtrl?): void => { action: (siteId, navCtrl?): void => {
this.courseHelper.navigateToModule(parseInt(params.id, 10), siteId, courseId, undefined, this.courseHelper.navigateToModule(parseInt(params.id, 10), siteId, courseId, undefined,
this.useModNameToGetModule ? this.modName : undefined, modParams, navCtrl); this.useModNameToGetModule ? this.modName : undefined, pageParams, navCtrl);
} }
}]; }];
} }

View File

@ -1197,13 +1197,6 @@ export class CoreCourseHelperProvider {
// Get the module. // Get the module.
return this.courseProvider.getModule(moduleId, courseId, sectionId, false, false, siteId, modName); return this.courseProvider.getModule(moduleId, courseId, sectionId, false, false, siteId, modName);
}).then((module) => { }).then((module) => {
const params = {
course: { id: courseId },
module: module,
sectionId: sectionId,
modParams: modParams
};
module.handlerData = this.moduleDelegate.getModuleDataFor(module.modname, module, courseId, sectionId); module.handlerData = this.moduleDelegate.getModuleDataFor(module.modname, module, courseId, sectionId);
if (navCtrl && module.handlerData && module.handlerData.action) { if (navCtrl && module.handlerData && module.handlerData.action) {
@ -1211,11 +1204,18 @@ export class CoreCourseHelperProvider {
// Otherwise, we will redirect below. // Otherwise, we will redirect below.
modal.dismiss(); modal.dismiss();
return module.handlerData.action(new Event('click'), navCtrl, module, courseId); return module.handlerData.action(new Event('click'), navCtrl, module, courseId, undefined, modParams);
} }
this.logger.warn('navCtrl was not passed to navigateToModule by the link handler for ' + module.modname); this.logger.warn('navCtrl was not passed to navigateToModule by the link handler for ' + module.modname);
const params = {
course: { id: courseId },
module: module,
sectionId: sectionId,
modParams: modParams
};
if (courseId == site.getSiteHomeId()) { if (courseId == site.getSiteHomeId()) {
// Check if site home is available. // Check if site home is available.
return this.siteHomeProvider.isAvailable().then(() => { return this.siteHomeProvider.isAvailable().then(() => {