Move ohif:hotkeys into ohif:core
This commit is contained in:
1 parent
1abd0e6f9d
commit
184c959037
10 files changed
+14
-1
No files matched your search
@@ -0,0 +1,65 @@
|
||||
import { OHIF } from 'meteor/ohif:core';
|
||||
|
||||
export class HotkeysContext {
|
||||
constructor(name, definitions, enabled) {
|
||||
this.name = name;
|
||||
this.definitions = Object.assign({}, definitions);
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
extend(definitions={}) {
|
||||
if (typeof definitions !== 'object') return;
|
||||
this.definitions = Object.assign({}, definitions);
|
||||
Object.keys(definitions).forEach(command => {
|
||||
const hotkey = definitions[command];
|
||||
this.unregister(command);
|
||||
if (hotkey) {
|
||||
this.register(command, hotkey);
|
||||
}
|
||||
|
||||
this.definitions[command] = hotkey;
|
||||
});
|
||||
}
|
||||
|
||||
register(command, hotkey) {
|
||||
if (!hotkey) {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
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(command => {
|
||||
const hotkey = this.definitions[command];
|
||||
this.register(command, hotkey);
|
||||
});
|
||||
}
|
||||
|
||||
destroy() {
|
||||
$(document).unbind(`keydown.hotkey.${this.name}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
import { ReactiveVar } from 'meteor/reactive-var';
|
||||
import { Tracker } from 'meteor/tracker';
|
||||
import { Session } from 'meteor/session';
|
||||
import { OHIF } from 'meteor/ohif:core';
|
||||
import { HotkeysContext } from 'meteor/ohif:hotkeys/client/classes/HotkeysContext';
|
||||
|
||||
export class HotkeysManager {
|
||||
constructor() {
|
||||
this.contexts = {};
|
||||
this.defaults = {};
|
||||
this.currentContextName = null;
|
||||
this.enabled = new ReactiveVar(true);
|
||||
this.retrieveFunction = null;
|
||||
this.storeFunction = null;
|
||||
this.changeObserver = new Tracker.Dependency();
|
||||
|
||||
Tracker.autorun(() => {
|
||||
const contextName = OHIF.context.get();
|
||||
|
||||
// Avoind falling in MongoDB collections reactivity
|
||||
Tracker.nonreactive(() => this.switchToContext(contextName));
|
||||
});
|
||||
}
|
||||
|
||||
setRetrieveFunction(retrieveFunction) {
|
||||
this.retrieveFunction = retrieveFunction;
|
||||
}
|
||||
|
||||
setStoreFunction(storeFunction) {
|
||||
this.storeFunction = storeFunction;
|
||||
}
|
||||
|
||||
store(contextName, definitions) {
|
||||
const storageKey = `hotkeysDefinitions.${contextName}`;
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.storeFunction) {
|
||||
this.storeFunction.call(this, storageKey, definitions).then(resolve).catch(reject);
|
||||
} else if (OHIF.user.userLoggedIn()) {
|
||||
OHIF.user.setData(storageKey, definitions).then(resolve).catch(reject);
|
||||
} else {
|
||||
const definitionsJSON = JSON.stringify(definitions);
|
||||
localStorage.setItem(storageKey, definitionsJSON);
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
retrieve(contextName) {
|
||||
const storageKey = `hotkeysDefinitions.${contextName}`;
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.retrieveFunction) {
|
||||
this.retrieveFunction(contextName).then(resolve).catch(reject);
|
||||
} else if (OHIF.user.userLoggedIn()) {
|
||||
try {
|
||||
resolve(OHIF.user.getData(storageKey));
|
||||
} catch(error) {
|
||||
reject(error);
|
||||
}
|
||||
} else {
|
||||
const definitionsJSON = localStorage.getItem(storageKey) || '';
|
||||
const definitions = JSON.parse(definitionsJSON) || undefined;
|
||||
resolve(definitions);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
disable() {
|
||||
this.enabled.set(false);
|
||||
}
|
||||
|
||||
enable() {
|
||||
this.enabled.set(true);
|
||||
}
|
||||
|
||||
getContext(contextName) {
|
||||
return this.contexts[contextName];
|
||||
}
|
||||
|
||||
getCurrentContext() {
|
||||
return this.getContext(this.currentContextName);
|
||||
}
|
||||
|
||||
load(contextName) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const context = this.getContext(contextName);
|
||||
if (!context) return reject();
|
||||
this.retrieve(contextName).then(defs => {
|
||||
const definitions = defs || this.defaults[contextName];
|
||||
if (!definitions) {
|
||||
this.changeObserver.changed();
|
||||
return reject();
|
||||
}
|
||||
|
||||
context.destroy();
|
||||
context.definitions = definitions;
|
||||
context.initialize();
|
||||
this.changeObserver.changed();
|
||||
resolve(definitions);
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
set(contextName, contextDefinitions, isDefaultDefinitions=false) {
|
||||
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;
|
||||
if (isDefaultDefinitions) {
|
||||
this.defaults[contextName] = contextDefinitions;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
delete this.contexts[contextName];
|
||||
delete this.defaults[contextName];
|
||||
}
|
||||
|
||||
resetDefaults(contextName) {
|
||||
const context = this.getContext(contextName);
|
||||
const definitions = this.defaults[contextName];
|
||||
if (!context || !definitions) return;
|
||||
context.extend(definitions);
|
||||
return this.store(contextName, definitions);
|
||||
}
|
||||
|
||||
switchToContext(contextName) {
|
||||
const currentContext = this.getCurrentContext();
|
||||
if (currentContext) {
|
||||
currentContext.destroy();
|
||||
}
|
||||
|
||||
const newContext = this.contexts[contextName];
|
||||
if (!newContext) return;
|
||||
|
||||
this.currentContextName = contextName;
|
||||
newContext.initialize();
|
||||
this.load(contextName).catch(() => {});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user