diff --git a/front/core/services/modules.js b/front/core/services/modules.js index b997df08c5..2c1862fa80 100644 --- a/front/core/services/modules.js +++ b/front/core/services/modules.js @@ -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(); } diff --git a/front/salix/module.js b/front/salix/module.js index 75bc18f2db..8f3a022daa 100644 --- a/front/salix/module.js +++ b/front/salix/module.js @@ -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); diff --git a/modules/claim/front/routes.json b/modules/claim/front/routes.json index a8ae49f1f9..3b7bbef3ed 100644 --- a/modules/claim/front/routes.json +++ b/modules/claim/front/routes.json @@ -80,6 +80,6 @@ {"state": "claim.card.action", "icon": "icon-actions"} ], "keybindings": [ - {"key": "r", "sref": "claim.index"} + {"key": "r", "state": "claim.index"} ] } \ No newline at end of file diff --git a/modules/client/front/routes.json b/modules/client/front/routes.json index c35c48b784..cdaa6b7c18 100644 --- a/modules/client/front/routes.json +++ b/modules/client/front/routes.json @@ -349,6 +349,6 @@ } ], "keybindings": [ - {"key": "c", "sref": "client.index"} + {"key": "c", "state": "client.index"} ] } diff --git a/modules/item/front/routes.json b/modules/item/front/routes.json index bd83276b87..d2514107dd 100644 --- a/modules/item/front/routes.json +++ b/modules/item/front/routes.json @@ -127,6 +127,6 @@ {"state": "item.card.last-entries", "icon": "icon-regentry"} ], "keybindings": [ - {"key": "a", "sref": "item.index"} + {"key": "a", "state": "item.index"} ] } \ No newline at end of file diff --git a/modules/ticket/front/routes.json b/modules/ticket/front/routes.json index cce565342e..8c78de7d3e 100644 --- a/modules/ticket/front/routes.json +++ b/modules/ticket/front/routes.json @@ -242,6 +242,6 @@ {"state": "ticket.card.request.index", "icon": "icon-100"} ], "keybindings": [ - {"key": "t", "sref": "ticket.index"} + {"key": "t", "state": "ticket.index"} ] } \ No newline at end of file