forked from quarkusio/quarkusio.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguides-app.js
301 lines (294 loc) · 9.7 KB
/
guides-app.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { createApp } from './vue.esm-browser.prod.js'
// https://blog.logrocket.com/debounce-throttle-vue/
function debounce(wait, fn) {
let timer;
return function(...args) {
if (timer) {
clearTimeout(timer) // clear any pre-existing timer
}
const context = this // get the current context
timer = setTimeout(() => {
fn.apply(context, args) // call the function if time expires
}, wait)
}
}
function concat(fn1, fn2) {
return function(...args) {
fn1.apply(this, args)
fn2.apply(this, args)
}
}
const appSelector = '#guides-app'
const appElement = document.querySelector(appSelector);
const app = createApp({
props: {
searchApiServer: String,
quarkusVersion: String,
language: String,
initialTimeout: Number,
moreTimeout: Number,
minChars: Number
},
data() {
return {
loading: false,
search: {
input: {
},
page: null,
result: {
hits: [],
hasMoreHits: false
}
},
guidesPathToCardHtmlElement: {
}
}
},
watch: {
'search.input.q': {
handler: concat(
// Without debouncing, we want to cancel the previous search and mark the view as "loading",
// so that we don't mistakenly display "no results" while debouncing the initial search.
// See https://github.com/quarkusio/search.quarkus.io/issues/200
function(newValue, oldValue) {
this.resetAndMarkLoading()
},
// "debounce" makes sure we only run search ~300ms after the user is done typing the text
// WARNING: we really do want to debounce here, NOT in setters,
// because debouncing in setters leads to data in input forms being refreshed after the timeout,
// causing problems when typing text relatively fast.
debounce(300, function(newValue, oldValue) {
this.resetAndSearch()
})
)
},
'search.input.categories': {
handler(newValue, oldValue) {
this.resetAndSearch()
}
}
},
computed: {
text: {
get() {
return this.search.input.q
},
set(val) {
this.search.input.q = val
}
},
category: {
get() {
// Turn "no entry" into "", because that's how the select box refers to "all categories"
return this.search.input.categories || ""
},
set(val) {
if (val) {
this.search.input.categories = val
}
// Turn null/"" into "no entry", because not specifying the category
// is how we get the search service to return all categories
else {
delete this.search.input.categories
}
}
},
hasInput() {
return this.search.input.q && this.search.input.q.length >= this.minChars || this.search.input.categories
},
hasInputWithTooFewChars() {
return this.search.input.q && this.search.input.q.length < this.minChars
},
hasHits() {
return this.search.result.hits.length > 0
},
searchHits() {
return this.search.result.hits
}
},
mounted() {
const appElement = this.$el.parentElement
// Retrieve cards from the static HTML, so that we can use them to display search results.
let cardSelector = '.docs-card'
let cards = appElement.querySelectorAll(cardSelector)
if (cards.length == 0) {
// Fallback for older versions of the docs
cardSelector = '.card'
cards = appElement.querySelectorAll(cardSelector)
}
this.guidesPathToCardHtmlElement = new Map(Array.from(cards)
.map(element => {
const link = element.querySelector('h4 a')
if (link) {
// new versions:
const url = link.getAttribute('href');
return [
new URL(link.href).pathname,
{
url: url,
title: link.innerHTML,
type: [...element.classList]
.filter(clazz => clazz.endsWith("bkg"))
.map(clazz => clazz.substring(0, clazz.length - "bkg".length))
.at(0),
summary: element.querySelector('div .description').innerHTML,
keywords: element.querySelector('div .keywords').innerHTML,
categories: element.querySelector('div .categories').innerHTML,
origin: element.querySelector('div .origin')?.innerHTML
}];
} else {
// older Quarkus versions:
const url = element.querySelector('a').getAttribute('href')
return [
url,
{
url: url,
title: element.querySelector('p.title').innerHTML,
summary: element.querySelector('div.description').innerHTML,
keywords: element.querySelector('div.keywords').innerHTML,
origin: element.querySelector('div.origin')?.innerHTML
}];
}
}))
// Load more results on scroll
document.addEventListener('scroll', e => {
if (!this.search.result.hasMoreHits) {
// No more hits to fetch.
return
}
const resultCards = appElement.querySelectorAll('.results ' + cardSelector)
const lastResultCard = resultCards.length == 0 ? null : resultCards[resultCards.length - 1]
if (!lastResultCard) {
// No result card is being displayed at the moment.
return
}
const scrollElement = document.documentElement // Scroll bar is on the <html> element
const bottomOfViewport = scrollElement.scrollTop + scrollElement.clientHeight
const topOfLastResultCard = lastResultCard.offsetTop
if (bottomOfViewport >= topOfLastResultCard) {
this.searchMore()
}
})
},
methods: {
async resetAndMarkLoading() {
if (this.loading) {
this.loading.abort()
}
this.loading = new AbortController()
this._resetResults()
},
async resetAndSearch() {
this.resetAndMarkLoading()
await this._searchBatch(this.loading, this.initialTimeout)
},
async searchMore() {
if (this.loading) {
return // Already searching
}
this.loading = new AbortController();
this.search.page = this.search.page + 1
await this._searchBatch(this.loading, this.moreTimeout)
},
_resetResults() {
this.search.page = 0
this.search.result.hits = []
this.search.result.hasMoreHits = false
},
async _searchBatch(controller, timeout) {
try {
if (!this.hasInput) {
// No input => no search
return
}
if (this.hasInputWithTooFewChars) {
throw 'Too few characters'
}
const queryParams = {
page: this.search.page,
version: this.quarkusVersion,
language: this.language,
contentSnippets: 2,
contentSnippetsLength: 120,
highlightCssClass: 'highlighted'
}
Object.assign(queryParams, this.search.input)
const result = await this._jsonFetch(controller, 'GET', queryParams, timeout)
this.search.result.hits = this.search.result.hits.concat(this._processHits(result.hits))
this.search.result.hasMoreHits = result.hits.length > 0
}
catch(error) {
console.error('Could not run search: ' + error)
if (this.loading != controller) {
// A concurrent search erased ours; most likely input changed while waiting for results.
// Ignore this search and let the concurrent one reset the data as it sees fit.
return
}
this._resetResults()
// Fall back to Javascript in-page search
const hits = this._searchUsingJavascript()
this.search.result.hits = hits
}
finally {
if (this.loading == controller) {
this.loading = null
}
}
},
_processHits(serverHits) {
return serverHits.map(hit => {
hit.content = hit?.content.map(paragraph => `...${paragraph}...`)
return hit;
})
},
async _jsonFetch(controller, method, queryParams, timeout) {
const timeoutId = setTimeout(() => controller.abort(), timeout)
const response = await fetch(`${this.searchApiServer}api/guides/search?${new URLSearchParams( queryParams )}`, {
method: method,
signal: controller.signal,
body: null
})
clearTimeout(timeoutId)
if (response.ok) {
return await response.json()
}
else {
throw 'Response status is ' + response.status + '; response: ' + await response.text()
}
},
_searchUsingJavascript() {
const terms = this.search.input.q ? this.search.input.q.split(' ').map(token => token.trim()) : null
const categories = this.search.input.categories
return Array.from(this.guidesPathToCardHtmlElement)
.filter(([path, card]) => this._javascriptFilter(card, terms, categories))
.map(([_, card]) => card)
},
_javascriptFilter(card, terms, categories) {
let match = true
if (match && categories) {
match = this._containsAllCaseInsensitive(card.categories, categories)
}
if (match && terms) {
match = this._containsAllCaseInsensitive(`${card.keywords}${card.summary}${card.title}${card.categories}`, terms)
}
return match
},
_containsAllCaseInsensitive(elem, terms) {
const text = (elem ? elem : '').toLowerCase();
for (let i in terms) {
if (text.indexOf(terms[i].toLowerCase()) < 0) {
return false
}
}
return true
}
}
},
// Pass data-* elements as props: https://stackoverflow.com/a/64010905/6692043
{ ...appElement.dataset }
)
app.mount(appSelector)
app.config.errorHandler = (err, instance, info) => {
console.error(err)
}