Registering tool commands

This commit is contained in:
Bruno Alves de Faria authored and Erik Ziegler committed 2017-06-14 09:19:49 +02:00
1 parent dbbcbe6e67
commit fa7ad2c51e
7 files changed
+72 -16

No files matched your search

@@ -1,5 +1,5 @@
import { ReactiveDict } from 'meteor/reactive-dict';
import { Tracker } from 'meteor/tracker';
import { _ } from 'meteor/underscore';
import { OHIF } from 'meteor/ohif:core';
export class CommandsManager {
@@ -16,22 +16,28 @@ export class CommandsManager {
getContext(contextName) {
const context = this.contexts[contextName];
if (!context) {
OHIF.log.warn(`No context found with name "${contextName}"`);
return OHIF.log.warn(`No context found with name "${contextName}"`);
}
return context;
}
getCurrentContext(contextName) {
return this.getContext(OHIF.context.get());
getCurrentContext() {
const contextName = OHIF.context.get();
if (!contextName) {
return OHIF.log.warn('There is no selected context');
}
return this.getContext(contextName);
}
createContext(contextName) {
if (!contextName) return;
if (this.contexts[contextName]) {
return this.unsetCommands(contextName);
}
this.contexts[contextName] = new ReactiveDict();
this.contexts[contextName] = {};
}
setCommands(contextName, definitions) {
@@ -40,21 +46,36 @@ export class CommandsManager {
if (!context) return;
this.unsetCommands(contextName);
Object.keys(definitions).forEach(key => context.set(key, definitions[key]));
Object.keys(definitions).forEach(command => (context[command] = definitions[command]));
}
registerCommand(contextName, key, definition) {
registerCommand(contextName, command, definition) {
if (typeof definition !== 'object') return;
const context = this.getContext(contextName);
if (!context) return;
context.set(key, definition);
context[command] = definition;
}
unsetCommands(contextName) {
const context = this.getContext(contextName);
if (!context) return;
if (!contextName) return;
this.contexts[contextName] = {};
}
context.clear();
run(command) {
const context = this.getCurrentContext();
if (!context) return;
const definition = context[command];
if (!definition) {
return OHIF.log.warn(`Command "${command}" not found in current context`);
}
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 {
return action(params);
}
}
}