navigation.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Page navigation methods called by shortcuts
  2. const state = {
  3. active_menu: 1,
  4. menu_selector: '.main-menu-item',
  5. menu_active_selector: '.active',
  6. };
  7. export function moveFocusLeft() {
  8. moveFocusLeftRight('left');
  9. }
  10. export function moveFocusRight() {
  11. moveFocusLeftRight('right');
  12. }
  13. export function moveFocusDown() {
  14. moveFocusUpDown('down');
  15. }
  16. export function moveFocusUp() {
  17. moveFocusUpDown('up');
  18. }
  19. // Navigate to whatever item has been selected in the UI by other shortcuts
  20. export function enterFocused() {
  21. const activeMainMenuItem = document.querySelector(state.menu_selector + '.focus a');
  22. if (activeMainMenuItem) {
  23. return (location.href = activeMainMenuItem.getAttribute('href'));
  24. }
  25. const activeUnit = document.querySelector(
  26. '.units .l-unit.focus .actions-panel__col.actions-panel__edit a'
  27. );
  28. if (activeUnit) {
  29. location.href = activeUnit.getAttribute('href');
  30. }
  31. }
  32. // Either click or follow a link based on the data-key-action attribute
  33. export function executeShortcut(elm) {
  34. const action = elm.dataset.keyAction;
  35. if (action === 'js') {
  36. return elm.querySelector('.data-controls').click();
  37. }
  38. if (action === 'href') {
  39. location.href = elm.querySelector('a').getAttribute('href');
  40. }
  41. }
  42. function moveFocusLeftRight(direction) {
  43. const menuSelector = state.menu_selector;
  44. const activeSelector = state.menu_active_selector;
  45. const menuItems = Array.from(document.querySelectorAll(menuSelector));
  46. const currentFocused = document.querySelector(`${menuSelector}.focus`);
  47. const currentActive = document.querySelector(menuSelector + activeSelector);
  48. let index = menuItems.indexOf(currentFocused);
  49. if (index === -1) {
  50. index = menuItems.indexOf(currentActive);
  51. }
  52. menuItems.forEach((item) => item.classList.remove('focus'));
  53. if (direction === 'left') {
  54. if (index > 0) {
  55. menuItems[index - 1].classList.add('focus');
  56. } else {
  57. switchMenu('last');
  58. }
  59. } else if (direction === 'right') {
  60. if (index < menuItems.length - 1) {
  61. menuItems[index + 1].classList.add('focus');
  62. } else {
  63. switchMenu('first');
  64. }
  65. }
  66. }
  67. function moveFocusUpDown(direction) {
  68. const unitSelector = '.units .l-unit:not(.header)';
  69. const units = Array.from(document.querySelectorAll(unitSelector));
  70. const currentFocused = document.querySelector('.units .l-unit.focus');
  71. let index = units.indexOf(currentFocused);
  72. if (index === -1) {
  73. index = 0;
  74. }
  75. if (direction === 'up' && index > 0) {
  76. index--;
  77. } else if (direction === 'down' && index < units.length - 1) {
  78. index++;
  79. } else {
  80. return;
  81. }
  82. if (currentFocused) {
  83. currentFocused.classList.remove('focus');
  84. }
  85. units[index].classList.add('focus');
  86. window.scrollTo({
  87. top: units[index].getBoundingClientRect().top - 200 + window.scrollY,
  88. behavior: 'smooth',
  89. });
  90. }
  91. function switchMenu(position = 'first') {
  92. if (state.active_menu === 0) {
  93. state.active_menu = 1;
  94. state.menu_selector = '.main-menu-item';
  95. state.menu_active_selector = '.active';
  96. const menuItems = document.querySelectorAll(state.menu_selector);
  97. const focusedIndex = position === 'first' ? 0 : menuItems.length - 1;
  98. menuItems[focusedIndex].classList.add('focus');
  99. }
  100. }