399 lines
16 KiB
TypeScript
Raw Normal View History

// (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 { NavController, NavOptions } from 'ionic-angular';
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';
import { CoreMainMenuDelegate, CoreMainMenuHandler, CoreMainMenuHandlerData } from '../../mainmenu/providers/delegate';
import {
CoreCourseModuleDelegate, CoreCourseModuleHandler, CoreCourseModuleHandlerData
} from '../../course/providers/module-delegate';
import { CoreCourseModulePrefetchDelegate } from '../../course/providers/module-prefetch-delegate';
import { CoreUserDelegate, CoreUserProfileHandler, CoreUserProfileHandlerData } from '../../user/providers/user-delegate';
import { CoreDelegateHandler } from '../../../classes/delegate';
import { CoreSiteAddonsModuleIndexComponent } from '../components/module-index/module-index';
import { CoreSiteAddonsProvider } from './siteaddons';
import { CoreSiteAddonsModulePrefetchHandler } from '../classes/module-prefetch-handler';
import { CoreCompileProvider } from '../../compile/providers/compile';
/**
* Helper service to provide functionalities regarding site addons. It basically has the features to load and register site
* addons.
*
* This code is split from CoreSiteAddonsProvider to prevent circular dependencies.
*/
@Injectable()
export class CoreSiteAddonsHelperProvider {
protected logger;
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private injector: Injector,
private mainMenuDelegate: CoreMainMenuDelegate, private moduleDelegate: CoreCourseModuleDelegate,
private userDelegate: CoreUserDelegate, private langProvider: CoreLangProvider,
private siteAddonsProvider: CoreSiteAddonsProvider, private prefetchDelegate: CoreCourseModulePrefetchDelegate,
private compileProvider: CoreCompileProvider, private utils: CoreUtilsProvider) {
this.logger = logger.getInstance('CoreSiteAddonsHelperProvider');
}
/**
* Bootstrap a handler if it has some bootstrap JS.
*
* @param {any} addon Data of the addon.
* @param {string} handlerName Name of the handler in the addon.
* @param {any} handlerSchema Data about the handler.
* @return {Promise<any>} Promise resolved when done. The resolve param is the result of the javascript execution (if any).
*/
protected bootstrapHandler(addon: any, handlerName: string, handlerSchema: any): Promise<any> {
if (!handlerSchema.bootstrap) {
return Promise.resolve();
}
const siteId = this.sitesProvider.getCurrentSiteId(),
preSets = {getFromCache: false}; // Try to ignore cache.
return this.siteAddonsProvider.getContent(addon.component, handlerSchema.bootstrap, {}, preSets).then((result) => {
if (!result.javascript || this.sitesProvider.getCurrentSiteId() != siteId) {
// No javascript or site has changed, stop.
return;
}
// Create a "fake" instance to hold all the libraries.
const instance = {};
this.compileProvider.injectLibraries(instance);
// Now execute the javascript using this instance.
return this.compileProvider.executeJavascript(instance, result.javascript);
});
}
/**
* Create a base handler for a site addon.
*
* @param {string} name Name of the handler.
* @return {CoreDelegateHandler} The base handler.
*/
protected getBaseHandler(name: string): CoreDelegateHandler {
return {
name: name,
isEnabled: (): boolean => {
return true;
}
};
}
/**
* Given a handler's unique name, return the prefix to add to its string keys.
*
* @param {string} handlerName Handler's unique name (result of getHandlerUniqueName).
* @return {string} Prefix.
*/
protected getHandlerPrefixForStrings(handlerName: string): string {
if (handlerName) {
return 'addon.' + handlerName + '.';
}
return '';
}
/**
* Given a handler's unique name and the key of a string, return the full string key (prefixed).
*
* @param {string} handlerName Handler's unique name (result of getHandlerUniqueName).
* @param {string} key The key of the string.
* @return {string} Full string key.
*/
protected getHandlerPrefixedString(handlerName: string, key: string): string {
return this.getHandlerPrefixForStrings(handlerName) + key;
}
/**
* Check if a certain addon is a site addon and it's enabled in a certain site.
*
* @param {any} addon Data of the addon.
* @param {CoreSite} site Site affected.
* @return {boolean} Whether it's a site addon and it's enabled.
*/
isSiteAddonEnabled(addon: any, site: CoreSite): boolean {
if (!site.isFeatureDisabled('siteAddOn_' + addon.component + '_' + addon.addon) && addon.handlers) {
// Site addon not disabled. Check if it has handlers.
try {
if (!addon.parsedHandlers) {
addon.parsedHandlers = JSON.parse(addon.handlers);
}
return !!(addon.parsedHandlers && Object.keys(addon.parsedHandlers).length);
} catch (ex) {
this.logger.warn('Error parsing site addon', ex);
}
}
return false;
}
/**
* Load the lang strings for a handler.
*
* @param {any} addon Data of the addon.
* @param {string} handlerName Name of the handler in the addon.
* @param {any} handlerSchema Data about the handler.
*/
loadHandlerLangStrings(addon: any, handlerName: string, handlerSchema: any): void {
if (!handlerSchema.lang) {
return;
}
for (const lang in handlerSchema.lang) {
const prefix = this.getHandlerPrefixForStrings(this.siteAddonsProvider.getHandlerUniqueName(addon, handlerName));
this.langProvider.addSiteAddonsStrings(lang, handlerSchema.lang[lang], prefix);
}
}
/**
* Load a site addon.
*
* @param {any} addon Data of the addon.
* @return {Promise<any>} Promise resolved when loaded.
*/
loadSiteAddon(addon: any): Promise<any> {
const promises = [];
try {
if (!addon.parsedHandlers) {
addon.parsedHandlers = JSON.parse(addon.handlers);
}
// Register all the handlers.
for (const name in addon.parsedHandlers) {
promises.push(this.registerHandler(addon, name, addon.parsedHandlers[name]));
}
} catch (ex) {
this.logger.warn('Error parsing site addon', ex);
}
return this.utils.allPromises(promises);
}
/**
* Register a site addon handler in the right delegate.
*
* @param {any} addon Data of the addon.
* @param {string} handlerName Name of the handler in the addon.
* @param {any} handlerSchema Data about the handler.
* @return {Promise<any>} Promise resolved when done.
*/
registerHandler(addon: any, handlerName: string, handlerSchema: any): Promise<any> {
this.loadHandlerLangStrings(addon, handlerName, handlerSchema);
// Wait for the bootstrap JS to be executed.
return this.bootstrapHandler(addon, handlerName, handlerSchema).then((result) => {
let uniqueName;
switch (handlerSchema.delegate) {
case 'CoreMainMenuDelegate':
uniqueName = this.registerMainMenuHandler(addon, handlerName, handlerSchema, result);
break;
case 'CoreCourseModuleDelegate':
uniqueName = this.registerModuleHandler(addon, handlerName, handlerSchema, result);
break;
case 'CoreUserDelegate':
uniqueName = this.registerUserProfileHandler(addon, handlerName, handlerSchema, result);
break;
default:
// Nothing to do.
}
if (uniqueName) {
// Store the handler data.
this.siteAddonsProvider.setSiteAddonHandler(uniqueName, {
addon: addon,
handlerName: handlerName,
handlerSchema: handlerSchema,
bootstrapResult: result
});
}
});
}
/**
* Given a handler in an addon, register it in the main menu delegate.
*
* @param {any} addon Data of the addon.
* @param {string} handlerName Name of the handler in the addon.
* @param {any} handlerSchema Data about the handler.
* @param {any} [bootstrapResult] Result of executing the bootstrap JS.
* @return {string} A string to identify the handler.
*/
protected registerMainMenuHandler(addon: any, handlerName: string, handlerSchema: any, bootstrapResult?: any): string {
if (!handlerSchema || !handlerSchema.displaydata) {
// Required data not provided, stop.
return;
}
// Create the base handler.
const uniqueName = this.siteAddonsProvider.getHandlerUniqueName(addon, handlerName),
baseHandler = this.getBaseHandler(uniqueName),
prefixedTitle = this.getHandlerPrefixedString(baseHandler.name, handlerSchema.displaydata.title);
let mainMenuHandler: CoreMainMenuHandler;
// Extend the base handler, adding the properties required by the delegate.
mainMenuHandler = Object.assign(baseHandler, {
priority: handlerSchema.priority,
getDisplayData: (): CoreMainMenuHandlerData => {
return {
title: prefixedTitle,
icon: handlerSchema.displaydata.icon,
class: handlerSchema.displaydata.class,
page: 'CoreSiteAddonsAddonPage',
pageParams: {
title: prefixedTitle,
component: addon.component,
method: handlerSchema.method,
bootstrapResult: bootstrapResult
}
};
}
});
this.mainMenuDelegate.registerHandler(mainMenuHandler);
return uniqueName;
}
/**
* Given a handler in an addon, register it in the module delegate.
*
* @param {any} addon Data of the addon.
* @param {string} handlerName Name of the handler in the addon.
* @param {any} handlerSchema Data about the handler.
* @param {any} [bootstrapResult] Result of executing the bootstrap JS.
* @return {string} A string to identify the handler.
*/
protected registerModuleHandler(addon: any, handlerName: string, handlerSchema: any, bootstrapResult?: any): string {
if (!handlerSchema || !handlerSchema.displaydata) {
// Required data not provided, stop.
return;
}
// Create the base handler.
const modName = addon.component.replace('mod_', ''),
baseHandler = this.getBaseHandler(modName),
hasOfflineFunctions = !!(handlerSchema.offlinefunctions && Object.keys(handlerSchema.offlinefunctions).length),
showDowloadButton = handlerSchema.downloadbutton;
let moduleHandler: CoreCourseModuleHandler;
// Extend the base handler, adding the properties required by the delegate.
moduleHandler = Object.assign(baseHandler, {
getData: (module: any, courseId: number, sectionId: number): CoreCourseModuleHandlerData => {
return {
title: module.name,
icon: handlerSchema.displaydata.icon,
class: handlerSchema.displaydata.class,
showDownloadButton: typeof showDowloadButton != 'undefined' ? showDowloadButton : hasOfflineFunctions,
action: (event: Event, navCtrl: NavController, module: any, courseId: number, options: NavOptions): void => {
event.preventDefault();
event.stopPropagation();
navCtrl.push('CoreSiteAddonsModuleIndexPage', {
title: module.name,
module: module,
courseId: courseId
}, options);
}
};
},
getMainComponent: (course: any, module: any): any => {
return CoreSiteAddonsModuleIndexComponent;
}
});
if (hasOfflineFunctions) {
// Register the prefetch handler.
this.prefetchDelegate.registerHandler(new CoreSiteAddonsModulePrefetchHandler(
this.injector, this.siteAddonsProvider, addon.component, modName, handlerSchema));
}
this.moduleDelegate.registerHandler(moduleHandler);
return modName;
}
/**
* Given a handler in an addon, register it in the user profile delegate.
*
* @param {any} addon Data of the addon.
* @param {string} handlerName Name of the handler in the addon.
* @param {any} handlerSchema Data about the handler.
* @param {any} [bootstrapResult] Result of executing the bootstrap JS.
* @return {string} A string to identify the handler.
*/
protected registerUserProfileHandler(addon: any, handlerName: string, handlerSchema: any, bootstrapResult?: any): string {
if (!handlerSchema || !handlerSchema.displaydata) {
// Required data not provided, stop.
return;
}
// Create the base handler.
const uniqueName = this.siteAddonsProvider.getHandlerUniqueName(addon, handlerName),
baseHandler = this.getBaseHandler(uniqueName),
prefixedTitle = this.getHandlerPrefixedString(baseHandler.name, handlerSchema.displaydata.title);
let userHandler: CoreUserProfileHandler;
// Extend the base handler, adding the properties required by the delegate.
userHandler = Object.assign(baseHandler, {
priority: handlerSchema.priority,
type: handlerSchema.type,
isEnabledForUser: (user: any, courseId: number, navOptions?: any, admOptions?: any): boolean => {
if (handlerSchema.restricted == 'current' && user.id != this.sitesProvider.getCurrentSite().getUserId()) {
return false;
}
return true;
},
getDisplayData: (user: any, courseId: number): CoreUserProfileHandlerData => {
return {
title: prefixedTitle,
icon: handlerSchema.displaydata.icon,
class: handlerSchema.displaydata.class,
action: (event: Event, navCtrl: NavController, user: any, courseId?: number): void => {
event.preventDefault();
event.stopPropagation();
navCtrl.push('CoreSiteAddonsAddonPage', {
title: prefixedTitle,
component: addon.component,
method: handlerSchema.method,
args: {
courseid: courseId,
userid: user.id
},
bootstrapResult: bootstrapResult
});
}
};
}
});
this.userDelegate.registerHandler(userHandler);
return uniqueName;
}
}