SearchForm.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <script>
  2. import Vue from 'vue'
  3. import router from 'vuejs/route'
  4. import { mapState } from 'vuex'
  5. export default {
  6. router,
  7. props: ['form'],
  8. data() {
  9. return {
  10. template: null,
  11. typed: null,
  12. autocomplete: null,
  13. $input: null
  14. // basePath: drupalSettings.path.baseUrl + drupalSettings.path.pathPrefix
  15. }
  16. },
  17. computed: {
  18. ...mapState({
  19. keys: state => state.Search.keys,
  20. term: state => state.Search.term
  21. })
  22. },
  23. methods: {
  24. submit() {
  25. console.log("search submited", this.typed, this.autocomplete)
  26. // unfocus the text input to hide the keyboard on mobile device
  27. this.$input.blur()
  28. // New search is triggered by Base.vue with router (which will also fill the store)
  29. this.$router.push({name:'base', query:{keys:this.typed,term:this.autocomplete}})
  30. // this.$router.push({
  31. // path:`${this.basePath}/base`,
  32. // query:{keys:this.typed,term:this.autocomplete}
  33. // })
  34. },
  35. onAutoCompleteSelect(event, ui){
  36. event.preventDefault();
  37. console.log('autoCompleteSelect', event, ui)
  38. this.typed = ui.item.label
  39. // we have to wait for typed watch to reset autocomplete before setting it
  40. setTimeout(function(){
  41. console.log('update autocomplete value after settimeout')
  42. this.autocomplete = ui.item.value
  43. if (this.typed !== this.keys || this.autocomplete !== this.term) {
  44. this.submit()
  45. }
  46. }.bind(this), 1)
  47. }
  48. },
  49. directives: {
  50. focus: {
  51. // directive definition
  52. inserted: function (el) {
  53. // do not focus the input as it popup the keyboard on mobile device
  54. // el.focus()
  55. }
  56. }
  57. },
  58. beforeMount() {
  59. // console.log('SearchForm beforeMount')
  60. if (this._props.form) {
  61. // console.log('SearchForm beforeMount if this._props.form ok')
  62. this.template = Vue.compile(this._props.form)
  63. // https://github.com/vuejs/vue/issues/9911
  64. this.$options.staticRenderFns = [];
  65. this._staticTrees = [];
  66. this.template.staticRenderFns.map(fn => (this.$options.staticRenderFns.push(fn)));
  67. }
  68. },
  69. watch: {
  70. typed(n, o){
  71. console.log('typed changed', o, n)
  72. // is changed also when autocomplete change it ...
  73. this.autocomplete = null
  74. },
  75. keys(n, o){
  76. console.log('keys changed', o, n)
  77. this.typed = n
  78. },
  79. term(n, o){
  80. console.log('autocomplete changed', o, n)
  81. this.autocomplete = n
  82. }
  83. },
  84. created() {
  85. this.typed = this.keys
  86. this.autocomplete = this.term
  87. },
  88. mounted(){
  89. // console.log('SearchForm mounted')
  90. Drupal.attachBehaviors(this.$el);
  91. // get the search input
  92. this.$input = this.$el.querySelector('#edit-search')
  93. // // focus on input
  94. // $input.focus()
  95. // Catch the jquery ui events for autocmplete widget
  96. jQuery(this.$input).on('autocompleteselect', this.onAutoCompleteSelect);
  97. },
  98. render(h) {
  99. // console.log('searchForm render')
  100. if (!this.template) {
  101. return h('span', 'Loading ...')
  102. } else {
  103. return this.template.render.call(this)
  104. }
  105. }
  106. }
  107. </script>
  108. <style lang="scss" scoped>
  109. </style>