Skip to content
Snippets Groups Projects
Commit e24d2894 authored by Henrik Askjer's avatar Henrik Askjer
Browse files

Merge branch 'fix_premature_autocomplete' into 'master'

Keep auto-select-first

See merge request spraksamlingane/beta.ordbok.uib.no!43
parents a2eacf9e 68cbe4e5
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,7 @@
return-object
rounded
hide-no-data
auto-select-first
no-filter
hide-details
label="Søk..."
......@@ -37,7 +38,7 @@
export default {
props: {
endpoint: String
endpoint: String,
},
data: function() {
return {
......@@ -45,14 +46,15 @@
items: [],
search: null,
select: null,
suggesting: null,
debounced: debounce(function(q, self) {
self.loading = true
return axios.get(self.endpoint + 'suggest?q=' + encodeURIComponent(q))
.then(
function(response) {
if (self.loading) {
if (self.suggesting) {
let hits = []
if (q == self.search) {
if (q == self.$refs.autocomplete.searchInput) {
response.data.forEach((item, i) => {
let match = encodeURIComponent(item.match)
......@@ -69,16 +71,22 @@
});
hits.reverse()
hits = hits.slice(0, 9)
}
hits.sort( (h1, h2) => {
hits.sort( (h1, h2) => {
let val1 = h1.label.length * 10 + (h1.label[0].toLowerCase() === h1.label[0] ? 0 : 1)
let val2 = h2.label.length * 10 + (h2.label[0].toLowerCase() === h2.label[0] ? 0 : 1)
return val1 - val2
})
self.items = hits
self.loading = false
let currentSearch = self.$refs.autocomplete.searchInput
if (q == currentSearch) {
self.items = hits
}
self.items.push({currentSearch: encodeURIComponent(currentSearch), label: currentSearch + ' '})
}
}
self.loading = false
})
}, 100)
}
......@@ -96,15 +104,18 @@
if (typeof item === 'string') {
item = {"q": encodeURIComponent(item)}
}
this.loading = false
this.items = []
this.suggesting = false
this.$emit('submit', item)
let self = this
setTimeout(() => self.$refs.autocomplete.$refs.input.select(), 1)
setTimeout(() => this.$refs.autocomplete.blur(), 1)
}
}
},
methods: {
run_query(q) {
this.items = []
this.suggesting = true
this.debounced(q, this)
}
},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment