Registering tool commands

This commit is contained in:
Bruno Alves de Faria 2017-04-25 08:08:10 -03:00 committed by Erik Ziegler
parent dbbcbe6e67
commit fa7ad2c51e
7 changed files with 72 additions and 16 deletions

View File

@ -8,6 +8,7 @@
ohif:polyfill ohif:polyfill
ohif:design ohif:design
ohif:core ohif:core
ohif:commands
ohif:hotkeys ohif:hotkeys
ohif:header ohif:header
ohif:cornerstone ohif:cornerstone

View File

@ -72,6 +72,7 @@ mongo-id@1.0.6
natestrauser:select2@4.0.3 natestrauser:select2@4.0.3
npm-mongo@2.2.11_2 npm-mongo@2.2.11_2
observe-sequence@1.0.14 observe-sequence@1.0.14
ohif:commands@0.0.1
ohif:core@0.0.1 ohif:core@0.0.1
ohif:cornerstone@0.0.1 ohif:cornerstone@0.0.1
ohif:design@0.0.1 ohif:design@0.0.1

View File

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

View File

@ -5,11 +5,14 @@ import { CommandsManager } from 'meteor/ohif:commands/client/classes/CommandsMan
// Create context namespace using a ReactiveVar // Create context namespace using a ReactiveVar
const context = new ReactiveVar(null); const context = new ReactiveVar(null);
// Create commands namespace using a CommandsManager class instance
const commands = new CommandsManager(context);
// Append context namespace to OHIF namespace // Append context namespace to OHIF namespace
OHIF.context = context; OHIF.context = context;
// Create commands namespace using a CommandsManager class instance
const commands = new CommandsManager(context);
// Append commands namespace to OHIF namespace
OHIF.commands = commands;
// Export relevant objects // Export relevant objects
export { context, commands }; export { context, commands };

View File

@ -10,12 +10,12 @@ Package.onUse(function(api) {
// Meteor packages // Meteor packages
api.use([ api.use([
'ecmascript', 'ecmascript',
'reactive-var', 'reactive-var'
'reactive-dict'
]); ]);
// OHIF dependencies // OHIF dependencies
api.use('ohif:core'); api.use('ohif:core');
api.use('ohif:log');
// Main module definition // Main module definition
api.mainModule('main.js', 'client'); api.mainModule('main.js', 'client');

View File

@ -17,6 +17,7 @@ Package.onUse(function(api) {
// OHIF dependencies // OHIF dependencies
api.use('ohif:core'); api.use('ohif:core');
api.use('ohif:commands');
// Main module definition // Main module definition
api.mainModule('main.js', 'client'); api.mainModule('main.js', 'client');

View File

@ -1,5 +1,6 @@
import { Meteor } from 'meteor/meteor'; import { Meteor } from 'meteor/meteor';
import { $ } from 'meteor/jquery'; import { $ } from 'meteor/jquery';
import { _ } from 'meteor/underscore';
import { OHIF } from 'meteor/ohif:core'; import { OHIF } from 'meteor/ohif:core';
import { toolManager } from './toolManager'; import { toolManager } from './toolManager';
import { switchToImageRelative } from './switchToImageRelative'; import { switchToImageRelative } from './switchToImageRelative';
@ -50,6 +51,33 @@ Meteor.startup(function() {
// For now // For now
OHIF.viewer.hotkeys = OHIF.viewer.defaultHotkeys; OHIF.viewer.hotkeys = OHIF.viewer.defaultHotkeys;
const contextName = 'viewer';
OHIF.commands.createContext(contextName);
const registerToolCommand = (commandName, toolId) => {
OHIF.commands.registerCommand(contextName, toolId, {
name: commandName,
action: toolManager.setActiveTool,
params: toolId
});
};
const registerToolCommands = map => _.each(map, registerToolCommand);
registerToolCommands({
wwwc: 'Levels',
zoom: 'Zoom',
angle: 'Angle',
dragProbe: 'Probe',
ellipticalRoi: 'Ellipse',
magnify: 'Magnify',
annotate: 'Annotate',
stackScroll: 'Stack Scroll',
pan: 'Pan',
length: 'Length',
spine: 'Spine',
wwwcRegion: 'ROI Window'
});
OHIF.viewer.hotkeyFunctions = { OHIF.viewer.hotkeyFunctions = {
wwwc: () => toolManager.setActiveTool('wwwc'), wwwc: () => toolManager.setActiveTool('wwwc'),
zoom: () => toolManager.setActiveTool('zoom'), zoom: () => toolManager.setActiveTool('zoom'),
@ -256,6 +284,7 @@ function enableHotkeys(hotkeys) {
OHIF.hotkeys.setContext('viewer', definitions); OHIF.hotkeys.setContext('viewer', definitions);
OHIF.hotkeys.switchToContext('viewer'); OHIF.hotkeys.switchToContext('viewer');
OHIF.context.set('viewer');
} }
/** /**