MapComplete/UI/Image/ImageUploadFlow.ts

119 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-10-14 12:15:09 +02:00
import {UIEventSource} from "../../Logic/UIEventSource";
import {UIElement} from "../UIElement";
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-11 22:51:45 +02:00
import ImgurUploader from "../../Logic/Web/ImgurUploader";
import UploadFlowStateUI from "../BigComponents/UploadFlowStateUI";
import LayerConfig from "../../Customizations/JSON/LayerConfig";
export class ImageUploadFlow extends UIElement {
2021-06-11 22:51:45 +02:00
private readonly _element: BaseUIElement;
2020-10-14 12:15:09 +02:00
private readonly _tags: UIEventSource<any>;
private readonly _selectedLicence: UIEventSource<string>;
2021-06-11 22:51:45 +02:00
private readonly _imagePrefix: string;
2020-10-14 12:15:09 +02:00
2021-06-11 22:51:45 +02:00
constructor(tagsSource: UIEventSource<any>, imagePrefix: string = "image") {
super(State.state.osmConnection.userDetails);
this._imagePrefix = imagePrefix;
2020-10-14 12:15:09 +02:00
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
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);
State.state.changes.addTag(tags.id, new Tag(key, url));
})
2021-06-11 22:51:45 +02:00
const licensePicker = new LicensePicker()
const t = Translations.t.image;
const label = new Combine([
Svg.camera_plus_svg().SetStyle("width: 36px;height: 36px;padding: 0.1em;margin-top: 5px;border-radius: 0;float: left;display:block"),
Translations.t.image.addPicture
2020-11-16 02:33:44 +01:00
]).SetClass("image-upload-flow-button")
2021-06-11 22:51:45 +02:00
const fileSelector = new FileSelectorButton(label)
fileSelector.GetValue().addCallback(filelist => {
if (filelist === undefined) {
return;
}
2021-06-11 22:51:45 +02:00
console.log("Received images from the user, starting upload")
const license = this._selectedLicence.data ?? "CC0"
2020-10-14 12:15:09 +02:00
2021-06-11 22:51:45 +02:00
const tags = this._tags.data;
2020-10-14 12:15:09 +02:00
2021-06-11 22:51:45 +02:00
const layout = State.state.layoutToUse.data
let matchingLayer: LayerConfig = undefined
for (const layer of layout.layers) {
if (layer.source.osmTags.matchesProperties(tags)) {
matchingLayer = layer;
break;
}
2020-10-14 12:15:09 +02:00
}
2021-06-11 22:51:45 +02:00
const title = matchingLayer?.title?.GetRenderValue(tags)?.ConstructElement().innerText ?? tags.name ?? "Unknown area";
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)
2021-06-11 22:51:45 +02:00
})
const uploadStateUi = new UploadFlowStateUI(uploader.queue, uploader.failed, uploader.success)
const uploadFlow: BaseUIElement = new Combine([
fileSelector,
Translations.t.image.respectPrivacy.SetStyle("font-size:small;"),
licensePicker,
uploadStateUi
]).SetClass("image-upload-flow")
.SetStyle("margin-top: 1em;margin-bottom: 2em;text-align: center;");
const pleaseLoginButton = t.pleaseLogin.Clone()
.onClick(() => State.state.osmConnection.AttemptLogin())
.SetClass("login-button-friendly");
this._element = new Toggle(
new Toggle(
/*We can show the actual upload button!*/
uploadFlow,
/* User not logged in*/ pleaseLoginButton,
State.state.osmConnection.userDetails.map(userinfo => userinfo.loggedIn)
),
undefined /* Nothing as the user badge is disabled*/, State.state.featureSwitchUserbadge
)
}
protected InnerRender(): string | BaseUIElement {
return this._element;
}
2021-06-11 22:51:45 +02:00
}