| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <v-form ref="file"> | 
					
						
							| 
									
										
										
										
											2021-11-23 18:57:24 -09:00
										 |  |  |     <input ref="uploader" class="d-none" type="file" :accept="accept" @change="onFileChanged" /> | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |     <slot v-bind="{ isSelecting, onButtonClick }"> | 
					
						
							| 
									
										
										
										
											2021-11-23 18:57:24 -09:00
										 |  |  |       <v-btn :loading="isSelecting" :small="small" color="info" :text="textBtn" @click="onButtonClick"> | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |         <v-icon left> {{ effIcon }}</v-icon> | 
					
						
							|  |  |  |         {{ text ? text : defaultText }} | 
					
						
							|  |  |  |       </v-btn> | 
					
						
							|  |  |  |     </slot> | 
					
						
							|  |  |  |   </v-form> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | <script lang="ts"> | 
					
						
							|  |  |  | import { defineComponent, ref, useContext } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | const UPLOAD_EVENT = "uploaded"; | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default defineComponent({ | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   props: { | 
					
						
							|  |  |  |     small: { | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  |       type: Boolean, | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |       default: false, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     post: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: true, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  |     url: { | 
					
						
							|  |  |  |       type: String, | 
					
						
							|  |  |  |       default: "", | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     text: { | 
					
						
							|  |  |  |       type: String, | 
					
						
							|  |  |  |       default: "", | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     icon: { | 
					
						
							|  |  |  |       type: String, | 
					
						
							|  |  |  |       default: null, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     fileName: { | 
					
						
							|  |  |  |       type: String, | 
					
						
							|  |  |  |       default: "archive", | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |     textBtn: { | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  |       type: Boolean, | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |       default: true, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-11-23 18:57:24 -09:00
										 |  |  |     accept: { | 
					
						
							|  |  |  |       type: String, | 
					
						
							|  |  |  |       default: "", | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |   setup(props, context) { | 
					
						
							|  |  |  |     const file = ref<File | null>(null); | 
					
						
							|  |  |  |     const uploader = ref<HTMLInputElement | null>(null); | 
					
						
							|  |  |  |     const isSelecting = ref(false); | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     const { i18n, $globals } = useContext(); | 
					
						
							|  |  |  |     const effIcon = props.icon ? props.icon : $globals.icons.upload; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     const defaultText = i18n.t("general.upload"); | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     const api = useUserApi(); | 
					
						
							|  |  |  |     async function upload() { | 
					
						
							|  |  |  |       if (file.value != null) { | 
					
						
							|  |  |  |         isSelecting.value = true; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |         if (!props.post) { | 
					
						
							|  |  |  |           context.emit(UPLOAD_EVENT, file.value); | 
					
						
							|  |  |  |           isSelecting.value = false; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const formData = new FormData(); | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |         formData.append(props.fileName, file.value); | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |         const response = await api.upload.file(props.url, formData); | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (response) { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |           context.emit(UPLOAD_EVENT, response); | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |         isSelecting.value = false; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function onFileChanged(e: Event) { | 
					
						
							|  |  |  |       const target = e.target as HTMLInputElement; | 
					
						
							|  |  |  |       if (target.files !== null && target.files.length > 0 && file.value !== null) { | 
					
						
							|  |  |  |         file.value = target.files[0]; | 
					
						
							|  |  |  |         upload(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function onButtonClick() { | 
					
						
							|  |  |  |       isSelecting.value = true; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |       window.addEventListener( | 
					
						
							|  |  |  |         "focus", | 
					
						
							|  |  |  |         () => { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |           isSelecting.value = false; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         { once: true } | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |       uploader.value?.click(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     return { | 
					
						
							|  |  |  |       file, | 
					
						
							|  |  |  |       uploader, | 
					
						
							|  |  |  |       isSelecting, | 
					
						
							|  |  |  |       effIcon, | 
					
						
							|  |  |  |       defaultText, | 
					
						
							|  |  |  |       onFileChanged, | 
					
						
							|  |  |  |       onButtonClick, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <style></style> |