utils.js 4.0 KB

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