This repository was archived by the owner on Jul 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-search.js
More file actions
91 lines (80 loc) · 3.1 KB
/
Copy path07-search.js
File metadata and controls
91 lines (80 loc) · 3.1 KB
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
; (function () {
'use strict'
function openSearchPopover () {
document.getElementById('search-background').style.display = 'block'
document.getElementById('search').style.display = 'block'
// focus the textbox after popover appears
focusSearchInput()
// add eventlisteners after popover appears
addNavigationToSearch()
}
function closeSearchPopover () {
document.getElementById('search-background').style.display = 'none'
document.getElementById('search').style.display = 'none'
}
function focusSearchInput () {
var searchInput = document.querySelector('.pagefind-modular-input')
if (searchInput) {
searchInput.focus()
}
}
function addNavigationToSearch () {
// focus the first search result when pressing enter in search input
document.getElementById('pfmod-input-0').addEventListener('keydown', goToSearchResultsOnEnter)
}
function goToSearchResultsOnEnter (event) {
var searchResults = document.getElementById('search-results')
if (event.key === 'Enter' && searchResults.childElementCount > 0) {
searchResults.firstChild.querySelector('a').focus()
searchResults.addEventListener('keydown', addNavigationInSearchResults)
}
}
function addNavigationInSearchResults (event) {
if (event.key === 'ArrowDown' && document.activeElement.classList.contains('pagefind-modular-list-link')) {
event.preventDefault() // prevent page scrolling
var nextSibling = document.activeElement.parentElement.parentElement.parentElement.nextElementSibling
if (nextSibling) {
nextSibling.querySelector('a').focus()
}
}
if (event.key === 'ArrowUp' && document.activeElement.classList.contains('pagefind-modular-list-link')) {
event.preventDefault() // prevent page scrolling
var previousSibling = document.activeElement.parentElement.parentElement.parentElement.previousElementSibling
if (previousSibling) {
previousSibling.querySelector('a').focus()
}
}
}
// open the popover when clicking the magnifying glass search icon
var searchButton = document.getElementById('search-button')
if (searchButton) {
searchButton.addEventListener('click', function (event) {
event.stopPropagation()
openSearchPopover()
})
}
// close functionality when clicking the background
var searchBackground = document.getElementById('search-background')
if (searchBackground) {
searchBackground.addEventListener('click', function (event) {
event.stopPropagation() // trap event
closeSearchPopover()
})
}
document.addEventListener('keydown', function (event) {
// open search with keyboard
if (event.ctrlKey && event.key === 'k') {
event.preventDefault() // prevent focussing the URL bar in the browser
openSearchPopover()
}
// close search with keyboard
if (event.key === 'Escape') {
closeSearchPopover()
}
// focus the search input when pressing / while search popover is open
if (event.key === '/') {
event.preventDefault() // prevent opening the browser search dialog
focusSearchInput()
}
})
})()