From 82611bcf3ad0dba4390af8799bc7950a474c0a6d Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Mon, 8 Jul 2019 12:15:29 +0200 Subject: [PATCH] MOBILE-2201 book: Tag area handler for book chapters --- scripts/langindex.json | 1 + src/addon/mod/book/book.module.ts | 9 ++- src/addon/mod/book/lang/en.json | 1 + .../mod/book/providers/tag-area-handler.ts | 75 +++++++++++++++++++ src/assets/lang/en.json | 1 + 5 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 src/addon/mod/book/providers/tag-area-handler.ts diff --git a/scripts/langindex.json b/scripts/langindex.json index a0dbf92e1..57c4bf18d 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -372,6 +372,7 @@ "addon.mod_assign_submission_onlinetext.wordlimitexceeded": "assignsubmission_onlinetext", "addon.mod_book.errorchapter": "book", "addon.mod_book.modulenameplural": "book", + "addon.mod_book.tagarea_book_chapters": "book", "addon.mod_book.toc": "book", "addon.mod_chat.beep": "chat", "addon.mod_chat.chatreport": "chat", diff --git a/src/addon/mod/book/book.module.ts b/src/addon/mod/book/book.module.ts index cc06e008d..ea425d1c8 100644 --- a/src/addon/mod/book/book.module.ts +++ b/src/addon/mod/book/book.module.ts @@ -22,6 +22,8 @@ import { AddonModBookPrefetchHandler } from './providers/prefetch-handler'; import { CoreCourseModuleDelegate } from '@core/course/providers/module-delegate'; import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate'; import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate'; +import { CoreTagAreaDelegate } from '@core/tag/providers/area-delegate'; +import { AddonModBookTagAreaHandler } from './providers/tag-area-handler'; // List of providers (without handlers). export const ADDON_MOD_BOOK_PROVIDERS: any[] = [ @@ -39,18 +41,21 @@ export const ADDON_MOD_BOOK_PROVIDERS: any[] = [ AddonModBookModuleHandler, AddonModBookLinkHandler, AddonModBookListLinkHandler, - AddonModBookPrefetchHandler + AddonModBookPrefetchHandler, + AddonModBookTagAreaHandler ] }) export class AddonModBookModule { constructor(moduleDelegate: CoreCourseModuleDelegate, moduleHandler: AddonModBookModuleHandler, contentLinksDelegate: CoreContentLinksDelegate, linkHandler: AddonModBookLinkHandler, prefetchDelegate: CoreCourseModulePrefetchDelegate, prefetchHandler: AddonModBookPrefetchHandler, - listLinkHandler: AddonModBookListLinkHandler) { + listLinkHandler: AddonModBookListLinkHandler, tagAreaDelegate: CoreTagAreaDelegate, + tagAreaHandler: AddonModBookTagAreaHandler) { moduleDelegate.registerHandler(moduleHandler); contentLinksDelegate.registerHandler(linkHandler); contentLinksDelegate.registerHandler(listLinkHandler); prefetchDelegate.registerHandler(prefetchHandler); + tagAreaDelegate.registerHandler(tagAreaHandler); } } diff --git a/src/addon/mod/book/lang/en.json b/src/addon/mod/book/lang/en.json index 7d1140fe4..4f8f32f54 100644 --- a/src/addon/mod/book/lang/en.json +++ b/src/addon/mod/book/lang/en.json @@ -1,5 +1,6 @@ { "errorchapter": "Error reading chapter of book.", "modulenameplural": "Books", + "tagarea_book_chapters": "Book chapters", "toc": "Table of contents" } \ No newline at end of file diff --git a/src/addon/mod/book/providers/tag-area-handler.ts b/src/addon/mod/book/providers/tag-area-handler.ts new file mode 100644 index 000000000..47c456cb6 --- /dev/null +++ b/src/addon/mod/book/providers/tag-area-handler.ts @@ -0,0 +1,75 @@ +// (C) Copyright 2015 Martin Dougiamas +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { Injectable, Injector } from '@angular/core'; +import { CoreTagAreaHandler } from '@core/tag/providers/area-delegate'; +import { CoreTagHelperProvider } from '@core/tag/providers/helper'; +import { CoreTagFeedComponent } from '@core/tag/components/feed/feed'; +import { CoreCourseProvider } from '@core/course/providers/course'; +import { CoreUrlUtilsProvider } from '@providers/utils/url'; +import { AddonModBookProvider } from './book'; + +/** + * Handler to support tags. + */ +@Injectable() +export class AddonModBookTagAreaHandler implements CoreTagAreaHandler { + name = 'AddonModBookTagAreaHandler'; + type = 'mod_book/book_chapters'; + + constructor(private tagHelper: CoreTagHelperProvider, private bookProvider: AddonModBookProvider, + private courseProvider: CoreCourseProvider, private urlUtils: CoreUrlUtilsProvider) {} + + /** + * Whether or not the handler is enabled on a site level. + * @return {boolean|Promise} Whether or not the handler is enabled on a site level. + */ + isEnabled(): boolean | Promise { + return this.bookProvider.isPluginEnabled(); + } + + /** + * Parses the rendered content of a tag index and returns the items. + * + * @param {string} content Rendered content. + * @return {any[]|Promise} Area items (or promise resolved with the items). + */ + parseContent(content: string): any[] | Promise { + const items = this.tagHelper.parseFeedContent(content); + + // Find module ids of the returned books, they are needed by the link delegate. + return Promise.all(items.map((item) => { + const params = this.urlUtils.extractUrlParams(item.url); + if (params.b && !params.id) { + const bookId = parseInt(params.b, 10); + + return this.courseProvider.getModuleBasicInfoByInstance(bookId, 'book').then((module) => { + item.url += '&id=' + module.id; + }); + } + })).then(() => { + return items; + }); + } + + /** + * Get the component to use to display items. + * + * @param {Injector} injector Injector. + * @return {any|Promise} The component (or promise resolved with component) to use, undefined if not found. + */ + getComponent(injector: Injector): any | Promise { + return CoreTagFeedComponent; + } +} diff --git a/src/assets/lang/en.json b/src/assets/lang/en.json index f48252c14..121bd0dba 100644 --- a/src/assets/lang/en.json +++ b/src/assets/lang/en.json @@ -372,6 +372,7 @@ "addon.mod_assign_submission_onlinetext.wordlimitexceeded": "The word limit for this assignment is {{$a.limit}} words and you are attempting to submit {{$a.count}} words. Please review your submission and try again.", "addon.mod_book.errorchapter": "Error reading chapter of book.", "addon.mod_book.modulenameplural": "Books", + "addon.mod_book.tagarea_book_chapters": "Book chapters", "addon.mod_book.toc": "Table of contents", "addon.mod_chat.beep": "Beep", "addon.mod_chat.chatreport": "Chat sessions",