| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <v-form ref="file"> | 
					
						
							|  |  |  |     <input ref="uploader" class="d-none" type="file" @change="onFileChanged" /> | 
					
						
							|  |  |  |     <slot v-bind="{ isSelecting, onButtonClick }"> | 
					
						
							|  |  |  |       <v-btn :loading="isSelecting" :small="small" color="accent" :text="textBtn" @click="onButtonClick"> | 
					
						
							|  |  |  |         <v-icon left> {{ effIcon }}</v-icon> | 
					
						
							|  |  |  |         {{ text ? text : defaultText }} | 
					
						
							|  |  |  |       </v-btn> | 
					
						
							|  |  |  |     </slot> | 
					
						
							|  |  |  |   </v-form> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script> | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | const UPLOAD_EVENT = "uploaded"; | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |   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-08-07 15:12:25 -08:00
										 |  |  |   setup() { | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |     const api = useUserApi(); | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { api }; | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   data: () => ({ | 
					
						
							|  |  |  |     file: null, | 
					
						
							|  |  |  |     isSelecting: false, | 
					
						
							|  |  |  |   }), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   computed: { | 
					
						
							|  |  |  |     effIcon() { | 
					
						
							|  |  |  |       return this.icon ? this.icon : this.$globals.icons.upload; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     defaultText() { | 
					
						
							|  |  |  |       return this.$t("general.upload"); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   methods: { | 
					
						
							|  |  |  |     async upload() { | 
					
						
							|  |  |  |       if (this.file != null) { | 
					
						
							|  |  |  |         this.isSelecting = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!this.post) { | 
					
						
							|  |  |  |           this.$emit(UPLOAD_EVENT, this.file); | 
					
						
							|  |  |  |           this.isSelecting = false; | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const formData = new FormData(); | 
					
						
							|  |  |  |         formData.append(this.fileName, this.file); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  |         const response = await this.api.upload.file(this.url, formData); | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (response) { | 
					
						
							|  |  |  |           this.$emit(UPLOAD_EVENT, response); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         this.isSelecting = false; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     onButtonClick() { | 
					
						
							|  |  |  |       this.isSelecting = true; | 
					
						
							|  |  |  |       window.addEventListener( | 
					
						
							|  |  |  |         "focus", | 
					
						
							|  |  |  |         () => { | 
					
						
							|  |  |  |           this.isSelecting = false; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { once: true } | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       this.$refs.uploader.click(); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     onFileChanged(e) { | 
					
						
							|  |  |  |       this.file = e.target.files[0]; | 
					
						
							|  |  |  |       this.upload(); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <style></style> |