Creating ohif:hotkeys package

This commit is contained in:
Bruno Alves de Faria 2017-04-21 20:30:29 -03:00 committed by Erik Ziegler
parent f43e0d45c7
commit 5e7edd91ee
7 changed files with 157 additions and 13 deletions

View File

@ -8,6 +8,7 @@
ohif:polyfill
ohif:design
ohif:core
ohif:hotkeys
ohif:header
ohif:cornerstone
ohif:viewerbase

View File

@ -72,15 +72,16 @@ mongo-id@1.0.6
natestrauser:select2@4.0.3
npm-mongo@2.2.11_2
observe-sequence@1.0.14
ohif:polyfill@0.0.1
ohif:core@0.0.1
ohif:cornerstone@0.0.1
ohif:design@0.0.1
ohif:dicom-services@0.0.1
ohif:hanging-protocols@0.0.1
ohif:header@0.0.1
ohif:hotkeys@0.0.1
ohif:log@0.0.1
ohif:metadata@0.0.1
ohif:polyfill@0.0.1
ohif:study-list@0.0.1
ohif:themes@0.0.1
ohif:themes-common@0.0.1

View File

@ -0,0 +1,45 @@
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);
}
$(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);
});
});
}
destroy() {
$(document).unbind(`keydown.hotkey.${this.name}`);
}
}

View File

@ -0,0 +1,59 @@
import { ReactiveVar } from 'meteor/reactive-var';
import { HotkeysContext } from 'meteor/ohif:hotkeys/client/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();
}
}

View File

@ -0,0 +1,18 @@
import { OHIF } from 'meteor/ohif:core';
import { HotkeysManager } from 'meteor/ohif:hotkeys/client/HotkeysManager';
/**
* Create hotkeys namespace using a HotkeysManager class instance
*/
const hotkeys = new HotkeysManager();
/**
* Append hotkeys namespace to OHIF namespace
*/
OHIF.hotkeys = hotkeys;
/**
* Export relevant objects
*/
export { hotkeys };

View File

@ -0,0 +1,16 @@
Package.describe({
name: 'ohif:hotkeys',
summary: 'OHIF hotkeys management',
version: '0.0.1'
});
Package.onUse(function(api) {
api.versionsFrom('1.4');
// Meteor packages
api.use('ecmascript');
api.use('reactive-dict');
// Main module definition
api.mainModule('main.js', 'client');
});

View File

@ -7,19 +7,23 @@ Package.describe({
Package.onUse(function(api) {
api.versionsFrom('1.4');
api.use('ecmascript');
api.use('standard-app-packages');
api.use('http');
api.use('jquery');
api.use('stylus');
api.use('momentjs:moment');
api.use('validatejs');
api.use('u2622:persistent-session');
api.use(['ecmascript',
'standard-app-packages',
'http',
'jquery',
'stylus',
'momentjs:moment',
'validatejs',
'u2622:persistent-session'
]);
// Our custom packages
api.use('ohif:design');
api.use('ohif:core');
api.use('ohif:log');
// OHIF dependencies
api.use([
'ohif:design',
'ohif:core',
'ohif:hotkeys',
'ohif:log'
]);
const assets = [
'assets/icons.svg',