SearchForm.vue 2.9 KB

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