Image.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <!--
  2. Assistant de création d'image
  3. https://norserium.github.io/vue-advanced-cropper/
  4. -->
  5. <template>
  6. <lazy-LayoutDialog :show="true">
  7. <template #dialogType>{{ $t('image_assistant') }}</template>
  8. <template #dialogTitle>{{ $t('modif_picture') }}</template>
  9. <template #dialogText>
  10. <div class="upload">
  11. <v-row
  12. v-if="fetchState.pending"
  13. class="fill-height ma-0 loading"
  14. align="center"
  15. justify="center"
  16. >
  17. <v-progress-circular
  18. indeterminate
  19. color="grey lighten-1"
  20. ></v-progress-circular>
  21. </v-row>
  22. <div v-else >
  23. <div class="upload__cropper-wrapper">
  24. <cropper
  25. ref="cropper"
  26. class="upload__cropper"
  27. check-orientation
  28. :src="image.src"
  29. :default-position="{left : coordinates.left, top : coordinates.top}"
  30. :default-size="coordinates.width ? {width : coordinates.width, height : coordinates.height}: defaultSize"
  31. @change="onChange"
  32. />
  33. <div v-if="image.src" class="upload__reset-button" title="Reset Image" @click="reset()">
  34. <v-icon>mdi-delete</v-icon>
  35. </div>
  36. </div>
  37. <div class="upload__buttons-wrapper">
  38. <button class="upload__button" @click="$refs.file.click()">
  39. <input ref="file" type="file" accept="image/*" @change="loadImage($event)" />
  40. {{$t('upload_image')}}
  41. </button>
  42. </div>
  43. </div>
  44. </div>
  45. </template>
  46. <template #dialogBtn>
  47. <v-btn class="mr-4 submitBtn ot_grey ot_white--text" @click="$emit('close')">
  48. {{ $t('cancel') }}
  49. </v-btn>
  50. <v-btn class="mr-4 submitBtn ot_danger ot_white--text" @click="save">
  51. {{ $t('save') }}
  52. </v-btn>
  53. </template>
  54. </lazy-LayoutDialog>
  55. </template>
  56. <script lang="ts">
  57. import {defineComponent, onUnmounted, Ref, ref, useContext, useFetch, watch} from '@nuxtjs/composition-api'
  58. import { Cropper } from 'vue-advanced-cropper'
  59. import 'vue-advanced-cropper/dist/style.css';
  60. import {AnyJson, ApiResponse} from "~/types/interfaces";
  61. import {UseImage} from "~/composables/data/useImage";
  62. import {WatchStopHandle} from "@vue/composition-api";
  63. import {QUERY_TYPE} from "~/types/enums";
  64. import {File} from "~/models/Core/File";
  65. import {repositoryHelper} from "~/services/store/repository";
  66. export default defineComponent({
  67. components: {
  68. Cropper,
  69. },
  70. props: {
  71. existingImageId:{
  72. type: Number,
  73. required: false
  74. },
  75. ownerId:{
  76. type: Number,
  77. required: false
  78. },
  79. field:{
  80. type: String,
  81. required: true
  82. }
  83. },
  84. fetchOnServer: false,
  85. setup (props, {emit}) {
  86. const fileToSave = new File()
  87. const cropper:Ref<any> = ref(null)
  88. const image: Ref<AnyJson> = ref({
  89. id: null,
  90. src: null,
  91. file: null,
  92. name: null
  93. })
  94. const coordinates:Ref<AnyJson> = ref({})
  95. const defaultSize = ({ imageSize, visibleArea }: any) => {
  96. return {
  97. width: (visibleArea || imageSize).width,
  98. height: (visibleArea || imageSize).height,
  99. };
  100. }
  101. const {$dataProvider, $config, $dataPersister} = useContext()
  102. //Si l'id est renseigné, il faut récupérer l'Item File afin d'avoir les informations de config, le nom, etc.
  103. if(props.existingImageId){
  104. useFetch(async () => {
  105. const result: ApiResponse = await $dataProvider.invoke({
  106. type: QUERY_TYPE.DEFAULT,
  107. url: 'api/files',
  108. id: props.existingImageId
  109. })
  110. const config = JSON.parse(result.data.config)
  111. coordinates.value.left = config.x
  112. coordinates.value.top = config.y
  113. coordinates.value.height = config.height
  114. coordinates.value.width = config.width
  115. image.value.name = result.data.name
  116. image.value.id = result.data.id
  117. })
  118. }
  119. //On récupère l'image...
  120. const { fetchState, imageLoaded } = new UseImage().getOne(props.existingImageId)
  121. const unwatch: WatchStopHandle = watch(imageLoaded, (newValue, oldValue) => {
  122. if (newValue === oldValue || typeof newValue === 'undefined') { return }
  123. image.value.src = newValue
  124. })
  125. /**
  126. * Quand l'utilisateur choisit une image sur sa machine
  127. * @param event
  128. */
  129. const loadImage = (event:any) => {
  130. const { files } = event.target
  131. if (files && files[0]) {
  132. reset()
  133. image.value.name = files[0].name
  134. image.value.src = URL.createObjectURL(files[0])
  135. image.value.file = files[0]
  136. }
  137. }
  138. /**
  139. * Losrque le cropper change de position/taille, on met à jour les coordonnées
  140. * @param config
  141. */
  142. const onChange = ({ coordinates: config } : any) => {
  143. coordinates.value = config;
  144. }
  145. /**
  146. * Lorsque l'on sauvegarde l'image
  147. */
  148. const save = async () => {
  149. fileToSave.config = JSON.stringify({
  150. x: coordinates.value.left,
  151. y: coordinates.value.top,
  152. height: coordinates.value.height,
  153. width: coordinates.value.width
  154. })
  155. //Cas d'un PUT : l'image existe déjà on bouge simplement le cropper
  156. if(image.value.id){
  157. fileToSave.id = image.value.id
  158. repositoryHelper.persist(File, fileToSave)
  159. await $dataPersister.invoke({
  160. type: QUERY_TYPE.MODEL,
  161. model: File,
  162. id: fileToSave.id
  163. })
  164. //On émet un évent afin de mettre à jour le formulaire de départ
  165. emit('reload')
  166. }
  167. //Post : on créer une nouvelle image donc on passe par l'api legacy...
  168. else{
  169. if(image.value.file){
  170. //On créer l'objet File à sauvegarder
  171. fileToSave.name = image.value.name
  172. fileToSave.imgFieldName = props.field
  173. fileToSave.visibility = 'EVERYBODY'
  174. fileToSave.folder = 'IMAGES'
  175. if(props.ownerId)
  176. fileToSave.ownerId = props.ownerId
  177. //Appel au datapersister
  178. const response: ApiResponse = await $dataPersister.invoke({
  179. type: QUERY_TYPE.FILE,
  180. baseUrl: $config.baseURL_Legacy,
  181. data: fileToSave.$toJson(),
  182. file: image.value.file
  183. })
  184. //On émet un évent afin de mettre à jour le formulaire de départ
  185. emit('update', response.data['@id'])
  186. }else{
  187. //On reset l'image : on a appuyer sur "poubelle" puis on enregistre
  188. emit('reset')
  189. }
  190. }
  191. }
  192. /**
  193. * On choisit de supprimer l'image présente
  194. */
  195. const reset = () => {
  196. image.value.src = null
  197. image.value.file = null
  198. image.value.name = null
  199. image.value.id = null
  200. URL.revokeObjectURL(image.value.src)
  201. }
  202. /**
  203. * Lorsqu'on démonte le component on supprime le watcher et on revoke l'objet URL
  204. */
  205. onUnmounted(() => {
  206. unwatch()
  207. if (image.value.src) {
  208. URL.revokeObjectURL(image.value.src)
  209. }
  210. })
  211. return {
  212. image,
  213. save,
  214. loadImage,
  215. cropper,
  216. reset,
  217. fetchState,
  218. coordinates,
  219. onChange,
  220. defaultSize
  221. }
  222. }
  223. })
  224. </script>
  225. <style lang="scss">
  226. .vue-advanced-cropper__stretcher{
  227. height: auto !important;
  228. width: auto !important;
  229. }
  230. .loading{
  231. height: 300px;
  232. }
  233. .upload {
  234. user-select: none;
  235. padding: 20px;
  236. display: block;
  237. &__cropper {
  238. border: solid 1px #eee;
  239. min-height: 500px;
  240. max-height: 500px;
  241. }
  242. &__cropper-wrapper {
  243. position: relative;
  244. }
  245. &__reset-button {
  246. position: absolute;
  247. right: 20px;
  248. bottom: 20px;
  249. cursor: pointer;
  250. display: flex;
  251. align-items: center;
  252. justify-content: center;
  253. height: 42px;
  254. width: 42px;
  255. background: rgba(#3fb37f, 0.7);
  256. transition: background 0.5s;
  257. &:hover {
  258. background: #3fb37f;
  259. }
  260. }
  261. &__buttons-wrapper {
  262. display: flex;
  263. justify-content: center;
  264. margin-top: 17px;
  265. }
  266. &__button {
  267. border: none;
  268. outline: solid transparent;
  269. color: white;
  270. font-size: 16px;
  271. padding: 10px 20px;
  272. background: #3fb37f;
  273. cursor: pointer;
  274. transition: background 0.5s;
  275. margin: 0 16px;
  276. &:hover,
  277. &:focus {
  278. background: #38d890;
  279. }
  280. input {
  281. display: none;
  282. }
  283. }
  284. }
  285. </style>