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> <ion-content>
<nav> <nav>
<ion-list> <ion-list>
<a ion-item text-wrap *ngFor="let chapter of chapters" (click)="loadChapter(chapter.id)" [class.core-nav-item-selected]="selected == chapter.id"> <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.title}}</p> <p [attr.padding-left]="chapter.level == 1 ? true : null">{{chapter.number}} {{chapter.title}}</p>
</a> </a>
</ion-list> </ion-list>
</nav> </nav>

View File

@ -80,11 +80,9 @@ export class AddonModBookProvider {
// Search the book. // Search the book.
if (response && response.books) { if (response && response.books) {
for (const i in response.books) { const book = response.books.find((book) => book[key] == value);
const book = response.books[i]; if (book) {
if (book[key] == value) { return book;
return book;
}
} }
} }
@ -278,21 +276,41 @@ export class AddonModBookProvider {
* @return The toc as a list. * @return The toc as a list.
*/ */
getTocList(contents: any[]): AddonModBookTocChapter[] { 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 = [], const chapters = [],
toc = this.getToc(contents); toc = this.getToc(contents);
let chapterNumber = 1;
toc.forEach((chapter) => { toc.forEach((chapter) => {
const tocChapter = getChapterInfo(chapter, chapterNumber);
// Add the chapter to the list. // Add the chapter to the list.
let chapterId = chapter.href.replace('/index.html', ''); chapters.push(tocChapter);
chapters.push({id: chapterId, title: chapter.title, level: chapter.level});
if (chapter.subitems) { if (chapter.subitems) {
let subChapterNumber = 1;
// Add all the subchapters to the list. // Add all the subchapters to the list.
chapter.subitems.forEach((subChapter) => { chapter.subitems.forEach((subChapter) => {
chapterId = subChapter.href.replace('/index.html', ''); chapters.push(getChapterInfo(subChapter, subChapterNumber, tocChapter.number));
chapters.push({id: chapterId, title: subChapter.title, level: subChapter.level}); subChapterNumber++;
}); });
} }
chapterNumber++;
}); });
return chapters; return chapters;
@ -376,7 +394,7 @@ export class AddonModBookProvider {
/** /**
* A book chapter inside the toc list. * A book chapter inside the toc list.
*/ */
export type AddonModBookTocChapter = { export interface AddonModBookTocChapter {
/** /**
* ID to identify the chapter. * ID to identify the chapter.
*/ */
@ -391,7 +409,17 @@ export type AddonModBookTocChapter = {
* The chapter's level. * The chapter's level.
*/ */
level: number; 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 * 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