PersonnalizedList.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <main>
  3. <v-menu
  4. bottom
  5. left
  6. transition="slide-y-transition"
  7. :close-on-content-click="false"
  8. min-width="500"
  9. >
  10. <template v-slot:activator="{ on, attrs }">
  11. <span
  12. v-bind="attrs"
  13. class="ot_green--text"
  14. v-on="on"
  15. >
  16. {{ $t('my_list') }}
  17. </span>
  18. </template>
  19. <v-list>
  20. <v-list-item>
  21. <v-list-item-title>
  22. <UiInputAutocomplete
  23. :label="$t('searchList')"
  24. :is-loading="isLoading"
  25. :items="items"
  26. :item-text="['label']"
  27. :return-object="true"
  28. @update="goOn"
  29. />
  30. </v-list-item-title>
  31. </v-list-item>
  32. </v-list>
  33. </v-menu>
  34. </main>
  35. </template>
  36. <script lang="ts">
  37. import { computed, defineComponent, useContext, useFetch, ref, Ref, ComputedRef } from '@nuxtjs/composition-api'
  38. import { Collection } from '@vuex-orm/core'
  39. import { QUERY_TYPE } from '~/types/enums'
  40. import { PersonalizedList } from '~/models/Access/PersonalizedList'
  41. import { repositoryHelper } from '~/services/store/repository'
  42. import { AnyJson } from '~/types/interfaces'
  43. import { $objectProperties } from '~/services/utils/objectProperties'
  44. export default defineComponent({
  45. fetchOnServer: false,
  46. setup () {
  47. const { $dataProvider, $config } = useContext()
  48. const isLoading:Ref<boolean> = ref(true)
  49. const homeUrl:string = $config.baseURL_adminLegacy
  50. useFetch(async () => {
  51. await $dataProvider.invoke({
  52. type: QUERY_TYPE.MODEL,
  53. model: PersonalizedList
  54. })
  55. isLoading.value = false
  56. })
  57. const items:ComputedRef<Array<AnyJson>> = computed(() => {
  58. const lists = repositoryHelper.findCollectionFromModel(PersonalizedList) as Collection<PersonalizedList>
  59. let listsGroupByKeyMenu:Array<AnyJson> = groupListByKey(lists)
  60. listsGroupByKeyMenu = $objectProperties.sortObjectByKey(listsGroupByKeyMenu)
  61. return constructAutoCompleteItems(listsGroupByKeyMenu)
  62. })
  63. const goOn = (list:PersonalizedList) => {
  64. window.location.href = `${homeUrl}/${list.entity}/list/${list.id}`
  65. }
  66. return {
  67. items,
  68. isLoading,
  69. goOn
  70. }
  71. }
  72. })
  73. /**
  74. * On regroupe la list par clé afin de constituer les groups
  75. * @param lists
  76. */
  77. function groupListByKey (lists:Collection<PersonalizedList>): Array<AnyJson> {
  78. const { app: { i18n } } = useContext()
  79. const listsGroupByKeyMenu:any = {}
  80. for (const list of lists) {
  81. const key = i18n.t(list.menuKey) as string
  82. listsGroupByKeyMenu[key] = listsGroupByKeyMenu[key] ?? []
  83. listsGroupByKeyMenu[key].push(list)
  84. }
  85. return listsGroupByKeyMenu
  86. }
  87. /**
  88. * On construit la list d'Item, constituée de Header et d'Item "sélectionnable"
  89. * @param listsGroupByKeyMenu
  90. */
  91. function constructAutoCompleteItems (listsGroupByKeyMenu:Array<any>) {
  92. const items: any = []
  93. for (const key in listsGroupByKeyMenu) {
  94. items.push({ header: key })
  95. for (const item of listsGroupByKeyMenu[key]) {
  96. items.push(item)
  97. }
  98. }
  99. return items
  100. }
  101. </script>
  102. <style scoped>
  103. </style>