extra.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Link tabs, as per https://facelessuser.github.io/pymdown-extensions/extensions/tabbed/#linked-tabs
  2. const savedTab = localStorage.getItem('savedTab')
  3. const tabs = document.querySelectorAll(".tabbed-set > input")
  4. for (const tab of tabs) {
  5. tab.addEventListener("click", () => {
  6. const current = document.querySelector(`label[for=${tab.id}]`)
  7. const pos = current.getBoundingClientRect().top
  8. const labelContent = current.innerHTML
  9. const labels = document.querySelectorAll('.tabbed-set > label, .tabbed-alternate > .tabbed-labels > label')
  10. for (const label of labels) {
  11. if (label.innerHTML === labelContent) {
  12. document.querySelector(`input[id=${label.getAttribute('for')}]`).checked = true
  13. }
  14. }
  15. // Preserve scroll position
  16. const delta = (current.getBoundingClientRect().top) - pos
  17. window.scrollBy(0, delta)
  18. // Save
  19. localStorage.setItem('savedTab', labelContent)
  20. })
  21. // Select saved tab
  22. const current = document.querySelector(`label[for=${tab.id}]`)
  23. const labelContent = current.innerHTML
  24. if (savedTab === labelContent) {
  25. tab.checked = true
  26. }
  27. }
  28. // Lightbox for screenshot
  29. const lightbox = document.createElement('div');
  30. lightbox.classList.add('lightbox');
  31. document.body.appendChild(lightbox);
  32. const showScreenshotOverlay = (e, el, group, index) => {
  33. lightbox.classList.add('show');
  34. document.addEventListener('keydown', nextScreenshotKeyboardListener);
  35. return showScreenshot(e, group, index);
  36. };
  37. const showScreenshot = (e, group, index) => {
  38. const actualIndex = resolveScreenshotIndex(group, index);
  39. lightbox.innerHTML = '<div class="close-lightbox"></div>' + screenshots[group][actualIndex].innerHTML;
  40. lightbox.querySelector('img').onclick = (e) => { return showScreenshot(e, group, actualIndex+1); };
  41. currentScreenshotGroup = group;
  42. currentScreenshotIndex = actualIndex;
  43. e.stopPropagation();
  44. return false;
  45. };
  46. const nextScreenshot = (e) => {
  47. return showScreenshot(e, currentScreenshotGroup, currentScreenshotIndex+1);
  48. };
  49. const previousScreenshot = (e) => {
  50. return showScreenshot(e, currentScreenshotGroup, currentScreenshotIndex-1);
  51. };
  52. const resolveScreenshotIndex = (group, index) => {
  53. if (index < 0) {
  54. return screenshots[group].length - 1;
  55. } else if (index > screenshots[group].length - 1) {
  56. return 0;
  57. }
  58. return index;
  59. };
  60. const hideScreenshotOverlay = (e) => {
  61. lightbox.classList.remove('show');
  62. document.removeEventListener('keydown', nextScreenshotKeyboardListener);
  63. };
  64. const nextScreenshotKeyboardListener = (e) => {
  65. switch (e.keyCode) {
  66. case 37:
  67. previousScreenshot(e);
  68. break;
  69. case 39:
  70. nextScreenshot(e);
  71. break;
  72. }
  73. };
  74. let currentScreenshotGroup = '';
  75. let currentScreenshotIndex = 0;
  76. let screenshots = {};
  77. Array.from(document.getElementsByClassName('screenshots')).forEach((sg) => {
  78. const group = sg.id;
  79. screenshots[group] = [...sg.querySelectorAll('a')];
  80. screenshots[group].forEach((el, index) => {
  81. el.onclick = (e) => { return showScreenshotOverlay(e, el, group, index); };
  82. });
  83. });
  84. lightbox.onclick = hideScreenshotOverlay;