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

Add new suggest API and new solution to autocomplete problem

parent 53dd68b1
No related branches found
No related tags found
No related merge requests found
......@@ -26,7 +26,7 @@
:dense="$vuetify.breakpoint.smAndDown"
>
<template v-slot:item="data">
<span class="search-hit">{{data.item.label}} </span> ({{data.item.lang_set ? Array.from(data.item.lang_set).sort().join(', ') : 'fritekstsøk'}})
<span class="search-hit">{{data.item.label}} </span> ({{data.item.lang ? data.item.lang : 'fritekstsøk'}})
</template>
</v-combobox>
</div>
......@@ -49,42 +49,26 @@
suggesting: null,
debounced: debounce(function(q, self) {
self.loading = true
return axios.get(self.endpoint + 'suggest?q=' + encodeURIComponent(q))
return axios.get(self.endpoint + 'suggest?', { params: {q: encodeURIComponent(q),
dict: this.$parent.lang,
n: 9}} )
.then(
function(response) {
if (self.suggesting) {
let hits = []
if (q == self.$refs.autocomplete.searchInput) {
response.data.forEach((item, i) => {
let match = encodeURIComponent(item.match)
response.data.forEach((item, i) => {
let hit = {q: q, word: encodeURIComponent(item[0]), label: item[0]}
hit.lang = item[1][1] ? "bm, nn" : {"bob": "bm", "nob": "nn"}[item[1][0]]
hits.push(hit)
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
})
let currentSearch = self.$refs.autocomplete.searchInput
if (q == currentSearch) {
self.items = hits
}
self.items.push({currentSearch: encodeURIComponent(currentSearch), label: currentSearch + ' '})
self.items = hits
let currentSearch = self.$refs.autocomplete.searchInput
self.items.push({currentSearch: encodeURIComponent(currentSearch), label: currentSearch + ' '})
}
}
self.loading = false
})
......@@ -101,21 +85,20 @@
},
select(item) {
if (item) {
if (typeof item === 'string') {
item = {"q": encodeURIComponent(item)}
}
this.items = []
this.suggesting = false
if (item.q === self.$refs.autocomplete.searchInput) {
this.items = []
this.suggesting = false
this.$emit('submit', item)
let self = this
setTimeout(() => self.$refs.autocomplete.$refs.input.select(), 1)
this.$emit('submit', item)
let self = this
setTimeout(() => self.$refs.autocomplete.$refs.input.select(), 1)
}
}
}
},
methods: {
run_query(q) {
this.items = []
this.suggesting = true
this.debounced(q, this)
}
......
......@@ -29,7 +29,7 @@
</v-radio>
</v-radio-group>
</div>
<Autocomplete @submit="select_result" :endpoint="api_pref">
<Autocomplete @submit="select_result" :endpoint="oda_endpoint">
</Autocomplete>
</div>
<div id="spinner" v-if="waiting">
......@@ -74,6 +74,7 @@ import SearchResults from './SearchResults.vue'
import Autocomplete from './Autocomplete.vue'
var api_endpoint = process.env.VUE_APP_API_PREFIX + '/api/dict'
const oda_endpoint = 'https://oda.uib.no/opal-api/'
function compare_by_hgno(lemma_text) {
return function(art1, art2) {
......@@ -188,6 +189,9 @@ export default {
},
api_pref: function() {
return api_endpoint + '/' + this.lang + '/article/'
},
oda_endpoint: function() {
return oda_endpoint
}
},
components: {
......
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