Creating a hotkeys context for viewer
This commit is contained in:
parent
5e7edd91ee
commit
2f81c0e2e7
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -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() {
|
||||
1
Packages/ohif-hotkeys/client/index.js
Normal file
1
Packages/ohif-hotkeys/client/index.js
Normal file
@ -0,0 +1 @@
|
||||
import './lib';
|
||||
1
Packages/ohif-hotkeys/client/lib/index.js
Normal file
1
Packages/ohif-hotkeys/client/lib/index.js
Normal file
@ -0,0 +1 @@
|
||||
import './routes';
|
||||
14
Packages/ohif-hotkeys/client/lib/routes.js
Normal file
14
Packages/ohif-hotkeys/client/lib/routes.js
Normal 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();
|
||||
});
|
||||
@ -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
|
||||
|
||||
@ -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');
|
||||
});
|
||||
|
||||
@ -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
|
||||
};
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user