SearchForm.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. setTimeout(function(){
  37. console.log('update autocomplete value after settimeout');
  38. this.autocomplete = ui.item.value
  39. if(this.typed !== this.keys && this.autocomplete !== this.term){
  40. this.submit()
  41. }
  42. }.bind(this), 1)
  43. }
  44. },
  45. directives: {
  46. focus: {
  47. // directive definition
  48. inserted: function (el) {
  49. el.focus()
  50. }
  51. }
  52. },
  53. beforeMount() {
  54. // console.log('SearchForm beforeMount');
  55. if(this._props.form){
  56. // console.log('SearchForm beforeMount if this._props.form ok');
  57. this.template = Vue.compile(this._props.form)
  58. // https://github.com/vuejs/vue/issues/9911
  59. this.$options.staticRenderFns = [];
  60. this._staticTrees = [];
  61. this.template.staticRenderFns.map(fn => (this.$options.staticRenderFns.push(fn)));
  62. }
  63. },
  64. watch: {
  65. typed(n, o){
  66. console.log('typed changed', o, n);
  67. // is changed also when autocomplete change it ...
  68. this.autocomplete = null
  69. },
  70. keys(n, o){
  71. console.log('keys changed', o, n);
  72. this.typed = n
  73. },
  74. term(n, o){
  75. console.log('autocomplete changed', o, n);
  76. this.autocomplete = n
  77. }
  78. },
  79. created() {
  80. this.typed = this.keys
  81. this.autocomplete = this.term
  82. },
  83. mounted(){
  84. // console.log('SearchForm mounted');
  85. Drupal.attachBehaviors(this.$el);
  86. // get the search input
  87. let $input = this.$el.querySelector('#edit-search')
  88. // // focus on input
  89. // $input.focus()
  90. // Catch the jquery ui events for autocmplete widget
  91. jQuery($input).on('autocompleteselect', this.onAutoCompleteSelect);
  92. },
  93. render(h) {
  94. // console.log('searchForm render');
  95. if(!this.template){
  96. return h('span', 'Loading ...')
  97. }else{
  98. return this.template.render.call(this)
  99. }
  100. }
  101. }
  102. </script>
  103. <style lang="scss" scoped>
  104. </style>