All files / atlas/docs/scripts search.js

0% Statements 0/20
0% Branches 0/8
0% Functions 0/5
0% Lines 0/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                             
(function() {
  const input = document.querySelector('#search')
  const targets = [ ...document.querySelectorAll('#sidebarNav li')]
  input.addEventListener('keyup', () => {
    // loop over each targets and hide the not corresponding ones
    targets.forEach(target => {
      if (!target.innerText.toLowerCase().includes(input.value.toLowerCase())) {
        target.style.display = 'none'
 
        /**
         * Detects an empty list
         * Remove the list and the list's title if the list is not displayed
         */
        const list = [...target.parentNode.childNodes].filter( elem => elem.style.display !== 'none')
 
        if (!list.length) {
          target.parentNode.style.display = 'none'
          target.parentNode.previousSibling.style.display = 'none'
        }
 
        /**
         * Detects empty category
         * Remove the entire category if no item is displayed
         */
        const category = [...target.parentNode.parentNode.childNodes]
          .filter( elem => elem.tagName !== 'H2' && elem.style.display !== 'none')
 
        if (!category.length) {
          target.parentNode.parentNode.style.display = 'none'
        }
      } else {
        target.parentNode.style.display = 'block'
        target.parentNode.previousSibling.style.display = 'block'
        target.parentNode.parentNode.style.display = 'block'
        target.style.display = 'block'
      }
    })
  })
})()