2023-06-14 20:39:36 +02:00
|
|
|
import { Store, UIEventSource } from "../../Logic/UIEventSource"
|
2020-10-14 12:15:09 +02:00
|
|
|
import Combine from "../Base/Combine"
|
|
|
|
import Translations from "../i18n/Translations"
|
2020-11-06 04:02:53 +01:00
|
|
|
import Svg from "../../Svg"
|
2023-06-14 20:39:36 +02:00
|
|
|
import { Tag } from "../../Logic/Tags/Tag"
|
2021-06-10 01:36:20 +02:00
|
|
|
import BaseUIElement from "../BaseUIElement"
|
2021-06-11 22:51:45 +02:00
|
|
|
import Toggle from "../Input/Toggle"
|
2021-06-12 02:58:32 +02:00
|
|
|
import FileSelectorButton from "../Input/FileSelectorButton"
|
2021-06-22 14:21:32 +02:00
|
|
|
import ImgurUploader from "../../Logic/ImageProviders/ImgurUploader"
|
2021-07-15 20:47:28 +02:00
|
|
|
import ChangeTagAction from "../../Logic/Osm/Actions/ChangeTagAction"
|
2021-08-07 23:11:34 +02:00
|
|
|
import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
|
2023-06-14 20:39:36 +02:00
|
|
|
import { FixedUiElement } from "../Base/FixedUiElement"
|
|
|
|
import { VariableUiElement } from "../Base/VariableUIElement"
|
2022-04-19 23:48:27 +02:00
|
|
|
import Loading from "../Base/Loading"
|
2023-06-14 20:39:36 +02:00
|
|
|
import { LoginToggle } from "../Popup/LoginButton"
|
2023-01-11 03:53:58 +01:00
|
|
|
import Constants from "../../Models/Constants"
|
2023-06-14 20:39:36 +02:00
|
|
|
import { SpecialVisualizationState } from "../SpecialVisualization"
|
2020-06-25 03:39:31 +02:00
|
|
|
|
2021-06-14 19:21:33 +02:00
|
|
|
export class ImageUploadFlow extends Toggle {
|
2021-10-20 00:34:26 +02:00
|
|
|
private static readonly uploadCountsPerId = new Map<string, UIEventSource<number>>()
|
2021-11-07 16:34:51 +01:00
|
|
|
|
2022-06-05 02:24:14 +02:00
|
|
|
constructor(
|
|
|
|
tagsSource: Store<any>,
|
2023-03-28 05:13:48 +02:00
|
|
|
state: SpecialVisualizationState,
|
2021-12-21 18:35:31 +01:00
|
|
|
imagePrefix: string = "image",
|
|
|
|
text: string = undefined
|
|
|
|
) {
|
2021-10-20 00:34:26 +02:00
|
|
|
const perId = ImageUploadFlow.uploadCountsPerId
|
|
|
|
const id = tagsSource.data.id
|
2021-11-07 16:34:51 +01:00
|
|
|
if (!perId.has(id)) {
|
2021-10-20 00:34:26 +02:00
|
|
|
perId.set(id, new UIEventSource<number>(0))
|
|
|
|
}
|
|
|
|
const uploadedCount = perId.get(id)
|
2022-09-02 12:39:40 +02:00
|
|
|
const uploader = new ImgurUploader(async (url) => {
|
2021-06-11 22:51:45 +02:00
|
|
|
// A file was uploaded - we add it to the tags of the object
|
2020-09-11 19:14:32 +02:00
|
|
|
|
2021-06-11 22:51:45 +02:00
|
|
|
const tags = tagsSource.data
|
|
|
|
let key = imagePrefix
|
|
|
|
if (tags[imagePrefix] !== undefined) {
|
|
|
|
let freeIndex = 0
|
|
|
|
while (tags[imagePrefix + ":" + freeIndex] !== undefined) {
|
|
|
|
freeIndex++
|
|
|
|
}
|
|
|
|
key = imagePrefix + ":" + freeIndex
|
|
|
|
}
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2022-09-02 12:39:40 +02:00
|
|
|
await state.changes.applyAction(
|
2021-10-06 02:36:58 +02:00
|
|
|
new ChangeTagAction(tags.id, new Tag(key, url), tagsSource.data, {
|
|
|
|
changeType: "add-image",
|
2023-03-28 05:13:48 +02:00
|
|
|
theme: state.layout.id,
|
2021-10-06 02:36:58 +02:00
|
|
|
})
|
2022-09-02 12:39:40 +02:00
|
|
|
)
|
|
|
|
console.log("Adding image:" + key, url)
|
|
|
|
uploadedCount.data++
|
|
|
|
uploadedCount.ping()
|
2021-06-11 22:51:45 +02:00
|
|
|
})
|
2021-11-07 16:34:51 +01:00
|
|
|
|
2020-07-30 11:30:04 +02:00
|
|
|
const t = Translations.t.image
|
2021-11-07 16:34:51 +01:00
|
|
|
|
|
|
|
let labelContent: BaseUIElement
|
|
|
|
if (text === undefined) {
|
|
|
|
labelContent = Translations.t.image.addPicture
|
|
|
|
.Clone()
|
|
|
|
.SetClass("block align-middle mt-1 ml-3 text-4xl ")
|
|
|
|
} else {
|
|
|
|
labelContent = new FixedUiElement(text).SetClass(
|
|
|
|
"block align-middle mt-1 ml-3 text-2xl "
|
|
|
|
)
|
|
|
|
}
|
2020-09-11 19:14:32 +02:00
|
|
|
const label = new Combine([
|
2023-05-08 01:55:21 +02:00
|
|
|
Svg.camera_plus_svg().SetClass("block w-12 h-12 p-1 text-4xl "),
|
2021-10-08 04:33:39 +02:00
|
|
|
labelContent,
|
2023-05-11 17:29:25 +02:00
|
|
|
]).SetClass("w-full flex justify-center items-center")
|
|
|
|
|
2023-01-13 02:48:48 +01:00
|
|
|
const licenseStore = state?.osmConnection?.GetPreference(
|
|
|
|
Constants.OsmPreferenceKeyPicturesLicense,
|
|
|
|
"CC0"
|
|
|
|
)
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2023-05-11 17:29:25 +02:00
|
|
|
const fileSelector = new FileSelectorButton(label, {
|
|
|
|
acceptType: "image/*",
|
|
|
|
allowMultiple: true,
|
2023-06-14 20:39:36 +02:00
|
|
|
labelClasses: "rounded-full border-2 border-black font-bold",
|
2023-05-11 17:29:25 +02:00
|
|
|
})
|
2023-06-14 20:39:36 +02:00
|
|
|
/* fileSelector.SetClass(
|
2023-05-11 17:29:25 +02:00
|
|
|
"p-2 border-4 border-detail rounded-full font-bold h-full align-middle w-full flex justify-center"
|
|
|
|
)
|
|
|
|
.SetStyle(" border-color: var(--foreground-color);")*/
|
2021-06-11 22:51:45 +02:00
|
|
|
fileSelector.GetValue().addCallback((filelist) => {
|
2021-10-09 23:51:04 +02:00
|
|
|
if (filelist === undefined || filelist.length === 0) {
|
2021-06-11 22:51:45 +02:00
|
|
|
return
|
|
|
|
}
|
2020-06-25 03:39:31 +02:00
|
|
|
|
2021-10-09 23:51:04 +02:00
|
|
|
for (var i = 0; i < filelist.length; i++) {
|
2021-11-07 16:34:51 +01:00
|
|
|
const sizeInBytes = filelist[i].size
|
2021-10-09 23:51:04 +02:00
|
|
|
console.log(filelist[i].name + " has a size of " + sizeInBytes + " Bytes")
|
2021-11-07 16:34:51 +01:00
|
|
|
if (sizeInBytes > uploader.maxFileSizeInMegabytes * 1000000) {
|
2021-10-09 23:51:04 +02:00
|
|
|
alert(
|
|
|
|
Translations.t.image.toBig.Subs({
|
|
|
|
actual_size: Math.floor(sizeInBytes / 1000000) + "MB",
|
2021-11-07 16:34:51 +01:00
|
|
|
max_size: uploader.maxFileSizeInMegabytes + "MB",
|
2021-10-09 23:51:04 +02:00
|
|
|
}).txt
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
|
2023-01-13 02:48:48 +01:00
|
|
|
const license = licenseStore?.data ?? "CC0"
|
2020-10-14 12:15:09 +02:00
|
|
|
|
2021-06-14 19:21:33 +02:00
|
|
|
const tags = tagsSource.data
|
2020-10-14 12:15:09 +02:00
|
|
|
|
2023-03-28 05:13:48 +02:00
|
|
|
const layout = state?.layout
|
2021-06-11 22:51:45 +02:00
|
|
|
let matchingLayer: LayerConfig = undefined
|
2021-06-14 19:21:33 +02:00
|
|
|
for (const layer of layout?.layers ?? []) {
|
2021-06-11 22:51:45 +02:00
|
|
|
if (layer.source.osmTags.matchesProperties(tags)) {
|
|
|
|
matchingLayer = layer
|
|
|
|
break
|
|
|
|
}
|
2020-10-14 12:15:09 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 03:21:18 +02:00
|
|
|
const title =
|
|
|
|
matchingLayer?.title?.GetRenderValue(tags)?.Subs(tags)?.ConstructElement()
|
|
|
|
?.textContent ??
|
|
|
|
tags.name ??
|
|
|
|
"https//osm.org/" + tags.id
|
2021-06-11 22:51:45 +02:00
|
|
|
const description = [
|
2021-12-21 18:35:31 +01:00
|
|
|
"author:" + state.osmConnection.userDetails.data.name,
|
2021-06-11 22:51:45 +02:00
|
|
|
"license:" + license,
|
|
|
|
"osmid:" + tags.id,
|
|
|
|
].join("\n")
|
2020-07-27 13:04:38 +02:00
|
|
|
|
2021-06-11 22:51:45 +02:00
|
|
|
uploader.uploadMany(title, description, filelist)
|
|
|
|
})
|
|
|
|
|
|
|
|
const uploadFlow: BaseUIElement = new Combine([
|
2021-10-20 00:19:03 +02:00
|
|
|
new VariableUiElement(
|
|
|
|
uploader.queue
|
|
|
|
.map((q) => q.length)
|
|
|
|
.map((l) => {
|
2021-11-07 16:34:51 +01:00
|
|
|
if (l == 0) {
|
2021-10-20 00:19:03 +02:00
|
|
|
return undefined
|
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
if (l == 1) {
|
2022-08-24 01:08:54 +02:00
|
|
|
return new Loading(t.uploadingPicture).SetClass("alert")
|
2021-11-07 16:34:51 +01:00
|
|
|
} else {
|
2022-08-24 01:08:54 +02:00
|
|
|
return new Loading(
|
2023-06-14 20:39:36 +02:00
|
|
|
t.uploadingMultiple.Subs({ count: "" + l })
|
2022-08-24 01:08:54 +02:00
|
|
|
).SetClass("alert")
|
2021-10-20 00:19:03 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
),
|
|
|
|
new VariableUiElement(
|
|
|
|
uploader.failed
|
|
|
|
.map((q) => q.length)
|
|
|
|
.map((l) => {
|
2021-11-07 16:34:51 +01:00
|
|
|
if (l == 0) {
|
2021-10-20 00:19:03 +02:00
|
|
|
return undefined
|
|
|
|
}
|
2022-08-24 01:08:54 +02:00
|
|
|
console.log(l)
|
|
|
|
return t.uploadFailed.SetClass("block alert")
|
2021-10-20 00:19:03 +02:00
|
|
|
})
|
|
|
|
),
|
|
|
|
new VariableUiElement(
|
|
|
|
uploadedCount.map((l) => {
|
2021-11-07 16:34:51 +01:00
|
|
|
if (l == 0) {
|
|
|
|
return undefined
|
2021-10-20 00:19:03 +02:00
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
if (l == 1) {
|
2022-04-19 23:43:28 +02:00
|
|
|
return t.uploadDone.Clone().SetClass("thanks block")
|
2021-10-20 00:34:26 +02:00
|
|
|
}
|
2023-06-14 20:39:36 +02:00
|
|
|
return t.uploadMultipleDone.Subs({ count: l }).SetClass("thanks block")
|
2021-10-20 00:19:03 +02:00
|
|
|
})
|
|
|
|
),
|
2021-11-07 16:34:51 +01:00
|
|
|
|
2021-06-11 22:51:45 +02:00
|
|
|
fileSelector,
|
2023-01-13 02:48:48 +01:00
|
|
|
new Combine([
|
|
|
|
Translations.t.image.respectPrivacy,
|
|
|
|
new VariableUiElement(
|
|
|
|
licenseStore.map((license) =>
|
2023-06-14 20:39:36 +02:00
|
|
|
Translations.t.image.currentLicense.Subs({ license })
|
2023-01-13 02:48:48 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
.onClick(() => {
|
|
|
|
console.log("Opening the license settings... ")
|
2023-04-14 02:42:57 +02:00
|
|
|
state.guistate.openUsersettings("picture-license")
|
2023-01-13 02:48:48 +01:00
|
|
|
})
|
|
|
|
.SetClass("underline"),
|
|
|
|
]).SetStyle("font-size:small;"),
|
2023-01-13 14:31:28 +01:00
|
|
|
]).SetClass("flex flex-col image-upload-flow mt-4 mb-8 text-center leading-none")
|
2021-06-11 22:51:45 +02:00
|
|
|
|
2021-06-14 19:21:33 +02:00
|
|
|
super(
|
2023-01-06 03:37:22 +01:00
|
|
|
new LoginToggle(
|
2021-06-11 22:51:45 +02:00
|
|
|
/*We can show the actual upload button!*/
|
|
|
|
uploadFlow,
|
2023-01-06 03:37:22 +01:00
|
|
|
/* User not logged in*/ t.pleaseLogin.Clone(),
|
|
|
|
state
|
2021-06-11 22:51:45 +02:00
|
|
|
),
|
2021-09-09 00:05:51 +02:00
|
|
|
undefined /* Nothing as the user badge is disabled*/,
|
2022-04-28 00:32:15 +02:00
|
|
|
state?.featureSwitchUserbadge
|
2021-06-11 22:51:45 +02:00
|
|
|
)
|
|
|
|
}
|
2020-06-25 03:39:31 +02:00
|
|
|
}
|