Creating a hotkeys context for viewer
This commit is contained in:
1 parent
5e7edd91ee
commit
2f81c0e2e7
9 files changed
+53
-91
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import { OHIF } from 'meteor/ohif:core';
|
||||
|
||||
export class HotkeysContext {
|
||||
constructor(name, definitions, enabled) {
|
||||
this.name = name;
|
||||
this.definitions = definitions;
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
extend(definitions={}) {
|
||||
if (typeof definitions !== 'object') return;
|
||||
this.destroy();
|
||||
Object.assign(this.definitions, definitions);
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
initialize() {
|
||||
Object.keys(this.definitions).forEach(definitionKey => {
|
||||
const { hotkey, action, disabled } = this.definitions[definitionKey];
|
||||
let message;
|
||||
if (!hotkey) {
|
||||
message = `No hotkey was defined for "${definitionKey}"`;
|
||||
} else if (!action) {
|
||||
message = `No action was defined for "${definitionKey}" using "${hotkey}" hotkey`;
|
||||
}
|
||||
|
||||
if (message) {
|
||||
return OHIF.log.warn(message);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
action(event);
|
||||
});
|
||||
|
||||
if (hotkey instanceof Array) {
|
||||
hotkey.forEach(hotkey => bindHotkey(hotkey));
|
||||
} else {
|
||||
bindHotkey(hotkey);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
destroy() {
|
||||
$(document).unbind(`keydown.hotkey.${this.name}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import { ReactiveVar } from 'meteor/reactive-var';
|
||||
import { HotkeysContext } from 'meteor/ohif:hotkeys/client/classes/HotkeysContext';
|
||||
|
||||
export class HotkeysManager {
|
||||
constructor() {
|
||||
this.contexts = {};
|
||||
this.currentContextName = null;
|
||||
this.enabled = new ReactiveVar(true);
|
||||
}
|
||||
|
||||
disable() {
|
||||
this.enabled.set(false);
|
||||
}
|
||||
|
||||
enable() {
|
||||
this.enabled.set(true);
|
||||
}
|
||||
|
||||
getContext(contextName) {
|
||||
return this.contexts[contextName];
|
||||
}
|
||||
|
||||
getCurrentContext() {
|
||||
return this.getContext(this.currentContextName);
|
||||
}
|
||||
|
||||
setContext(contextName, contextDefinitions) {
|
||||
const enabled = this.enabled;
|
||||
const context = new HotkeysContext(contextName, contextDefinitions, enabled);
|
||||
const currentContext = this.getCurrentContext();
|
||||
if (currentContext && currentContext.name === contextName) {
|
||||
currentContext.destroy();
|
||||
context.initialize();
|
||||
}
|
||||
|
||||
this.contexts[contextName] = context;
|
||||
}
|
||||
|
||||
unsetContext(contextName) {
|
||||
if (contextName === this.currentContextName) {
|
||||
this.getCurrentContext().destroy();
|
||||
}
|
||||
|
||||
delete this.contexts[contextName];
|
||||
}
|
||||
|
||||
switchToContext(contextName) {
|
||||
const currentContext = this.getCurrentContext();
|
||||
if (currentContext) {
|
||||
currentContext.destroy();
|
||||
}
|
||||
|
||||
const newContext = this.contexts[contextName];
|
||||
if (!newContext) return;
|
||||
|
||||
this.currentContextName = contextName;
|
||||
newContext.initialize();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user