From d6a42f992ba90dfe3fe34bd0913f7eece4923173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Mon, 9 Nov 2020 13:37:31 +0100 Subject: [PATCH] MOBILE-3565 settings: Implement settings delegate --- src/app/app.module.ts | 2 +- src/app/classes/delegate-sorted.ts | 104 ++++++++++ src/app/classes/delegate.ts | 53 +++++- src/app/components/file/file.ts | 2 +- src/app/core/mainmenu/handlers/mainmenu.ts | 2 +- src/app/core/mainmenu/mainmenu.module.ts | 2 +- src/app/core/mainmenu/pages/home/home.page.ts | 2 +- src/app/core/mainmenu/pages/menu/menu.page.ts | 4 +- src/app/core/mainmenu/pages/more/more.page.ts | 18 +- src/app/core/mainmenu/services/delegate.ts | 177 ------------------ .../core/mainmenu/services/home.delegate.ts | 90 +-------- .../mainmenu/services/mainmenu.delegate.ts | 101 ++++++++++ src/app/core/mainmenu/services/mainmenu.ts | 4 +- .../settings/services/settings.delegate.ts | 74 ++++++++ src/app/services/filepool.ts | 2 +- ...le-delegate.ts => plugin-file.delegate.ts} | 10 +- 16 files changed, 347 insertions(+), 300 deletions(-) create mode 100644 src/app/classes/delegate-sorted.ts delete mode 100644 src/app/core/mainmenu/services/delegate.ts create mode 100644 src/app/core/mainmenu/services/mainmenu.delegate.ts create mode 100644 src/app/core/settings/services/settings.delegate.ts rename src/app/services/{plugin-file-delegate.ts => plugin-file.delegate.ts} (97%) diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 2c865e6c2..7ff90cadd 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -37,7 +37,7 @@ import { CoreGroupsProvider } from '@services/groups'; import { CoreInitDelegate, CoreInit } from '@services/init'; import { CoreLangProvider } from '@services/lang'; import { CoreLocalNotificationsProvider } from '@services/local-notifications'; -import { CorePluginFileDelegate } from '@services/plugin-file-delegate'; +import { CorePluginFileDelegate } from '@services/plugin-file.delegate'; import { CoreSitesProvider, CoreSites } from '@services/sites'; import { CoreSyncProvider } from '@services/sync'; import { CoreUpdateManagerProvider, CoreUpdateManager } from '@services/update-manager'; diff --git a/src/app/classes/delegate-sorted.ts b/src/app/classes/delegate-sorted.ts new file mode 100644 index 000000000..1b8c121e6 --- /dev/null +++ b/src/app/classes/delegate-sorted.ts @@ -0,0 +1,104 @@ +// (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. + +import { BehaviorSubject, Subject } from 'rxjs'; +import { CoreEvents } from '../singletons/events'; +import { CoreDelegate, CoreDelegateDisplayHandler, CoreDelegateToDisplay } from './delegate'; + +/** + * Superclass to help creating sorted delegates. + */ +export class CoreSortedDelegate< + DisplayType extends CoreDelegateToDisplay, + HandlerType extends CoreDelegateDisplayHandler> + extends CoreDelegate { + + protected loaded = false; + protected sortedHandlersRxJs: Subject = new BehaviorSubject([]); + protected sortedHandlers: DisplayType[] = []; + + /** + * Constructor of the Delegate. + * + * @param delegateName Delegate name used for logging purposes. + * @param listenSiteEvents Whether to update the handler when a site event occurs (login, site updated, ...). + */ + constructor(delegateName: string) { + super(delegateName, true); + + CoreEvents.on(CoreEvents.LOGOUT, this.clearSortedHandlers.bind(this)); + } + + /** + * Check if handlers are loaded. + * + * @return True if handlers are loaded, false otherwise. + */ + areHandlersLoaded(): boolean { + return this.loaded; + } + + /** + * Clear current site handlers. Reserved for core use. + */ + protected clearSortedHandlers(): void { + this.loaded = false; + this.sortedHandlersRxJs.next([]); + this.sortedHandlers = []; + } + + /** + * Get the handlers for the current site. + * + * @return An observable that will receive the handlers. + */ + getHandlers(): DisplayType[] { + return this.sortedHandlers; + } + + /** + * Get the handlers for the current site. + * + * @return An observable that will receive the handlers. + */ + getHandlersObservable(): Subject { + return this.sortedHandlersRxJs; + } + + /** + * Update handlers Data. + */ + updateData(): void { + const displayData: DisplayType[] = []; + + for (const name in this.enabledHandlers) { + const handler = this.enabledHandlers[name]; + const data = handler.getDisplayData(); + + data.priority = handler.priority; + data.name = handler.name; + + displayData.push(data); + } + + // Sort them by priority. + displayData.sort((a, b) => (b.priority || 0) - (a.priority || 0)); + + this.loaded = true; + this.sortedHandlersRxJs.next(displayData); + this.sortedHandlers = displayData; + } + +} + diff --git a/src/app/classes/delegate.ts b/src/app/classes/delegate.ts index 29dbbec7c..190391180 100644 --- a/src/app/classes/delegate.ts +++ b/src/app/classes/delegate.ts @@ -20,7 +20,7 @@ import { CoreLogger } from '@singletons/logger'; /** * Superclass to help creating delegates */ -export class CoreDelegate { +export class CoreDelegate { /** * Logger instance. @@ -30,17 +30,17 @@ export class CoreDelegate { /** * List of registered handlers. */ - protected handlers: { [s: string]: CoreDelegateHandler } = {}; + protected handlers: { [s: string]: HandlerType } = {}; /** * List of registered handlers enabled for the current site. */ - protected enabledHandlers: { [s: string]: CoreDelegateHandler } = {}; + protected enabledHandlers: { [s: string]: HandlerType } = {}; /** * Default handler */ - protected defaultHandler?: CoreDelegateHandler; + protected defaultHandler?: HandlerType; /** * Time when last updateHandler functions started. @@ -136,7 +136,7 @@ export class CoreDelegate { * @param params Parameters to pass to the function. * @return Function returned value or default value. */ - private execute(handler: CoreDelegateHandler, fnName: string, params?: unknown[]): T | undefined { + private execute(handler: HandlerType, fnName: string, params?: unknown[]): T | undefined { if (handler && handler[fnName]) { return handler[fnName].apply(handler, params); } else if (this.defaultHandler && this.defaultHandler[fnName]) { @@ -151,7 +151,7 @@ export class CoreDelegate { * @param enabled Only enabled, or any. * @return Handler. */ - protected getHandler(handlerName: string, enabled: boolean = false): CoreDelegateHandler { + protected getHandler(handlerName: string, enabled: boolean = false): HandlerType { return enabled ? this.enabledHandlers[handlerName] : this.handlers[handlerName]; } @@ -218,7 +218,7 @@ export class CoreDelegate { * @param handler The handler delegate object to register. * @return True when registered, false if already registered. */ - registerHandler(handler: CoreDelegateHandler): boolean { + registerHandler(handler: HandlerType): boolean { const key = handler[this.handlerNameProperty] || handler.name; if (typeof this.handlers[key] !== 'undefined') { @@ -240,7 +240,7 @@ export class CoreDelegate { * @param time Time this update process started. * @return Resolved when done. */ - protected updateHandler(handler: CoreDelegateHandler): Promise { + protected updateHandler(handler: HandlerType): Promise { const siteId = CoreSites.instance.getCurrentSiteId(); const currentSite = CoreSites.instance.getCurrentSite(); let promise: Promise; @@ -287,7 +287,7 @@ export class CoreDelegate { * @param site Site to check. * @return Whether is enabled or disabled in site. */ - protected isFeatureDisabled(handler: CoreDelegateHandler, site: CoreSite): boolean { + protected isFeatureDisabled(handler: HandlerType, site: CoreSite): boolean { return typeof this.featurePrefix != 'undefined' && site.isFeatureDisabled(this.featurePrefix + handler.name); } @@ -334,6 +334,9 @@ export class CoreDelegate { } +/** + * Base interface for any delegate. + */ export interface CoreDelegateHandler { /** * Name of the handler, or name and sub context (AddonMessages, AddonMessages:blockContact, ...). @@ -348,3 +351,35 @@ export interface CoreDelegateHandler { */ isEnabled(): Promise; } + +/** + * Data returned by the delegate for each handler to be displayed. + */ +export interface CoreDelegateToDisplay { + /** + * Name of the handler. + */ + name?: string; + + /** + * Priority of the handler. + */ + priority?: number; +} + +/** + * Base interface for a core delegate needed to be displayed. + */ +export interface CoreDelegateDisplayHandler extends CoreDelegateHandler { + /** + * The highest priority is displayed first. + */ + priority?: number; + + /** + * Returns the data needed to render the handler. + * + * @return Data. + */ + getDisplayData(): HandlerData; +} diff --git a/src/app/components/file/file.ts b/src/app/components/file/file.ts index fcdea964e..7b006e8ae 100644 --- a/src/app/components/file/file.ts +++ b/src/app/components/file/file.ts @@ -16,7 +16,7 @@ import { Component, Input, Output, OnInit, OnDestroy, EventEmitter } from '@angu import { CoreApp } from '@services/app'; import { CoreFilepool } from '@services/filepool'; import { CoreFileHelper } from '@services/file-helper'; -import { CorePluginFileDelegate } from '@services/plugin-file-delegate'; +import { CorePluginFileDelegate } from '@services/plugin-file.delegate'; import { CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreMimetypeUtils } from '@services/utils/mimetype'; diff --git a/src/app/core/mainmenu/handlers/mainmenu.ts b/src/app/core/mainmenu/handlers/mainmenu.ts index a4c2d1fad..4a6a6dd4c 100644 --- a/src/app/core/mainmenu/handlers/mainmenu.ts +++ b/src/app/core/mainmenu/handlers/mainmenu.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { CoreMainMenuHandler, CoreMainMenuHandlerData } from '../services/delegate'; +import { CoreMainMenuHandler, CoreMainMenuHandlerData } from '../services/mainmenu.delegate'; /** * Handler to add Home into main menu. diff --git a/src/app/core/mainmenu/mainmenu.module.ts b/src/app/core/mainmenu/mainmenu.module.ts index 8e4e5fde5..332b231c7 100644 --- a/src/app/core/mainmenu/mainmenu.module.ts +++ b/src/app/core/mainmenu/mainmenu.module.ts @@ -21,7 +21,7 @@ import { TranslateModule } from '@ngx-translate/core'; import { CoreComponentsModule } from '@components/components.module'; import { CoreDirectivesModule } from '@directives/directives.module'; -import { CoreMainMenuDelegate } from './services/delegate'; +import { CoreMainMenuDelegate } from './services/mainmenu.delegate'; import { CoreMainMenuRoutingModule } from './mainmenu-routing.module'; import { CoreMainMenuPage } from './pages/menu/menu.page'; diff --git a/src/app/core/mainmenu/pages/home/home.page.ts b/src/app/core/mainmenu/pages/home/home.page.ts index 4add6d7ce..d0d23aa5f 100644 --- a/src/app/core/mainmenu/pages/home/home.page.ts +++ b/src/app/core/mainmenu/pages/home/home.page.ts @@ -44,7 +44,7 @@ export class CoreHomePage implements OnInit { * Initialize the component. */ ngOnInit(): void { - this.subscription = this.homeDelegate.getHandlers().subscribe((handlers) => { + this.subscription = this.homeDelegate.getHandlersObservable().subscribe((handlers) => { handlers && this.initHandlers(handlers); }); } diff --git a/src/app/core/mainmenu/pages/menu/menu.page.ts b/src/app/core/mainmenu/pages/menu/menu.page.ts index 4516e8f30..48f6af306 100644 --- a/src/app/core/mainmenu/pages/menu/menu.page.ts +++ b/src/app/core/mainmenu/pages/menu/menu.page.ts @@ -21,7 +21,7 @@ import { CoreApp } from '@services/app'; import { CoreSites } from '@services/sites'; import { CoreEvents, CoreEventObserver, CoreEventLoadPageMainMenuData } from '@singletons/events'; import { CoreMainMenu } from '../../services/mainmenu'; -import { CoreMainMenuDelegate, CoreMainMenuHandlerToDisplay } from '../../services/delegate'; +import { CoreMainMenuDelegate, CoreMainMenuHandlerToDisplay } from '../../services/mainmenu.delegate'; import { CoreDomUtils } from '@/app/services/utils/dom'; import { Translate } from '@/app/singletons/core.singletons'; @@ -97,7 +97,7 @@ export class CoreMainMenuPage implements OnInit, OnDestroy { } }); - this.subscription = this.menuDelegate.getHandlers().subscribe((handlers) => { + this.subscription = this.menuDelegate.getHandlersObservable().subscribe((handlers) => { // Remove the handlers that should only appear in the More menu. this.allHandlers = handlers.filter((handler) => !handler.onlyInMore); diff --git a/src/app/core/mainmenu/pages/more/more.page.ts b/src/app/core/mainmenu/pages/more/more.page.ts index 4346287dd..43f77bc8e 100644 --- a/src/app/core/mainmenu/pages/more/more.page.ts +++ b/src/app/core/mainmenu/pages/more/more.page.ts @@ -19,7 +19,7 @@ import { CoreSites } from '@services/sites'; import { CoreUtils } from '@services/utils/utils'; import { CoreSiteInfo } from '@classes/site'; import { CoreLoginHelper } from '@core/login/services/helper'; -import { CoreMainMenuDelegate, CoreMainMenuHandlerData } from '../../services/delegate'; +import { CoreMainMenuDelegate, CoreMainMenuHandlerData } from '../../services/mainmenu.delegate'; import { CoreMainMenu, CoreMainMenuCustomItem } from '../../services/mainmenu'; import { CoreEventObserver, CoreEvents } from '@singletons/events'; @@ -69,7 +69,7 @@ export class CoreMainMenuMorePage implements OnInit, OnDestroy { */ ngOnInit(): void { // Load the handlers. - this.subscription = this.menuDelegate.getHandlers().subscribe((handlers) => { + this.subscription = this.menuDelegate.getHandlersObservable().subscribe((handlers) => { this.allHandlers = handlers; this.initHandlers(); @@ -149,20 +149,6 @@ export class CoreMainMenuMorePage implements OnInit, OnDestroy { // @todo } - /** - * Open app settings page. - */ - openAppSettings(): void { - // @todo - } - - /** - * Open site settings page. - */ - openSitePreferences(): void { - // @todo - } - /** * Scan and treat a QR code. */ diff --git a/src/app/core/mainmenu/services/delegate.ts b/src/app/core/mainmenu/services/delegate.ts deleted file mode 100644 index dd2867fa4..000000000 --- a/src/app/core/mainmenu/services/delegate.ts +++ /dev/null @@ -1,177 +0,0 @@ -// (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. - -import { Injectable } from '@angular/core'; -import { Params } from '@angular/router'; -import { Subject, BehaviorSubject } from 'rxjs'; - -import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate'; -import { CoreEvents } from '@singletons/events'; - -/** - * Interface that all main menu handlers must implement. - */ -export interface CoreMainMenuHandler extends CoreDelegateHandler { - /** - * The highest priority is displayed first. - */ - priority?: number; - - /** - * Returns the data needed to render the handler. - * - * @return Data. - */ - getDisplayData(): CoreMainMenuHandlerData; -} - -/** - * Data needed to render a main menu handler. It's returned by the handler. - */ -export interface CoreMainMenuHandlerData { - /** - * Name of the page to load for the handler. - */ - page: string; - - /** - * Title to display for the handler. - */ - title: string; - - /** - * Name of the icon to display for the handler. - */ - icon: string; // Name of the icon to display in the tab. - - /** - * Class to add to the displayed handler. - */ - class?: string; - - /** - * If the handler has badge to show or not. - */ - showBadge?: boolean; - - /** - * Text to display on the badge. Only used if showBadge is true. - */ - badge?: string; - - /** - * If true, the badge number is being loaded. Only used if showBadge is true. - */ - loading?: boolean; - - /** - * Params to pass to the page. - */ - pageParams?: Params; - - /** - * Whether the handler should only appear in More menu. - */ - onlyInMore?: boolean; -} - -/** - * Data returned by the delegate for each handler. - */ -export interface CoreMainMenuHandlerToDisplay extends CoreMainMenuHandlerData { - /** - * Name of the handler. - */ - name?: string; - - /** - * Priority of the handler. - */ - priority?: number; - - /** - * Hide tab. Used then resizing. - */ - hide?: boolean; -} - -/** - * Service to interact with plugins to be shown in the main menu. Provides functions to register a plugin - * and notify an update in the data. - */ -@Injectable({ - providedIn: 'root', -}) -export class CoreMainMenuDelegate extends CoreDelegate { - - protected loaded = false; - protected siteHandlers: Subject = new BehaviorSubject([]); - protected featurePrefix = 'CoreMainMenuDelegate_'; - - constructor() { - super('CoreMainMenuDelegate', true); - - CoreEvents.on(CoreEvents.LOGOUT, this.clearSiteHandlers.bind(this)); - } - - /** - * Check if handlers are loaded. - * - * @return True if handlers are loaded, false otherwise. - */ - areHandlersLoaded(): boolean { - return this.loaded; - } - - /** - * Clear current site handlers. Reserved for core use. - */ - protected clearSiteHandlers(): void { - this.loaded = false; - this.siteHandlers.next([]); - } - - /** - * Get the handlers for the current site. - * - * @return An observable that will receive the handlers. - */ - getHandlers(): Subject { - return this.siteHandlers; - } - - /** - * Update handlers Data. - */ - updateData(): void { - const displayData: CoreMainMenuHandlerToDisplay[] = []; - - for (const name in this.enabledHandlers) { - const handler = this.enabledHandlers[name]; - const data = handler.getDisplayData(); - - data.name = name; - data.priority = handler.priority; - - displayData.push(data); - } - - // Sort them by priority. - displayData.sort((a, b) => (b.priority || 0) - (a.priority || 0)); - - this.loaded = true; - this.siteHandlers.next(displayData); - } - -} diff --git a/src/app/core/mainmenu/services/home.delegate.ts b/src/app/core/mainmenu/services/home.delegate.ts index 6d500d9a3..170a36311 100644 --- a/src/app/core/mainmenu/services/home.delegate.ts +++ b/src/app/core/mainmenu/services/home.delegate.ts @@ -14,27 +14,14 @@ import { Injectable } from '@angular/core'; import { Params } from '@angular/router'; -import { Subject, BehaviorSubject } from 'rxjs'; -import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate'; -import { CoreEvents } from '@singletons/events'; +import { CoreDelegateDisplayHandler, CoreDelegateToDisplay } from '@classes/delegate'; +import { CoreSortedDelegate } from '@classes/delegate-sorted'; /** - * Interface that all main menu handlers must implement. + * Interface that all home handlers must implement. */ -export interface CoreHomeHandler extends CoreDelegateHandler { - /** - * The highest priority is displayed first. - */ - priority?: number; - - /** - * Returns the data needed to render the handler. - * - * @return Data. - */ - getDisplayData(): CoreHomeHandlerData; -} +export type CoreHomeHandler = CoreDelegateDisplayHandler; /** * Data needed to render a main menu handler. It's returned by the handler. @@ -84,17 +71,7 @@ export interface CoreHomeHandlerData { /** * Data returned by the delegate for each handler. */ -export interface CoreHomeHandlerToDisplay extends CoreHomeHandlerData { - /** - * Name of the handler. - */ - name?: string; - - /** - * Priority of the handler. - */ - priority?: number; - +export interface CoreHomeHandlerToDisplay extends CoreDelegateToDisplay, CoreHomeHandlerData { /** * Priority to select handler. */ @@ -108,65 +85,12 @@ export interface CoreHomeHandlerToDisplay extends CoreHomeHandlerData { @Injectable({ providedIn: 'root', }) -export class CoreHomeDelegate extends CoreDelegate { +export class CoreHomeDelegate extends CoreSortedDelegate { - protected loaded = false; - protected siteHandlers: Subject = new BehaviorSubject([]); protected featurePrefix = 'CoreHomeDelegate_'; constructor() { - super('CoreHomeDelegate', true); - - CoreEvents.on(CoreEvents.LOGOUT, this.clearSiteHandlers.bind(this)); - } - - /** - * Check if handlers are loaded. - * - * @return True if handlers are loaded, false otherwise. - */ - areHandlersLoaded(): boolean { - return this.loaded; - } - - /** - * Clear current site handlers. Reserved for core use. - */ - protected clearSiteHandlers(): void { - this.loaded = false; - this.siteHandlers.next([]); - } - - /** - * Get the handlers for the current site. - * - * @return An observable that will receive the handlers. - */ - getHandlers(): Subject { - return this.siteHandlers; - } - - /** - * Update handlers Data. - */ - updateData(): void { - const displayData: CoreHomeHandlerToDisplay[] = []; - - for (const name in this.enabledHandlers) { - const handler = this.enabledHandlers[name]; - const data = handler.getDisplayData(); - - data.name = name; - data.priority = handler.priority; - - displayData.push(data); - } - - // Sort them by priority. - displayData.sort((a, b) => (b.priority || 0) - (a.priority || 0)); - - this.loaded = true; - this.siteHandlers.next(displayData); + super('CoreHomeDelegate'); } } diff --git a/src/app/core/mainmenu/services/mainmenu.delegate.ts b/src/app/core/mainmenu/services/mainmenu.delegate.ts new file mode 100644 index 000000000..2a0208749 --- /dev/null +++ b/src/app/core/mainmenu/services/mainmenu.delegate.ts @@ -0,0 +1,101 @@ +// (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. + +import { Injectable } from '@angular/core'; +import { Params } from '@angular/router'; + +import { CoreDelegateDisplayHandler, CoreDelegateToDisplay } from '@classes/delegate'; +import { CoreSortedDelegate } from '@classes/delegate-sorted'; + +/** + * Interface that all main menu handlers must implement. + */ +export type CoreMainMenuHandler = CoreDelegateDisplayHandler; + +/** + * Data needed to render a main menu handler. It's returned by the handler. + */ +export interface CoreMainMenuHandlerData { + /** + * Name of the page to load for the handler. + */ + page: string; + + /** + * Title to display for the handler. + */ + title: string; + + /** + * Name of the icon to display for the handler. + */ + icon: string; // Name of the icon to display in the tab. + + /** + * Class to add to the displayed handler. + */ + class?: string; + + /** + * If the handler has badge to show or not. + */ + showBadge?: boolean; + + /** + * Text to display on the badge. Only used if showBadge is true. + */ + badge?: string; + + /** + * If true, the badge number is being loaded. Only used if showBadge is true. + */ + loading?: boolean; + + /** + * Params to pass to the page. + */ + pageParams?: Params; + + /** + * Whether the handler should only appear in More menu. + */ + onlyInMore?: boolean; +} + +/** + * Data returned by the delegate for each handler. + */ +export interface CoreMainMenuHandlerToDisplay extends CoreDelegateToDisplay, CoreMainMenuHandlerData { + /** + * Hide tab. Used then resizing. + */ + hide?: boolean; +} + +/** + * Service to interact with plugins to be shown in the main menu. Provides functions to register a plugin + * and notify an update in the data. + */ +@Injectable({ + providedIn: 'root', +}) +export class CoreMainMenuDelegate extends CoreSortedDelegate { + + protected featurePrefix = 'CoreMainMenuDelegate_'; + + constructor() { + super('CoreMainMenuDelegate'); + } + +} diff --git a/src/app/core/mainmenu/services/mainmenu.ts b/src/app/core/mainmenu/services/mainmenu.ts index f54076df6..4d733b128 100644 --- a/src/app/core/mainmenu/services/mainmenu.ts +++ b/src/app/core/mainmenu/services/mainmenu.ts @@ -19,7 +19,7 @@ import { CoreLang } from '@services/lang'; import { CoreSites } from '@services/sites'; import { CoreUtils } from '@services/utils/utils'; import { CoreConstants } from '@core/constants'; -import { CoreMainMenuDelegate, CoreMainMenuHandlerToDisplay } from './delegate'; +import { CoreMainMenuDelegate, CoreMainMenuHandlerToDisplay } from './mainmenu.delegate'; import { makeSingleton } from '@singletons/core.singletons'; /** @@ -47,7 +47,7 @@ export class CoreMainMenuProvider { getCurrentMainMenuHandlers(): Promise { const deferred = CoreUtils.instance.promiseDefer(); - const subscription = this.menuDelegate.getHandlers().subscribe((handlers) => { + const subscription = this.menuDelegate.getHandlersObservable().subscribe((handlers) => { subscription?.unsubscribe(); // Remove the handlers that should only appear in the More menu. diff --git a/src/app/core/settings/services/settings.delegate.ts b/src/app/core/settings/services/settings.delegate.ts new file mode 100644 index 000000000..68f3deec1 --- /dev/null +++ b/src/app/core/settings/services/settings.delegate.ts @@ -0,0 +1,74 @@ +// (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. + +import { Injectable } from '@angular/core'; +import { Params } from '@angular/router'; + +import { CoreDelegateDisplayHandler, CoreDelegateToDisplay } from '@classes/delegate'; +import { CoreSortedDelegate } from '@classes/delegate-sorted'; + +/** + * Interface that all settings handlers must implement. + */ +export type CoreSettingsHandler = CoreDelegateDisplayHandler; + +/** + * Data needed to render a setting handler. It's returned by the handler. + */ +export interface CoreSettingsHandlerData { + /** + * Name of the page to load for the handler. + */ + page: string; + + /** + * Params list of the page to load for the handler. + */ + params?: Params; + + /** + * Title to display for the handler. + */ + title: string; + + /** + * Name of the icon to display for the handler. + */ + icon?: string; // Name of the icon to display in the menu. + + /** + * Class to add to the displayed handler. + */ + class?: string; +} + +/** + * Data returned by the delegate for each handler. + */ +export type CoreSettingsHandlerToDisplay = CoreDelegateToDisplay & CoreSettingsHandlerData; + +/** + * Service to interact with addons to be shown in app settings. Provides functions to register a plugin + * and notify an update in the data. + */ +@Injectable({ + providedIn: 'root', +}) +export class CoreSettingsDelegate extends CoreSortedDelegate { + + constructor() { + super('CoreSettingsDelegate'); + } + +} diff --git a/src/app/services/filepool.ts b/src/app/services/filepool.ts index b9bdbed64..bed2a0680 100644 --- a/src/app/services/filepool.ts +++ b/src/app/services/filepool.ts @@ -19,7 +19,7 @@ import { CoreApp } from '@services/app'; import { CoreEvents } from '@singletons/events'; import { CoreFile } from '@services/file'; import { CoreInit } from '@services/init'; -import { CorePluginFile } from '@services/plugin-file-delegate'; +import { CorePluginFile } from '@services/plugin-file.delegate'; import { CoreSites } from '@services/sites'; import { CoreWS, CoreWSExternalFile } from '@services/ws'; import { CoreDomUtils } from '@services/utils/dom'; diff --git a/src/app/services/plugin-file-delegate.ts b/src/app/services/plugin-file.delegate.ts similarity index 97% rename from src/app/services/plugin-file-delegate.ts rename to src/app/services/plugin-file.delegate.ts index 572cc0913..bc0c51953 100644 --- a/src/app/services/plugin-file-delegate.ts +++ b/src/app/services/plugin-file.delegate.ts @@ -25,7 +25,7 @@ import { makeSingleton } from '@singletons/core.singletons'; * Delegate to register pluginfile information handlers. */ @Injectable() -export class CorePluginFileDelegate extends CoreDelegate { +export class CorePluginFileDelegate extends CoreDelegate { protected handlerNameProperty = 'component'; @@ -98,7 +98,7 @@ export class CorePluginFileDelegate extends CoreDelegate { */ getComponentRevisionRegExp(args: string[]): RegExp | void { // Get handler based on component (args[1]). - const handler = this.getHandler(args[1], true); + const handler = this.getHandler(args[1], true); if (handler && handler.getComponentRevisionRegExp) { return handler.getComponentRevisionRegExp(args); @@ -116,7 +116,7 @@ export class CorePluginFileDelegate extends CoreDelegate { let files = []; for (const component in this.enabledHandlers) { - const handler = this.enabledHandlers[component]; + const handler = this.enabledHandlers[component]; if (handler && handler.getDownloadableFilesFromHTML) { files = files.concat(handler.getDownloadableFilesFromHTML(container)); @@ -217,7 +217,7 @@ export class CorePluginFileDelegate extends CoreDelegate { */ protected getHandlerForFile(file: CoreWSExternalFile): CorePluginFileHandler | undefined { for (const component in this.enabledHandlers) { - const handler = this.enabledHandlers[component]; + const handler = this.enabledHandlers[component]; if (handler && handler.shouldHandleFile && handler.shouldHandleFile(file)) { return handler; @@ -252,7 +252,7 @@ export class CorePluginFileDelegate extends CoreDelegate { */ removeRevisionFromUrl(url: string, args: string[]): string { // Get handler based on component (args[1]). - const handler = this.getHandler(args[1], true); + const handler = this.getHandler(args[1], true); if (handler && handler.getComponentRevisionRegExp && handler.getComponentRevisionReplace) { const revisionRegex = handler.getComponentRevisionRegExp(args);