utils.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. (function (x) {
  2. var o = x.prototype;
  3. o.after || (o.after = function () { var e, m = arguments, l = m.length, i = 0, t = this, p = t.parentNode, n = Node, s = String, d = document; if (p !== null) { while (i < l) { ((e = m[i]) instanceof n) ? (((t = t.nextSibling) !== null) ? p.insertBefore(e, t) : p.appendChild(e)) : p.appendChild(d.createTextNode(s(e))); ++i; } } });
  4. }(Element));
  5. // from: https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/before()/before().md
  6. (function (arr) {
  7. arr.forEach(function (item) {
  8. if (item.hasOwnProperty('before')) {
  9. return;
  10. }
  11. Object.defineProperty(item, 'before', {
  12. configurable: true,
  13. enumerable: true,
  14. writable: true,
  15. value: function before() {
  16. var argArr = Array.prototype.slice.call(arguments),
  17. docFrag = document.createDocumentFragment();
  18. argArr.forEach(function (argItem) {
  19. var isNode = argItem instanceof Node;
  20. docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
  21. });
  22. this.parentNode.insertBefore(docFrag, this);
  23. }
  24. });
  25. });
  26. })([Element.prototype, CharacterData.prototype, DocumentType.prototype])
  27. window.shuffleArray = function (array) {
  28. for (let i = array.length - 1; i > 0; i--) {
  29. const j = Math.floor(Math.random() * (i + 1));
  30. [array[i], array[j]] = [array[j], array[i]];
  31. }
  32. return array;
  33. };
  34. function isInViewport(el) {
  35. var rect = el.getBoundingClientRect();
  36. return (
  37. rect.top >= 0 &&
  38. rect.bottom <= (window.innerHeight || document.documentElement.clientHeight)
  39. );
  40. }
  41. function isVisible(el) {
  42. return !!el.offsetParent;
  43. }
  44. function lazyLoad(parent) {
  45. parent.querySelectorAll('[lazy]').forEach(el => {
  46. if (isVisible(el)) {
  47. console.log("Lazy load", el, isVisible(el));
  48. const attributeKeys = el.getAttributeNames();
  49. attributeKeys.forEach(akey => {
  50. if (akey.startsWith("lazy-")) {
  51. console.log("Load attribute " + akey);
  52. const realkey = akey.substring(5);
  53. const value = el.getAttribute(akey);
  54. el.setAttribute(realkey, value);
  55. console.log("Set", realkey, "=", value);
  56. } else {
  57. console.log("Skip attribute", akey, "not lazy");
  58. }
  59. });
  60. }
  61. });
  62. }
  63. let floatingHeader = null;
  64. const updateFloatingHeader = () => {
  65. const header = document.querySelector("body > header");
  66. const siteTitle = document.querySelector("#siteTitle");
  67. if (!floatingHeader) {
  68. console.info("Create floating header!");
  69. floatingHeader = header.cloneNode(true);
  70. floatingHeader.classList.add("floating");
  71. let toTopBtn = floatingHeader.querySelector(".toggleNavOnPortraitButton");
  72. if (toTopBtn) {
  73. let newTopBtn=toTopBtn.cloneNode(true);
  74. toTopBtn.parentNode.replaceChild(newTopBtn,toTopBtn);
  75. toTopBtn=newTopBtn;
  76. toTopBtn.removeAttribute("toggle");
  77. toTopBtn.setAttribute("class", "toggleNavOnPortraitButton fas fa-angle-up");
  78. toTopBtn.setAttribute("title", "To top");
  79. toTopBtn.addEventListener("click",(ev) => {
  80. window.scrollTo({
  81. top: 0,
  82. left: 0,
  83. behavior: 'smooth'
  84. });
  85. });
  86. }
  87. document.body.append(floatingHeader);
  88. }
  89. if (!isInViewport(siteTitle)) {
  90. // if (floatingHeader.style.display != visibleDisplay) {
  91. // console.log("Show floating header");
  92. floatingHeader.style.visibility = "visible";
  93. // }
  94. } else {
  95. // if (floatingHeader && floatingHeader.style.display == visibleDisplay) {
  96. // console.log("Hide floating header");
  97. floatingHeader.style.visibility = "hidden";
  98. // }
  99. }
  100. };
  101. document.addEventListener("scroll", updateFloatingHeader);
  102. window.scrollToElement = (contentAnchor) => {
  103. const anchorBound = contentAnchor.getBoundingClientRect();
  104. const floatingHBound = floatingHeader.getBoundingClientRect();
  105. // contentAnchor.scrollIntoView({
  106. // behavior:"smooth",
  107. // block:"start",
  108. // inline:"nearest"
  109. // });
  110. window.scrollTo({
  111. top: anchorBound.top + window.scrollY - floatingHBound.bottom ,
  112. left: 0,
  113. behavior: 'smooth'
  114. });
  115. console.log("Scroll content into view");
  116. }
  117. window.addEventListener("DOMContentLoaded", function () {
  118. updateFloatingHeader();
  119. const scrollNow=()=>{
  120. window.scrollTo(0, 0);
  121. if (location.hash&&location.hash!="#") {
  122. const contentAnchor = document.querySelector(location.hash);
  123. if(contentAnchor)window.scrollToElement(contentAnchor);
  124. } else {
  125. const contentAnchor = document.querySelector("#content");
  126. if (contentAnchor) window.scrollToElement(contentAnchor);
  127. }
  128. };
  129. setTimeout(() => {
  130. scrollNow();
  131. }, 1);
  132. window.addEventListener("hashchange", function () {
  133. if(location.hash&&location.hash!="#")scrollNow();
  134. });
  135. document.querySelectorAll("[toggle]").forEach(el => {
  136. const toggleId = el.getAttribute("toggle");
  137. if (!toggleId) return;
  138. const parent = document.querySelector("#" + toggleId);
  139. if (!parent) return;
  140. el.addEventListener("click", () => {
  141. parent.querySelectorAll(".expandable").forEach(el2 => {
  142. if (el2.classList.contains("expandedOnPortrait"))
  143. el2.classList.remove("expandedOnPortrait");
  144. else el2.classList.add("expandedOnPortrait");
  145. });
  146. parent.querySelectorAll(".toggleable").forEach(el2 => {
  147. let toggled = el2.classList.contains("toggledOn");
  148. let toggledPortrait = el2.classList.contains("toggledOnPortrait");
  149. let notToggledPortrait = el2.classList.contains("toggledOffPortrait");
  150. toggled = toggled || toggledPortrait;
  151. const portrait = notToggledPortrait || toggledPortrait;
  152. console.log("Toggle ", toggled ? "on" : "off'")
  153. if (toggled) {
  154. // el.removeAttribute("toggled",false);
  155. el2.classList.add(portrait ? "toggledOffPortrait" : "toggledOff");
  156. el2.classList.remove("toggledOn");
  157. el2.classList.remove("toggledOnPortrait");
  158. } else {
  159. // el.setAttribute("toggled",true);
  160. el2.classList.remove("toggledOff");
  161. el2.classList.remove("toggledOffPortrait");
  162. el2.classList.add(portrait ? "toggledOnPortrait" : "toggledOn");
  163. }
  164. });
  165. });
  166. })
  167. });