From 2f81c0e2e7dbb063b3e5d1a69b77abbff467e030 Mon Sep 17 00:00:00 2001 From: Bruno Alves de Faria Date: Fri, 21 Apr 2017 23:04:02 -0300 Subject: [PATCH] Creating a hotkeys context for viewer --- .../client/{ => classes}/HotkeysContext.js | 8 +- .../client/{ => classes}/HotkeysManager.js | 2 +- Packages/ohif-hotkeys/client/index.js | 1 + Packages/ohif-hotkeys/client/lib/index.js | 1 + Packages/ohif-hotkeys/client/lib/routes.js | 14 +++ Packages/ohif-hotkeys/main.js | 2 +- Packages/ohif-hotkeys/package.js | 14 ++- .../ohif-viewerbase/client/lib/hotkeyUtils.js | 101 +++--------------- Packages/ohif-viewerbase/package.js | 1 - 9 files changed, 53 insertions(+), 91 deletions(-) rename Packages/ohif-hotkeys/client/{ => classes}/HotkeysContext.js (82%) rename Packages/ohif-hotkeys/client/{ => classes}/HotkeysManager.js (94%) create mode 100644 Packages/ohif-hotkeys/client/index.js create mode 100644 Packages/ohif-hotkeys/client/lib/index.js create mode 100644 Packages/ohif-hotkeys/client/lib/routes.js diff --git a/Packages/ohif-hotkeys/client/HotkeysContext.js b/Packages/ohif-hotkeys/client/classes/HotkeysContext.js similarity index 82% rename from Packages/ohif-hotkeys/client/HotkeysContext.js rename to Packages/ohif-hotkeys/client/classes/HotkeysContext.js index 0ea5aebdc..99cc84a4b 100644 --- a/Packages/ohif-hotkeys/client/HotkeysContext.js +++ b/Packages/ohif-hotkeys/client/classes/HotkeysContext.js @@ -28,7 +28,7 @@ export class HotkeysContext { return OHIF.log.warn(message); } - $(document).bind(`keydown.hotkey.${this.name}`, hotkey, event => { + const bindHotkey = hotkey => $(document).bind(`keydown.hotkey.${this.name}`, hotkey, event => { if (!this.enabled.get()) return; if (typeof disabled !== undefined) { if ((typeof disabled === 'function' && disabled()) || disabled) return; @@ -36,6 +36,12 @@ export class HotkeysContext { action(event); }); + + if (hotkey instanceof Array) { + hotkey.forEach(hotkey => bindHotkey(hotkey)); + } else { + bindHotkey(hotkey); + } }); } diff --git a/Packages/ohif-hotkeys/client/HotkeysManager.js b/Packages/ohif-hotkeys/client/classes/HotkeysManager.js similarity index 94% rename from Packages/ohif-hotkeys/client/HotkeysManager.js rename to Packages/ohif-hotkeys/client/classes/HotkeysManager.js index 14d653044..591e3ef69 100644 --- a/Packages/ohif-hotkeys/client/HotkeysManager.js +++ b/Packages/ohif-hotkeys/client/classes/HotkeysManager.js @@ -1,5 +1,5 @@ import { ReactiveVar } from 'meteor/reactive-var'; -import { HotkeysContext } from 'meteor/ohif:hotkeys/client/HotkeysContext'; +import { HotkeysContext } from 'meteor/ohif:hotkeys/client/classes/HotkeysContext'; export class HotkeysManager { constructor() { diff --git a/Packages/ohif-hotkeys/client/index.js b/Packages/ohif-hotkeys/client/index.js new file mode 100644 index 000000000..cf327e497 --- /dev/null +++ b/Packages/ohif-hotkeys/client/index.js @@ -0,0 +1 @@ +import './lib'; diff --git a/Packages/ohif-hotkeys/client/lib/index.js b/Packages/ohif-hotkeys/client/lib/index.js new file mode 100644 index 000000000..86c73e446 --- /dev/null +++ b/Packages/ohif-hotkeys/client/lib/index.js @@ -0,0 +1 @@ +import './routes'; diff --git a/Packages/ohif-hotkeys/client/lib/routes.js b/Packages/ohif-hotkeys/client/lib/routes.js new file mode 100644 index 000000000..7efc2bede --- /dev/null +++ b/Packages/ohif-hotkeys/client/lib/routes.js @@ -0,0 +1,14 @@ +import { Router } from 'meteor/iron:router'; +import { Session } from 'meteor/session'; +import { hotkeys } from 'meteor/ohif:hotkeys'; + +Router.onBeforeAction(function() { + const lastRoute = Session.get('lastRoute'); + const currentRoute = this.router.current().route.getName(); + if (currentRoute !== lastRoute) { + hotkeys.switchToContext(null); + Session.set('lastRoute', currentRoute); + } + + this.next(); +}); diff --git a/Packages/ohif-hotkeys/main.js b/Packages/ohif-hotkeys/main.js index 3a4fa72a1..6d925f26c 100644 --- a/Packages/ohif-hotkeys/main.js +++ b/Packages/ohif-hotkeys/main.js @@ -1,5 +1,5 @@ import { OHIF } from 'meteor/ohif:core'; -import { HotkeysManager } from 'meteor/ohif:hotkeys/client/HotkeysManager'; +import { HotkeysManager } from 'meteor/ohif:hotkeys/client/classes/HotkeysManager'; /** * Create hotkeys namespace using a HotkeysManager class instance diff --git a/Packages/ohif-hotkeys/package.js b/Packages/ohif-hotkeys/package.js index 2115b1c23..b923a7aac 100644 --- a/Packages/ohif-hotkeys/package.js +++ b/Packages/ohif-hotkeys/package.js @@ -8,9 +8,19 @@ Package.onUse(function(api) { api.versionsFrom('1.4'); // Meteor packages - api.use('ecmascript'); - api.use('reactive-dict'); + api.use([ + 'ecmascript', + 'reactive-var', + 'session', + 'iron:router' + ]); + + // OHIF dependencies + api.use('ohif:core'); // Main module definition api.mainModule('main.js', 'client'); + + // Client imports + api.addFiles('client/index.js', 'client'); }); diff --git a/Packages/ohif-viewerbase/client/lib/hotkeyUtils.js b/Packages/ohif-viewerbase/client/lib/hotkeyUtils.js index c4b6f25f2..b312cc054 100644 --- a/Packages/ohif-viewerbase/client/lib/hotkeyUtils.js +++ b/Packages/ohif-viewerbase/client/lib/hotkeyUtils.js @@ -63,6 +63,11 @@ Meteor.startup(function() { length: () => toolManager.setActiveTool('length'), spine: () => toolManager.setActiveTool('spine'), wwwcRegion: () => toolManager.setActiveTool('wwwcRegion'), + WLPresetSoftTissue: () => WLPresets.applyWLPresetToActiveElement('SoftTissue'), + WLPresetLung: () => WLPresets.applyWLPresetToActiveElement('Lung'), + WLPresetLiver: () => WLPresets.applyWLPresetToActiveElement('Liver'), + WLPresetBone: () => WLPresets.applyWLPresetToActiveElement('Bone'), + WLPresetBrain: () => WLPresets.applyWLPresetToActiveElement('Brain'), zoomIn() { const button = document.getElementById('zoomIn'); @@ -212,38 +217,6 @@ function setOHIFHotkeys(hotkeys) { OHIF.viewer.hotkeys = hotkeys; } -/** - * Global function to merge different hotkeys configurations - * but avoiding conflicts between different keys with same action - * When this occurs, it will delete the action from OHIF's configuration - * So if you want to keep all OHIF's actions, use an unused-ohif-key - * Used for compatibility with others systems only - * - * @param hotkeysActions {object} Object with actions map - * @return {object} - */ -function mergeHotkeys(hotkeysActions) { - // Merge hotkeys, overriding OHIF's settings - const mergedHotkeys = Object.assign({}, OHIF.viewer.defaultHotkeys, hotkeysActions); - - const defaultHotkeys = OHIF.viewer.defaultHotkeys; - const hotkeysKeys = Object.keys(hotkeysActions); - - // Check for conflicts with same keys but different actions - Object.keys(defaultHotkeys).forEach(ohifAction => { - hotkeysKeys.forEach(definedAction => { - // Different action but same key: - // Remove action from merge if is not in "hotkeysActions" - // If it is, it's already merged so nothing to do - if (ohifAction !== definedAction && hotkeysActions[definedAction] === defaultHotkeys[ohifAction] && !hotkeysActions[ohifAction]) { - delete mergedHotkeys[ohifAction]; - } - }); - }); - - return mergedHotkeys; -} - /** * Add an active class to a button for 100ms only * to give the impressiont the button was pressed. @@ -263,44 +236,6 @@ function flashButton(button) { }, 100); } -/** - * Binds a task to a hotkey keydown event - * @param {String} hotkey keyboard key - * @param {String} task task function name - */ -function bindHotkey(hotkey, task) { - const hotkeyFunctions = OHIF.viewer.hotkeyFunctions; - - // Only bind defined, non-empty HotKeys - if (!hotkey || hotkey === '') { - return; - } - - let fn; - if (task.indexOf('WLPreset') > -1) { - const presetName = task.replace('WLPreset', ''); - fn = function() { - WLPresets.applyWLPresetToActiveElement(presetName); - }; - } else { - fn = hotkeyFunctions[task]; - - // If the function doesn't exist in the - // hotkey function list, try the viewer-specific function list - if (!fn && OHIF.viewer && OHIF.viewer.functionList) { - fn = OHIF.viewer.functionList[task]; - } - } - - if (!fn) { - return; - } - - const hotKeyForBinding = hotkey.toLowerCase(); - - $(document).bind('keydown', hotKeyForBinding, fn); -} - /** * Binds all hotkeys keydown events to the tasks defined in * OHIF.viewer.hotkeys or a given param @@ -309,21 +244,18 @@ function bindHotkey(hotkey, task) { function enableHotkeys(hotkeys) { const viewerHotkeys = hotkeys || OHIF.viewer.hotkeys; - $(document).unbind('keydown'); - - Object.keys(viewerHotkeys).forEach(function(task) { - const taskHotkeys = viewerHotkeys[task]; - if (!taskHotkeys || !taskHotkeys.length) { - return; - } - - if (taskHotkeys instanceof Array) { - taskHotkeys.forEach(hotkey => bindHotkey(hotkey, task)); - } else { - // taskHotkeys represents a single key - bindHotkey(taskHotkeys, task); - } + const definitions = {}; + Object.keys(viewerHotkeys).forEach(definition => { + const hotkey = viewerHotkeys[definition]; + const action = OHIF.viewer.hotkeyFunctions[definition]; + definitions[definition] = { + hotkey, + action + }; }); + + OHIF.hotkeys.setContext('viewer', definitions); + OHIF.hotkeys.switchToContext('viewer'); } /** @@ -333,7 +265,6 @@ function enableHotkeys(hotkeys) { const hotkeyUtils = { setOHIFRefLines, /* @TODO: find a better place for this... */ setOHIFHotkeys, - mergeHotkeys, enableHotkeys }; diff --git a/Packages/ohif-viewerbase/package.js b/Packages/ohif-viewerbase/package.js index 36dc00a6a..05a82eb91 100644 --- a/Packages/ohif-viewerbase/package.js +++ b/Packages/ohif-viewerbase/package.js @@ -83,7 +83,6 @@ Package.onUse(function(api) { api.addAssets(assets, 'client'); - // TODO: Use NPM depends for these api.addFiles('client/compatibility/jquery.hotkeys.js', 'client', { bare: true