Adding reset to defaults button

This commit is contained in:
Bruno Alves de Faria 2017-04-28 16:05:19 -03:00 committed by Erik Ziegler
parent a77838d550
commit 22f1e659fa
7 changed files with 94 additions and 40 deletions

View File

@ -3,12 +3,13 @@ import { OHIF } from 'meteor/ohif:core';
export class HotkeysContext { export class HotkeysContext {
constructor(name, definitions, enabled) { constructor(name, definitions, enabled) {
this.name = name; this.name = name;
this.definitions = definitions; this.definitions = Object.assign({}, definitions);
this.enabled = enabled; this.enabled = enabled;
} }
extend(definitions={}) { extend(definitions={}) {
if (typeof definitions !== 'object') return; if (typeof definitions !== 'object') return;
this.definitions = Object.assign({}, definitions);
Object.keys(definitions).forEach(command => { Object.keys(definitions).forEach(command => {
const hotkey = definitions[command]; const hotkey = definitions[command];
this.unregister(command); this.unregister(command);

View File

@ -8,10 +8,12 @@ import { HotkeysContext } from 'meteor/ohif:hotkeys/client/classes/HotkeysContex
export class HotkeysManager { export class HotkeysManager {
constructor(retrieveFunction, storeFunction) { constructor(retrieveFunction, storeFunction) {
this.contexts = {}; this.contexts = {};
this.defaults = {};
this.currentContextName = null; this.currentContextName = null;
this.enabled = new ReactiveVar(true); this.enabled = new ReactiveVar(true);
this.retrieveFunction = retrieveFunction; this.retrieveFunction = retrieveFunction;
this.storeFunction = storeFunction; this.storeFunction = storeFunction;
this.changeObserver = new Tracker.Dependency();
Tracker.autorun(() => { Tracker.autorun(() => {
const contextName = OHIF.context.get(); const contextName = OHIF.context.get();
@ -21,24 +23,33 @@ export class HotkeysManager {
store(contextName, definitions) { store(contextName, definitions) {
const storageKey = `hotkeysDefinitions.${contextName}`; const storageKey = `hotkeysDefinitions.${contextName}`;
if (this.storeFunction) { return new Promise((resolve, reject) => {
this.storeFunction(contextName, definitions); if (this.storeFunction) {
} else if (Meteor.userId()) { this.storeFunction(contextName, definitions).then(resolve).catch(reject);
OHIF.user.setData(storageKey, definitions); } else if (Meteor.userId()) {
} else { OHIF.user.setData(storageKey, definitions).then(resolve).catch(reject);
Session.setPersistent(storageKey, definitions); } else {
} Session.setPersistent(storageKey, definitions);
resolve();
}
});
} }
retrieve(contextName) { retrieve(contextName) {
const storageKey = `hotkeysDefinitions.${contextName}`; const storageKey = `hotkeysDefinitions.${contextName}`;
if (this.retrieveFunction) { return new Promise((resolve, reject) => {
return this.retrieveFunction(contextName); if (this.retrieveFunction) {
} else if (Meteor.userId()) { this.retrieveFunction(contextName).then(resolve).catch(reject);
return OHIF.user.getData(storageKey); } else if (Meteor.userId()) {
} else { try {
return Session.get(storageKey); resolve(OHIF.user.getData(storageKey));
} } catch(error) {
reject(error);
}
} else {
resolve(Session.get(storageKey));
}
});
} }
disable() { disable() {
@ -58,14 +69,19 @@ export class HotkeysManager {
} }
load(contextName) { load(contextName) {
const context = this.getContext(contextName); return new Promise((resolve, reject) => {
if (!context) return; const context = this.getContext(contextName);
const definitions = this.retrieve(contextName); if (!context) return;
if (!definitions) return; this.retrieve(contextName).then(definitions => {
context.extend(definitions); if (!definitions) return reject();
context.extend(definitions);
this.changeObserver.changed();
resolve(definitions);
}).catch(reject);
});
} }
set(contextName, contextDefinitions) { set(contextName, contextDefinitions, isDefaultDefinitions=false) {
const enabled = this.enabled; const enabled = this.enabled;
const context = new HotkeysContext(contextName, contextDefinitions, enabled); const context = new HotkeysContext(contextName, contextDefinitions, enabled);
const currentContext = this.getCurrentContext(); const currentContext = this.getCurrentContext();
@ -75,6 +91,9 @@ export class HotkeysManager {
} }
this.contexts[contextName] = context; this.contexts[contextName] = context;
if (isDefaultDefinitions) {
this.defaults[contextName] = contextDefinitions;
}
} }
register(contextName, command, hotkey) { register(contextName, command, hotkey) {
@ -93,6 +112,15 @@ export class HotkeysManager {
} }
delete this.contexts[contextName]; delete this.contexts[contextName];
delete this.defaults[contextName];
}
resetDefauls(contextName) {
const context = this.getContext(contextName);
const definitions = this.defaults[contextName];
if (!context || !definitions) return;
context.extend(definitions);
return this.store(contextName, definitions).then(this.changeObserver.changed);
} }
switchToContext(contextName) { switchToContext(contextName) {

View File

@ -4,6 +4,9 @@
{{>inputText (extend hotkeyInputInformation class='hotkey')}} {{>inputText (extend hotkeyInputInformation class='hotkey')}}
{{/each}} {{/each}}
<hr> <hr>
{{#button class='btn btn-primary' action='save'}}Save{{/button}} <div class="clearfix">
{{#button class='btn btn-primary pull-right' action='save'}}Save{{/button}}
{{#button class='btn btn-secondary pull-right m-r-1' action='resetDefaults'}}Reset to Defaults{{/button}}
</div>
{{/form}} {{/form}}
</template> </template>

View File

@ -12,7 +12,19 @@ Template.hotkeysForm.onCreated(() => {
const { contextName } = instance.data; const { contextName } = instance.data;
const form = instance.$('form').first().data('component'); const form = instance.$('form').first().data('component');
const definitions = form.value(); const definitions = form.value();
OHIF.hotkeys.store(contextName, definitions); return OHIF.hotkeys.store(contextName, definitions);
},
resetDefaults() {
const { contextName } = instance.data;
const dialogOptions = {
title: 'Reset Shortcuts to Default',
message: 'Are you sure you want to reset all the shortcuts to their defaults?'
};
return OHIF.ui.showDialog('dialogConfirm', dialogOptions).then(() => {
return OHIF.hotkeys.resetDefauls(contextName);
});
} }
}; };
@ -100,7 +112,7 @@ Template.hotkeysForm.events({
Template.hotkeysForm.helpers({ Template.hotkeysForm.helpers({
getHotkeyInputInformationList() { getHotkeyInputInformationList() {
OHIF.context.dep.depend(); OHIF.hotkeys.changeObserver.depend();
const instance = Template.instance(); const instance = Template.instance();
const { contextName } = instance.data; const { contextName } = instance.data;
const hotkeysInputInformation = []; const hotkeysInputInformation = [];

View File

@ -26,9 +26,21 @@ OHIF.user.getData = key => {
// Store the persistent data by giving a key and a value to store // Store the persistent data by giving a key and a value to store
OHIF.user.setData = (key, value) => { OHIF.user.setData = (key, value) => {
// Check if there is an user logged in return new Promise((resolve, reject) => {
OHIF.user.validate(); try {
// Check if there is an user logged in
OHIF.user.validate();
} catch(error) {
reject(error);
}
// Call the update method on server-side // Call the update method on server-side
Meteor.call('ohif.user.data.set', key, value); Meteor.call('ohif.user.data.set', key, value, error => {
if (error) {
reject(error);
}
resolve();
});
});
}; };

View File

@ -85,6 +85,14 @@
border-bottom-color: #23557f border-bottom-color: #23557f
border-width: 10px border-width: 10px
button.btn-secondary
background-color: #CFE3F5;
border: 1px solid #E5F3FF;
font-size: 12px !important;
color:#0D416D
&:hover
color:#0D416D
#textMarkerRelabelDialog #textMarkerRelabelDialog
margin: 0 margin: 0
.relabelOptions .relabelOptions
@ -112,14 +120,6 @@ button.viewerBtn
&:hover &:hover
color:#FFF color:#FFF
button.btn-secondary
background-color: #CFE3F5;
border: 1px solid #E5F3FF;
font-size: 12px !important;
color:#0D416D
&:hover
color:#0D416D
.iconSwitch .iconSwitch
margin:0px 5px margin:0px 5px
.btn .btn
@ -145,5 +145,3 @@ button.btn-secondary
display:block display:block
.off .off
display: none display: none

View File

@ -248,7 +248,7 @@ function setOHIFHotkeys(hotkeys) {
*/ */
function enableHotkeys(hotkeys) { function enableHotkeys(hotkeys) {
const definitions = hotkeys || OHIF.viewer.hotkeys; const definitions = hotkeys || OHIF.viewer.hotkeys;
OHIF.hotkeys.set('viewer', definitions); OHIF.hotkeys.set('viewer', definitions, true);
OHIF.context.set('viewer'); OHIF.context.set('viewer');
} }