Keybindings bugs fixed

This commit is contained in:
Juan Ferrer 2019-01-24 09:25:51 +01:00
parent 30749f0f4d
commit 498ae5d4fe
6 changed files with 31 additions and 28 deletions

View File

@ -25,7 +25,7 @@ export default class Modules {
let keyBind;
if (mod.keybindings) {
let res = mod.keybindings.find(i => i.sref == route.state);
let res = mod.keybindings.find(i => i.state == route.state);
if (res) keyBind = res.key.toUpperCase();
}

View File

@ -6,8 +6,8 @@ export const appName = 'salix';
const ngModule = ng.module('salix', ['vnCore']);
export default ngModule;
run.$inject = ['$window', '$rootScope', 'vnAuth', 'vnApp', '$state', '$document'];
export function run($window, $rootScope, vnAuth, vnApp, $state, $document) {
run.$inject = ['$window', '$rootScope', 'vnAuth', 'vnApp', '$state'];
export function run($window, $rootScope, vnAuth, vnApp, $state) {
$window.validations = {};
vnApp.name = appName;
@ -21,35 +21,38 @@ export function run($window, $rootScope, vnAuth, vnApp, $state, $document) {
});
if ($window.routes) {
let keybindings = {};
for (const mod of $window.routes) {
if (!mod || !mod.keybindings)
continue;
for (const binding of mod.keybindings) {
if (!binding.key || !binding.sref)
throw new Error('Binding not formed correctly');
let err;
if (!binding.key)
err = `Missing attribute 'key' in binding`;
else if (!binding.state)
err = `Missing attribute 'state' in binding`;
else if (keybindings[binding.key])
err = `Binding key redeclared`;
$document.on('keyup', function(e) {
if (e.defaultPrevented) return;
let shortcut = {
altKey: true,
ctrlKey: true,
key: binding.key
};
let correctShortcut = true;
for (const key in shortcut)
correctShortcut = correctShortcut && shortcut[key] == e[key];
if (correctShortcut) {
$state.go(binding.sref);
e.preventDefault();
}
});
if (err)
console.warn(`${err}: ${mod.module}: ${JSON.stringify(binding)}`);
else
keybindings[binding.key] = binding.state;
}
}
$window.addEventListener('keyup', function(event) {
if (event.defaultPrevented || !event.altKey || !event.ctrlKey)
return;
let state = keybindings[event.key];
if (state) {
$state.go(state);
event.preventDefault();
}
});
}
}
ngModule.run(run);

View File

@ -80,6 +80,6 @@
{"state": "claim.card.action", "icon": "icon-actions"}
],
"keybindings": [
{"key": "r", "sref": "claim.index"}
{"key": "r", "state": "claim.index"}
]
}

View File

@ -349,6 +349,6 @@
}
],
"keybindings": [
{"key": "c", "sref": "client.index"}
{"key": "c", "state": "client.index"}
]
}

View File

@ -127,6 +127,6 @@
{"state": "item.card.last-entries", "icon": "icon-regentry"}
],
"keybindings": [
{"key": "a", "sref": "item.index"}
{"key": "a", "state": "item.index"}
]
}

View File

@ -242,6 +242,6 @@
{"state": "ticket.card.request.index", "icon": "icon-100"}
],
"keybindings": [
{"key": "t", "sref": "ticket.index"}
{"key": "t", "state": "ticket.index"}
]
}