gitea

Development moved to Codeberg

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
function getComputedStyleProperty(el, prop) {
  const cs = el ? window.getComputedStyle(el) : null;
  return cs ? cs[prop] : null;
}

function isShown(el) {
  return getComputedStyleProperty(el, 'display') !== 'none';
}

function assertShown(el, expectShown) {
  if (window.config.runModeIsProd) return;

  // to help developers to catch display bugs, this assertion can be removed after next release cycle or if it has been proved that there is no bug.
  if (expectShown && !isShown(el)) {
    throw new Error('element is hidden but should be shown');
  } else if (!expectShown && isShown(el)) {
    throw new Error('element is shown but should be hidden');
  }
}

function elementsCall(el, func, ...args) {
  if (typeof el === 'string' || el instanceof String) {
    el = document.querySelectorAll(el);
  }
  if (el instanceof Node) {
    func(el, ...args);
  } else if (el.length !== undefined) {
    // this works for: NodeList, HTMLCollection, Array, jQuery
    for (const e of el) {
      func(e, ...args);
    }
  } else {
    throw new Error('invalid argument to be shown/hidden');
  }
}

/**
 * @param el string (selector), Node, NodeList, HTMLCollection, Array or jQuery
 * @param force force=true to show or force=false to hide, undefined to toggle
 */
function toggleShown(el, force) {
  if (force === true) {
    el.classList.remove('gt-hidden');
    assertShown(el, true);
  } else if (force === false) {
    el.classList.add('gt-hidden');
    assertShown(el, false);
  } else if (force === undefined) {
    const wasShown = window.config.runModeIsProd ? undefined : isShown(el);
    el.classList.toggle('gt-hidden');
    if (wasShown !== undefined) {
      assertShown(el, !wasShown);
    }
  } else {
    throw new Error('invalid force argument');
  }
}

export function showElem(el) {
  elementsCall(el, toggleShown, true);
}

export function hideElem(el) {
  elementsCall(el, toggleShown, false);
}

export function toggleElem(el, force) {
  elementsCall(el, toggleShown, force);
}