Image.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!--
  2. Composant Image permettant d'afficher une image stockée sur les serveurs Opentalent à partir de son id.
  3. Permet d'afficher une image par défaut si l'image demandée n'est pas disponible ou invalide.
  4. -->
  5. <template>
  6. <main>
  7. <div class="image-wrapper" :style="{width: width + 'px'}">
  8. <v-img
  9. :src="imageSrc"
  10. :lazy-src="defaultImagePath"
  11. :height="height"
  12. :width="width"
  13. aspect-ratio="1"
  14. >
  15. <template #placeholder>
  16. <v-row
  17. class="fill-height ma-0"
  18. align="center"
  19. justify="center"
  20. v-if="pending"
  21. >
  22. <v-progress-circular
  23. :indeterminate="true"
  24. color="neutral"
  25. />
  26. </v-row>
  27. </template>
  28. <div v-if="!pending && overlayIcon" class="overlay" @click="emit('overlay-clicked')">
  29. <v-icon>fas fa-upload</v-icon>
  30. </div>
  31. </v-img>
  32. </div>
  33. </main>
  34. </template>
  35. <script setup lang="ts">
  36. import {useImageFetch} from "~/composables/data/useImageFetch";
  37. import {onUnmounted, PropType, watch, WatchStopHandle} from "@vue/runtime-core";
  38. import ImageManager from "~/services/data/imageManager";
  39. const props = defineProps({
  40. /**
  41. * Id de l'image (null si aucune)
  42. */
  43. imageId: {
  44. type: Number as PropType<number | null>,
  45. required: false,
  46. default: null
  47. },
  48. /**
  49. * Image par défaut
  50. */
  51. defaultImage: {
  52. type: String,
  53. required: false
  54. },
  55. /**
  56. * Hauteur de l'image à l'écran (en px)
  57. */
  58. height: {
  59. type: Number,
  60. required: false
  61. },
  62. /**
  63. * Largeur de l'image à l'écran (en px)
  64. */
  65. width: {
  66. type: Number,
  67. required: false
  68. },
  69. overlayIcon: {
  70. type: String,
  71. required: false,
  72. default: null
  73. }
  74. })
  75. const { fetch } = useImageFetch()
  76. const defaultImagePath = props.defaultImage ?? ImageManager.defaultImage
  77. const { data: imageSrc, pending, refresh: refreshImage } = fetch(props.imageId ?? null, defaultImagePath, props.height, props.width) as any
  78. const emit = defineEmits(['overlay-clicked'])
  79. /**
  80. * Si l'id change, on recharge l'image
  81. */
  82. const unwatch: WatchStopHandle = watch(() => props.imageId, async () => {
  83. await refreshImage()
  84. })
  85. const refresh = () => {
  86. refreshImage()
  87. }
  88. defineExpose({ refresh })
  89. /**
  90. * Lorsqu'on démonte le component, on supprime le watcher
  91. */
  92. onUnmounted(() => {
  93. unwatch()
  94. })
  95. </script>
  96. <style lang="scss">
  97. div.image-wrapper {
  98. display: block;
  99. position: relative;
  100. img {
  101. display: block;
  102. max-width: 100%;
  103. }
  104. .overlay {
  105. position: absolute;
  106. top: 0;
  107. bottom: 0;
  108. left: 0;
  109. right: 0;
  110. height: 100%;
  111. width: 100%;
  112. opacity: 0;
  113. display: flex;
  114. align-items: center;
  115. justify-content: center;
  116. transition: .3s ease;
  117. }
  118. .overlay:hover {
  119. opacity: 0.8;
  120. background-color: rgb(var(--v-theme-neutral-strong));
  121. cursor: pointer;
  122. }
  123. .overlay .v-icon {
  124. color: rgb(var(--v-theme-on-neutral-strong));
  125. font-size: 36px;
  126. }
  127. }
  128. </style>