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 {
constructor(name, definitions, enabled) {
this.name = name;
this.definitions = definitions;
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);

View File

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

View File

@ -4,6 +4,9 @@
{{>inputText (extend hotkeyInputInformation class='hotkey')}}
{{/each}}
<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}}
</template>

View File

@ -12,7 +12,19 @@ Template.hotkeysForm.onCreated(() => {
const { contextName } = instance.data;
const form = instance.$('form').first().data('component');
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({
getHotkeyInputInformationList() {
OHIF.context.dep.depend();
OHIF.hotkeys.changeObserver.depend();
const instance = Template.instance();
const { contextName } = instance.data;
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
OHIF.user.setData = (key, value) => {
// Check if there is an user logged in
OHIF.user.validate();
return new Promise((resolve, reject) => {
try {
// Check if there is an user logged in
OHIF.user.validate();
} catch(error) {
reject(error);
}
// Call the update method on server-side
Meteor.call('ohif.user.data.set', key, value);
// Call the update method on server-side
Meteor.call('ohif.user.data.set', key, value, error => {
if (error) {
reject(error);
}
resolve();
});
});
};

View File

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

View File

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