MapComplete/UI/Input/FileSelectorButton.ts

116 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-09-08 21:40:48 +02:00
import BaseUIElement from "../BaseUIElement"
import {InputElement} from "./InputElement"
import {UIEventSource} from "../../Logic/UIEventSource"
2021-06-11 22:51:45 +02:00
/**
* @deprecated
*/
2021-06-11 22:51:45 +02:00
export default class FileSelectorButton extends InputElement<FileList> {
private static _nextid = 0
2022-09-08 21:40:48 +02:00
private readonly _value = new UIEventSource<FileList>(undefined)
private readonly _label: BaseUIElement
private readonly _acceptType: string
private readonly allowMultiple: boolean
private readonly _labelClasses: string
2022-09-08 21:40:48 +02:00
constructor(
label: BaseUIElement,
options?: {
acceptType: "image/*" | string
allowMultiple: true | boolean
2023-05-11 17:29:25 +02:00
labelClasses?: string
2022-09-08 21:40:48 +02:00
}
) {
super()
this._label = label
this._acceptType = options?.acceptType ?? "image/*"
this._labelClasses = options?.labelClasses ?? ""
2021-06-14 19:21:33 +02:00
this.SetClass("block cursor-pointer")
label.SetClass("cursor-pointer")
this.allowMultiple = options?.allowMultiple ?? true
2021-06-11 22:51:45 +02:00
}
GetValue(): UIEventSource<FileList> {
2022-09-08 21:40:48 +02:00
return this._value
2021-06-11 22:51:45 +02:00
}
IsValid(t: FileList): boolean {
2022-09-08 21:40:48 +02:00
return true
2021-06-11 22:51:45 +02:00
}
protected InnerConstructElement(): HTMLElement {
2022-09-08 21:40:48 +02:00
const self = this
2021-06-11 22:51:45 +02:00
const el = document.createElement("form")
2021-06-14 19:21:33 +02:00
const label = document.createElement("label")
label.appendChild(this._label.ConstructElement())
label.classList.add(...this._labelClasses.split(" ").filter((t) => t !== ""))
2021-06-14 19:21:33 +02:00
el.appendChild(label)
2021-06-11 22:51:45 +02:00
2022-09-08 21:40:48 +02:00
const actualInputElement = document.createElement("input")
actualInputElement.style.cssText = "display:none"
actualInputElement.type = "file"
actualInputElement.accept = this._acceptType
actualInputElement.name = "picField"
actualInputElement.multiple = this.allowMultiple
actualInputElement.id = "fileselector" + FileSelectorButton._nextid
FileSelectorButton._nextid++
2021-06-11 22:51:45 +02:00
2022-09-08 21:40:48 +02:00
label.htmlFor = actualInputElement.id
2021-06-14 19:21:33 +02:00
actualInputElement.onchange = () => {
if (actualInputElement.files !== null) {
self._value.setData(actualInputElement.files)
}
2021-06-11 22:51:45 +02:00
}
2022-09-08 21:40:48 +02:00
el.addEventListener("submit", (e) => {
2021-06-14 19:21:33 +02:00
if (actualInputElement.files !== null) {
self._value.setData(actualInputElement.files)
}
actualInputElement.classList.remove("glowing-shadow");
2021-06-14 19:21:33 +02:00
e.preventDefault()
})
el.appendChild(actualInputElement)
function setDrawAttention(isOn: boolean){
if(isOn){
label.classList.add("glowing-shadow")
}else{
label.classList.remove("glowing-shadow")
}
}
2022-09-08 21:40:48 +02:00
el.addEventListener("dragover", (event) => {
event.stopPropagation()
event.preventDefault()
setDrawAttention(true)
// Style the drag-and-drop as a "copy file" operation.
2022-09-08 21:40:48 +02:00
event.dataTransfer.dropEffect = "copy"
})
window.document.addEventListener("dragenter", () =>{
setDrawAttention(true)
2022-09-08 21:40:48 +02:00
})
window.document.addEventListener("dragend", () => {
setDrawAttention(false)
})
2022-09-08 21:40:48 +02:00
el.addEventListener("drop", (event) => {
event.stopPropagation()
event.preventDefault()
label.classList.remove("glowing-shadow")
2022-09-08 21:40:48 +02:00
const fileList = event.dataTransfer.files
this._value.setData(fileList)
2022-09-08 21:40:48 +02:00
})
2022-09-08 21:40:48 +02:00
return el
2021-06-11 22:51:45 +02:00
}
2022-09-08 21:40:48 +02:00
}