utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. let PROGRESS_BAR_INTERVAL = null;
  35. window.addEventListener("load", function () {
  36. document.getElementById("pageLoadingProgress").style.display = "none";
  37. if (PROGRESS_BAR_INTERVAL) {
  38. clearInterval(PROGRESS_BAR_INTERVAL);
  39. PROGRESS_BAR_INTERVAL = null;
  40. }
  41. });
  42. PROGRESS_BAR_INTERVAL = setInterval(function () {
  43. const bar = document.getElementById("pageLoadingProgress");
  44. if (!bar) return;
  45. bar.value += 1;
  46. if (bar.value >= bar.max) bar.value = 0;
  47. }, 20);
  48. window.addEventListener("DOMContentLoaded", function () {
  49. document.querySelectorAll(".toggleNavOnPortraitButton").forEach(el=>{
  50. const toggleId=el.getAttribute("toggle");
  51. if(!toggleId)return;
  52. const parent=document.querySelector("#"+toggleId);
  53. if(!parent)return;
  54. el.addEventListener("click",()=>{
  55. let toggled=el.getAttribute("toggled");
  56. console.log("Toggle ",toggled?"on":"off'")
  57. parent.querySelectorAll("div").forEach(el2=>{
  58. if(toggled){
  59. el.removeAttribute("toggled",false);
  60. el2.classList.add("toggledOff");
  61. el2.classList.remove("toggledOn");
  62. }else{
  63. el.setAttribute("toggled",true);
  64. el2.classList.remove("toggledOff");
  65. el2.classList.add("toggledOn");
  66. }
  67. });
  68. });
  69. })
  70. document.getElementById("pageLoadingProgress").style.display = "none";
  71. if (PROGRESS_BAR_INTERVAL) {
  72. clearInterval(PROGRESS_BAR_INTERVAL);
  73. PROGRESS_BAR_INTERVAL = null;
  74. }
  75. });
  76. function lazyLoad(parent){
  77. parent.querySelectorAll('[lazy]').forEach(el=>{
  78. console.log("Lazy load",el);
  79. const attributeKeys=el.getAttributeNames();
  80. attributeKeys.forEach(akey=>{
  81. if(akey.startsWith("lazy-")){
  82. console.log("Load attribute "+akey);
  83. const realkey=akey.substring(5);
  84. const value=el.getAttribute(akey);
  85. el.setAttribute(realkey,value);
  86. console.log("Set",realkey,"=",value);
  87. }else{
  88. console.log("Skip attribute",akey,"not lazy");
  89. }
  90. })
  91. });
  92. }