MOBILE-2391 core: Use different property for modName/etc in delegates

main
Dani Palou 2018-03-23 13:24:31 +01:00
parent c1cf6e5b21
commit 0b52f98960
33 changed files with 111 additions and 43 deletions

View File

@ -24,7 +24,8 @@ import { CoreCourseProvider } from '@core/course/providers/course';
*/
@Injectable()
export class AddonModBookModuleHandler implements CoreCourseModuleHandler {
name = 'book';
name = 'AddonModBook';
modName = 'book';
constructor(protected bookProvider: AddonModBookProvider, private courseProvider: CoreCourseProvider) { }

View File

@ -21,7 +21,8 @@ import { AddonModBookProvider } from './book';
*/
@Injectable()
export class AddonModBookPrefetchHandler extends CoreCourseModulePrefetchHandlerBase {
name = 'book';
name = 'AddonModBook';
modName = 'book';
component = AddonModBookProvider.COMPONENT;
updatesNames = /^configuration$|^.*files$|^entries$/;
isResource = true;

View File

@ -23,7 +23,8 @@ import { CoreCourseProvider } from '@core/course/providers/course';
*/
@Injectable()
export class AddonModFolderModuleHandler implements CoreCourseModuleHandler {
name = 'folder';
name = 'AddonModFolder';
modName = 'folder';
constructor(private courseProvider: CoreCourseProvider) { }

View File

@ -21,7 +21,8 @@ import { AddonModFolderProvider } from './folder';
*/
@Injectable()
export class AddonModFolderPrefetchHandler extends CoreCourseModulePrefetchHandlerBase {
name = 'folder';
name = 'AddonModFolder';
modName = 'folder';
component = AddonModFolderProvider.COMPONENT;
isResource = true;

View File

@ -20,7 +20,8 @@ import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@core/cour
*/
@Injectable()
export class AddonModLabelModuleHandler implements CoreCourseModuleHandler {
name = 'label';
name = 'AddonModLabel';
modName = 'label';
constructor() {
// Nothing to do.

View File

@ -24,7 +24,8 @@ import { CoreCourseProvider } from '@core/course/providers/course';
*/
@Injectable()
export class AddonModPageModuleHandler implements CoreCourseModuleHandler {
name = 'page';
name = 'AddonModPage';
modName = 'page';
constructor(private courseProvider: CoreCourseProvider, protected pageProvider: AddonModPageProvider) { }

View File

@ -23,7 +23,8 @@ import { AddonModPageHelperProvider } from './helper';
*/
@Injectable()
export class AddonModPagePrefetchHandler extends CoreCourseModulePrefetchHandlerBase {
name = 'page';
name = 'AddonModPage';
modName = 'page';
component = AddonModPageProvider.COMPONENT;
updatesNames = /^configuration$|^.*files$/;
isResource = true;

View File

@ -26,7 +26,8 @@ import { CoreMimetypeUtilsProvider } from '@providers/utils/mimetype';
*/
@Injectable()
export class AddonModResourceModuleHandler implements CoreCourseModuleHandler {
name = 'resource';
name = 'AddonModResource';
modName = 'resource';
constructor(protected resourceProvider: AddonModResourceProvider, private courseProvider: CoreCourseProvider,
protected mimetypeUtils: CoreMimetypeUtilsProvider, private resourceHelper: AddonModResourceHelperProvider) { }

View File

@ -23,7 +23,8 @@ import { CoreFilepoolProvider } from '@providers/filepool';
*/
@Injectable()
export class AddonModResourcePrefetchHandler extends CoreCourseModulePrefetchHandlerBase {
name = 'resource';
name = 'AddonModResource';
modName = 'resource';
component = AddonModResourceProvider.COMPONENT;
isResource = true;

View File

@ -23,7 +23,8 @@ import { CoreCourseProvider } from '@core/course/providers/course';
*/
@Injectable()
export class AddonModSurveyModuleHandler implements CoreCourseModuleHandler {
name = 'survey';
name = 'AddonModSurvey';
modName = 'survey';
constructor(private courseProvider: CoreCourseProvider) { }

View File

@ -22,7 +22,8 @@ import { AddonModSurveyHelperProvider } from './helper';
*/
@Injectable()
export class AddonModSurveyPrefetchHandler extends CoreCourseModulePrefetchHandlerBase {
name = 'survey';
name = 'AddonModSurvey';
modName = 'survey';
component = AddonModSurveyProvider.COMPONENT;
updatesNames = /^configuration$|^.*files$|^answers$/;

View File

@ -25,7 +25,8 @@ import { AddonModUrlHelperProvider } from './helper';
*/
@Injectable()
export class AddonModUrlModuleHandler implements CoreCourseModuleHandler {
name = 'url';
name = 'AddonModUrl';
modName = 'url';
constructor(private courseProvider: CoreCourseProvider, private urlProvider: AddonModUrlProvider,
private urlHelper: AddonModUrlHelperProvider) { }

View File

@ -21,7 +21,8 @@ import { AddonUserProfileFieldCheckboxComponent } from '../component/checkbox';
*/
@Injectable()
export class AddonUserProfileFieldCheckboxHandler implements CoreUserProfileFieldHandler {
name = 'checkbox';
name = 'AddonUserProfileFieldCheckbox';
type = 'checkbox';
constructor() {
// Nothing to do.

View File

@ -21,7 +21,8 @@ import { AddonUserProfileFieldDatetimeComponent } from '../component/datetime';
*/
@Injectable()
export class AddonUserProfileFieldDatetimeHandler implements CoreUserProfileFieldHandler {
name = 'datetime';
name = 'AddonUserProfileFieldDatetime';
type = 'datetime';
constructor() {
// Nothing to do.

View File

@ -21,7 +21,8 @@ import { AddonUserProfileFieldMenuComponent } from '../component/menu';
*/
@Injectable()
export class AddonUserProfileFieldMenuHandler implements CoreUserProfileFieldHandler {
name = 'menu';
name = 'AddonUserProfileFieldMenu';
type = 'menu';
constructor() {
// Nothing to do.

View File

@ -22,7 +22,8 @@ import { CoreTextUtilsProvider } from '@providers/utils/text';
*/
@Injectable()
export class AddonUserProfileFieldTextHandler implements CoreUserProfileFieldHandler {
name = 'text';
name = 'AddonUserProfileFieldText';
type = 'text';
constructor(private textUtils: CoreTextUtilsProvider) { }

View File

@ -22,7 +22,8 @@ import { CoreTextUtilsProvider } from '@providers/utils/text';
*/
@Injectable()
export class AddonUserProfileFieldTextareaHandler implements CoreUserProfileFieldHandler {
name = 'textarea';
name = 'AddonUserProfileFieldTextarea';
type = 'textarea';
constructor(private textUtils: CoreTextUtilsProvider) { }

View File

@ -20,6 +20,7 @@ import { CoreEventsProvider } from '@providers/events';
export interface CoreDelegateHandler {
/**
* Name of the handler, or name and sub context (AddonMessages, AddonMessages:blockContact, ...).
* This name will be used to check if the feature is disabled.
* @type {string}
*/
name: string;
@ -74,6 +75,14 @@ export class CoreDelegate {
*/
protected featurePrefix: string;
/**
* Name of the property to be used to index the handlers. By default, the handler's name will be used.
* If your delegate uses a Moodle component name to identify the handlers, please override this property.
* E.g. CoreCourseModuleDelegate uses 'modName' to index the handlers.
* @type {string}
*/
protected handlerNameProperty = 'name';
/**
* Constructor of the Delegate.
*
@ -182,14 +191,14 @@ export class CoreDelegate {
* @return {boolean} True when registered, false if already registered.
*/
registerHandler(handler: CoreDelegateHandler): boolean {
if (typeof this.handlers[handler.name] !== 'undefined') {
this.logger.log(`Addon '${handler.name}' already registered`);
if (typeof this.handlers[handler[this.handlerNameProperty]] !== 'undefined') {
this.logger.log(`Handler '${handler[this.handlerNameProperty]}' already registered`);
return false;
}
this.logger.log(`Registered addon '${handler.name}'`);
this.handlers[handler.name] = handler;
this.logger.log(`Registered handler '${handler[this.handlerNameProperty]}'`);
this.handlers[handler[this.handlerNameProperty]] = handler;
return true;
}
@ -222,9 +231,9 @@ export class CoreDelegate {
// Check that site hasn't changed since the check started.
if (this.isLastUpdateCall(time) && this.sitesProvider.getCurrentSiteId() === siteId) {
if (enabled) {
this.enabledHandlers[handler.name] = handler;
this.enabledHandlers[handler[this.handlerNameProperty]] = handler;
} else {
delete this.enabledHandlers[handler.name];
delete this.enabledHandlers[handler[this.handlerNameProperty]];
}
}
});

View File

@ -53,11 +53,17 @@ export type prefetchFunction = (module: any, courseId: number, single: boolean,
* recommended to call the prefetchPackage function since it'll handle changing the status of the module.
*/
export class CoreCourseModulePrefetchHandlerBase implements CoreCourseModulePrefetchHandler {
/**
* Name of the handler.
* @type {string}
*/
name = 'CoreCourseModulePrefetchHandler';
/**
* Name of the module. It should match the "modname" of the module returned in core_course_get_contents.
* @type {string}
*/
name = '';
modName = 'default';
/**
* The handler's component.

View File

@ -21,7 +21,8 @@ import { CoreCourseFormatSingleActivityComponent } from '../components/singleact
*/
@Injectable()
export class CoreCourseFormatSingleActivityHandler implements CoreCourseFormatHandler {
name = 'singleactivity';
name = 'CoreCourseFormatSingleActivity';
format = 'singleactivity';
constructor() {
// Nothing to do.

View File

@ -21,5 +21,6 @@ import { CoreCourseFormatSingleActivityHandler } from '../../singleactivity/prov
*/
@Injectable()
export class CoreCourseFormatSocialHandler extends CoreCourseFormatSingleActivityHandler {
name = 'social';
name = 'CoreCourseFormatSocial';
format = 'social';
}

View File

@ -20,7 +20,8 @@ import { CoreCourseFormatHandler } from '../../../providers/format-delegate';
*/
@Injectable()
export class CoreCourseFormatTopicsHandler implements CoreCourseFormatHandler {
name = 'topics';
name = 'CoreCourseFormatTopics';
format = 'topics';
constructor() {
// Nothing to do.

View File

@ -22,7 +22,8 @@ import { CoreConstants } from '../../../../constants';
*/
@Injectable()
export class CoreCourseFormatWeeksHandler implements CoreCourseFormatHandler {
name = 'weeks';
name = 'CoreCourseFormatWeeks';
format = 'weeks';
constructor(private timeUtils: CoreTimeUtilsProvider) { }

View File

@ -23,7 +23,8 @@ import { CoreCourseProvider } from './course';
*/
@Injectable()
export class CoreCourseFormatDefaultHandler implements CoreCourseFormatHandler {
name = 'default';
name = 'CoreCourseFormatDefault';
format = 'default';
constructor(private coursesProvider: CoreCoursesProvider) { }

View File

@ -25,6 +25,12 @@ import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate';
* Interface that all course format handlers must implement.
*/
export interface CoreCourseFormatHandler extends CoreDelegateHandler {
/**
* Name of the format the handler supports. E.g. 'singleactivity'.
* @type {string}
*/
format: string;
/**
* Get the title to use in course page. If not defined, course fullname.
* This function will be called without sections first, and then call it again when the sections are retrieved.
@ -151,7 +157,8 @@ export interface CoreCourseFormatHandler extends CoreDelegateHandler {
export class CoreCourseFormatDelegate extends CoreDelegate {
protected handlers: { [s: string]: CoreCourseFormatHandler } = {}; // All registered handlers.
protected enabledHandlers: { [s: string]: CoreCourseFormatHandler } = {}; // Handlers enabled for the current site.
protected featurePrefix = 'CoreCourseFormatHandler_';
protected featurePrefix = 'CoreCourseFormatDelegate_';
protected handlerNameProperty = 'format';
constructor(loggerProvider: CoreLoggerProvider, protected sitesProvider: CoreSitesProvider, eventsProvider: CoreEventsProvider,
protected defaultHandler: CoreCourseFormatDefaultHandler) {

View File

@ -25,6 +25,12 @@ import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate';
* Interface that all course module handlers must implement.
*/
export interface CoreCourseModuleHandler extends CoreDelegateHandler {
/**
* Name of the module. It should match the "modname" of the module returned in core_course_get_contents.
* @type {string}
*/
modName: string;
/**
* Get the data required to display the module in the course contents view.
*
@ -169,6 +175,7 @@ export class CoreCourseModuleDelegate extends CoreDelegate {
protected handlers: { [s: string]: CoreCourseModuleHandler } = {}; // All registered handlers.
protected enabledHandlers: { [s: string]: CoreCourseModuleHandler } = {}; // Handlers enabled for the current site.
protected featurePrefix = 'CoreCourseModuleDelegate_';
protected handlerNameProperty = 'modName';
constructor(loggerProvider: CoreLoggerProvider, protected sitesProvider: CoreSitesProvider, eventsProvider: CoreEventsProvider,
protected courseProvider: CoreCourseProvider) {

View File

@ -56,6 +56,12 @@ export type CoreCourseModulesProgressFunction = (data: CoreCourseModulesProgress
* Interface that all course prefetch handlers must implement.
*/
export interface CoreCourseModulePrefetchHandler extends CoreDelegateHandler {
/**
* Name of the module. It should match the "modname" of the module returned in core_course_get_contents.
* @type {string}
*/
modName: string;
/**
* The handler's component.
* @type {string}
@ -214,6 +220,7 @@ export class CoreCourseModulePrefetchDelegate extends CoreDelegate {
protected ROOT_CACHE_KEY = 'mmCourse:';
protected statusCache = new CoreCache();
protected handlerNameProperty = 'modName';
// Promises for check updates, to prevent performing the same request twice at the same time.
protected courseUpdatesPromises: { [s: string]: { [s: string]: Promise<any> } } = {};

View File

@ -22,7 +22,7 @@ import { CoreSitePluginsCourseFormatComponent } from '../components/course-forma
*/
export class CoreSitePluginsCourseFormatHandler extends CoreSitePluginsBaseHandler implements CoreCourseFormatHandler {
constructor(name: string, protected handlerSchema: any) {
constructor(name: string, public format: string, protected handlerSchema: any) {
super(name);
}

View File

@ -24,7 +24,7 @@ import { CoreSitePluginsModuleIndexComponent } from '../components/module-index/
export class CoreSitePluginsModuleHandler extends CoreSitePluginsBaseHandler implements CoreCourseModuleHandler {
priority: number;
constructor(name: string, protected handlerSchema: any) {
constructor(name: string, public modName: string, protected handlerSchema: any) {
super(name);
}

View File

@ -22,12 +22,13 @@ import { CoreCourseModulePrefetchHandlerBase } from '@core/course/classes/module
export class CoreSitePluginsModulePrefetchHandler extends CoreCourseModulePrefetchHandlerBase {
protected ROOT_CACHE_KEY = 'CoreSitePluginsModulePrefetchHandler:';
constructor(injector: Injector, protected sitePluginsProvider: CoreSitePluginsProvider, component: string, modName: string,
protected handlerSchema: any) {
constructor(injector: Injector, protected sitePluginsProvider: CoreSitePluginsProvider, component: string, name: string,
modName: string, protected handlerSchema: any) {
super(injector);
this.component = component;
this.name = modName;
this.name = name;
this.modName = modName;
this.isResource = handlerSchema.isresource;
if (handlerSchema.updatesnames) {

View File

@ -22,7 +22,7 @@ import { CoreSitePluginsUserProfileFieldComponent } from '../components/user-pro
*/
export class CoreSitePluginsUserProfileFieldHandler extends CoreSitePluginsBaseHandler implements CoreUserProfileFieldHandler {
constructor(name: string) {
constructor(name: string, public type: string) {
super(name);
}

View File

@ -353,8 +353,9 @@ export class CoreSitePluginsHelperProvider {
this.logger.debug('Register site plugin in course format delegate:', plugin, handlerSchema, bootstrapResult);
// Create and register the handler.
const formatName = plugin.component.replace('format_', '');
this.courseFormatDelegate.registerHandler(new CoreSitePluginsCourseFormatHandler(formatName, handlerSchema));
const uniqueName = this.sitePluginsProvider.getHandlerUniqueName(plugin, handlerName),
formatName = plugin.component.replace('format_', '');
this.courseFormatDelegate.registerHandler(new CoreSitePluginsCourseFormatHandler(uniqueName, formatName, handlerSchema));
return formatName;
}
@ -437,14 +438,15 @@ export class CoreSitePluginsHelperProvider {
this.logger.debug('Register site plugin in module delegate:', plugin, handlerSchema, bootstrapResult);
// Create and register the handler.
const modName = plugin.component.replace('mod_', '');
const uniqueName = this.sitePluginsProvider.getHandlerUniqueName(plugin, handlerName),
modName = plugin.component.replace('mod_', '');
this.moduleDelegate.registerHandler(new CoreSitePluginsModuleHandler(modName, handlerSchema));
this.moduleDelegate.registerHandler(new CoreSitePluginsModuleHandler(uniqueName, modName, handlerSchema));
if (handlerSchema.offlinefunctions && Object.keys(handlerSchema.offlinefunctions).length) {
// Register the prefetch handler.
this.prefetchDelegate.registerHandler(new CoreSitePluginsModulePrefetchHandler(
this.injector, this.sitePluginsProvider, plugin.component, modName, handlerSchema));
this.injector, this.sitePluginsProvider, plugin.component, uniqueName, modName, handlerSchema));
}
return modName;
@ -502,8 +504,9 @@ export class CoreSitePluginsHelperProvider {
// Execute the main method and its JS. The template returned will be used in the profile field component.
return this.executeMethodAndJS(plugin, handlerSchema.method).then((result) => {
// Create and register the handler.
const fieldType = plugin.component.replace('profilefield_', ''),
fieldHandler = new CoreSitePluginsUserProfileFieldHandler(fieldType);
const uniqueName = this.sitePluginsProvider.getHandlerUniqueName(plugin, handlerName),
fieldType = plugin.component.replace('profilefield_', ''),
fieldHandler = new CoreSitePluginsUserProfileFieldHandler(uniqueName, fieldType);
// Store in handlerSchema some data required by the component.
handlerSchema.methodTemplates = result.templates;

View File

@ -19,6 +19,11 @@ import { CoreSitesProvider } from '@providers/sites';
import { CoreEventsProvider } from '@providers/events';
export interface CoreUserProfileFieldHandler extends CoreDelegateHandler {
/**
* Type of the field the handler supports. E.g. 'checkbox'.
* @type {string}
*/
type: string;
/**
* Return the Component to use to display the user profile field.
@ -68,6 +73,7 @@ export interface CoreUserProfileFieldHandlerData {
export class CoreUserProfileFieldDelegate extends CoreDelegate {
protected handlers: { [s: string]: CoreUserProfileFieldHandler } = {};
protected enabledHandlers: { [s: string]: CoreUserProfileFieldHandler } = {};
protected handlerNameProperty = 'type';
constructor(protected loggerProvider: CoreLoggerProvider, protected sitesProvider: CoreSitesProvider,
protected eventsProvider: CoreEventsProvider) {