Files
ohif-viewer/platform/core/src/extensions/ExtensionManager.js
T

261 lines
7.4 KiB
JavaScript
Raw Normal View History

2019-08-13 15:36:46 -04:00
import MODULE_TYPES from './MODULE_TYPES.js';
import log from './../log.js';
export default class ExtensionManager {
2020-05-06 14:17:44 -03:00
constructor({ commandsManager, servicesManager, api, appConfig = {} }) {
2019-08-13 15:36:46 -04:00
this.modules = {};
this.registeredExtensionIds = [];
this.moduleTypeNames = Object.values(MODULE_TYPES);
//
this._commandsManager = commandsManager;
this._servicesManager = servicesManager;
2019-12-16 16:07:53 -03:00
this._appConfig = appConfig;
2020-05-06 14:17:44 -03:00
this._api = api;
2019-08-13 15:36:46 -04:00
this.modulesMap = {};
2019-08-13 15:36:46 -04:00
this.moduleTypeNames.forEach(moduleType => {
this.modules[moduleType] = [];
});
this.dataSourceMap = {};
2020-06-05 17:36:11 +02:00
this.defaultDataSourceName = appConfig.defaultDataSourceName;
this.activeDataSource = undefined;
}
2020-06-05 17:36:11 +02:00
setActiveDataSource(dataSourceName) {
this.activeDataSource = dataSourceName;
2019-08-13 15:36:46 -04:00
}
/**
* An array of extensions, or an array of arrays that contains extension
* configuration pairs.
*
* @param {Object[]} extensions - Array of extensions
*/
2020-05-11 18:56:46 +02:00
registerExtensions = (extensions, dataSources) => {
2019-08-13 15:36:46 -04:00
extensions.forEach(extension => {
const hasConfiguration = Array.isArray(extension);
if (hasConfiguration) {
const [ohifExtension, configuration] = extension;
2020-05-11 18:56:46 +02:00
this.registerExtension(ohifExtension, dataSources, configuration);
2019-08-13 15:36:46 -04:00
} else {
2020-05-11 18:56:46 +02:00
this.registerExtension(extension, dataSources);
2019-08-13 15:36:46 -04:00
}
});
};
2019-08-13 15:36:46 -04:00
/**
*
* TODO: Id Management: SopClassHandlers currently refer to viewport module by id; setting the extension id as viewport module id is a workaround for now
* @param {Object} extension
* @param {Object} configuration
*/
2020-05-11 18:56:46 +02:00
registerExtension = (extension, dataSources, configuration = {}) => {
2019-08-13 15:36:46 -04:00
if (!extension) {
log.warn(
'Attempting to register a null/undefined extension. Exiting early.'
);
return;
}
let extensionId = extension.id;
if (!extensionId) {
// Note: Mode framework cannot function without IDs.
log.warn(extension);
throw new Error(`Extension ID not set`);
2019-08-13 15:36:46 -04:00
}
if (this.registeredExtensionIds.includes(extensionId)) {
log.warn(
`Extension ID ${extensionId} has already been registered. Exiting before duplicating modules.`
);
return;
}
// preRegistrationHook
if (extension.preRegistration) {
extension.preRegistration({
2019-11-14 11:50:37 -03:00
servicesManager: this._servicesManager,
2019-12-02 13:29:04 -03:00
commandsManager: this._commandsManager,
2019-12-16 16:07:53 -03:00
appConfig: this._appConfig,
configuration,
});
2019-08-13 15:36:46 -04:00
}
// Register Modules
this.moduleTypeNames.forEach(moduleType => {
const extensionModule = this._getExtensionModule(
moduleType,
extension,
2019-12-16 16:07:53 -03:00
extensionId,
configuration
2019-08-13 15:36:46 -04:00
);
if (extensionModule) {
switch (moduleType) {
case MODULE_TYPES.COMMANDS:
this._initCommandsModule(extensionModule);
break;
case MODULE_TYPES.DATA_SOURCE:
this._initDataSourcesModule(
extensionModule,
extensionId,
dataSources
);
break;
case MODULE_TYPES.TOOLBAR:
2020-05-15 19:07:21 +02:00
case MODULE_TYPES.VIEWPORT:
case MODULE_TYPES.PANEL:
case MODULE_TYPES.SOP_CLASS_HANDLER:
case MODULE_TYPES.CONTEXT:
case MODULE_TYPES.LAYOUT_TEMPLATE:
// Default for most extension points,
// Just adds each entry ready for consumption by mode.
extensionModule.forEach(element => {
this.modulesMap[
`${extensionId}.${moduleType}.${element.name}`
] = element;
});
2020-05-15 19:07:21 +02:00
break;
}
2019-08-13 15:36:46 -04:00
this.modules[moduleType].push({
extensionId,
module: extensionModule,
});
}
});
// Track extension registration
this.registeredExtensionIds.push(extensionId);
};
2019-08-13 15:36:46 -04:00
getModuleEntry = stringEntry => {
return this.modulesMap[stringEntry];
};
2020-05-11 18:56:46 +02:00
getDataSources = dataSourceName => {
2020-06-05 17:36:11 +02:00
if (dataSourceName === undefined) {
2020-06-26 11:01:13 +02:00
// Default to the activeDataSource
2020-06-05 17:36:11 +02:00
dataSourceName = this.activeDataSource;
}
// Note: this currently uses the data source name, which feels weird...
2020-05-11 18:56:46 +02:00
return this.dataSourceMap[dataSourceName];
};
2020-06-26 11:01:13 +02:00
getActiveDataSource = () => {
return this.activeDataSource;
};
2019-08-13 15:36:46 -04:00
/**
* @private
* @param {string} moduleType
* @param {Object} extension
* @param {string} extensionId - Used for logging warnings
*/
_getExtensionModule = (moduleType, extension, extensionId, configuration) => {
2019-08-13 15:36:46 -04:00
const getModuleFnName = 'get' + _capitalizeFirstCharacter(moduleType);
const getModuleFn = extension[getModuleFnName];
if (!getModuleFn) {
return;
}
try {
2019-11-14 19:29:40 -03:00
const extensionModule = getModuleFn({
appConfig: this._appConfig,
2020-05-20 01:26:44 -04:00
getDataSources: this.getDataSources, // Why pass this in if we're passing in `extensionManager`?
2019-12-02 13:29:04 -03:00
commandsManager: this._commandsManager,
extensionManager: this,
servicesManager: this._servicesManager,
2019-12-16 16:07:53 -03:00
configuration,
2020-05-15 14:29:27 +02:00
api: this._api,
2019-11-14 19:29:40 -03:00
});
2019-08-13 15:36:46 -04:00
if (!extensionModule) {
log.warn(
`Null or undefined returned when registering the ${getModuleFnName} module for the ${extensionId} extension`
);
}
return extensionModule;
} catch (ex) {
log.error(
`Exception thrown while trying to call ${getModuleFnName} for the ${extensionId} extension`
);
log.error(ex);
2019-08-13 15:36:46 -04:00
}
};
2019-08-13 15:36:46 -04:00
_initDataSourcesModule(extensionModule, extensionId, dataSources) {
extensionModule.forEach(element => {
const namespace = `${extensionId}.${MODULE_TYPES.DATA_SOURCE}.${element.name}`;
dataSources.forEach(dataSource => {
if (dataSource.namespace === namespace) {
const dataSourceInstance = element.createDataSource(
dataSource.configuration
);
if (this.dataSourceMap[dataSource.sourceName]) {
this.dataSourceMap[dataSource.sourceName].push(dataSourceInstance);
} else {
this.dataSourceMap[dataSource.sourceName] = [dataSourceInstance];
}
2019-08-13 15:36:46 -04:00
}
});
});
extensionModule.forEach(element => {
this.modulesMap[
`${extensionId}.${MODULE_TYPES.DATA_SOURCE}.${element.name}`
] = element;
});
}
2019-08-13 15:36:46 -04:00
/**
*
* @private
* @param {Object[]} commandDefinitions
*/
_initCommandsModule = extensionModule => {
let { definitions, defaultContext } = extensionModule;
if (!definitions || Object.keys(definitions).length === 0) {
log.warn('Commands Module contains no command definitions');
return;
}
defaultContext = defaultContext || 'VIEWER';
2019-08-13 15:36:46 -04:00
if (!this._commandsManager.getContext(defaultContext)) {
this._commandsManager.createContext(defaultContext);
}
Object.keys(definitions).forEach(commandName => {
const commandDefinition = definitions[commandName];
2019-08-13 15:36:46 -04:00
const commandHasContextThatDoesNotExist =
commandDefinition.context &&
!this._commandsManager.getContext(commandDefinition.context);
if (commandHasContextThatDoesNotExist) {
this._commandsManager.createContext(commandDefinition.context);
}
this._commandsManager.registerCommand(
commandDefinition.context || defaultContext,
commandName,
commandDefinition
);
});
};
2019-08-13 15:36:46 -04:00
}
/**
* @private
* @param {string} lower
*/
function _capitalizeFirstCharacter(lower) {
return lower.charAt(0).toUpperCase() + lower.substr(1);
}