block-navigation.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* eslint-disable */
  2. var jumpToCode = (function init() {
  3. // Classes of code we would like to highlight in the file view
  4. var missingCoverageClasses = ['.cbranch-no', '.cstat-no', '.fstat-no'];
  5. // Elements to highlight in the file listing view
  6. var fileListingElements = ['td.pct.low'];
  7. // We don't want to select elements that are direct descendants of another match
  8. var notSelector = ':not(' + missingCoverageClasses.join('):not(') + ') > '; // becomes `:not(a):not(b) > `
  9. // Selecter that finds elements on the page to which we can jump
  10. var selector =
  11. fileListingElements.join(', ') +
  12. ', ' +
  13. notSelector +
  14. missingCoverageClasses.join(', ' + notSelector); // becomes `:not(a):not(b) > a, :not(a):not(b) > b`
  15. // The NodeList of matching elements
  16. var missingCoverageElements = document.querySelectorAll(selector);
  17. var currentIndex;
  18. function toggleClass(index) {
  19. missingCoverageElements.item(currentIndex).classList.remove('highlighted');
  20. missingCoverageElements.item(index).classList.add('highlighted');
  21. }
  22. function makeCurrent(index) {
  23. toggleClass(index);
  24. currentIndex = index;
  25. missingCoverageElements.item(index).scrollIntoView({
  26. behavior: 'smooth',
  27. block: 'center',
  28. inline: 'center',
  29. });
  30. }
  31. function goToPrevious() {
  32. var nextIndex = 0;
  33. if (typeof currentIndex !== 'number' || currentIndex === 0) {
  34. nextIndex = missingCoverageElements.length - 1;
  35. } else if (missingCoverageElements.length > 1) {
  36. nextIndex = currentIndex - 1;
  37. }
  38. makeCurrent(nextIndex);
  39. }
  40. function goToNext() {
  41. var nextIndex = 0;
  42. if (typeof currentIndex === 'number' && currentIndex < missingCoverageElements.length - 1) {
  43. nextIndex = currentIndex + 1;
  44. }
  45. makeCurrent(nextIndex);
  46. }
  47. return function jump(event) {
  48. if (
  49. document.getElementById('fileSearch') === document.activeElement &&
  50. document.activeElement != null
  51. ) {
  52. // if we're currently focused on the search input, we don't want to navigate
  53. return;
  54. }
  55. switch (event.which) {
  56. case 78: // n
  57. case 74: // j
  58. goToNext();
  59. break;
  60. case 66: // b
  61. case 75: // k
  62. case 80: // p
  63. goToPrevious();
  64. break;
  65. }
  66. };
  67. })();
  68. window.addEventListener('keydown', jumpToCode);