From fa7ad2c51e9e2629817b76dce2b972a0aaca453f Mon Sep 17 00:00:00 2001 From: Bruno Alves de Faria Date: Tue, 25 Apr 2017 08:08:10 -0300 Subject: [PATCH] Registering tool commands --- OHIFViewer/.meteor/packages | 1 + OHIFViewer/.meteor/versions | 1 + .../client/classes/CommandsManager.js | 43 ++++++++++++++----- Packages/ohif-commands/main.js | 9 ++-- Packages/ohif-commands/package.js | 4 +- Packages/ohif-hotkeys/package.js | 1 + .../ohif-viewerbase/client/lib/hotkeyUtils.js | 29 +++++++++++++ 7 files changed, 72 insertions(+), 16 deletions(-) diff --git a/OHIFViewer/.meteor/packages b/OHIFViewer/.meteor/packages index 8d550978f..d4d38fe64 100644 --- a/OHIFViewer/.meteor/packages +++ b/OHIFViewer/.meteor/packages @@ -8,6 +8,7 @@ ohif:polyfill ohif:design ohif:core +ohif:commands ohif:hotkeys ohif:header ohif:cornerstone diff --git a/OHIFViewer/.meteor/versions b/OHIFViewer/.meteor/versions index 2910e856b..bcaedfb10 100644 --- a/OHIFViewer/.meteor/versions +++ b/OHIFViewer/.meteor/versions @@ -72,6 +72,7 @@ mongo-id@1.0.6 natestrauser:select2@4.0.3 npm-mongo@2.2.11_2 observe-sequence@1.0.14 +ohif:commands@0.0.1 ohif:core@0.0.1 ohif:cornerstone@0.0.1 ohif:design@0.0.1 diff --git a/Packages/ohif-commands/client/classes/CommandsManager.js b/Packages/ohif-commands/client/classes/CommandsManager.js index 179ba4d3e..a5c253fbe 100644 --- a/Packages/ohif-commands/client/classes/CommandsManager.js +++ b/Packages/ohif-commands/client/classes/CommandsManager.js @@ -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); + } } } diff --git a/Packages/ohif-commands/main.js b/Packages/ohif-commands/main.js index e1328cf0a..e78b61acf 100644 --- a/Packages/ohif-commands/main.js +++ b/Packages/ohif-commands/main.js @@ -5,11 +5,14 @@ import { CommandsManager } from 'meteor/ohif:commands/client/classes/CommandsMan // Create context namespace using a ReactiveVar const context = new ReactiveVar(null); -// Create commands namespace using a CommandsManager class instance -const commands = new CommandsManager(context); - // Append context namespace to OHIF namespace 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 { context, commands }; diff --git a/Packages/ohif-commands/package.js b/Packages/ohif-commands/package.js index aacd09aaf..f2eac0a1d 100644 --- a/Packages/ohif-commands/package.js +++ b/Packages/ohif-commands/package.js @@ -10,12 +10,12 @@ Package.onUse(function(api) { // Meteor packages api.use([ 'ecmascript', - 'reactive-var', - 'reactive-dict' + 'reactive-var' ]); // OHIF dependencies api.use('ohif:core'); + api.use('ohif:log'); // Main module definition api.mainModule('main.js', 'client'); diff --git a/Packages/ohif-hotkeys/package.js b/Packages/ohif-hotkeys/package.js index b923a7aac..4718480b2 100644 --- a/Packages/ohif-hotkeys/package.js +++ b/Packages/ohif-hotkeys/package.js @@ -17,6 +17,7 @@ Package.onUse(function(api) { // OHIF dependencies api.use('ohif:core'); + api.use('ohif:commands'); // Main module definition api.mainModule('main.js', 'client'); diff --git a/Packages/ohif-viewerbase/client/lib/hotkeyUtils.js b/Packages/ohif-viewerbase/client/lib/hotkeyUtils.js index b312cc054..1df8c0c3c 100644 --- a/Packages/ohif-viewerbase/client/lib/hotkeyUtils.js +++ b/Packages/ohif-viewerbase/client/lib/hotkeyUtils.js @@ -1,5 +1,6 @@ import { Meteor } from 'meteor/meteor'; import { $ } from 'meteor/jquery'; +import { _ } from 'meteor/underscore'; import { OHIF } from 'meteor/ohif:core'; import { toolManager } from './toolManager'; import { switchToImageRelative } from './switchToImageRelative'; @@ -50,6 +51,33 @@ Meteor.startup(function() { // For now 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 = { wwwc: () => toolManager.setActiveTool('wwwc'), zoom: () => toolManager.setActiveTool('zoom'), @@ -256,6 +284,7 @@ function enableHotkeys(hotkeys) { OHIF.hotkeys.setContext('viewer', definitions); OHIF.hotkeys.switchToContext('viewer'); + OHIF.context.set('viewer'); } /**