Merge pull request #2100 from crazyserver/MOBILE-2872

MOBILE-2872 book: Show hidden chapters
main
Juan Leyva 2019-10-01 10:40:48 +02:00 committed by GitHub
commit 843fc6276b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 13 deletions

View File

@ -11,8 +11,8 @@
<ion-content>
<nav>
<ion-list>
<a ion-item text-wrap *ngFor="let chapter of chapters" (click)="loadChapter(chapter.id)" [class.core-nav-item-selected]="selected == chapter.id">
<p [attr.padding-left]="chapter.level == 1 ? true : null">{{chapter.title}}</p>
<a ion-item text-wrap *ngFor="let chapter of chapters" (click)="loadChapter(chapter.id)" [class.core-nav-item-selected]="selected == chapter.id" [class.item-dimmed]="chapter.hidden">
<p [attr.padding-left]="chapter.level == 1 ? true : null">{{chapter.number}} {{chapter.title}}</p>
</a>
</ion-list>
</nav>

View File

@ -80,11 +80,9 @@ export class AddonModBookProvider {
// Search the book.
if (response && response.books) {
for (const i in response.books) {
const book = response.books[i];
if (book[key] == value) {
return book;
}
const book = response.books.find((book) => book[key] == value);
if (book) {
return book;
}
}
@ -278,21 +276,41 @@ export class AddonModBookProvider {
* @return The toc as a list.
*/
getTocList(contents: any[]): AddonModBookTocChapter[] {
// Convenience function to get chapter info.
const getChapterInfo = (chapter: any, chapterNumber: number, previousNumber: string = ''): AddonModBookTocChapter => {
chapter.hidden = !!parseInt(chapter.hidden, 10);
const fullChapterNumber = previousNumber + (chapter.hidden ? 'x.' : chapterNumber + '.');
return {
id: chapter.href.replace('/index.html', ''),
title: chapter.title,
level: chapter.level,
number: fullChapterNumber,
hidden: chapter.hidden
};
};
const chapters = [],
toc = this.getToc(contents);
let chapterNumber = 1;
toc.forEach((chapter) => {
const tocChapter = getChapterInfo(chapter, chapterNumber);
// Add the chapter to the list.
let chapterId = chapter.href.replace('/index.html', '');
chapters.push({id: chapterId, title: chapter.title, level: chapter.level});
chapters.push(tocChapter);
if (chapter.subitems) {
let subChapterNumber = 1;
// Add all the subchapters to the list.
chapter.subitems.forEach((subChapter) => {
chapterId = subChapter.href.replace('/index.html', '');
chapters.push({id: chapterId, title: subChapter.title, level: subChapter.level});
chapters.push(getChapterInfo(subChapter, subChapterNumber, tocChapter.number));
subChapterNumber++;
});
}
chapterNumber++;
});
return chapters;
@ -376,7 +394,7 @@ export class AddonModBookProvider {
/**
* A book chapter inside the toc list.
*/
export type AddonModBookTocChapter = {
export interface AddonModBookTocChapter {
/**
* ID to identify the chapter.
*/
@ -391,7 +409,17 @@ export type AddonModBookTocChapter = {
* The chapter's level.
*/
level: number;
};
/**
* The chapter is hidden.
*/
hidden: boolean;
/**
* The chapter's number'.
*/
number: string;
}
/**
* Map of book contents. For each chapter it has its index URL and the list of paths of the files the chapter has. Each path