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

Merge navigate_to_search and navigate_to_word etc.

parent 908830c6
No related branches found
No related tags found
No related merge requests found
......@@ -29,7 +29,7 @@
<span class="search-hit">
{{data.item.label}}
</span>
({{data.item.lang? data.item.lang[1] ? "bm, nn" : {"bob": "bm", "nob": "nn"}[data.item.lang[0]] : data.item.mode}})
({{data.item.lang? data.item.lang[1] ? "bm, nn" : {"bob": "bm", "nob": "nn"}[data.item.lang[0]] : ["fulltekst","fritekst","avansert søk"][data.item.mode]}})
</template>
<template slot="no-data">
<div></div>
......@@ -79,42 +79,37 @@
// Keep full text search in the list while requesting suggestions
if (this.items[0]) {
if (this.items[0].time < time) {
this.items.splice(0,1, {q: q, label: q, time: time, mode: 'fritekst'})
if (/_|\*|\|/.test(q)) {
this.items.splice(0,1, {q: q, label: q, time: time, mode: 2})
} else {
this.items.splice(0,1, {q: q+"*", label: q, time: time, mode: 1})
}
}
}
else {
this.items.push({q: q, label: q, time: time})
this.items.splice(0,1, {q: q+"*", label: q, time: time, mode: 1})
}
let self = this
self.api.get('suggest?', {params: {q: q, dict: self.$parent.lang, n: 80, scope: 'nf'}})
.then(async (response) => {
if (self.$refs.autocomplete.searchInput == q & self.suggesting) {
let word_hits = response.data.a.n.map(item => ({q: q, match: item[0], label: item[0], time: time, lang: item[1]}))
let text_hits = response.data.a.f.map(item => ({q: q, match: item[0], label: item[0], time: time, mode: 0}))
let word_matches = response.data.a.n.map(item => ({q: q, match: item[0], label: item[0], time: time, lang: item[1]}))
let text_matches = response.data.a.f.map(item => ({q: q, match: item[0], label: item[0], time: time, mode: 0}))
let hits = []
if( q.charAt(q.length-1) === '!') hits = text_hits.concat(word_hits)
else hits = word_hits.concat(text_hits)
let suggestions = []
if( q.charAt(q.length-1) === '!') suggestions = text_matches.concat(word_matches)
else suggestions = word_matches.concat(text_matches)
if (/_|\*|\|/.test(q)) {
hits.push({q: q, label: q + ' ', time: time, mode: "mønster"})
suggestions.unshift({q: q, label: q, time: time, mode: 2})
} else {
suggestions.push({q: q+"*", label: q + ' ', time: time, mode: 1})
}
response.data.a.n.forEach((item) => {
let match = item[0]
let hit = {q: q, match: match, label: match, time: time, lang: item[1]}
hits.push(hit)
});
response.data.a.f.forEach((item) => {
let match = item[0]
let hit = {q: q, match: match, label: match, time: time, mode: "fulltekst"}
hits.push(hit)
});
// whitespace necessary because duplicates aren't allowed in the dropdown
hits.push({q: q, label: q + ' ', time: time, mode: "fritekst"})
self.items = hits
self.items = suggestions
}
self.loading = false
})
......
......@@ -87,6 +87,7 @@ const oda_api = setup({
})
const api_endpoint = process.env.VUE_APP_API_PREFIX + '/api/dict'
const oda_dev = 'https://oda.uib.no/opal/dev'
......@@ -151,16 +152,14 @@ function navigate_to_search(self, query) {
function navigate_to_word(self, word) {
let event = self.event ? self.event : {q: word,
match: word}
let query = self.event ? self.event : {q: word}
// Get article IDs
oda_api.get('articles?', {params: {lord: event.match, dict: self.lang}}).then((response) => {
event.article_ids = response.data
oda_api.get('articles?', {params: {lord: query.match || query.q, dict: self.lang}}).then((response) => {
let article_ids = response.data
let unwrapped = []
for (const d in event.article_ids) {
event.article_ids[d].forEach(i => unwrapped.push({
for (const d in article_ids) {
article_ids[d].forEach(i => unwrapped.push({
dictionary: d,
id: i
}))
......@@ -174,14 +173,17 @@ function navigate_to_word(self, word) {
.then((response) => {
self.search_results = response.map((element, index) => {
return Object.assign(element.data, {
match: event.match,
dictionary: unwrapped[index].dictionary
})
})
self.article = null
if (! self.search_results.length) {
dicts[self.lang]
self.error = `Ordet «${event.match}» finnes ikke i ${dicts}`
if (query.match) {
self.error = `Ordet «${query.match}» finnes ikke i ${dicts[self.lang]}`
} else {
self.error = `Søk på «${query.q}» gir ingen treff i ${dicts[self.lang]}. Søk med * inne i ordet dersom du er usikker på skrivemåten. Bruk knappen «begge ordbøkene» om du har søkt i feil ordbok.`
}
}
else {
self.error = null
......@@ -242,35 +244,22 @@ export default {
methods: {
select_result: function (event) {
this.event = event
if (event.match) {
let route = `/${this.lang}/w/${event.match}`
this.$router.push(route)
navigate_to_word(this)
} else {
this.waiting_for_articles = true
this.article = null
event.match = '<fritekstsøk>'
let route = `/${this.lang}/search/${event.q}`
this.$router.push(route)
navigate_to_search(this, event.q)
}
let route = `/${this.lang}/search/${event.match || event.q}`
this.$router.push(route)
navigate_to_word(this)
// Tracking
let props = {query: event.q}
if (event.match) props.match = event.match
this.$plausible.trackEvent(event.update_lang ? "language" : 'dropdown selection', {
props: {
query: event.q,
match: event.match
}
props: props
})
},
update_lang_form: function (lang) {
this.lang = lang
if(this.$route.name == 'word') {
navigate_to_word(this, this.$route.params.word)
}
else if (this.$route.name == 'search') {
navigate_to_search(this, this.$route.params.query)
}
let route = `/${this.lang}/search/${this.$route.params.query}`
this.$router.push(route)
navigate_to_word(this, this.$route.params.query)
},
article_link_click: function(item) {
if (this.article && this.article.article_id == item.article_id){
......
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