SearchForm.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <script>
  2. import Vue from 'vue'
  3. import { mapState, mapActions } from 'vuex'
  4. export default {
  5. props: ['form'],
  6. data() {
  7. return {
  8. template: null
  9. // keys: "",
  10. // autocomplete: ""
  11. }
  12. },
  13. computed: {
  14. // ...mapState(['Search'])
  15. // ...mapState({
  16. // // keys: state => state.Search.keys,
  17. // autocomplete: state => state.Search.autocomplete
  18. // }),
  19. keys: {
  20. get(){
  21. return this.$store.state.Search.keys
  22. },
  23. set(value){
  24. this.$store.commit('Search/setKeys', value)
  25. }
  26. },
  27. autocomplete: {
  28. get(){
  29. return this.$store.state.Search.autocomplete
  30. },
  31. set(value){
  32. this.$store.commit('Search/setAutocomplete', value)
  33. }
  34. }
  35. },
  36. methods: {
  37. ...mapActions({
  38. getResults: 'Search/getResults'
  39. }),
  40. submit() {
  41. console.log("search clicked", this.keys, this.autocomplete);
  42. this.getResults();
  43. },
  44. onAutoCompleteSelect(event, ui){
  45. event.preventDefault();
  46. // console.log('autoCompleteSelect', event, ui);
  47. this.keys = ui.item.label
  48. this.autocomplete = ui.item.value
  49. }
  50. },
  51. beforeMount() {
  52. // console.log('SearchForm beforeMount');
  53. if(this._props.form){
  54. // console.log('SearchForm beforeMount if this._props.form ok');
  55. this.template = Vue.compile(this._props.form)
  56. // https://github.com/vuejs/vue/issues/9911
  57. this.$options.staticRenderFns = [];
  58. this._staticTrees = [];
  59. this.template.staticRenderFns.map(fn => (this.$options.staticRenderFns.push(fn)));
  60. }
  61. },
  62. watch: {
  63. keys(){
  64. console.log('keys changed', this.keys);
  65. }
  66. },
  67. mounted(){
  68. // console.log('SearchForm mounted');
  69. Drupal.attachBehaviors(this.$el);
  70. // Catch the jquery ui events for autocmplete widget
  71. jQuery(this.$el.querySelector('#edit-search')).on('autocompleteselect', this.onAutoCompleteSelect);
  72. },
  73. render(h) {
  74. // console.log('searchForm render');
  75. if(!this.template){
  76. return h('span', 'Loading ...')
  77. }else{
  78. return this.template.render.call(this)
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. </style>