My ham website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.9 KiB

6 years ago
  1. var jumpToCode = (function init () {
  2. // Classes of code we would like to highlight
  3. var missingCoverageClasses = [ '.cbranch-no', '.cstat-no', '.fstat-no' ];
  4. // We don't want to select elements that are direct descendants of another match
  5. var notSelector = ':not(' + missingCoverageClasses.join('):not(') + ') > '; // becomes `:not(a):not(b) > `
  6. // Selecter that finds elements on the page to which we can jump
  7. var selector = notSelector + missingCoverageClasses.join(', ' + notSelector); // becomes `:not(a):not(b) > a, :not(a):not(b) > b`
  8. // The NodeList of matching elements
  9. var missingCoverageElements = document.querySelectorAll(selector);
  10. var currentIndex;
  11. function toggleClass(index) {
  12. missingCoverageElements.item(currentIndex).classList.remove('highlighted');
  13. missingCoverageElements.item(index).classList.add('highlighted');
  14. }
  15. function makeCurrent(index) {
  16. toggleClass(index);
  17. currentIndex = index;
  18. missingCoverageElements.item(index)
  19. .scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'center' });
  20. }
  21. function goToPrevious() {
  22. var nextIndex = 0;
  23. if (typeof currentIndex !== 'number' || currentIndex === 0) {
  24. nextIndex = missingCoverageElements.length - 1;
  25. } else if (missingCoverageElements.length > 1) {
  26. nextIndex = currentIndex - 1;
  27. }
  28. makeCurrent(nextIndex);
  29. }
  30. function goToNext() {
  31. var nextIndex = 0;
  32. if (typeof currentIndex === 'number' && currentIndex < (missingCoverageElements.length - 1)) {
  33. nextIndex = currentIndex + 1;
  34. }
  35. makeCurrent(nextIndex);
  36. }
  37. return function jump(event) {
  38. switch (event.which) {
  39. case 78: // n
  40. case 74: // j
  41. goToNext();
  42. break;
  43. case 66: // b
  44. case 75: // k
  45. case 80: // p
  46. goToPrevious();
  47. break;
  48. }
  49. };
  50. }());
  51. window.addEventListener('keydown', jumpToCode);