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.

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