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 }')
|
2017-07-21 01:20:40 +00:00
|
|
|
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>
|
2017-10-30 01:36:05 +00:00
|
|
|
/* global siteRoot, socket, $ */
|
|
|
|
|
2017-06-24 19:54:31 +00:00
|
|
|
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) => {
|
2017-07-21 01:20:40 +00:00
|
|
|
self.searchres = self._.map(data.match, m => {
|
|
|
|
m.entryPath = `${siteRoot}/${m.entryPath}`
|
|
|
|
return m
|
|
|
|
})
|
2017-06-24 19:54:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
2017-06-24 19:54:31 +00:00
|
|
|
searchmoveidx: function (val, oldVal) {
|
|
|
|
if (val > 0) {
|
2017-10-30 01:36:05 +00:00
|
|
|
this.searchmovekey = (this.searchmovearr[val - 1]) ?
|
|
|
|
'res.' + this.searchmovearr[val - 1].entryPath :
|
|
|
|
'sug.' + this.searchmovearr[val - 1]
|
2017-06-24 19:54:31 +00:00
|
|
|
} else {
|
|
|
|
this.searchmovekey = ''
|
2017-05-21 03:21:16 +00:00
|
|
|
}
|
2017-06-24 19:54:31 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
useSuggestion: function (sug) {
|
|
|
|
this.searchq = sug
|
|
|
|
},
|
|
|
|
closeSearch: function () {
|
|
|
|
this.searchq = ''
|
2017-05-21 03:21:16 +00:00
|
|
|
},
|
2017-06-24 19:54:31 +00:00
|
|
|
moveSelectSearch: function () {
|
|
|
|
if (this.searchmoveidx < 1) { return }
|
|
|
|
let i = this.searchmoveidx - 1
|
2017-05-21 03:21:16 +00:00
|
|
|
|
2017-06-24 19:54:31 +00:00
|
|
|
if (this.searchmovearr[i]) {
|
2017-07-21 01:20:40 +00:00
|
|
|
window.location.assign(this.searchmovearr[i].entryPath)
|
2017-06-24 19:54:31 +00:00
|
|
|
} else {
|
|
|
|
this.searchq = this.searchmovearr[i]
|
2017-05-21 03:21:16 +00:00
|
|
|
}
|
|
|
|
},
|
2017-06-24 19:54:31 +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
|
|
|
}
|
2017-06-24 19:54:31 +00:00
|
|
|
},
|
|
|
|
mounted: function () {
|
|
|
|
let self = this
|
|
|
|
$('main').on('click', self.closeSearch)
|
2017-05-21 03:21:16 +00:00
|
|
|
}
|
2017-06-24 19:54:31 +00:00
|
|
|
}
|
2017-05-21 03:21:16 +00:00
|
|
|
</script>
|