block-navigation.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 (
  43. typeof currentIndex === "number" &&
  44. currentIndex < missingCoverageElements.length - 1
  45. ) {
  46. nextIndex = currentIndex + 1;
  47. }
  48. makeCurrent(nextIndex);
  49. }
  50. return function jump(event) {
  51. switch (event.which) {
  52. case 78: // n
  53. case 74: // j
  54. goToNext();
  55. break;
  56. case 66: // b
  57. case 75: // k
  58. case 80: // p
  59. goToPrevious();
  60. break;
  61. }
  62. };
  63. })();
  64. window.addEventListener("keydown", jumpToCode);