diff --git a/src/components/Autocomplete.vue b/src/components/Autocomplete.vue index 598f48a654f488084f43b370f9bccff1162dcd78..f3cc318cec579b7952b07e05bd28d03f16385ca0 100644 --- a/src/components/Autocomplete.vue +++ b/src/components/Autocomplete.vue @@ -50,33 +50,35 @@ return axios.get(self.endpoint + 'suggest?q=' + encodeURIComponent(q)) .then( function(response) { - let hits = [] - if (q == self.search) { - response.data.forEach((item, i) => { - let match = encodeURIComponent(item.match) + if (self.loading) { + let hits = [] + if (q == self.search) { + response.data.forEach((item, i) => { + let match = encodeURIComponent(item.match) - if (! hits[0] || hits[0].word != match) { - hits.splice(0, 0, {q: encodeURIComponent(q), lang_set: new Set(), word: match, articles: []}) - } - hits[0].lang_set.add(item.dictionary == 'bob' ? 'bm' : 'nn') - hits[0].articles.push(item) - }); - hits.forEach(function (hit) { - if (hit.lang_set) { - hit.label = decodeURIComponent(hit.word) - } - }); - hits.reverse() - hits = hits.slice(0, 9) - } - 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 - }) + if (! hits[0] || hits[0].word != match) { + hits.splice(0, 0, {q: encodeURIComponent(q), lang_set: new Set(), word: match, articles: []}) + } + hits[0].lang_set.add(item.dictionary == 'bob' ? 'bm' : 'nn') + hits[0].articles.push(item) + }); + hits.forEach(function (hit) { + if (hit.lang_set) { + hit.label = decodeURIComponent(hit.word) + } + }); + hits.reverse() + hits = hits.slice(0, 9) + } + 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 + self.items = hits + self.loading = false + } }) }, 100) } @@ -90,13 +92,15 @@ } }, select(item) { - if (typeof item === 'string') { - item = {"q": encodeURIComponent(item)} + if (item) { + if (typeof item === 'string') { + item = {"q": encodeURIComponent(item)} + } + this.loading = false + this.$emit('submit', item) + let self = this + setTimeout(() => self.$refs.autocomplete.$refs.input.select(), 1) } - this.$emit('submit', item) - this.$refs.autocomplete.blur() - let self = this - setTimeout(() => self.$refs.autocomplete.$refs.input.select(), 1) } }, methods: {