1
0
Fork 0
mirror of https://github.com/Findus23/RPGnotes.git synced 2024-09-19 15:43:45 +02:00
RPGnotes/static/js/autocomplete.js
2022-04-12 00:07:34 +02:00

32 lines
756 B
JavaScript

const form = document.getElementById("autocomplete-form")
form.addEventListener("submit", function (e) {
e.preventDefault()
})
new Autocomplete('#autocomplete', {
search: input => {
const url = `/search/autocomplete/?q=${encodeURI(input)}`
return new Promise(resolve => {
if (input.length === 0) {
return resolve([])
}
fetch(url)
.then(response => response.json())
.then(data => {
resolve(data)
})
})
},
getResultValue: result => result.name,
onSubmit: result => {
if (!result) {
form.submit()
return
}
location.href = result.url
}
})