2020-10-14 12:15:09 +02:00
|
|
|
import {UIEventSource} from "../../Logic/UIEventSource";
|
|
|
|
import State from "../../State";
|
|
|
|
import Combine from "../Base/Combine";
|
|
|
|
import Translations from "../i18n/Translations";
|
2020-11-06 04:02:53 +01:00
|
|
|
import Svg from "../../Svg";
|
2021-03-29 00:41:53 +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 LicensePicker from "../BigComponents/LicensePicker";
|
|
|
|
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-06-11 22:51:45 +02:00
|
|
|
import UploadFlowStateUI from "../BigComponents/UploadFlowStateUI";
|
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";
|
2021-10-08 04:33:39 +02:00
|
|
|
import {FixedUiElement} from "../Base/FixedUiElement";
|
2020-06-25 03:39:31 +02:00
|
|
|
|
2021-06-14 19:21:33 +02:00
|
|
|
export class ImageUploadFlow extends Toggle {
|
2020-10-14 12:15:09 +02:00
|
|
|
|
2021-10-08 04:33:39 +02:00
|
|
|
constructor(tagsSource: UIEventSource<any>, imagePrefix: string = "image", text: string = undefined) {
|
2021-06-11 22:51:45 +02:00
|
|
|
const uploader = new ImgurUploader(url => {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
console.log("Adding image:" + key, url);
|
2021-09-29 01:12:29 +02:00
|
|
|
Promise.resolve(State.state.changes
|
2021-07-15 20:47:28 +02:00
|
|
|
.applyAction(new ChangeTagAction(
|
2021-10-06 02:36:58 +02:00
|
|
|
tags.id, new Tag(key, url), tagsSource.data,
|
|
|
|
{
|
|
|
|
changeType: "add-image",
|
|
|
|
theme: State.state.layoutToUse.id
|
|
|
|
}
|
2021-09-29 01:12:29 +02:00
|
|
|
)))
|
2021-06-11 22:51:45 +02:00
|
|
|
})
|
2021-10-09 23:51:04 +02:00
|
|
|
|
|
|
|
uploader.queue.addCallbackD(q => console.log("Image upload queue is ", q))
|
|
|
|
uploader.failed.addCallbackD(q => console.log("Image upload fail list is ", q))
|
|
|
|
uploader.success.addCallbackD(q => console.log("Image upload success list is ", q))
|
2020-06-25 03:39:31 +02:00
|
|
|
|
2021-06-11 22:51:45 +02:00
|
|
|
const licensePicker = new LicensePicker()
|
2020-06-25 03:39:31 +02:00
|
|
|
|
2020-07-30 11:30:04 +02:00
|
|
|
const t = Translations.t.image;
|
2021-10-08 04:33:39 +02: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([
|
2021-10-08 04:33:39 +02:00
|
|
|
Svg.camera_plus_ui().SetClass("block w-12 h-12 p-1 text-4xl "),
|
|
|
|
labelContent
|
|
|
|
]).SetClass("p-2 border-4 border-black rounded-full font-bold h-full align-middle w-full flex justify-center")
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2021-06-11 22:51:45 +02:00
|
|
|
const fileSelector = new FileSelectorButton(label)
|
|
|
|
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++) {
|
|
|
|
const sizeInBytes= filelist[i].size
|
|
|
|
console.log(filelist[i].name + " has a size of " + sizeInBytes + " Bytes");
|
|
|
|
if(sizeInBytes > uploader.maxFileSizeInMegabytes * 1000000){
|
|
|
|
alert(Translations.t.image.toBig.Subs({
|
|
|
|
actual_size: (Math.floor(sizeInBytes / 1000000)) + "MB",
|
|
|
|
max_size: uploader.maxFileSizeInMegabytes+"MB"
|
|
|
|
}).txt)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 22:51:45 +02:00
|
|
|
console.log("Received images from the user, starting upload")
|
2021-06-15 16:18:58 +02:00
|
|
|
const license = licensePicker.GetValue()?.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
|
|
|
|
2021-09-28 18:00:44 +02:00
|
|
|
const layout = State.state?.layoutToUse
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-09 23:51:04 +02:00
|
|
|
const title = matchingLayer?.title?.GetRenderValue(tags)?.ConstructElement()?.innerText ?? tags.name ?? "Unknown area";
|
2021-06-11 22:51:45 +02:00
|
|
|
const description = [
|
|
|
|
"author:" + State.state.osmConnection.userDetails.data.name,
|
|
|
|
"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)
|
2020-06-28 00:06:23 +02:00
|
|
|
|
2021-06-11 22:51:45 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const uploadStateUi = new UploadFlowStateUI(uploader.queue, uploader.failed, uploader.success)
|
|
|
|
|
|
|
|
const uploadFlow: BaseUIElement = new Combine([
|
2021-10-09 23:51:04 +02:00
|
|
|
uploadStateUi,
|
2021-06-11 22:51:45 +02:00
|
|
|
fileSelector,
|
2021-06-14 19:21:33 +02:00
|
|
|
Translations.t.image.respectPrivacy.Clone().SetStyle("font-size:small;"),
|
2021-10-09 23:51:04 +02:00
|
|
|
licensePicker
|
2021-06-14 19:21:33 +02:00
|
|
|
]).SetClass("flex flex-col image-upload-flow mt-4 mb-8 text-center")
|
2021-06-11 22:51:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
const pleaseLoginButton = t.pleaseLogin.Clone()
|
|
|
|
.onClick(() => State.state.osmConnection.AttemptLogin())
|
|
|
|
.SetClass("login-button-friendly");
|
2021-06-14 19:21:33 +02:00
|
|
|
super(
|
2021-06-11 22:51:45 +02:00
|
|
|
new Toggle(
|
|
|
|
/*We can show the actual upload button!*/
|
|
|
|
uploadFlow,
|
|
|
|
/* User not logged in*/ pleaseLoginButton,
|
2021-06-14 19:21:33 +02:00
|
|
|
State.state?.osmConnection?.isLoggedIn
|
2021-06-11 22:51:45 +02:00
|
|
|
),
|
2021-09-09 00:05:51 +02:00
|
|
|
undefined /* Nothing as the user badge is disabled*/,
|
2021-06-14 19:21:33 +02:00
|
|
|
State.state.featureSwitchUserbadge
|
2021-06-11 22:51:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-25 03:39:31 +02:00
|
|
|
}
|