ohif-viewer/Packages/ohif-commands/client/classes/CommandsManager.js

88 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-04-24 22:07:47 +02:00
import { Tracker } from 'meteor/tracker';
2017-04-25 22:42:52 +02:00
import { Session } from 'meteor/session';
2017-04-25 13:08:10 +02:00
import { _ } from 'meteor/underscore';
2017-04-24 22:07:47 +02:00
import { OHIF } from 'meteor/ohif:core';
export class CommandsManager {
constructor() {
this.contexts = {};
Tracker.autorun(() => {
const currentContextName = OHIF.context.get();
OHIF.log.info(currentContextName);
// TODO: initialize hotkeys context
});
}
getContext(contextName) {
const context = this.contexts[contextName];
if (!context) {
2017-04-25 13:08:10 +02:00
return OHIF.log.warn(`No context found with name "${contextName}"`);
2017-04-24 22:07:47 +02:00
}
return context;
}
2017-04-25 13:08:10 +02:00
getCurrentContext() {
const contextName = OHIF.context.get();
if (!contextName) {
return OHIF.log.warn('There is no selected context');
}
return this.getContext(contextName);
2017-04-24 22:07:47 +02:00
}
createContext(contextName) {
2017-04-25 13:08:10 +02:00
if (!contextName) return;
2017-04-24 22:07:47 +02:00
if (this.contexts[contextName]) {
2017-04-25 22:42:52 +02:00
return this.clear(contextName);
2017-04-24 22:07:47 +02:00
}
2017-04-25 13:08:10 +02:00
this.contexts[contextName] = {};
2017-04-24 22:07:47 +02:00
}
2017-04-25 22:42:52 +02:00
set(contextName, definitions, extend=false) {
2017-04-24 22:07:47 +02:00
if (typeof definitions !== 'object') return;
const context = this.getContext(contextName);
if (!context) return;
2017-04-25 22:42:52 +02:00
if (!extend) {
this.clear(contextName);
}
2017-04-25 13:08:10 +02:00
Object.keys(definitions).forEach(command => (context[command] = definitions[command]));
2017-04-24 22:07:47 +02:00
}
2017-04-25 22:42:52 +02:00
register(contextName, command, definition) {
2017-04-24 22:07:47 +02:00
if (typeof definition !== 'object') return;
const context = this.getContext(contextName);
if (!context) return;
2017-04-25 13:08:10 +02:00
context[command] = definition;
2017-04-24 22:07:47 +02:00
}
2017-04-25 22:42:52 +02:00
clear(contextName) {
2017-04-25 13:08:10 +02:00
if (!contextName) return;
this.contexts[contextName] = {};
}
run(command) {
const context = this.getCurrentContext();
2017-04-24 22:07:47 +02:00
if (!context) return;
2017-04-25 13:08:10 +02:00
const definition = context[command];
if (!definition) {
return OHIF.log.warn(`Command "${command}" not found in current context`);
}
2017-04-24 22:07:47 +02:00
2017-04-25 13:08:10 +02:00
const { action, disabled, params } = definition;
if ((_.isFunction(disabled) && disabled()) || (!_.isUndefined(disabled) && disabled)) return;
if (typeof action !== 'function') {
return OHIF.log.warn(`No action was defined for command "${command}"`);
} else {
2017-04-25 22:42:52 +02:00
const result = action(params);
Session.set('lastCommand', command);
return result;
2017-04-25 13:08:10 +02:00
}
2017-04-24 22:07:47 +02:00
}
}