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.

83 lines
2.0 KiB

  1. "use strict";
  2. if($('#search-input').length) {
  3. $('#search-input').focus();
  4. $('.searchresults').css('display', 'block');
  5. var vueHeader = new Vue({
  6. el: '#header-container',
  7. data: {
  8. searchq: '',
  9. searchres: [],
  10. searchsuggest: [],
  11. searchload: 0,
  12. searchactive: false,
  13. searchmoveidx: 0,
  14. searchmovekey: '',
  15. searchmovearr: []
  16. },
  17. watch: {
  18. searchq: (val, oldVal) => {
  19. vueHeader.searchmoveidx = 0;
  20. if(val.length >= 3) {
  21. vueHeader.searchactive = true;
  22. vueHeader.searchload++;
  23. socket.emit('search', { terms: val }, (data) => {
  24. vueHeader.searchres = data.match;
  25. vueHeader.searchsuggest = data.suggest;
  26. vueHeader.searchmovearr = _.concat([], vueHeader.searchres, vueHeader.searchsuggest);
  27. if(vueHeader.searchload > 0) { vueHeader.searchload--; }
  28. });
  29. } else {
  30. vueHeader.searchactive = false;
  31. vueHeader.searchres = [];
  32. vueHeader.searchsuggest = [];
  33. vueHeader.searchmovearr = [];
  34. vueHeader.searchload = 0;
  35. }
  36. },
  37. searchmoveidx: (val, oldVal) => {
  38. if(val > 0) {
  39. vueHeader.searchmovekey = (vueHeader.searchmovearr[val - 1]) ?
  40. 'res.' + vueHeader.searchmovearr[val - 1]._id :
  41. 'sug.' + vueHeader.searchmovearr[val - 1];
  42. } else {
  43. vueHeader.searchmovekey = '';
  44. }
  45. }
  46. },
  47. methods: {
  48. useSuggestion: (sug) => {
  49. vueHeader.searchq = sug;
  50. },
  51. closeSearch: () => {
  52. vueHeader.searchq = '';
  53. },
  54. moveSelectSearch: () => {
  55. if(vueHeader.searchmoveidx < 1) { return; }
  56. let i = vueHeader.searchmoveidx - 1;
  57. if(vueHeader.searchmovearr[i]) {
  58. window.location.assign('/' + vueHeader.searchmovearr[i]._id);
  59. } else {
  60. vueHeader.searchq = vueHeader.searchmovearr[i];
  61. }
  62. },
  63. moveDownSearch: () => {
  64. if(vueHeader.searchmoveidx < vueHeader.searchmovearr.length) {
  65. vueHeader.searchmoveidx++;
  66. }
  67. },
  68. moveUpSearch: () => {
  69. if(vueHeader.searchmoveidx > 0) {
  70. vueHeader.searchmoveidx--;
  71. }
  72. }
  73. }
  74. });
  75. $('main').on('click', vueHeader.closeSearch);
  76. }