summaryrefslogtreecommitdiff
path: root/assets/js/faq.js
blob: c1a281b96ea93135dc55608ba58aa260651d374b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(function(){
  // the container which contains the faq
  var el = document.querySelector('.container.page > div')

  // hide or show content
  var hs = function(e,val){
    // val = show it?
    var symb = e.querySelector(':scope > .hideshow')
    if (val === undefined) {
      if (symb.textContent == '[+]') {
	val = true
      } else {
	val = false
      }
    }
    if (val) {
      symb.textContent = '[−]'
    } else {
      symb.textContent = '[+]'
    }
    e.classList.toggle('collapsed',!val);

    var ns = e.nextSibling
    while (ns && !ns.onclick) {
      if (ns.classList) {
        ns.classList.toggle('hidden',!val)
      }
      ns = ns.nextSibling
    }
  }

  var kbdclickfn = function(f){
    return function(e){
      if (e.key === ' ' || e.key === 'Enter') {
        e.preventDefault()
        f(e)
      }
    }
  }
  // toggle one
  var hsclick = function(e){
    hs(e.currentTarget)
  }
  var hskbd = kbdclickfn(function(e){
    hs(e.currentTarget.parentNode)
  })
  // show all
  var hsclickall = function(e){
    el.querySelectorAll(':scope > h3').forEach(
      function(e){hs(e,true)}
    )
  }
  var hskbdall = kbdclickfn(function(e){
    hsclickall(e)
  })
  // hide all
  var hsclicknone = function(e){
    el.querySelectorAll(':scope > h3').forEach(
      function(e){hs(e,false)}
    )
  }
  var hskbdnone = kbdclickfn(function(e){
    hsclicknone(e)
  })

  // add ...all links
  var h1 = el.querySelector(':scope > h1')

  var togelnone = document.createElement('a')
  togelnone.onclick = hsclicknone
  togelnone.onkeypress = hskbdnone
  togelnone.tabIndex = 0
  togelnone.innerHTML = '[collapse all]'
  togelnone.style.cursor = 'pointer'
  h1.parentNode.insertBefore(togelnone, h1.nextSibling)

  h1.parentNode.insertBefore(document.createTextNode(' '), h1.nextSibling)

  var togelall = document.createElement('a')
  togelall.onclick = hsclickall
  togelall.onkeypress = hskbdall
  togelall.tabIndex = 0
  togelall.innerHTML = '[expand all]'
  togelall.style.cursor = 'pointer'
  h1.parentNode.insertBefore(togelall, h1.nextSibling)

  var retf = function(e){ return false; }

  // add bindings
  el.querySelectorAll(':scope > h3').forEach(
    function(e){
      e.onclick = hsclick
      var newel = document.createElement('a')
      newel.onkeypress = hskbd
      newel.onclick = retf
      newel.tabIndex = 0
      newel.innerHTML = '[+]'
      newel.className = 'hideshow'
      newel.style.cursor = 'pointer'
      newel.href='#'+e.id
      e.insertBefore(document.createTextNode(' '), e.childNodes[0])
      e.insertBefore(newel, e.childNodes[0])
    }
  )

  // collapse all by default
  el.querySelectorAll(':scope > h3').forEach(
    function(e){hs(e,('#' + e.id == window.location.hash))}
  )
})()