diff --git a/resources/script.js b/resources/script.js index fe4588c..b37e2b9 100644 --- a/resources/script.js +++ b/resources/script.js @@ -50,25 +50,42 @@ function smartFocusEditById(id) { } } +function findFocusableElements(parentArea) { + const focussableElements = 'a:not([disabled])'; + return Array.prototype.filter.call(parentArea.querySelectorAll(focussableElements), + function (element) { + return true; + } + ); +} + // Modified: https://stackoverflow.com/a/35173443/12271202 // dir: 1 for down, -1 for up function indexFocusSwitch(dir) { const parentArea = document.getElementById('page-content'); if (parentArea) { - var focussableElements = 'a:not([disabled])'; - var focussable = Array.prototype.filter.call(parentArea.querySelectorAll(focussableElements), - function (element) { - return true; - } - ); + const focussable = findFocusableElements(parentArea); var index = focussable.indexOf(document.activeElement); - if(focussable.length > 0) { + if (focussable.length > 0) { var nextElement = focussable[(index + dir + focussable.length) % focussable.length] || focussable[0]; nextElement.focus(); } } } +// dir: -1 start, 1 end +function indexFocusStartOrEnd(dir) { + const parentArea = document.getElementById('page-content'); + if (parentArea) { + const focussable = findFocusableElements(parentArea); + var index = focussable.indexOf(document.activeElement); + if (focussable.length > 0) { + var nextElement = focussable[dir === -1 ? 0 : focussable.length - 1] || focussable[0]; + nextElement.focus(); + } + } +} + Mousetrap.bind('n', function() { // window.location.href = '?edit=true'; reliableClickById('new-button'); @@ -120,12 +137,20 @@ Mousetrap.bind(['ctrl+right', 'l'], function() { } }); +Mousetrap.bind(['ctrl+up', 'k'], function() { + indexFocusSwitch(-1); +}); + Mousetrap.bind(['ctrl+down', 'j'], function() { indexFocusSwitch(1); }); -Mousetrap.bind(['ctrl+up', 'k'], function() { - indexFocusSwitch(-1); +Mousetrap.bindGlobal(['g k'], function() { + indexFocusStartOrEnd(-1); +}); + +Mousetrap.bindGlobal(['g j'], function() { + indexFocusStartOrEnd(1); }); Mousetrap.bind(['/'], function() {