Integrating hotkeys with commands

This commit is contained in:
Bruno Alves de Faria authored and Erik Ziegler committed 2017-06-14 09:19:49 +02:00
1 parent 199115a9d3
commit 1a0729336b
7 files changed
+102 -89

No files matched your search

@@ -9,39 +9,48 @@ export class HotkeysContext {
extend(definitions={}) {
if (typeof definitions !== 'object') return;
this.destroy();
Object.assign(this.definitions, definitions);
this.initialize();
Object.keys(definitions).forEach(command => {
const hotkey = definitions[command];
this.unregister(command);
this.register(command, hotkey);
this.definitions[command] = hotkey;
});
}
register(command, hotkey) {
if (!hotkey) {
return OHIF.log.warn(`No hotkey was defined for "${command}"`);
}
if (!command) {
return OHIF.log.warn(`No command was defined for hotkey "${hotkey}"`);
}
const bindingKey = `keydown.hotkey.${this.name}.${command}`;
const bind = hotkey => $(document).bind(bindingKey, hotkey, event => {
if (!this.enabled.get()) return;
OHIF.commands.run(command);
});
if (hotkey instanceof Array) {
hotkey.forEach(hotkey => bind(hotkey));
} else {
bind(hotkey);
}
}
unregister(command) {
const bindingKey = `keydown.hotkey.${this.name}.${command}`;
if (this.definitions[command]) {
$(document).unbind(bindingKey);
delete this.definitions[command];
}
}
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);
}
Object.keys(this.definitions).forEach(command => {
const hotkey = this.definitions[command];
this.register(command, hotkey);
});
}
@@ -1,4 +1,6 @@
import { ReactiveVar } from 'meteor/reactive-var';
import { Tracker } from 'meteor/tracker';
import { OHIF } from 'meteor/ohif:core';
import { HotkeysContext } from 'meteor/ohif:hotkeys/client/classes/HotkeysContext';
export class HotkeysManager {
@@ -6,6 +8,11 @@ export class HotkeysManager {
this.contexts = {};
this.currentContextName = null;
this.enabled = new ReactiveVar(true);
Tracker.autorun(() => {
const contextName = OHIF.context.get();
this.switchToContext(contextName);
});
}
disable() {
@@ -24,7 +31,7 @@ export class HotkeysManager {
return this.getContext(this.currentContextName);
}
setContext(contextName, contextDefinitions) {
set(contextName, contextDefinitions, extend=false) {
const enabled = this.enabled;
const context = new HotkeysContext(contextName, contextDefinitions, enabled);
const currentContext = this.getCurrentContext();
@@ -36,6 +43,16 @@ export class HotkeysManager {
this.contexts[contextName] = context;
}
register(contextName, command, hotkey) {
if (!command || !hotkey) return;
const context = this.getContext(contextName);
if (!context) {
this.set(contextName, {});
}
context.register(command, hotkey);
}
unsetContext(contextName) {
if (contextName === this.currentContextName) {
this.getCurrentContext().destroy();