You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.1 KiB

  1. <template lang="pug">
  2. .nav-item
  3. p.control(v-bind:class='{ "is-loading": searchload > 0 }')
  4. 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")')
  5. transition(name='searchresults')
  6. .searchresults(v-show='searchactive', v-cloak)
  7. p.searchresults-label {{ $t('search.results') }}
  8. ul.searchresults-list
  9. li(v-if='searchres.length === 0')
  10. a: em {{ $t('search.nomatch') }}
  11. li(v-for='sres in searchres', v-bind:class='{ "is-active": searchmovekey === "res." + sres.entryPath }')
  12. a(v-bind:href='siteRoot + "/" + sres.entryPath') {{ sres.title }}
  13. p.searchresults-label(v-if='searchsuggest.length > 0') {{ $t('search.didyoumean') }}
  14. ul.searchresults-list(v-if='searchsuggest.length > 0')
  15. li(v-for='sug in searchsuggest', v-bind:class='{ "is-active": searchmovekey === "sug." + sug }')
  16. a(v-on:click='useSuggestion(sug)') {{ sug }}
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. searchq: '',
  23. searchres: [],
  24. searchsuggest: [],
  25. searchload: 0,
  26. searchactive: false,
  27. searchmoveidx: 0,
  28. searchmovekey: '',
  29. searchmovearr: []
  30. }
  31. },
  32. watch: {
  33. searchq: function (val, oldVal) {
  34. let self = this
  35. self.searchmoveidx = 0
  36. if (val.length >= 3) {
  37. self.searchactive = true
  38. self.searchload++
  39. socket.emit('search', { terms: val }, (data) => {
  40. self.searchres = data.match
  41. self.searchsuggest = data.suggest
  42. self.searchmovearr = self._.concat([], self.searchres, self.searchsuggest)
  43. if (self.searchload > 0) { self.searchload-- }
  44. })
  45. } else {
  46. self.searchactive = false
  47. self.searchres = []
  48. self.searchsuggest = []
  49. self.searchmovearr = []
  50. self.searchload = 0
  51. }
  52. },
  53. searchmoveidx: function (val, oldVal) {
  54. if (val > 0) {
  55. this.searchmovekey = (this.searchmovearr[val - 1])
  56. ? 'res.' + this.searchmovearr[val - 1].entryPath
  57. : 'sug.' + this.searchmovearr[val - 1]
  58. } else {
  59. this.searchmovekey = ''
  60. }
  61. }
  62. },
  63. methods: {
  64. useSuggestion: function (sug) {
  65. this.searchq = sug
  66. },
  67. closeSearch: function () {
  68. this.searchq = ''
  69. },
  70. moveSelectSearch: function () {
  71. if (this.searchmoveidx < 1) { return }
  72. let i = this.searchmoveidx - 1
  73. if (this.searchmovearr[i]) {
  74. window.location.assign(siteRoot + '/' + this.searchmovearr[i].entryPath)
  75. } else {
  76. this.searchq = this.searchmovearr[i]
  77. }
  78. },
  79. moveDownSearch: function () {
  80. if (this.searchmoveidx < this.searchmovearr.length) {
  81. this.searchmoveidx++
  82. }
  83. },
  84. moveUpSearch: function () {
  85. if (this.searchmoveidx > 0) {
  86. this.searchmoveidx--
  87. }
  88. }
  89. },
  90. mounted: function () {
  91. let self = this
  92. $('main').on('click', self.closeSearch)
  93. }
  94. }
  95. </script>