MOBILE-2824 book: Support chapterid param in links

main
Dani Palou 2019-01-22 12:21:54 +01:00
parent dde19b17a6
commit a3dda152f9
4 changed files with 42 additions and 2 deletions

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Component, Optional, Injector } from '@angular/core';
import { Component, Optional, Injector, Input } from '@angular/core';
import { Content, PopoverController } from 'ionic-angular';
import { CoreAppProvider } from '@providers/app';
import { CoreCourseProvider } from '@core/course/providers/course';
@ -29,6 +29,8 @@ import { AddonModBookTocPopoverComponent } from '../../components/toc-popover/to
templateUrl: 'addon-mod-book-index.html',
})
export class AddonModBookIndexComponent extends CoreCourseModuleMainResourceComponent {
@Input() initialChapterId: string; // The initial chapter ID to load.
component = AddonModBookProvider.COMPONENT;
chapterContent: string;
previousChapter: string;
@ -128,7 +130,19 @@ export class AddonModBookIndexComponent extends CoreCourseModuleMainResourceComp
this.contentsMap = this.bookProvider.getContentsMap(this.module.contents);
this.chapters = this.bookProvider.getTocList(this.module.contents);
if (typeof this.currentChapter == 'undefined' && typeof this.initialChapterId != 'undefined' && this.chapters) {
// Initial chapter set. Validate that the chapter exists.
const chapter = this.chapters.find((chapter) => {
return chapter.id == this.initialChapterId;
});
if (chapter) {
this.currentChapter = this.initialChapterId;
}
}
if (typeof this.currentChapter == 'undefined') {
// Load the first chapter.
this.currentChapter = this.bookProvider.getFirstChapter(this.chapters);
}

View File

@ -12,5 +12,5 @@
<ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
</ion-refresher>
<addon-mod-book-index [module]="module" [courseId]="courseId" (dataRetrieved)="updateData($event)"></addon-mod-book-index>
<addon-mod-book-index [module]="module" [courseId]="courseId" [initialChapterId]="chapterId" (dataRetrieved)="updateData($event)"></addon-mod-book-index>
</ion-content>

View File

@ -30,10 +30,12 @@ export class AddonModBookIndexPage {
title: string;
module: any;
courseId: number;
chapterId: number;
constructor(navParams: NavParams) {
this.module = navParams.get('module') || {};
this.courseId = navParams.get('courseId');
this.chapterId = navParams.get('chapterId');
this.title = this.module.name;
}

View File

@ -15,6 +15,7 @@
import { Injectable } from '@angular/core';
import { CoreContentLinksModuleIndexHandler } from '@core/contentlinks/classes/module-index-handler';
import { CoreCourseHelperProvider } from '@core/course/providers/helper';
import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate';
/**
* Handler to treat links to book.
@ -26,4 +27,27 @@ export class AddonModBookLinkHandler extends CoreContentLinksModuleIndexHandler
constructor(courseHelper: CoreCourseHelperProvider) {
super(courseHelper, 'AddonModBook', 'book');
}
/**
* Get the list of actions for a link (url).
*
* @param {string[]} siteIds List of sites the URL belongs to.
* @param {string} url The URL to treat.
* @param {any} params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1}
* @param {number} [courseId] Course ID related to the URL. Optional but recommended.
* @return {CoreContentLinksAction[]|Promise<CoreContentLinksAction[]>} List of (or promise resolved with list of) actions.
*/
getActions(siteIds: string[], url: string, params: any, courseId?: number):
CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> {
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);
}
}];
}
}