From bc1e3f00e7fe53b594acf44da6372052f5b5b192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Thu, 11 Jul 2024 22:40:08 +0200 Subject: [PATCH] MOBILE-4616 book: Move book constants to a file --- src/addons/mod/book/book-lazy.module.ts | 2 +- src/addons/mod/book/book.module.ts | 7 ++-- src/addons/mod/book/components/index/index.ts | 6 +-- src/addons/mod/book/components/toc/toc.ts | 3 +- src/addons/mod/book/constants.ts | 34 +++++++++++++++++ .../mod/book/pages/contents/contents.ts | 5 +-- src/addons/mod/book/services/book.ts | 38 +++++-------------- .../mod/book/services/handlers/module.ts | 5 +-- .../mod/book/services/handlers/prefetch.ts | 5 ++- 9 files changed, 60 insertions(+), 45 deletions(-) create mode 100644 src/addons/mod/book/constants.ts diff --git a/src/addons/mod/book/book-lazy.module.ts b/src/addons/mod/book/book-lazy.module.ts index 43b4af496..95c30c6f2 100644 --- a/src/addons/mod/book/book-lazy.module.ts +++ b/src/addons/mod/book/book-lazy.module.ts @@ -43,4 +43,4 @@ const routes: Routes = [ AddonModBookContentsPage, ], }) -export class AddonModBookLazyModule {} +export default class AddonModBookLazyModule {} diff --git a/src/addons/mod/book/book.module.ts b/src/addons/mod/book/book.module.ts index 82c5f9f33..9ae4af590 100644 --- a/src/addons/mod/book/book.module.ts +++ b/src/addons/mod/book/book.module.ts @@ -19,18 +19,19 @@ import { CoreCourseModuleDelegate } from '@features/course/services/module-deleg import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate'; import { CoreMainMenuTabRoutingModule } from '@features/mainmenu/mainmenu-tab-routing.module'; import { CoreTagAreaDelegate } from '@features/tag/services/tag-area-delegate'; -import { AddonModBookModuleHandler, AddonModBookModuleHandlerService } from './services/handlers/module'; +import { AddonModBookModuleHandler } from './services/handlers/module'; import { AddonModBookIndexLinkHandler } from './services/handlers/index-link'; import { AddonModBookListLinkHandler } from './services/handlers/list-link'; import { AddonModBookPrefetchHandler } from './services/handlers/prefetch'; import { AddonModBookTagAreaHandler } from './services/handlers/tag-area'; import { CORE_SITE_SCHEMAS } from '@services/sites'; import { BOOK_SITE_SCHEMA } from './services/database/book'; +import { ADDON_MOD_BOOK_PAGE_NAME } from './constants'; const routes: Routes = [ { - path: AddonModBookModuleHandlerService.PAGE_NAME, - loadChildren: () => import('./book-lazy.module').then(m => m.AddonModBookLazyModule), + path: ADDON_MOD_BOOK_PAGE_NAME, + loadChildren: () => import('./book-lazy.module'), }, ]; diff --git a/src/addons/mod/book/components/index/index.ts b/src/addons/mod/book/components/index/index.ts index aa77f2d33..1a03cb0db 100644 --- a/src/addons/mod/book/components/index/index.ts +++ b/src/addons/mod/book/components/index/index.ts @@ -14,12 +14,12 @@ import { Component, Optional, OnInit, OnDestroy } from '@angular/core'; import { CoreCourseModuleMainResourceComponent } from '@features/course/classes/main-resource-component'; -import { AddonModBook, AddonModBookBookWSData, AddonModBookNumbering, AddonModBookTocChapter } from '../../services/book'; +import { AddonModBook, AddonModBookBookWSData, AddonModBookTocChapter } from '../../services/book'; import { CoreCourseContentsPage } from '@features/course/pages/contents/contents'; import { CoreCourse } from '@features/course/services/course'; import { CoreNavigator } from '@services/navigator'; -import { AddonModBookModuleHandlerService } from '../../services/handlers/module'; import { CoreUtils } from '@services/utils/utils'; +import { ADDON_MOD_BOOK_PAGE_NAME, AddonModBookNumbering } from '../../constants'; /** * Component that displays a book entry page. @@ -116,7 +116,7 @@ export class AddonModBookIndexComponent extends CoreCourseModuleMainResourceComp */ async openBook(chapterId?: number): Promise { await CoreNavigator.navigateToSitePath( - `${AddonModBookModuleHandlerService.PAGE_NAME}/${this.courseId}/${this.module.id}/contents`, + `${ADDON_MOD_BOOK_PAGE_NAME}/${this.courseId}/${this.module.id}/contents`, { params: { chapterId } }, ); diff --git a/src/addons/mod/book/components/toc/toc.ts b/src/addons/mod/book/components/toc/toc.ts index ed38ca1aa..f59ccdf2f 100644 --- a/src/addons/mod/book/components/toc/toc.ts +++ b/src/addons/mod/book/components/toc/toc.ts @@ -14,7 +14,8 @@ import { Component, Input, OnInit } from '@angular/core'; import { ModalController } from '@singletons'; -import { AddonModBookTocChapter, AddonModBookBookWSData, AddonModBookNumbering } from '../../services/book'; +import { AddonModBookTocChapter, AddonModBookBookWSData } from '../../services/book'; +import { AddonModBookNumbering } from '../../constants'; /** * Modal to display the TOC of a book. diff --git a/src/addons/mod/book/constants.ts b/src/addons/mod/book/constants.ts new file mode 100644 index 000000000..74f83cfdb --- /dev/null +++ b/src/addons/mod/book/constants.ts @@ -0,0 +1,34 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// 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. + +export const ADDON_MOD_BOOK_COMPONENT = 'mmaModBook'; +export const ADDON_MOD_BOOK_PAGE_NAME = 'mod_book'; +/** + * Constants to define how the chapters and subchapters of a book should be displayed in that table of contents. + */ +export const enum AddonModBookNumbering { + NONE = 0, + NUMBERS = 1, + BULLETS = 2, + INDENTED = 3, +} + +/** + * Constants to define the navigation style used within a book. + */ +export const enum AddonModBookNavStyle { + TOC_ONLY = 0, + IMAGE = 1, + TEXT = 2, +} diff --git a/src/addons/mod/book/pages/contents/contents.ts b/src/addons/mod/book/pages/contents/contents.ts index bff523109..653974be2 100644 --- a/src/addons/mod/book/pages/contents/contents.ts +++ b/src/addons/mod/book/pages/contents/contents.ts @@ -35,12 +35,11 @@ import { AddonModBook, AddonModBookBookWSData, AddonModBookContentsMap, - AddonModBookNavStyle, - AddonModBookProvider, AddonModBookTocChapter, } from '../../services/book'; import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics'; import { CoreUrlUtils } from '@services/utils/url'; +import { ADDON_MOD_BOOK_COMPONENT, AddonModBookNavStyle } from '../../constants'; /** * Page that displays a book contents. @@ -58,7 +57,7 @@ export class AddonModBookContentsPage implements OnInit, OnDestroy { cmId!: number; courseId!: number; initialChapterId?: number; - component = AddonModBookProvider.COMPONENT; + component = ADDON_MOD_BOOK_COMPONENT; manager?: CoreSwipeSlidesItemsManager; warning = ''; displayNavBar = true; diff --git a/src/addons/mod/book/services/book.ts b/src/addons/mod/book/services/book.ts index 69ec1c329..e8da3e010 100644 --- a/src/addons/mod/book/services/book.ts +++ b/src/addons/mod/book/services/book.ts @@ -27,27 +27,7 @@ import { CoreDomUtils } from '@services/utils/dom'; import { CoreFile } from '@services/file'; import { CoreError } from '@classes/errors/error'; import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site'; - -/** - * Constants to define how the chapters and subchapters of a book should be displayed in that table of contents. - */ -export const enum AddonModBookNumbering { - NONE = 0, - NUMBERS = 1, - BULLETS = 2, - INDENTED = 3, -} - -/** - * Constants to define the navigation style used within a book. - */ -export const enum AddonModBookNavStyle { - TOC_ONLY = 0, - IMAGE = 1, - TEXT = 2, -} - -const ROOT_CACHE_KEY = 'mmaModBook:'; +import { ADDON_MOD_BOOK_COMPONENT } from '../constants'; /** * Service that provides some features for books. @@ -55,7 +35,7 @@ const ROOT_CACHE_KEY = 'mmaModBook:'; @Injectable({ providedIn: 'root' }) export class AddonModBookProvider { - static readonly COMPONENT = 'mmaModBook'; + protected static readonly ROOT_CACHE_KEY = 'mmaModBook:'; /** * Get a book by course module ID. @@ -92,7 +72,7 @@ export class AddonModBookProvider { const preSets: CoreSiteWSPreSets = { cacheKey: this.getBookDataCacheKey(courseId), updateFrequency: CoreSite.FREQUENCY_RARELY, - component: AddonModBookProvider.COMPONENT, + component: ADDON_MOD_BOOK_COMPONENT, ...CoreSites.getReadingStrategyPreSets(options.readingStrategy), }; @@ -114,7 +94,7 @@ export class AddonModBookProvider { * @returns Cache key. */ protected getBookDataCacheKey(courseId: number): string { - return ROOT_CACHE_KEY + 'book:' + courseId; + return AddonModBookProvider.ROOT_CACHE_KEY + 'book:' + courseId; } /** @@ -140,7 +120,7 @@ export class AddonModBookProvider { const siteId = CoreSites.getCurrentSiteId(); - const url = await CoreFilepool.downloadUrl(siteId, indexUrl, false, AddonModBookProvider.COMPONENT, moduleId); + const url = await CoreFilepool.downloadUrl(siteId, indexUrl, false, ADDON_MOD_BOOK_COMPONENT, moduleId); const content = await CoreWS.getText(url); @@ -226,7 +206,7 @@ export class AddonModBookProvider { */ async getLastChapterViewed(id: number, siteId?: string): Promise { const site = await CoreSites.getSite(siteId); - const entry = await site.getLastViewed(AddonModBookProvider.COMPONENT, id); + const entry = await site.getLastViewed(ADDON_MOD_BOOK_COMPONENT, id); const chapterId = Number(entry?.value); @@ -327,7 +307,7 @@ export class AddonModBookProvider { const promises: Promise[] = []; promises.push(this.invalidateBookData(courseId, siteId)); - promises.push(CoreFilepool.invalidateFilesByComponent(siteId, AddonModBookProvider.COMPONENT, moduleId)); + promises.push(CoreFilepool.invalidateFilesByComponent(siteId, ADDON_MOD_BOOK_COMPONENT, moduleId)); promises.push(CoreCourse.invalidateModule(moduleId, siteId)); return CoreUtils.allPromises(promises); @@ -372,7 +352,7 @@ export class AddonModBookProvider { await CoreCourseLogHelper.log( 'mod_book_view_book', params, - AddonModBookProvider.COMPONENT, + ADDON_MOD_BOOK_COMPONENT, id, siteId, ); @@ -390,7 +370,7 @@ export class AddonModBookProvider { async storeLastChapterViewed(id: number, chapterId: number, courseId: number, siteId?: string): Promise { const site = await CoreSites.getSite(siteId); - await site.storeLastViewed(AddonModBookProvider.COMPONENT, id, chapterId, { data: String(courseId) }); + await site.storeLastViewed(ADDON_MOD_BOOK_COMPONENT, id, chapterId, { data: String(courseId) }); } } diff --git a/src/addons/mod/book/services/handlers/module.ts b/src/addons/mod/book/services/handlers/module.ts index 85abc0d02..341acacc0 100644 --- a/src/addons/mod/book/services/handlers/module.ts +++ b/src/addons/mod/book/services/handlers/module.ts @@ -18,6 +18,7 @@ import { CoreConstants, ModPurpose } from '@/core/constants'; import { CoreCourseModuleHandler } from '@features/course/services/module-delegate'; import { makeSingleton } from '@singletons'; import { CoreModuleHandlerBase } from '@features/course/classes/module-base-handler'; +import { ADDON_MOD_BOOK_PAGE_NAME } from '../../constants'; /** * Handler to support book modules. @@ -25,11 +26,9 @@ import { CoreModuleHandlerBase } from '@features/course/classes/module-base-hand @Injectable({ providedIn: 'root' }) export class AddonModBookModuleHandlerService extends CoreModuleHandlerBase implements CoreCourseModuleHandler { - static readonly PAGE_NAME = 'mod_book'; - name = 'AddonModBook'; modName = 'book'; - protected pageName = AddonModBookModuleHandlerService.PAGE_NAME; + protected pageName = ADDON_MOD_BOOK_PAGE_NAME; supportedFeatures = { [CoreConstants.FEATURE_MOD_ARCHETYPE]: CoreConstants.MOD_ARCHETYPE_RESOURCE, diff --git a/src/addons/mod/book/services/handlers/prefetch.ts b/src/addons/mod/book/services/handlers/prefetch.ts index a4cd7ac76..640a69e40 100644 --- a/src/addons/mod/book/services/handlers/prefetch.ts +++ b/src/addons/mod/book/services/handlers/prefetch.ts @@ -19,7 +19,8 @@ import { CoreCourseModuleData } from '@features/course/services/course-helper'; import { CoreUtils } from '@services/utils/utils'; import { CoreWSFile } from '@services/ws'; import { makeSingleton } from '@singletons'; -import { AddonModBook, AddonModBookProvider } from '../book'; +import { AddonModBook } from '../book'; +import { ADDON_MOD_BOOK_COMPONENT } from '../../constants'; /** * Handler to prefetch books. @@ -29,7 +30,7 @@ export class AddonModBookPrefetchHandlerService extends CoreCourseResourcePrefet name = 'AddonModBook'; modName = 'book'; - component = AddonModBookProvider.COMPONENT; + component = ADDON_MOD_BOOK_COMPONENT; updatesNames = /^configuration$|^.*files$|^entries$/; /**