From 3c57fb364fe2bf5115f6081f71418fc11dd25f77 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 7 May 2018 15:18:54 +0200 Subject: [PATCH] MOBILE-2376 siteplugins: Support settings handlers --- .../siteplugins/classes/settings-handler.ts | 50 +++++++++++++++++++ src/core/siteplugins/providers/helper.ts | 41 +++++++++++++-- 2 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/core/siteplugins/classes/settings-handler.ts diff --git a/src/core/siteplugins/classes/settings-handler.ts b/src/core/siteplugins/classes/settings-handler.ts new file mode 100644 index 000000000..a90764c8b --- /dev/null +++ b/src/core/siteplugins/classes/settings-handler.ts @@ -0,0 +1,50 @@ +// (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 { CoreSettingsHandler, CoreSettingsHandlerData } from '@core/settings/providers/delegate'; +import { CoreSitePluginsBaseHandler } from './base-handler'; + +/** + * Handler to display a site plugin in the settings. + */ +export class CoreSitePluginsSettingsHandler extends CoreSitePluginsBaseHandler implements CoreSettingsHandler { + priority: number; + + constructor(name: string, protected title: string, protected plugin: any, protected handlerSchema: any, + protected initResult: any) { + super(name); + + this.priority = handlerSchema.priority; + } + + /** + * Returns the data needed to render the handler. + * + * @return {CoreSettingsHandlerData} Data. + */ + getDisplayData(): CoreSettingsHandlerData { + return { + title: this.title, + icon: this.handlerSchema.displaydata.icon, + class: this.handlerSchema.displaydata.class, + page: 'CoreSitePluginsPluginPage', + params: { + title: this.title, + component: this.plugin.component, + method: this.handlerSchema.method, + initResult: this.initResult + } + }; + } +} diff --git a/src/core/siteplugins/providers/helper.ts b/src/core/siteplugins/providers/helper.ts index 1e94fc03f..cdd034b58 100644 --- a/src/core/siteplugins/providers/helper.ts +++ b/src/core/siteplugins/providers/helper.ts @@ -34,6 +34,7 @@ import { CoreCourseOptionsDelegate } from '@core/course/providers/options-delega import { CoreCourseFormatDelegate } from '@core/course/providers/format-delegate'; import { CoreUserDelegate } from '@core/user/providers/user-delegate'; import { CoreUserProfileFieldDelegate } from '@core/user/providers/user-profile-field-delegate'; +import { CoreSettingsDelegate } from '@core/settings/providers/delegate'; import { AddonMessageOutputDelegate } from '@addon/messageoutput/providers/delegate'; // Handler classes. @@ -44,6 +45,7 @@ import { CoreSitePluginsModulePrefetchHandler } from '../classes/module-prefetch import { CoreSitePluginsMainMenuHandler } from '../classes/main-menu-handler'; import { CoreSitePluginsUserProfileHandler } from '../classes/user-handler'; import { CoreSitePluginsUserProfileFieldHandler } from '../classes/user-profile-field-handler'; +import { CoreSitePluginsSettingsHandler } from '../classes/settings-handler'; import { CoreSitePluginsMessageOutputHandler } from '../classes/message-output-handler'; /** @@ -67,7 +69,7 @@ export class CoreSitePluginsHelperProvider { private courseOptionsDelegate: CoreCourseOptionsDelegate, eventsProvider: CoreEventsProvider, private courseFormatDelegate: CoreCourseFormatDelegate, private profileFieldDelegate: CoreUserProfileFieldDelegate, private textUtils: CoreTextUtilsProvider, private filepoolProvider: CoreFilepoolProvider, - private messageOutputDelegate: AddonMessageOutputDelegate) { + private settingsDelegate: CoreSettingsDelegate, private messageOutputDelegate: AddonMessageOutputDelegate) { this.logger = logger.getInstance('CoreSitePluginsHelperProvider'); // Fetch the plugins on login. @@ -436,7 +438,11 @@ export class CoreSitePluginsHelperProvider { promise = Promise.resolve(this.registerUserProfileFieldHandler(plugin, handlerName, handlerSchema, result)); break; - case 'AddonMessageOutputHandler': + case 'CoreSettingsDelegate': + promise = Promise.resolve(this.registerSettingsHandler(plugin, handlerName, handlerSchema, result)); + break; + + case 'AddonMessageOutputDelegate': promise = Promise.resolve(this.registerMessageOutputHandler(plugin, handlerName, handlerSchema, result)); break; @@ -540,7 +546,7 @@ export class CoreSitePluginsHelperProvider { } /** - * Given a handler in an plugin, register it in the main menu delegate. + * Given a handler in an plugin, register it in the message output delegate. * * @param {any} plugin Data of the plugin. * @param {string} handlerName Name of the handler in the plugin. @@ -603,6 +609,35 @@ export class CoreSitePluginsHelperProvider { return modName; } + /** + * Given a handler in an plugin, register it in the settings delegate. + * + * @param {any} plugin Data of the plugin. + * @param {string} handlerName Name of the handler in the plugin. + * @param {any} handlerSchema Data about the handler. + * @param {any} initResult Result of the init WS call. + * @return {string} A string to identify the handler. + */ + protected registerSettingsHandler(plugin: any, handlerName: string, handlerSchema: any, initResult: any): string { + if (!handlerSchema.displaydata) { + // Required data not provided, stop. + this.logger.warn('Ignore site plugin because it doesn\'t provide displaydata', plugin, handlerSchema); + + return; + } + + this.logger.debug('Register site plugin in settings delegate:', plugin, handlerSchema, initResult); + + // Create and register the handler. + const uniqueName = this.sitePluginsProvider.getHandlerUniqueName(plugin, handlerName), + prefixedTitle = this.getPrefixedString(plugin.addon, handlerSchema.displaydata.title); + + this.settingsDelegate.registerHandler( + new CoreSitePluginsSettingsHandler(uniqueName, prefixedTitle, plugin, handlerSchema, initResult)); + + return uniqueName; + } + /** * Given a handler in an plugin, register it in the user profile delegate. *