SearchForm.vue 2.5 KB

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