| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  | import BaseUIElement from "../BaseUIElement" | 
					
						
							| 
									
										
										
										
											2021-06-14 19:21:33 +02:00
										 |  |  | import { InputElement } from "./InputElement" | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  | import { UIEventSource } from "../../Logic/UIEventSource" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class FileSelectorButton extends InputElement<FileList> { | 
					
						
							| 
									
										
										
										
											2021-06-14 19:21:33 +02:00
										 |  |  |     private static _nextid | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  |     IsSelected: UIEventSource<boolean> | 
					
						
							| 
									
										
										
										
											2022-01-19 20:34:04 +01:00
										 |  |  |     private readonly _value = new UIEventSource<FileList>(undefined) | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  |     private readonly _label: BaseUIElement | 
					
						
							|  |  |  |     private readonly _acceptType: string | 
					
						
							| 
									
										
										
										
											2022-01-19 20:34:04 +01:00
										 |  |  |     private readonly allowMultiple: boolean | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 20:34:04 +01:00
										 |  |  |     constructor( | 
					
						
							|  |  |  |         label: BaseUIElement, | 
					
						
							|  |  |  |         options?: { | 
					
						
							| 
									
										
										
										
											2022-01-26 21:40:38 +01:00
										 |  |  |             acceptType: "image/*" | string | 
					
						
							|  |  |  |             allowMultiple: true | boolean | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  |         super() | 
					
						
							|  |  |  |         this._label = label | 
					
						
							| 
									
										
										
										
											2022-01-19 20:34:04 +01:00
										 |  |  |         this._acceptType = options?.acceptType ?? "image/*" | 
					
						
							| 
									
										
										
										
											2021-06-14 19:21:33 +02:00
										 |  |  |         this.SetClass("block cursor-pointer") | 
					
						
							|  |  |  |         label.SetClass("cursor-pointer") | 
					
						
							| 
									
										
										
										
											2022-01-19 20:34:04 +01:00
										 |  |  |         this.allowMultiple = options?.allowMultiple ?? true | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     GetValue(): UIEventSource<FileList> { | 
					
						
							|  |  |  |         return this._value | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     IsValid(t: FileList): boolean { | 
					
						
							|  |  |  |         return true | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected InnerConstructElement(): HTMLElement { | 
					
						
							|  |  |  |         const self = this | 
					
						
							|  |  |  |         const el = document.createElement("form") | 
					
						
							| 
									
										
										
										
											2021-06-14 19:21:33 +02:00
										 |  |  |         const label = document.createElement("label") | 
					
						
							|  |  |  |         label.appendChild(this._label.ConstructElement()) | 
					
						
							|  |  |  |         el.appendChild(label) | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 19:21:33 +02:00
										 |  |  |         const actualInputElement = document.createElement("input") | 
					
						
							|  |  |  |         actualInputElement.style.cssText = "display:none" | 
					
						
							|  |  |  |         actualInputElement.type = "file" | 
					
						
							|  |  |  |         actualInputElement.accept = this._acceptType | 
					
						
							|  |  |  |         actualInputElement.name = "picField" | 
					
						
							| 
									
										
										
										
											2022-01-19 20:34:04 +01:00
										 |  |  |         actualInputElement.multiple = this.allowMultiple | 
					
						
							| 
									
										
										
										
											2021-06-14 19:21:33 +02:00
										 |  |  |         actualInputElement.id = "fileselector" + FileSelectorButton._nextid | 
					
						
							|  |  |  |         FileSelectorButton._nextid++ | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 19:21:33 +02:00
										 |  |  |         label.htmlFor = actualInputElement.id | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         actualInputElement.onchange = () => { | 
					
						
							|  |  |  |             if (actualInputElement.files !== null) { | 
					
						
							|  |  |  |                 self._value.setData(actualInputElement.files) | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 19:21:33 +02:00
										 |  |  |         el.addEventListener("submit", (e) => { | 
					
						
							|  |  |  |             if (actualInputElement.files !== null) { | 
					
						
							|  |  |  |                 self._value.setData(actualInputElement.files) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             e.preventDefault() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         el.appendChild(actualInputElement) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 20:34:04 +01:00
										 |  |  |         el.addEventListener("dragover", (event) => { | 
					
						
							|  |  |  |             event.stopPropagation() | 
					
						
							|  |  |  |             event.preventDefault() | 
					
						
							|  |  |  |             // Style the drag-and-drop as a "copy file" operation.
 | 
					
						
							|  |  |  |             event.dataTransfer.dropEffect = "copy" | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         el.addEventListener("drop", (event) => { | 
					
						
							|  |  |  |             event.stopPropagation() | 
					
						
							|  |  |  |             event.preventDefault() | 
					
						
							|  |  |  |             const fileList = event.dataTransfer.files | 
					
						
							|  |  |  |             this._value.setData(fileList) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 19:21:33 +02:00
										 |  |  |         return el | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |