Creating a hotkeys context for viewer

This commit is contained in:
Bruno Alves de Faria 2017-04-21 23:04:02 -03:00 committed by Erik Ziegler
parent 5e7edd91ee
commit 2f81c0e2e7
9 changed files with 53 additions and 91 deletions

View File

@ -28,7 +28,7 @@ export class HotkeysContext {
return OHIF.log.warn(message); 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 (!this.enabled.get()) return;
if (typeof disabled !== undefined) { if (typeof disabled !== undefined) {
if ((typeof disabled === 'function' && disabled()) || disabled) return; if ((typeof disabled === 'function' && disabled()) || disabled) return;
@ -36,6 +36,12 @@ export class HotkeysContext {
action(event); action(event);
}); });
if (hotkey instanceof Array) {
hotkey.forEach(hotkey => bindHotkey(hotkey));
} else {
bindHotkey(hotkey);
}
}); });
} }

View File

@ -1,5 +1,5 @@
import { ReactiveVar } from 'meteor/reactive-var'; 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 { export class HotkeysManager {
constructor() { constructor() {

View File

@ -0,0 +1 @@
import './lib';

View File

@ -0,0 +1 @@
import './routes';

View File

@ -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();
});

View File

@ -1,5 +1,5 @@
import { OHIF } from 'meteor/ohif:core'; 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 * Create hotkeys namespace using a HotkeysManager class instance

View File

@ -8,9 +8,19 @@ Package.onUse(function(api) {
api.versionsFrom('1.4'); api.versionsFrom('1.4');
// Meteor packages // Meteor packages
api.use('ecmascript'); api.use([
api.use('reactive-dict'); 'ecmascript',
'reactive-var',
'session',
'iron:router'
]);
// OHIF dependencies
api.use('ohif:core');
// Main module definition // Main module definition
api.mainModule('main.js', 'client'); api.mainModule('main.js', 'client');
// Client imports
api.addFiles('client/index.js', 'client');
}); });

View File

@ -63,6 +63,11 @@ Meteor.startup(function() {
length: () => toolManager.setActiveTool('length'), length: () => toolManager.setActiveTool('length'),
spine: () => toolManager.setActiveTool('spine'), spine: () => toolManager.setActiveTool('spine'),
wwwcRegion: () => toolManager.setActiveTool('wwwcRegion'), 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() { zoomIn() {
const button = document.getElementById('zoomIn'); const button = document.getElementById('zoomIn');
@ -212,38 +217,6 @@ function setOHIFHotkeys(hotkeys) {
OHIF.viewer.hotkeys = 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 * Add an active class to a button for 100ms only
* to give the impressiont the button was pressed. * to give the impressiont the button was pressed.
@ -263,44 +236,6 @@ function flashButton(button) {
}, 100); }, 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 * Binds all hotkeys keydown events to the tasks defined in
* OHIF.viewer.hotkeys or a given param * OHIF.viewer.hotkeys or a given param
@ -309,21 +244,18 @@ function bindHotkey(hotkey, task) {
function enableHotkeys(hotkeys) { function enableHotkeys(hotkeys) {
const viewerHotkeys = hotkeys || OHIF.viewer.hotkeys; const viewerHotkeys = hotkeys || OHIF.viewer.hotkeys;
$(document).unbind('keydown'); const definitions = {};
Object.keys(viewerHotkeys).forEach(definition => {
Object.keys(viewerHotkeys).forEach(function(task) { const hotkey = viewerHotkeys[definition];
const taskHotkeys = viewerHotkeys[task]; const action = OHIF.viewer.hotkeyFunctions[definition];
if (!taskHotkeys || !taskHotkeys.length) { definitions[definition] = {
return; hotkey,
} action
};
if (taskHotkeys instanceof Array) {
taskHotkeys.forEach(hotkey => bindHotkey(hotkey, task));
} else {
// taskHotkeys represents a single key
bindHotkey(taskHotkeys, task);
}
}); });
OHIF.hotkeys.setContext('viewer', definitions);
OHIF.hotkeys.switchToContext('viewer');
} }
/** /**
@ -333,7 +265,6 @@ function enableHotkeys(hotkeys) {
const hotkeyUtils = { const hotkeyUtils = {
setOHIFRefLines, /* @TODO: find a better place for this... */ setOHIFRefLines, /* @TODO: find a better place for this... */
setOHIFHotkeys, setOHIFHotkeys,
mergeHotkeys,
enableHotkeys enableHotkeys
}; };

View File

@ -83,7 +83,6 @@ Package.onUse(function(api) {
api.addAssets(assets, 'client'); api.addAssets(assets, 'client');
// TODO: Use NPM depends for these // TODO: Use NPM depends for these
api.addFiles('client/compatibility/jquery.hotkeys.js', 'client', { api.addFiles('client/compatibility/jquery.hotkeys.js', 'client', {
bare: true bare: true