Skip to content

Commit

Permalink
search: Hide completely empty sections [fix #28]
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-w committed Aug 21, 2024
1 parent 2ee61b0 commit ba932b3
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions _includes/search-dictionary.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
}
}

showOnlyNonEmptySections()

// Report results.
const foundMessage = 'Found ' +
entryCount +
Expand All @@ -108,6 +110,24 @@
updateStatusBox({ showAll, entryCount, foundMessage });
}

function showOnlyNonEmptySections() {
const sectionHeaders = document.querySelectorAll('div.content h3[id]')
for (const sectionHeader of sectionHeaders) {
const ulElement = sectionHeader.nextElementSibling
const items = Array.from(ulElement.children).filter(it => it.tagName === 'LI')
const hiddenItems = items.filter(it => it.classList.contains('is-hidden'))
const shouldHide = items.length <= hiddenItems.length
console.log({shouldHide, sectionHeader, itemCount: items.length, hiddenItemCount: hiddenItems.length})
if (shouldHide) {
sectionHeader.classList.add('is-hidden')
ulElement.classList.add('is-hidden')
} else {
sectionHeader.classList.remove('is-hidden')
ulElement.classList.remove('is-hidden')
}
}
}

function updateStatusBox({ showAll, entryCount, foundMessage }) {
const statusBox = document.querySelector('#search-dictionary-results')
statusBox.classList.remove(
Expand Down Expand Up @@ -159,15 +179,17 @@
totalMatchCount += matchCount
}

showOnlyNonEmptySections()

// Report results.
const foundMessage = 'Showing ' +
totalMatchCount +
(' example' + (totalMatchCount !== 1 ? 's' : '')) +
' across ' +
entryCount +
(' entr' + (entryCount !== 1 ? 'ies' : 'y')) +
' with Orthic notation containing ' +
('“' + query + '”.')
totalMatchCount +
(' example' + (totalMatchCount !== 1 ? 's' : '')) +
' across ' +
entryCount +
(' entr' + (entryCount !== 1 ? 'ies' : 'y')) +
' with Orthic notation containing ' +
('“' + query + '”.')
updateStatusBox({ showAll, entryCount, foundMessage });
}

Expand Down

0 comments on commit ba932b3

Please sign in to comment.