home.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* All the things */
  2. let currentUrl = window.location.hostname;
  3. if (window.location.port) {
  4. currentUrl += ':' + window.location.port
  5. }
  6. /* Screenshots */
  7. const lightbox = document.getElementById("lightbox");
  8. const showScreenshotOverlay = (e, el, index) => {
  9. lightbox.classList.add('show');
  10. document.addEventListener('keydown', nextScreenshotKeyboardListener);
  11. return showScreenshot(e, index);
  12. };
  13. const showScreenshot = (e, index) => {
  14. const actualIndex = resolveScreenshotIndex(index);
  15. lightbox.innerHTML = '<div class="close-lightbox"></div>' + screenshots[actualIndex].innerHTML;
  16. lightbox.querySelector('img').onclick = (e) => { return showScreenshot(e,actualIndex+1); };
  17. currentScreenshotIndex = actualIndex;
  18. e.stopPropagation();
  19. return false;
  20. };
  21. const nextScreenshot = (e) => {
  22. return showScreenshot(e, currentScreenshotIndex+1);
  23. };
  24. const previousScreenshot = (e) => {
  25. return showScreenshot(e, currentScreenshotIndex-1);
  26. };
  27. const resolveScreenshotIndex = (index) => {
  28. if (index < 0) {
  29. return screenshots.length - 1;
  30. } else if (index > screenshots.length - 1) {
  31. return 0;
  32. }
  33. return index;
  34. };
  35. const hideScreenshotOverlay = (e) => {
  36. lightbox.classList.remove('show');
  37. document.removeEventListener('keydown', nextScreenshotKeyboardListener);
  38. };
  39. const nextScreenshotKeyboardListener = (e) => {
  40. switch (e.keyCode) {
  41. case 37:
  42. previousScreenshot(e);
  43. break;
  44. case 39:
  45. nextScreenshot(e);
  46. break;
  47. }
  48. };
  49. let currentScreenshotIndex = 0;
  50. const screenshots = [...document.querySelectorAll("#screenshots a")];
  51. screenshots.forEach((el, index) => {
  52. el.onclick = (e) => { return showScreenshotOverlay(e, el, index); };
  53. });
  54. lightbox.onclick = hideScreenshotOverlay;
  55. // Add anchor links
  56. document.querySelectorAll('.anchor').forEach((el) => {
  57. if (el.hasAttribute('id')) {
  58. const id = el.getAttribute('id');
  59. const anchor = document.createElement('a');
  60. anchor.innerHTML = `<a href="#${id}" class="anchorLink">#</a>`;
  61. el.appendChild(anchor);
  62. }
  63. });
  64. // Change ntfy.sh url and protocol to match self-hosted one
  65. document.querySelectorAll('.ntfyUrl').forEach((el) => {
  66. el.innerHTML = currentUrl;
  67. });
  68. document.querySelectorAll('.ntfyProtocol').forEach((el) => {
  69. el.innerHTML = window.location.protocol + "//";
  70. });