wikijs-fork/client/js/components/search.vue

102 lines
3.2 KiB
Vue
Raw Normal View History

2017-05-21 03:21:16 +00:00
<template lang="pug">
.nav-item
p.control(v-bind:class='{ "is-loading": searchload > 0 }')
input.input#search-input(type='text', v-model='searchq', autofocus, @keyup.esc='closeSearch', @keyup.down='moveDownSearch', @keyup.up='moveUpSearch', @keyup.enter='moveSelectSearch', debounce='400', v-bind:placeholder='$t("search.placeholder")')
transition(name='searchresults')
.searchresults(v-show='searchactive', v-cloak)
p.searchresults-label {{ $t('search.results') }}
ul.searchresults-list
li(v-if='searchres.length === 0')
a: em {{ $t('search.nomatch') }}
li(v-for='sres in searchres', v-bind:class='{ "is-active": searchmovekey === "res." + sres.entryPath }')
a(v-bind:href='sres.entryPath') {{ sres.title }}
2017-05-21 03:21:16 +00:00
p.searchresults-label(v-if='searchsuggest.length > 0') {{ $t('search.didyoumean') }}
ul.searchresults-list(v-if='searchsuggest.length > 0')
li(v-for='sug in searchsuggest', v-bind:class='{ "is-active": searchmovekey === "sug." + sug }')
a(v-on:click='useSuggestion(sug)') {{ sug }}
</template>
<script>
export default {
data() {
return {
searchq: '',
searchres: [],
searchsuggest: [],
searchload: 0,
searchactive: false,
searchmoveidx: 0,
searchmovekey: '',
searchmovearr: []
}
},
watch: {
searchq: function (val, oldVal) {
let self = this
self.searchmoveidx = 0
if (val.length >= 3) {
self.searchactive = true
self.searchload++
socket.emit('search', { terms: val }, (data) => {
self.searchres = self._.map(data.match, m => {
m.entryPath = `${siteRoot}/${m.entryPath}`
return m
})
self.searchsuggest = data.suggest
self.searchmovearr = self._.concat([], self.searchres, self.searchsuggest)
if (self.searchload > 0) { self.searchload-- }
})
} else {
self.searchactive = false
self.searchres = []
self.searchsuggest = []
self.searchmovearr = []
self.searchload = 0
2017-05-21 03:21:16 +00:00
}
},
searchmoveidx: function (val, oldVal) {
if (val > 0) {
this.searchmovekey = (this.searchmovearr[val - 1])
? 'res.' + this.searchmovearr[val - 1].entryPath
: 'sug.' + this.searchmovearr[val - 1]
} else {
this.searchmovekey = ''
2017-05-21 03:21:16 +00:00
}
}
},
methods: {
useSuggestion: function (sug) {
this.searchq = sug
},
closeSearch: function () {
this.searchq = ''
2017-05-21 03:21:16 +00:00
},
moveSelectSearch: function () {
if (this.searchmoveidx < 1) { return }
let i = this.searchmoveidx - 1
2017-05-21 03:21:16 +00:00
if (this.searchmovearr[i]) {
window.location.assign(this.searchmovearr[i].entryPath)
} else {
this.searchq = this.searchmovearr[i]
2017-05-21 03:21:16 +00:00
}
},
moveDownSearch: function () {
if (this.searchmoveidx < this.searchmovearr.length) {
this.searchmoveidx++
}
},
moveUpSearch: function () {
if (this.searchmoveidx > 0) {
this.searchmoveidx--
}
2017-05-21 03:21:16 +00:00
}
},
mounted: function () {
let self = this
$('main').on('click', self.closeSearch)
2017-05-21 03:21:16 +00:00
}
}
2017-05-21 03:21:16 +00:00
</script>