2018-02-15 14:57:18 +01:00
|
|
|
// (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.
|
|
|
|
|
2018-02-16 11:34:29 +01:00
|
|
|
import { Injectable, Injector } from '@angular/core';
|
2018-03-12 15:44:41 +01:00
|
|
|
import { CoreEventsProvider } from '@providers/events';
|
|
|
|
import { CoreLangProvider } from '@providers/lang';
|
|
|
|
import { CoreLoggerProvider } from '@providers/logger';
|
|
|
|
import { CoreSite } from '@classes/site';
|
|
|
|
import { CoreSitesProvider } from '@providers/sites';
|
|
|
|
import { CoreUtilsProvider } from '@providers/utils/utils';
|
2018-03-09 14:36:30 +01:00
|
|
|
import { CoreSitePluginsProvider } from './siteplugins';
|
2018-03-12 15:44:41 +01:00
|
|
|
import { CoreCompileProvider } from '@core/compile/providers/compile';
|
2018-03-09 11:32:41 +01:00
|
|
|
|
|
|
|
// Delegates
|
2018-03-12 15:44:41 +01:00
|
|
|
import { CoreMainMenuDelegate } from '@core/mainmenu/providers/delegate';
|
|
|
|
import { CoreCourseModuleDelegate } from '@core/course/providers/module-delegate';
|
|
|
|
import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate';
|
|
|
|
import { CoreCourseOptionsDelegate } from '@core/course/providers/options-delegate';
|
|
|
|
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';
|
2018-03-09 11:32:41 +01:00
|
|
|
|
|
|
|
// Handler classes.
|
2018-03-09 14:36:30 +01:00
|
|
|
import { CoreSitePluginsCourseFormatHandler } from '../classes/course-format-handler';
|
|
|
|
import { CoreSitePluginsCourseOptionHandler } from '../classes/course-option-handler';
|
|
|
|
import { CoreSitePluginsModuleHandler } from '../classes/module-handler';
|
|
|
|
import { CoreSitePluginsModulePrefetchHandler } from '../classes/module-prefetch-handler';
|
|
|
|
import { CoreSitePluginsMainMenuHandler } from '../classes/main-menu-handler';
|
|
|
|
import { CoreSitePluginsUserProfileHandler } from '../classes/user-handler';
|
|
|
|
import { CoreSitePluginsUserProfileFieldHandler } from '../classes/user-profile-field-handler';
|
2018-02-15 14:57:18 +01:00
|
|
|
|
|
|
|
/**
|
2018-03-09 14:36:30 +01:00
|
|
|
* Helper service to provide functionalities regarding site plugins. It basically has the features to load and register site
|
|
|
|
* plugin.
|
2018-02-15 14:57:18 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* This code is split from CoreSitePluginsProvider to prevent circular dependencies.
|
2018-03-02 15:25:00 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @todo: Support ViewChild and similar in site plugins. Possible solution: make components and directives inject the instance
|
2018-03-02 15:25:00 +01:00
|
|
|
* inside the host DOM element?
|
2018-02-15 14:57:18 +01:00
|
|
|
*/
|
|
|
|
@Injectable()
|
2018-03-09 14:36:30 +01:00
|
|
|
export class CoreSitePluginsHelperProvider {
|
2018-02-15 14:57:18 +01:00
|
|
|
protected logger;
|
|
|
|
|
2018-02-16 11:34:29 +01:00
|
|
|
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private injector: Injector,
|
2018-02-15 14:57:18 +01:00
|
|
|
private mainMenuDelegate: CoreMainMenuDelegate, private moduleDelegate: CoreCourseModuleDelegate,
|
|
|
|
private userDelegate: CoreUserDelegate, private langProvider: CoreLangProvider,
|
2018-03-09 14:36:30 +01:00
|
|
|
private sitePluginsProvider: CoreSitePluginsProvider, private prefetchDelegate: CoreCourseModulePrefetchDelegate,
|
2018-03-09 11:32:41 +01:00
|
|
|
private compileProvider: CoreCompileProvider, private utils: CoreUtilsProvider,
|
|
|
|
private courseOptionsDelegate: CoreCourseOptionsDelegate, eventsProvider: CoreEventsProvider,
|
2018-03-08 15:42:44 +01:00
|
|
|
private courseFormatDelegate: CoreCourseFormatDelegate, private profileFieldDelegate: CoreUserProfileFieldDelegate) {
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger = logger.getInstance('CoreSitePluginsHelperProvider');
|
2018-03-09 09:00:43 +01:00
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
// Fetch the plugins on login.
|
2018-03-09 09:00:43 +01:00
|
|
|
eventsProvider.on(CoreEventsProvider.LOGIN, () => {
|
|
|
|
const siteId = this.sitesProvider.getCurrentSiteId();
|
2018-03-09 14:36:30 +01:00
|
|
|
this.fetchSitePlugins(siteId).then((plugins) => {
|
|
|
|
// Plugins fetched, check that site hasn't changed.
|
|
|
|
if (siteId == this.sitesProvider.getCurrentSiteId() && plugins.length) {
|
|
|
|
// Site is still the same. Load the plugins and trigger the event.
|
|
|
|
this.loadSitePlugins(plugins).then(() => {
|
|
|
|
eventsProvider.trigger(CoreEventsProvider.SITE_PLUGINS_LOADED, {}, siteId);
|
2018-03-09 09:00:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
// Unload plugins on logout if any.
|
2018-03-09 09:00:43 +01:00
|
|
|
eventsProvider.on(CoreEventsProvider.LOGOUT, () => {
|
2018-03-09 14:52:55 +01:00
|
|
|
if (this.sitePluginsProvider.hasSitePluginsLoaded) {
|
2018-03-09 09:00:43 +01:00
|
|
|
// Temporary fix. Reload the page to unload all plugins.
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
});
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
|
|
|
|
2018-02-21 16:18:03 +01:00
|
|
|
/**
|
2018-02-23 12:28:36 +01:00
|
|
|
* Bootstrap a handler if it has some bootstrap method.
|
2018-02-21 16:18:03 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @param {any} plugin Data of the plugin.
|
2018-02-21 16:18:03 +01:00
|
|
|
* @param {any} handlerSchema Data about the handler.
|
2018-03-01 13:08:27 +01:00
|
|
|
* @return {Promise<any>} Promise resolved when done. It returns the results of the getContent call and the data returned by
|
|
|
|
* the bootstrap JS (if any).
|
2018-02-21 16:18:03 +01:00
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
protected bootstrapHandler(plugin: any, handlerSchema: any): Promise<any> {
|
2018-02-21 16:18:03 +01:00
|
|
|
if (!handlerSchema.bootstrap) {
|
2018-02-23 12:28:36 +01:00
|
|
|
return Promise.resolve({});
|
2018-02-21 16:18:03 +01:00
|
|
|
}
|
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
return this.executeMethodAndJS(plugin, handlerSchema.bootstrap);
|
2018-03-08 15:42:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a get_content method and run its javascript (if any).
|
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @param {any} plugin Data of the plugin.
|
2018-03-08 15:42:44 +01:00
|
|
|
* @param {string} method The method to call.
|
|
|
|
* @return {Promise<any>} Promise resolved when done. It returns the results of the getContent call and the data returned by
|
|
|
|
* the JS (if any).
|
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
protected executeMethodAndJS(plugin: any, method: string): Promise<any> {
|
2018-02-21 16:18:03 +01:00
|
|
|
const siteId = this.sitesProvider.getCurrentSiteId(),
|
|
|
|
preSets = {getFromCache: false}; // Try to ignore cache.
|
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
return this.sitePluginsProvider.getContent(plugin.component, method, {}, preSets).then((result) => {
|
2018-02-21 16:18:03 +01:00
|
|
|
if (!result.javascript || this.sitesProvider.getCurrentSiteId() != siteId) {
|
|
|
|
// No javascript or site has changed, stop.
|
2018-03-01 13:08:27 +01:00
|
|
|
return result;
|
2018-02-21 16:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a "fake" instance to hold all the libraries.
|
|
|
|
const instance = {};
|
|
|
|
this.compileProvider.injectLibraries(instance);
|
|
|
|
|
2018-03-01 13:08:27 +01:00
|
|
|
// Add some data of the WS call result.
|
2018-03-09 14:36:30 +01:00
|
|
|
const jsData = this.sitePluginsProvider.createDataForJS(result);
|
2018-03-01 13:08:27 +01:00
|
|
|
for (const name in jsData) {
|
|
|
|
instance[name] = jsData[name];
|
|
|
|
}
|
|
|
|
|
2018-02-21 16:18:03 +01:00
|
|
|
// Now execute the javascript using this instance.
|
2018-03-01 13:08:27 +01:00
|
|
|
result.jsResult = this.compileProvider.executeJavascript(instance, result.javascript);
|
2018-02-23 12:28:36 +01:00
|
|
|
|
2018-03-01 13:08:27 +01:00
|
|
|
return result;
|
2018-02-21 16:18:03 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-09 09:00:43 +01:00
|
|
|
/**
|
2018-03-09 14:36:30 +01:00
|
|
|
* Fetch site plugins.
|
2018-03-09 09:00:43 +01:00
|
|
|
*
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, current site.
|
2018-03-09 14:36:30 +01:00
|
|
|
* @return {Promise<any[]>} Promise resolved when done. Returns the list of plugins to load.
|
2018-03-09 09:00:43 +01:00
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
fetchSitePlugins(siteId?: string): Promise<any[]> {
|
|
|
|
const plugins = [];
|
2018-03-09 09:00:43 +01:00
|
|
|
|
|
|
|
return this.sitesProvider.getSite(siteId).then((site) => {
|
2018-03-09 14:36:30 +01:00
|
|
|
if (!this.sitePluginsProvider.isGetContentAvailable(site)) {
|
|
|
|
// Cannot load site plugins, so there's no point to fetch them.
|
|
|
|
return plugins;
|
2018-03-09 09:00:43 +01:00
|
|
|
}
|
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
// Get the list of plugins. Try not to use cache.
|
2018-03-09 09:00:43 +01:00
|
|
|
return site.read('tool_mobile_get_plugins_supporting_mobile', {}, { getFromCache: false }).then((data) => {
|
2018-03-09 14:36:30 +01:00
|
|
|
data.plugins.forEach((plugin: any) => {
|
|
|
|
// Check if it's a site plugin and it's enabled.
|
|
|
|
if (this.isSitePluginEnabled(plugin, site)) {
|
|
|
|
plugins.push(plugin);
|
2018-03-09 09:00:43 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
return plugins;
|
2018-03-09 09:00:43 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-15 14:57:18 +01:00
|
|
|
/**
|
2018-03-12 12:52:58 +01:00
|
|
|
* Given an addon name, return the prefix to add to its string keys.
|
2018-02-15 14:57:18 +01:00
|
|
|
*
|
2018-03-12 12:52:58 +01:00
|
|
|
* @param {string} addon Name of the addon (plugin.addon).
|
2018-02-15 14:57:18 +01:00
|
|
|
* @return {string} Prefix.
|
|
|
|
*/
|
2018-03-12 12:52:58 +01:00
|
|
|
protected getPrefixForStrings(addon: string): string {
|
|
|
|
if (addon) {
|
|
|
|
return 'plugin.' + addon + '.';
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-12 12:52:58 +01:00
|
|
|
* Given an addon name and the key of a string, return the full string key (prefixed).
|
2018-02-15 14:57:18 +01:00
|
|
|
*
|
2018-03-12 12:52:58 +01:00
|
|
|
* @param {string} addon Name of the addon (plugin.addon).
|
2018-02-15 14:57:18 +01:00
|
|
|
* @param {string} key The key of the string.
|
|
|
|
* @return {string} Full string key.
|
|
|
|
*/
|
2018-03-12 12:52:58 +01:00
|
|
|
protected getPrefixedString(addon: string, key: string): string {
|
|
|
|
return this.getPrefixForStrings(addon) + key;
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-09 14:36:30 +01:00
|
|
|
* Check if a certain plugin is a site plugin and it's enabled in a certain site.
|
2018-02-15 14:57:18 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @param {any} plugin Data of the plugin.
|
2018-02-15 14:57:18 +01:00
|
|
|
* @param {CoreSite} site Site affected.
|
2018-03-09 14:36:30 +01:00
|
|
|
* @return {boolean} Whether it's a site plugin and it's enabled.
|
2018-02-15 14:57:18 +01:00
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
isSitePluginEnabled(plugin: any, site: CoreSite): boolean {
|
|
|
|
if (!site.isFeatureDisabled('sitePlugin_' + plugin.component + '_' + plugin.addon) && plugin.handlers) {
|
|
|
|
// Site plugin not disabled. Check if it has handlers.
|
2018-02-15 14:57:18 +01:00
|
|
|
try {
|
2018-03-09 14:36:30 +01:00
|
|
|
if (!plugin.parsedHandlers) {
|
|
|
|
plugin.parsedHandlers = JSON.parse(plugin.handlers);
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
return !!(plugin.parsedHandlers && Object.keys(plugin.parsedHandlers).length);
|
2018-02-15 14:57:18 +01:00
|
|
|
} catch (ex) {
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger.warn('Error parsing site plugin', ex);
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-12 12:52:58 +01:00
|
|
|
* Load the lang strings for a plugin.
|
2018-02-15 14:57:18 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @param {any} plugin Data of the plugin.
|
2018-02-15 14:57:18 +01:00
|
|
|
*/
|
2018-03-12 12:52:58 +01:00
|
|
|
loadLangStrings(plugin: any): void {
|
|
|
|
if (!plugin.parsedLang) {
|
2018-02-15 14:57:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-12 12:52:58 +01:00
|
|
|
for (const lang in plugin.parsedLang) {
|
|
|
|
const prefix = this.getPrefixForStrings(plugin.addon);
|
2018-02-15 14:57:18 +01:00
|
|
|
|
2018-03-12 12:52:58 +01:00
|
|
|
this.langProvider.addSitePluginsStrings(lang, plugin.parsedLang[lang], prefix);
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-09 14:36:30 +01:00
|
|
|
* Load a site plugin.
|
2018-02-15 14:57:18 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @param {any} plugin Data of the plugin.
|
2018-02-21 16:18:03 +01:00
|
|
|
* @return {Promise<any>} Promise resolved when loaded.
|
2018-02-15 14:57:18 +01:00
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
loadSitePlugin(plugin: any): Promise<any> {
|
2018-02-21 16:18:03 +01:00
|
|
|
const promises = [];
|
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger.debug('Load site plugin:', plugin);
|
2018-03-09 09:00:43 +01:00
|
|
|
|
2018-02-15 14:57:18 +01:00
|
|
|
try {
|
2018-03-09 14:36:30 +01:00
|
|
|
if (!plugin.parsedHandlers) {
|
|
|
|
plugin.parsedHandlers = JSON.parse(plugin.handlers);
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
2018-03-12 12:52:58 +01:00
|
|
|
if (!plugin.parsedLang && plugin.lang) {
|
|
|
|
plugin.parsedLang = JSON.parse(plugin.lang);
|
|
|
|
}
|
2018-02-15 14:57:18 +01:00
|
|
|
|
2018-03-09 14:52:55 +01:00
|
|
|
this.sitePluginsProvider.hasSitePluginsLoaded = true;
|
2018-03-09 09:00:43 +01:00
|
|
|
|
2018-03-12 12:52:58 +01:00
|
|
|
// Register lang strings.
|
|
|
|
this.loadLangStrings(plugin);
|
|
|
|
|
2018-02-15 14:57:18 +01:00
|
|
|
// Register all the handlers.
|
2018-03-09 14:36:30 +01:00
|
|
|
for (const name in plugin.parsedHandlers) {
|
|
|
|
promises.push(this.registerHandler(plugin, name, plugin.parsedHandlers[name]));
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
|
|
|
} catch (ex) {
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger.warn('Error parsing site plugin', ex);
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
2018-02-21 16:18:03 +01:00
|
|
|
|
|
|
|
return this.utils.allPromises(promises);
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
|
|
|
|
2018-03-09 09:00:43 +01:00
|
|
|
/**
|
2018-03-09 14:36:30 +01:00
|
|
|
* Load site plugins.
|
2018-03-09 09:00:43 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @param {any[]} plugins The plugins to load.
|
2018-03-09 09:00:43 +01:00
|
|
|
* @return {Promise<any>} Promise resolved when loaded.
|
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
loadSitePlugins(plugins: any[]): Promise<any> {
|
2018-03-09 09:00:43 +01:00
|
|
|
const promises = [];
|
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
plugins.forEach((plugin) => {
|
|
|
|
promises.push(this.loadSitePlugin(plugin));
|
2018-03-09 09:00:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return this.utils.allPromises(promises);
|
|
|
|
}
|
|
|
|
|
2018-02-15 14:57:18 +01:00
|
|
|
/**
|
2018-03-09 14:36:30 +01:00
|
|
|
* Register a site plugin handler in the right delegate.
|
2018-02-15 14:57:18 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @param {any} plugin Data of the plugin.
|
|
|
|
* @param {string} handlerName Name of the handler in the plugin.
|
2018-02-15 14:57:18 +01:00
|
|
|
* @param {any} handlerSchema Data about the handler.
|
2018-02-21 16:18:03 +01:00
|
|
|
* @return {Promise<any>} Promise resolved when done.
|
2018-02-15 14:57:18 +01:00
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
registerHandler(plugin: any, handlerName: string, handlerSchema: any): Promise<any> {
|
2018-02-15 14:57:18 +01:00
|
|
|
|
2018-02-21 16:18:03 +01:00
|
|
|
// Wait for the bootstrap JS to be executed.
|
2018-03-09 14:36:30 +01:00
|
|
|
return this.bootstrapHandler(plugin, handlerSchema).then((result) => {
|
2018-03-08 15:42:44 +01:00
|
|
|
let promise;
|
2018-02-15 14:57:18 +01:00
|
|
|
|
2018-02-21 16:18:03 +01:00
|
|
|
switch (handlerSchema.delegate) {
|
|
|
|
case 'CoreMainMenuDelegate':
|
2018-03-09 14:36:30 +01:00
|
|
|
promise = Promise.resolve(this.registerMainMenuHandler(plugin, handlerName, handlerSchema, result));
|
2018-02-21 16:18:03 +01:00
|
|
|
break;
|
2018-02-15 14:57:18 +01:00
|
|
|
|
2018-02-21 16:18:03 +01:00
|
|
|
case 'CoreCourseModuleDelegate':
|
2018-03-09 14:36:30 +01:00
|
|
|
promise = Promise.resolve(this.registerModuleHandler(plugin, handlerName, handlerSchema, result));
|
2018-02-21 16:18:03 +01:00
|
|
|
break;
|
2018-02-15 14:57:18 +01:00
|
|
|
|
2018-02-21 16:18:03 +01:00
|
|
|
case 'CoreUserDelegate':
|
2018-03-09 14:36:30 +01:00
|
|
|
promise = Promise.resolve(this.registerUserProfileHandler(plugin, handlerName, handlerSchema, result));
|
2018-02-21 16:18:03 +01:00
|
|
|
break;
|
|
|
|
|
2018-02-23 15:53:45 +01:00
|
|
|
case 'CoreCourseOptionsDelegate':
|
2018-03-09 14:36:30 +01:00
|
|
|
promise = Promise.resolve(this.registerCourseOptionHandler(plugin, handlerName, handlerSchema, result));
|
2018-02-23 15:53:45 +01:00
|
|
|
break;
|
|
|
|
|
2018-02-28 16:14:37 +01:00
|
|
|
case 'CoreCourseFormatDelegate':
|
2018-03-09 14:36:30 +01:00
|
|
|
promise = Promise.resolve(this.registerCourseFormatHandler(plugin, handlerName, handlerSchema, result));
|
2018-03-08 15:42:44 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'CoreUserProfileFieldDelegate':
|
2018-03-09 14:36:30 +01:00
|
|
|
promise = Promise.resolve(this.registerUserProfileFieldHandler(plugin, handlerName, handlerSchema, result));
|
2018-02-28 16:14:37 +01:00
|
|
|
break;
|
|
|
|
|
2018-02-21 16:18:03 +01:00
|
|
|
default:
|
|
|
|
// Nothing to do.
|
2018-03-08 15:42:44 +01:00
|
|
|
promise = Promise.resolve();
|
2018-02-21 16:18:03 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 15:42:44 +01:00
|
|
|
return promise.then((uniqueName) => {
|
|
|
|
if (uniqueName) {
|
|
|
|
// Store the handler data.
|
2018-03-09 14:36:30 +01:00
|
|
|
this.sitePluginsProvider.setSitePluginHandler(uniqueName, {
|
|
|
|
plugin: plugin,
|
2018-03-08 15:42:44 +01:00
|
|
|
handlerName: handlerName,
|
|
|
|
handlerSchema: handlerSchema,
|
|
|
|
bootstrapResult: result
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch((err) => {
|
|
|
|
this.logger.error('Error executing bootstrap method', handlerSchema.bootstrap, err);
|
2018-02-21 16:18:03 +01:00
|
|
|
});
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 16:14:37 +01:00
|
|
|
/**
|
2018-03-09 14:36:30 +01:00
|
|
|
* Given a handler in a plugin, register it in the course format delegate.
|
2018-02-28 16:14:37 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @param {any} plugin Data of the plugin.
|
|
|
|
* @param {string} handlerName Name of the handler in the plugin.
|
2018-02-28 16:14:37 +01:00
|
|
|
* @param {any} handlerSchema Data about the handler.
|
2018-03-01 13:08:27 +01:00
|
|
|
* @param {any} bootstrapResult Result of the bootstrap WS call.
|
2018-02-28 16:14:37 +01:00
|
|
|
* @return {string} A string to identify the handler.
|
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
protected registerCourseFormatHandler(plugin: any, handlerName: string, handlerSchema: any, bootstrapResult: any): string {
|
|
|
|
this.logger.debug('Register site plugin in course format delegate:', plugin, handlerSchema, bootstrapResult);
|
2018-02-28 16:14:37 +01:00
|
|
|
|
2018-03-09 11:32:41 +01:00
|
|
|
// Create and register the handler.
|
2018-03-09 14:36:30 +01:00
|
|
|
const formatName = plugin.component.replace('format_', '');
|
|
|
|
this.courseFormatDelegate.registerHandler(new CoreSitePluginsCourseFormatHandler(formatName, handlerSchema));
|
2018-02-28 16:14:37 +01:00
|
|
|
|
|
|
|
return formatName;
|
|
|
|
}
|
|
|
|
|
2018-02-23 15:53:45 +01:00
|
|
|
/**
|
2018-03-09 14:36:30 +01:00
|
|
|
* Given a handler in an plugin, register it in the course options delegate.
|
2018-02-23 15:53:45 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @param {any} plugin Data of the plugin.
|
|
|
|
* @param {string} handlerName Name of the handler in the plugin.
|
2018-02-23 15:53:45 +01:00
|
|
|
* @param {any} handlerSchema Data about the handler.
|
2018-03-01 13:08:27 +01:00
|
|
|
* @param {any} bootstrapResult Result of the bootstrap WS call.
|
2018-02-23 15:53:45 +01:00
|
|
|
* @return {string} A string to identify the handler.
|
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
protected registerCourseOptionHandler(plugin: any, handlerName: string, handlerSchema: any, bootstrapResult: any): string {
|
2018-03-09 09:00:43 +01:00
|
|
|
if (!handlerSchema.displaydata) {
|
2018-02-23 15:53:45 +01:00
|
|
|
// Required data not provided, stop.
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger.warn('Ignore site plugin because it doesn\'t provide displaydata', plugin, handlerSchema);
|
2018-03-09 09:00:43 +01:00
|
|
|
|
2018-02-23 15:53:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger.debug('Register site plugin in course option delegate:', plugin, handlerSchema, bootstrapResult);
|
2018-03-09 09:00:43 +01:00
|
|
|
|
2018-03-09 11:32:41 +01:00
|
|
|
// Create and register the handler.
|
2018-03-09 14:36:30 +01:00
|
|
|
const uniqueName = this.sitePluginsProvider.getHandlerUniqueName(plugin, handlerName),
|
2018-03-12 12:52:58 +01:00
|
|
|
prefixedTitle = this.getPrefixedString(plugin.addon, handlerSchema.displaydata.title);
|
2018-02-23 15:53:45 +01:00
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
this.courseOptionsDelegate.registerHandler(new CoreSitePluginsCourseOptionHandler(uniqueName, prefixedTitle, plugin,
|
|
|
|
handlerSchema, bootstrapResult, this.sitePluginsProvider));
|
2018-02-23 15:53:45 +01:00
|
|
|
|
|
|
|
return uniqueName;
|
|
|
|
}
|
|
|
|
|
2018-02-15 14:57:18 +01:00
|
|
|
/**
|
2018-03-09 14:36:30 +01:00
|
|
|
* Given a handler in an plugin, register it in the main menu delegate.
|
2018-02-15 14:57:18 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @param {any} plugin Data of the plugin.
|
|
|
|
* @param {string} handlerName Name of the handler in the plugin.
|
2018-02-15 14:57:18 +01:00
|
|
|
* @param {any} handlerSchema Data about the handler.
|
2018-03-01 13:08:27 +01:00
|
|
|
* @param {any} bootstrapResult Result of the bootstrap WS call.
|
2018-02-21 16:18:03 +01:00
|
|
|
* @return {string} A string to identify the handler.
|
2018-02-15 14:57:18 +01:00
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
protected registerMainMenuHandler(plugin: any, handlerName: string, handlerSchema: any, bootstrapResult: any): string {
|
2018-03-09 09:00:43 +01:00
|
|
|
if (!handlerSchema.displaydata) {
|
2018-02-15 14:57:18 +01:00
|
|
|
// Required data not provided, stop.
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger.warn('Ignore site plugin because it doesn\'t provide displaydata', plugin, handlerSchema);
|
2018-03-09 09:00:43 +01:00
|
|
|
|
2018-02-15 14:57:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger.debug('Register site plugin in main menu delegate:', plugin, handlerSchema, bootstrapResult);
|
2018-03-09 09:00:43 +01:00
|
|
|
|
2018-03-09 11:32:41 +01:00
|
|
|
// Create and register the handler.
|
2018-03-09 14:36:30 +01:00
|
|
|
const uniqueName = this.sitePluginsProvider.getHandlerUniqueName(plugin, handlerName),
|
2018-03-12 12:52:58 +01:00
|
|
|
prefixedTitle = this.getPrefixedString(plugin.addon, handlerSchema.displaydata.title);
|
2018-02-15 14:57:18 +01:00
|
|
|
|
2018-03-09 11:32:41 +01:00
|
|
|
this.mainMenuDelegate.registerHandler(
|
2018-03-09 14:36:30 +01:00
|
|
|
new CoreSitePluginsMainMenuHandler(uniqueName, prefixedTitle, plugin, handlerSchema, bootstrapResult));
|
2018-02-21 16:18:03 +01:00
|
|
|
|
|
|
|
return uniqueName;
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-09 14:36:30 +01:00
|
|
|
* Given a handler in an plugin, register it in the module delegate.
|
2018-02-15 14:57:18 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @param {any} plugin Data of the plugin.
|
|
|
|
* @param {string} handlerName Name of the handler in the plugin.
|
2018-02-15 14:57:18 +01:00
|
|
|
* @param {any} handlerSchema Data about the handler.
|
2018-03-01 13:08:27 +01:00
|
|
|
* @param {any} bootstrapResult Result of the bootstrap WS call.
|
2018-02-21 16:18:03 +01:00
|
|
|
* @return {string} A string to identify the handler.
|
2018-02-15 14:57:18 +01:00
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
protected registerModuleHandler(plugin: any, handlerName: string, handlerSchema: any, bootstrapResult: any): string {
|
2018-03-09 09:00:43 +01:00
|
|
|
if (!handlerSchema.displaydata) {
|
2018-02-15 14:57:18 +01:00
|
|
|
// Required data not provided, stop.
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger.warn('Ignore site plugin because it doesn\'t provide displaydata', plugin, handlerSchema);
|
2018-03-09 09:00:43 +01:00
|
|
|
|
2018-02-15 14:57:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger.debug('Register site plugin in module delegate:', plugin, handlerSchema, bootstrapResult);
|
2018-03-09 09:00:43 +01:00
|
|
|
|
2018-03-09 11:32:41 +01:00
|
|
|
// Create and register the handler.
|
2018-03-09 14:36:30 +01:00
|
|
|
const modName = plugin.component.replace('mod_', '');
|
2018-03-09 11:32:41 +01:00
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
this.moduleDelegate.registerHandler(new CoreSitePluginsModuleHandler(modName, handlerSchema));
|
2018-02-15 14:57:18 +01:00
|
|
|
|
2018-03-09 11:32:41 +01:00
|
|
|
if (handlerSchema.offlinefunctions && Object.keys(handlerSchema.offlinefunctions).length) {
|
2018-02-16 11:34:29 +01:00
|
|
|
// Register the prefetch handler.
|
2018-03-09 14:36:30 +01:00
|
|
|
this.prefetchDelegate.registerHandler(new CoreSitePluginsModulePrefetchHandler(
|
|
|
|
this.injector, this.sitePluginsProvider, plugin.component, modName, handlerSchema));
|
2018-02-16 11:34:29 +01:00
|
|
|
}
|
|
|
|
|
2018-02-21 16:18:03 +01:00
|
|
|
return modName;
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-09 14:36:30 +01:00
|
|
|
* Given a handler in an plugin, register it in the user profile delegate.
|
2018-02-15 14:57:18 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @param {any} plugin Data of the plugin.
|
|
|
|
* @param {string} handlerName Name of the handler in the plugin.
|
2018-02-15 14:57:18 +01:00
|
|
|
* @param {any} handlerSchema Data about the handler.
|
2018-03-01 13:08:27 +01:00
|
|
|
* @param {any} bootstrapResult Result of the bootstrap WS call.
|
2018-02-21 16:18:03 +01:00
|
|
|
* @return {string} A string to identify the handler.
|
2018-02-15 14:57:18 +01:00
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
protected registerUserProfileHandler(plugin: any, handlerName: string, handlerSchema: any, bootstrapResult: any): string {
|
2018-03-09 09:00:43 +01:00
|
|
|
if (!handlerSchema.displaydata) {
|
2018-02-15 14:57:18 +01:00
|
|
|
// Required data not provided, stop.
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger.warn('Ignore site plugin because it doesn\'t provide displaydata', plugin, handlerSchema);
|
2018-03-09 09:00:43 +01:00
|
|
|
|
2018-02-15 14:57:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger.debug('Register site plugin in user profile delegate:', plugin, handlerSchema, bootstrapResult);
|
2018-03-09 09:00:43 +01:00
|
|
|
|
2018-03-09 11:32:41 +01:00
|
|
|
// Create and register the handler.
|
2018-03-09 14:36:30 +01:00
|
|
|
const uniqueName = this.sitePluginsProvider.getHandlerUniqueName(plugin, handlerName),
|
2018-03-12 12:52:58 +01:00
|
|
|
prefixedTitle = this.getPrefixedString(plugin.addon, handlerSchema.displaydata.title);
|
2018-02-15 14:57:18 +01:00
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
this.userDelegate.registerHandler(new CoreSitePluginsUserProfileHandler(uniqueName, prefixedTitle, plugin, handlerSchema,
|
|
|
|
bootstrapResult, this.sitePluginsProvider));
|
2018-02-21 16:18:03 +01:00
|
|
|
|
|
|
|
return uniqueName;
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|
2018-03-08 15:42:44 +01:00
|
|
|
|
|
|
|
/**
|
2018-03-09 14:36:30 +01:00
|
|
|
* Given a handler in an plugin, register it in the user profile field delegate.
|
2018-03-08 15:42:44 +01:00
|
|
|
*
|
2018-03-09 14:36:30 +01:00
|
|
|
* @param {any} plugin Data of the plugin.
|
|
|
|
* @param {string} handlerName Name of the handler in the plugin.
|
2018-03-08 15:42:44 +01:00
|
|
|
* @param {any} handlerSchema Data about the handler.
|
|
|
|
* @param {any} bootstrapResult Result of the bootstrap WS call.
|
|
|
|
* @return {string|Promise<string>} A string (or a promise resolved with a string) to identify the handler.
|
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
protected registerUserProfileFieldHandler(plugin: any, handlerName: string, handlerSchema: any, bootstrapResult: any)
|
2018-03-08 15:42:44 +01:00
|
|
|
: string | Promise<string> {
|
2018-03-09 09:00:43 +01:00
|
|
|
if (!handlerSchema.method) {
|
2018-03-08 15:42:44 +01:00
|
|
|
// Required data not provided, stop.
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger.warn('Ignore site plugin because it doesn\'t provide method', plugin, handlerSchema);
|
2018-03-09 09:00:43 +01:00
|
|
|
|
2018-03-08 15:42:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
this.logger.debug('Register site plugin in user profile field delegate:', plugin, handlerSchema, bootstrapResult);
|
2018-03-09 09:00:43 +01:00
|
|
|
|
2018-03-08 15:42:44 +01:00
|
|
|
// Execute the main method and its JS. The template returned will be used in the profile field component.
|
2018-03-09 14:36:30 +01:00
|
|
|
return this.executeMethodAndJS(plugin, handlerSchema.method).then((result) => {
|
2018-03-09 11:32:41 +01:00
|
|
|
// Create and register the handler.
|
2018-03-09 14:36:30 +01:00
|
|
|
const fieldType = plugin.component.replace('profilefield_', ''),
|
|
|
|
fieldHandler = new CoreSitePluginsUserProfileFieldHandler(fieldType);
|
2018-03-08 15:42:44 +01:00
|
|
|
|
|
|
|
// Store in handlerSchema some data required by the component.
|
|
|
|
handlerSchema.methodTemplates = result.templates;
|
|
|
|
handlerSchema.methodJSResult = result.jsResult;
|
|
|
|
|
2018-03-09 11:32:41 +01:00
|
|
|
if (result && result.jsResult) {
|
|
|
|
// Override default handler functions with the result of the method JS.
|
|
|
|
for (const property in fieldHandler) {
|
|
|
|
if (property != 'constructor' && typeof fieldHandler[property] == 'function' &&
|
|
|
|
typeof result.jsResult[property] == 'function') {
|
|
|
|
fieldHandler[property] = result.jsResult[property].bind(fieldHandler);
|
2018-03-08 15:42:44 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-09 11:32:41 +01:00
|
|
|
}
|
2018-03-08 15:42:44 +01:00
|
|
|
|
|
|
|
this.profileFieldDelegate.registerHandler(fieldHandler);
|
|
|
|
|
|
|
|
return fieldType;
|
|
|
|
}).catch((err) => {
|
|
|
|
this.logger.error('Error executing main method', handlerSchema.method, err);
|
|
|
|
});
|
|
|
|
}
|
2018-02-15 14:57:18 +01:00
|
|
|
}
|