SearchForm.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 clicked", 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. this.autocomplete = ui.item.value
  37. }
  38. },
  39. beforeMount() {
  40. // console.log('SearchForm beforeMount');
  41. if(this._props.form){
  42. // console.log('SearchForm beforeMount if this._props.form ok');
  43. this.template = Vue.compile(this._props.form)
  44. // https://github.com/vuejs/vue/issues/9911
  45. this.$options.staticRenderFns = [];
  46. this._staticTrees = [];
  47. this.template.staticRenderFns.map(fn => (this.$options.staticRenderFns.push(fn)));
  48. }
  49. },
  50. watch: {
  51. keys(n, o){
  52. console.log('keys changed', n, o);
  53. this.typed = n
  54. },
  55. term(n, o){
  56. console.log('autocomplete changed', n, o);
  57. this.autocomplete = n
  58. }
  59. },
  60. created() {
  61. this.typed = this.keys
  62. this.autocomplete = this.term
  63. },
  64. mounted(){
  65. // console.log('SearchForm mounted');
  66. Drupal.attachBehaviors(this.$el);
  67. // get the search input
  68. let $input = this.$el.querySelector('#edit-search')
  69. // focus on input
  70. $input.focus()
  71. // Catch the jquery ui events for autocmplete widget
  72. jQuery($input).on('autocompleteselect', this.onAutoCompleteSelect);
  73. },
  74. render(h) {
  75. // console.log('searchForm render');
  76. if(!this.template){
  77. return h('span', 'Loading ...')
  78. }else{
  79. return this.template.render.call(this)
  80. }
  81. }
  82. }
  83. </script>
  84. <style lang="scss" scoped>
  85. </style>