search.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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='"/" + 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. import * as _ from 'lodash'
  20. import * as $ from 'jquery'
  21. export default {
  22. data () {
  23. return {
  24. searchq: '',
  25. searchres: [],
  26. searchsuggest: [],
  27. searchload: 0,
  28. searchactive: false,
  29. searchmoveidx: 0,
  30. searchmovekey: '',
  31. searchmovearr: []
  32. }
  33. },
  34. watch: {
  35. searchq: function (val, oldVal) {
  36. let self = this
  37. self.searchmoveidx = 0
  38. if (val.length >= 3) {
  39. self.searchactive = true
  40. self.searchload++
  41. socket.emit('search', { terms: val }, (data) => {
  42. self.searchres = data.match
  43. self.searchsuggest = data.suggest
  44. self.searchmovearr = _.concat([], self.searchres, self.searchsuggest)
  45. if (self.searchload > 0) { self.searchload-- }
  46. })
  47. } else {
  48. self.searchactive = false
  49. self.searchres = []
  50. self.searchsuggest = []
  51. self.searchmovearr = []
  52. self.searchload = 0
  53. }
  54. },
  55. searchmoveidx: function (val, oldVal) {
  56. if (val > 0) {
  57. this.searchmovekey = (this.searchmovearr[val - 1])
  58. ? 'res.' + this.searchmovearr[val - 1].entryPath
  59. : 'sug.' + this.searchmovearr[val - 1]
  60. } else {
  61. this.searchmovekey = ''
  62. }
  63. }
  64. },
  65. methods: {
  66. useSuggestion: function (sug) {
  67. this.searchq = sug
  68. },
  69. closeSearch: function() {
  70. this.searchq = ''
  71. },
  72. moveSelectSearch: function () {
  73. if (this.searchmoveidx < 1) { return }
  74. let i = this.searchmoveidx - 1
  75. if (this.searchmovearr[i]) {
  76. window.location.assign('/' + this.searchmovearr[i].entryPath)
  77. } else {
  78. this.searchq = this.searchmovearr[i]
  79. }
  80. },
  81. moveDownSearch: function () {
  82. if (this.searchmoveidx < this.searchmovearr.length) {
  83. this.searchmoveidx++
  84. }
  85. },
  86. moveUpSearch: function () {
  87. if (this.searchmoveidx > 0) {
  88. this.searchmoveidx--
  89. }
  90. }
  91. },
  92. mounted: function () {
  93. let self = this
  94. $('main').on('click', self.closeSearch)
  95. }
  96. }
  97. </script>