Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<script>
import {normalizeQuery, parseTokens, compileTokens} from '@/search'
export default {
props: {
defaultQuery: {type: String, default: '', required: false},
},
methods: {
getTokenValue (key, fallback) {
let matching = this.search.tokens.filter(t => {
return t.field === key
})
if (matching.length > 0) {
return matching[0].value
}
return fallback
},
addSearchToken (key, value) {
if (!value) {
// we remove existing matching tokens, if any
this.search.tokens = this.search.tokens.filter(t => {
return t.field != key
})
} else {
let existing = this.search.tokens.filter(t => {
return t.field === key
})
if (existing.length > 0) {
// we replace the value in existing tokens, if any
existing.forEach(t => {
t.value = value
})
} else {
// we add a new token
this.search.tokens.push({field: key, value})
}
}
},
},
watch: {
'search.query' (newValue) {
this.search.tokens = parseTokens(normalizeQuery(newValue))
},
'search.tokens': {
handler (newValue) {
this.search.query = compileTokens(newValue)
this.page = 1
this.fetchData()
},
deep: true
},
}
}
</script>