-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.ts
164 lines (140 loc) · 4.68 KB
/
search.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
;(() => {
const searchStringElement = document.querySelector(
'#search--searchString'
) as HTMLInputElement
const resultsContainerElement = document.querySelector(
'#resultsContainer'
) as HTMLElement
let agendaMetadataIsLoaded = false
let agendaMetadata: AgendaMetadata[] = []
function renderAgendas(): void {
if (!agendaMetadataIsLoaded) {
return
}
const searchStringPieces = searchStringElement.value
.trim()
.toLowerCase()
.split(' ')
const panelElement = document.createElement('div')
panelElement.className = 'panel'
for (const agenda of agendaMetadata) {
if (agenda.searchCriteria === undefined) {
agenda.searchCriteria =
agenda.agendaDate +
' ' +
agenda.agendaTitle.toLowerCase() +
' ' +
(agenda.outline ?? []).join(' ').toLowerCase() +
' ' +
(agenda.fullContent ?? '')
delete agenda.fullContent
}
let showAgenda = true
for (const searchStringPiece of searchStringPieces) {
if (!agenda.searchCriteria.includes(searchStringPiece)) {
showAgenda = false
break
}
}
if (showAgenda) {
const panelBlockElement = document.createElement('a')
panelBlockElement.className = 'panel-block is-block'
panelBlockElement.href = agenda.url
panelBlockElement.target = '_blank'
panelBlockElement.innerHTML = `<div class="level is-mobile">
<div class="level-left">
<div class="level-item">
<i class="fas fa-lg fa-file-pdf" aria-hidden="true"></i>
</div>
<div class="level-item">
<h2 class="title is-6" data-field="agendaTitle"></h2>
</div>
</div>
<div class="level-right">
<div class="level-item" data-field="agendaDate"></div>
</div>
</div>`
panelBlockElement.querySelector(
'[data-field="agendaTitle"]'
)!.textContent = agenda.agendaTitle
panelBlockElement.querySelector(
'[data-field="agendaDate"]'
)!.textContent = agenda.agendaDate
if ((agenda.outline ?? []).length > 0) {
panelBlockElement.insertAdjacentHTML(
'beforeend',
'<ul class="fa-ul"></ul>'
)
for (const [index, outlineItem] of agenda.outline!.entries()) {
if (index > 10) {
panelBlockElement.querySelector('ul')!.insertAdjacentHTML(
'beforeend',
`<li>
<span class="fa-li"><i class="fas fa-ellipsis-h" aria-hidden="true"></i></span>
<em>Plus More</em>
</li>`
)
break
}
const liElement = document.createElement('li')
liElement.innerHTML =
'<span class="fa-li"><i class="fas fa-bookmark" aria-hidden="true"></i></span>'
// eslint-disable-next-line unicorn/prefer-modern-dom-apis
liElement.insertAdjacentText('beforeend', outlineItem)
panelBlockElement.querySelector('ul')!.append(liElement)
}
}
panelElement.append(panelBlockElement)
}
}
if (panelElement.hasChildNodes()) {
resultsContainerElement.textContent = ''
resultsContainerElement.append(panelElement)
} else {
resultsContainerElement.innerHTML = `<div class="message is-info">
<p class="message-body">
<strong>There are no agenda documents that meet your search criteria.</strong><br />
Note that not all agenda documents are well tagged for searching.
</p>
</div>`
}
}
function loadMetadata(): void {
window
.fetch('metadata.json')
// eslint-disable-next-line @typescript-eslint/promise-function-async
.then((response) => {
return response.json()
})
.then((json) => {
agendaMetadata = json
agendaMetadataIsLoaded = true
return agendaMetadata
})
.then(() => {
renderAgendas()
return true
})
.catch(() => {
alert('Error loading agenda data')
})
}
loadMetadata()
function debounce(function_, timeout = 300): () => void {
let timer
return (...arguments_) => {
clearTimeout(timer)
timer = setTimeout(() => {
function_.apply(this, arguments_)
}, timeout)
}
}
const debouncedRenderAgendas = debounce(() => {
renderAgendas()
})
searchStringElement.addEventListener('keyup', debouncedRenderAgendas)
document.querySelector('form')?.addEventListener('submit', (formEvent) => {
formEvent.preventDefault()
renderAgendas()
})
})()