main.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Utils
  3. */
  4. // Throttle
  5. //
  6. const throttle = (callback, limit) => {
  7. let timeoutHandler = null;
  8. return () => {
  9. if (timeoutHandler == null) {
  10. timeoutHandler = setTimeout(() => {
  11. callback();
  12. timeoutHandler = null;
  13. }, limit);
  14. }
  15. }
  16. }
  17. // addEventListener Helper
  18. //
  19. const listen = (ele, e, callback) => {
  20. if (document.querySelector(ele) !== null) {
  21. document.querySelector(ele).addEventListener(e, callback);
  22. }
  23. }
  24. /**
  25. * Functions
  26. */
  27. // Set inner width into CSS variable
  28. //
  29. function setVw() {
  30. let vw = document.documentElement.clientWidth / 100;
  31. document.documentElement.style.setProperty('--vw', `${vw}px`);
  32. }
  33. setVw();
  34. window.addEventListener('resize', setVw);
  35. // ToC toggle
  36. //
  37. function toggleToc() {
  38. const toc = document.getElementById('toc');
  39. if (toc.style.display === 'block') {
  40. toc.style.display = 'none';
  41. } else {
  42. toc.style.display = 'block';
  43. }
  44. }
  45. listen ("#toc-btn", "click", toggleToc);
  46. // Scroll to Top
  47. //
  48. const scrollBtn = document.getElementById('scroll-top-btn');
  49. const btnVisibility = () => {
  50. if ((window.scrollY > 400) && (window.innerWidth >= 1530)) {
  51. scrollBtn.style.display = "block";
  52. } else {
  53. scrollBtn.style.display = "none";
  54. }
  55. };
  56. if (scrollBtn !== null) {
  57. scrollBtn.addEventListener("click", () => {
  58. window.scrollTo(0,0);
  59. });
  60. }
  61. // Anchor points for list page
  62. //
  63. document.querySelectorAll('.post-year').forEach((ele)=> {
  64. ele.addEventListener('click', () => {
  65. window.location.hash = '#' + ele.id;
  66. });
  67. });
  68. // Load Comments
  69. //
  70. let commentsLoaded = false;
  71. let comments = document.getElementById('isso-thread');
  72. let commentsLoader = document.getElementById('comments-loader');
  73. const loadComments = () => {
  74. let script = document.createElement("script");
  75. script.setAttribute("type", "text/javascript");
  76. script.setAttribute("src", "/js/embed.min.js");
  77. // add relevant data-isso attributes here
  78. script.setAttribute("data-isso", "https://comment.ojbk.im");
  79. script.setAttribute("data-isso-vote", "false");
  80. // script.setAttribute("data-isso-max-comments-top", "10");
  81. // script.setAttribute("data-isso-reveal-on-click", "10");
  82. script.setAttribute("data-isso-css", "false");
  83. document.getElementsByTagName("head")[0].appendChild(script);
  84. commentsLoader.style.display = 'none';
  85. }
  86. // Load comments if the window is not scrollable
  87. if ((comments !== null) && (comments.offsetTop < window.innerHeight)) {
  88. loadComments();
  89. commentsLoaded = true;
  90. }
  91. window.addEventListener('scroll', throttle(() => {
  92. if ((comments !== null) && (commentsLoaded == false)) {
  93. if (window.pageYOffset + window.innerHeight > comments.offsetTop) {
  94. loadComments();
  95. commentsLoaded = true;
  96. }
  97. }
  98. if (scrollBtn !== null) {
  99. btnVisibility();
  100. }
  101. }, 150));