Search.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <div id="search" class="" :class="[isloading ? loading : '', wrapperClass]">
  3. <form class="search-form row">
  4. <fieldset class="" :class="['search', 'small-col-10', firstFieldsetClass]">
  5. <div>
  6. <label for="keys">Search</label>
  7. <input
  8. id="keys"
  9. v-model="keys"
  10. type="text"
  11. placeholder="search"
  12. @keydown.enter.prevent="submit"
  13. >
  14. <v-select
  15. id="search-type"
  16. type="select"
  17. placeholder="dans ..."
  18. append-to-body
  19. :calculate-position="dropDownMenuPos"
  20. :options="searchTypeOptions"
  21. :clearable="false"
  22. :value="searchTypeValue"
  23. @input="onSearchTypeSelected"
  24. />
  25. </div>
  26. <span
  27. v-if="!isloading"
  28. class="mdi mdi-magnify"
  29. title="rechercher"
  30. @click.prevent="submit"
  31. @keydown.enter.prevent="submit"
  32. />
  33. <span
  34. v-else
  35. class="mdi mdi-loading"
  36. title="chargement"
  37. />
  38. </fieldset>
  39. <fieldset v-if="filters.texts.length" class="filters filters-texts small-col-4 med-col-4 large-col-4">
  40. <v-select
  41. id="filters-texts"
  42. type="select"
  43. placeholder="filtrer par texte"
  44. append-to-body
  45. :calculate-position="dropDownMenuPos"
  46. :options="textsOptions"
  47. :value="activeFilters.texts"
  48. @input="onFiltersTextSelected"
  49. />
  50. </fieldset>
  51. <fieldset v-if="filters.persons.length" class="filters filters-nominum small-col-4 med-col-2 large-col-2">
  52. <v-select
  53. id="filters-nominum"
  54. type="select"
  55. placeholder="filtrer par personne"
  56. append-to-body
  57. :calculate-position="dropDownMenuPos"
  58. :options="personsOptions"
  59. :value="activeFilters.persons"
  60. multiple
  61. @input="onFiltersNominumSelected"
  62. />
  63. </fieldset>
  64. <fieldset v-if="filters.places.length" class="filters filters-locorum small-col-4 med-col-2 large-col-2">
  65. <v-select
  66. id="filters-locorum"
  67. type="select"
  68. placeholder="filtrer par lieux"
  69. append-to-body
  70. :calculate-position="dropDownMenuPos"
  71. :options="placesOptions"
  72. :value="activeFilters.places"
  73. multiple
  74. @input="onFiltersLocorumSelected"
  75. />
  76. </fieldset>
  77. <fieldset v-if="filters.objects.length" class="filters filters-operum small-col-4 med-col-2 large-col-2">
  78. <v-select
  79. id="filters-operum"
  80. type="select"
  81. placeholder="filtrer par objet"
  82. append-to-body
  83. :calculate-position="dropDownMenuPos"
  84. :options="objectsOptions"
  85. :value="activeFilters.objects"
  86. multiple
  87. @input="onFiltersOperumSelected"
  88. />
  89. </fieldset>
  90. </form>
  91. </div>
  92. </template>
  93. <script>
  94. import { createPopper } from '@popperjs/core'
  95. import { mapActions, mapMutations, mapState } from 'vuex'
  96. export default {
  97. name: 'Search',
  98. data: () => ({
  99. }),
  100. computed: {
  101. ...mapState({
  102. isloading: state => state.Search.isloading,
  103. searchTypeOptions: state => state.Search.searchTypeOptions,
  104. searchTypeValue: state => state.Search.searchTypeValue,
  105. filters: state => state.Search.filters,
  106. activeFilters: state => state.Search.activeFilters,
  107. corpusLoaded: state => state.Corpus.corpusLoaded,
  108. results: state => state.Corpus.results
  109. }),
  110. // TODO: do not synch keys instantetly (infinite loading will drop)
  111. wrapperClass () {
  112. console.log('this.$route.name', this.$route.name)
  113. if (this.$route.name === 'home' && (!this.results || !this.results.length)) {
  114. return 'small-col-11 med-col-4 large-col-4'
  115. } else {
  116. return 'med-col-11 large-col-11'
  117. }
  118. },
  119. firstFieldsetClass () {
  120. return (this.$route.name === 'home' && (!this.results || !this.results.length)) ? 'med-col-10 large-col-10' : 'med-col-4 large-col-4'
  121. },
  122. keys: {
  123. get () { return this.$store.state.Search.keys },
  124. set (value) { this.$store.commit('Search/setKeys', value) }
  125. },
  126. personsOptions () {
  127. return this.filters.persons.filter(option => !this.activeFilters.persons.includes(option))
  128. },
  129. placesOptions () {
  130. return this.filters.places.filter(option => !this.activeFilters.places.includes(option))
  131. },
  132. objectsOptions () {
  133. return this.filters.objects.filter(option => !this.activeFilters.objects.includes(option))
  134. },
  135. textsOptions () {
  136. return this.filters.texts.filter(option => !this.activeFilters.texts.includes(option))
  137. }
  138. },
  139. methods: {
  140. ...mapMutations({
  141. setSearchTypeValue: 'Search/setSearchTypeValue',
  142. setActiveFilters: 'Search/setActiveFilters'
  143. }),
  144. ...mapActions({
  145. newSearch: 'Search/newSearch',
  146. updateSearch: 'Search/updateSearch'
  147. }),
  148. submit () {
  149. // console.log('submited', this.keys)
  150. this.newSearch()
  151. },
  152. dropDownMenuPos (dropdownList, component, { width }) {
  153. /**
  154. * We need to explicitly define the dropdown width since
  155. * it is usually inherited from the parent with CSS.
  156. */
  157. dropdownList.style.width = width
  158. /**
  159. * Here we position the dropdownList relative to the $refs.toggle Element.
  160. *
  161. * The 'offset' modifier aligns the dropdown so that the $refs.toggle and
  162. * the dropdownList overlap by 1 pixel.
  163. *
  164. * The 'toggleClass' modifier adds a 'drop-up' class to the Vue Select
  165. * wrapper so that we can set some styles for when the dropdown is placed
  166. * above.
  167. */
  168. const popper = createPopper(component.$refs.toggle, dropdownList, {
  169. placement: 'top',
  170. modifiers: [
  171. {
  172. name: 'offset',
  173. options: {
  174. offset: [0, -1]
  175. }
  176. },
  177. {
  178. name: 'toggleClass',
  179. enabled: true,
  180. phase: 'write',
  181. fn ({ state }) {
  182. component.$el.classList.toggle('drop-up', state.placement === 'top')
  183. }
  184. }]
  185. })
  186. /**
  187. * To prevent memory leaks Popper needs to be destroyed.
  188. * If you return function, it will be called just before dropdown is removed from DOM.
  189. */
  190. return () => popper.destroy()
  191. },
  192. onSearchTypeSelected (e) {
  193. console.log('onSearchTypeSelected', e)
  194. this.setSearchTypeValue(e)
  195. },
  196. onFiltersNominumSelected (e) {
  197. console.log('onFiltersNominumSelected', e)
  198. this.setActiveFilters({ filter: 'persons', value: e })
  199. this.updateSearch()
  200. },
  201. onFiltersLocorumSelected (e) {
  202. console.log('onFiltersLocorumSelected', e)
  203. this.setActiveFilters({ filter: 'places', value: e })
  204. this.updateSearch()
  205. },
  206. onFiltersOperumSelected (e) {
  207. console.log('onFiltersOperumSelected', e)
  208. this.setActiveFilters({ filter: 'objects', value: e })
  209. this.updateSearch()
  210. },
  211. onFiltersTextSelected (e) {
  212. console.log('onFiltersTextSelected', e)
  213. // as texts is not multiple, convert one object value to array [e]
  214. this.setActiveFilters({ filter: 'texts', value: e ? [e] : [] })
  215. this.updateSearch()
  216. }
  217. }
  218. }
  219. </script>
  220. <style lang="scss" scoped>
  221. </style>