2020-06-25 03:39:31 +02:00
|
|
|
import $ from "jquery"
|
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 {FixedUiElement} from "../Base/FixedUiElement";
|
|
|
|
import {Imgur} from "../../Logic/Web/Imgur";
|
|
|
|
import {DropDown} from "../Input/DropDown";
|
|
|
|
import {Tag} from "../../Logic/Tags";
|
|
|
|
import Translations from "../i18n/Translations";
|
2020-11-06 04:02:53 +01:00
|
|
|
import Svg from "../../Svg";
|
2020-06-25 03:39:31 +02:00
|
|
|
|
|
|
|
export class ImageUploadFlow extends UIElement {
|
2020-10-14 12:15:09 +02:00
|
|
|
private readonly _licensePicker: UIElement;
|
|
|
|
private readonly _tags: UIEventSource<any>;
|
|
|
|
private readonly _selectedLicence: UIEventSource<string>;
|
|
|
|
private readonly _isUploading: UIEventSource<number> = new UIEventSource<number>(0)
|
|
|
|
private readonly _didFail: UIEventSource<boolean> = new UIEventSource<boolean>(false);
|
|
|
|
private readonly _allDone: UIEventSource<boolean> = new UIEventSource<boolean>(false);
|
|
|
|
private readonly _connectButton: UIElement;
|
2020-10-17 02:37:53 +02:00
|
|
|
private readonly _imagePrefix: string;
|
2020-10-14 12:15:09 +02:00
|
|
|
|
2020-10-17 02:37:53 +02:00
|
|
|
constructor(tags: UIEventSource<any>, imagePrefix: string = "image") {
|
2020-07-31 01:45:54 +02:00
|
|
|
super(State.state.osmConnection.userDetails);
|
2020-10-14 12:15:09 +02:00
|
|
|
this._tags = tags;
|
2020-10-17 02:37:53 +02:00
|
|
|
this._imagePrefix = imagePrefix;
|
2020-10-14 12:15:09 +02:00
|
|
|
|
2020-06-25 03:39:31 +02:00
|
|
|
this.ListenTo(this._isUploading);
|
2020-07-21 22:50:54 +02:00
|
|
|
this.ListenTo(this._didFail);
|
2020-07-30 11:30:04 +02:00
|
|
|
this.ListenTo(this._allDone);
|
2020-06-28 00:06:23 +02:00
|
|
|
|
2020-07-21 01:13:51 +02:00
|
|
|
const licensePicker = new DropDown(Translations.t.image.willBePublished,
|
2020-06-25 03:39:31 +02:00
|
|
|
[
|
2020-07-21 01:13:51 +02:00
|
|
|
{value: "CC0", shown: Translations.t.image.cco},
|
|
|
|
{value: "CC-BY-SA 4.0", shown: Translations.t.image.ccbs},
|
|
|
|
{value: "CC-BY 4.0", shown: Translations.t.image.ccb}
|
2020-07-01 17:38:48 +02:00
|
|
|
],
|
2020-10-14 12:15:09 +02:00
|
|
|
State.state.osmConnection.GetPreference("pictures-license")
|
2020-09-11 19:14:32 +02:00
|
|
|
);
|
|
|
|
licensePicker.SetStyle("float:left");
|
|
|
|
|
|
|
|
const t = Translations.t.image;
|
|
|
|
|
2020-06-28 00:06:23 +02:00
|
|
|
this._licensePicker = licensePicker;
|
2020-07-20 21:39:07 +02:00
|
|
|
this._selectedLicence = licensePicker.GetValue();
|
2020-09-11 19:14:32 +02:00
|
|
|
|
|
|
|
this._connectButton = new Combine([t.pleaseLogin])
|
2020-08-25 00:10:48 +02:00
|
|
|
.onClick(() => State.state.osmConnection.AttemptLogin())
|
2020-09-11 19:14:32 +02:00
|
|
|
.SetClass("login-button-friendly");
|
2020-06-28 00:06:23 +02:00
|
|
|
|
2020-06-25 03:39:31 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 21:39:07 +02:00
|
|
|
InnerRender(): string {
|
2020-06-25 03:39:31 +02:00
|
|
|
|
2020-07-30 11:30:04 +02:00
|
|
|
const t = Translations.t.image;
|
2020-07-31 01:45:54 +02:00
|
|
|
if (State.state.osmConnection.userDetails === undefined) {
|
2020-07-30 11:30:04 +02:00
|
|
|
return ""; // No user details -> logging in is probably disabled or smthing
|
|
|
|
}
|
|
|
|
|
2020-07-31 01:45:54 +02:00
|
|
|
if (!State.state.osmConnection.userDetails.data.loggedIn) {
|
2020-08-25 00:10:48 +02:00
|
|
|
return this._connectButton.Render();
|
2020-06-27 03:06:51 +02:00
|
|
|
}
|
2020-07-20 17:30:02 +02:00
|
|
|
|
2020-07-30 11:30:04 +02:00
|
|
|
let currentState: UIElement[] = [];
|
2020-06-28 00:06:23 +02:00
|
|
|
if (this._isUploading.data == 1) {
|
2020-07-30 11:30:04 +02:00
|
|
|
currentState.push(t.uploadingPicture);
|
|
|
|
} else if (this._isUploading.data > 0) {
|
2020-09-25 17:57:01 +02:00
|
|
|
currentState.push(t.uploadingMultiple.Subs({count: ""+this._isUploading.data}));
|
2020-06-28 00:06:23 +02:00
|
|
|
}
|
2020-07-30 11:30:04 +02:00
|
|
|
|
|
|
|
if (this._didFail.data) {
|
|
|
|
currentState.push(t.uploadFailed);
|
2020-06-25 03:39:31 +02:00
|
|
|
}
|
2020-07-30 11:30:04 +02:00
|
|
|
|
|
|
|
if (this._allDone.data) {
|
|
|
|
currentState.push(t.uploadDone)
|
2020-07-21 22:50:54 +02:00
|
|
|
}
|
2020-06-25 03:39:31 +02:00
|
|
|
|
2020-09-25 17:57:01 +02:00
|
|
|
let currentStateHtml : UIElement = new FixedUiElement("");
|
2020-07-31 01:45:54 +02:00
|
|
|
if (currentState.length > 0) {
|
2020-09-25 17:57:01 +02:00
|
|
|
currentStateHtml = new Combine(currentState);
|
2020-07-31 01:45:54 +02:00
|
|
|
if (!this._allDone.data) {
|
2020-09-25 17:57:01 +02:00
|
|
|
currentStateHtml.SetClass("alert");
|
|
|
|
}else{
|
|
|
|
currentStateHtml.SetClass("thanks");
|
2020-07-31 01:45:54 +02:00
|
|
|
}
|
2020-09-25 17:57:01 +02:00
|
|
|
currentStateHtml.SetStyle("display:block ruby")
|
2020-07-31 01:45:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:14:32 +02:00
|
|
|
const extraInfo = new Combine([
|
2020-10-19 12:08:42 +02:00
|
|
|
Translations.t.image.respectPrivacy.SetStyle("font-size:small;"),
|
2020-09-11 19:14:32 +02:00
|
|
|
"<br/>",
|
|
|
|
this._licensePicker,
|
|
|
|
"<br/>",
|
|
|
|
currentStateHtml,
|
|
|
|
"<br/>"
|
|
|
|
]);
|
|
|
|
|
|
|
|
const label = new Combine([
|
2020-11-06 04:02:53 +01:00
|
|
|
Svg.camera_plus_ui().SetStyle("width: 36px;height: 36px;padding: 0.1em;margin-top: 5px;border-radius: 0;float: left;display:block"),
|
2020-09-11 19:14:32 +02:00
|
|
|
Translations.t.image.addPicture
|
2020-09-12 23:15:17 +02:00
|
|
|
.SetStyle("width:max-content;font-size: 28px;" +
|
|
|
|
"font-weight: bold;" +
|
|
|
|
"float: left;" +
|
|
|
|
"margin-top: 4px;" +
|
|
|
|
"padding-top: 4px;" +
|
|
|
|
"padding-bottom: 4px;" +
|
|
|
|
"padding-left: 13px;"),
|
|
|
|
|
|
|
|
]).SetStyle(" display: flex;" +
|
|
|
|
"cursor:pointer;" +
|
|
|
|
"padding: 0.5em;" +
|
|
|
|
"border-radius: 1em;" +
|
|
|
|
"border: 3px solid black;" +
|
|
|
|
"box-sizing:border-box;")
|
2020-09-11 19:14:32 +02:00
|
|
|
|
|
|
|
const actualInputElement =
|
|
|
|
`<input style='display: none' id='fileselector-${this.id}' type='file' accept='image/*' name='picField' multiple='multiple' alt=''/>`;
|
|
|
|
|
|
|
|
const form = "<form id='fileselector-form-" + this.id + "'>" +
|
|
|
|
`<label for='fileselector-${this.id}'>` +
|
|
|
|
label.Render() +
|
2020-06-28 00:06:23 +02:00
|
|
|
"</label>" +
|
2020-10-14 12:15:09 +02:00
|
|
|
actualInputElement +
|
2020-09-11 19:14:32 +02:00
|
|
|
"</form>";
|
2020-10-14 12:15:09 +02:00
|
|
|
|
2020-09-11 19:14:32 +02:00
|
|
|
return new Combine([
|
|
|
|
form,
|
|
|
|
extraInfo
|
|
|
|
]).SetStyle("margin-top: 1em;margin-bottom: 2em;text-align: center;")
|
|
|
|
.Render();
|
2020-06-25 03:39:31 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 12:15:09 +02:00
|
|
|
|
|
|
|
private handleSuccessfulUpload(url) {
|
|
|
|
const tags = this._tags.data;
|
2020-10-17 02:37:53 +02:00
|
|
|
let key = this._imagePrefix;
|
|
|
|
if (tags[this._imagePrefix] !== undefined) {
|
2020-10-14 12:15:09 +02:00
|
|
|
|
|
|
|
let freeIndex = 0;
|
2020-10-17 02:37:53 +02:00
|
|
|
while (tags[this._imagePrefix + ":" + freeIndex] !== undefined) {
|
2020-10-14 12:15:09 +02:00
|
|
|
freeIndex++;
|
|
|
|
}
|
2020-10-17 02:37:53 +02:00
|
|
|
key = this._imagePrefix + ":" + freeIndex;
|
2020-10-14 12:15:09 +02:00
|
|
|
}
|
|
|
|
console.log("Adding image:" + key, url);
|
|
|
|
State.state.changes.addTag(tags.id, new Tag(key, url));
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleFiles(files) {
|
2020-10-17 02:37:53 +02:00
|
|
|
console.log("Received images from the user, starting upload")
|
2020-10-14 12:15:09 +02:00
|
|
|
this._isUploading.setData(files.length);
|
|
|
|
this._allDone.setData(false);
|
|
|
|
|
|
|
|
if (this._selectedLicence.data === undefined) {
|
|
|
|
this._selectedLicence.setData("CC0");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const tags = this._tags.data;
|
|
|
|
const title = tags.name ?? "Unknown area";
|
|
|
|
const description = [
|
|
|
|
"author:" + State.state.osmConnection.userDetails.data.name,
|
|
|
|
"license:" + (this._selectedLicence.data ?? "CC0"),
|
|
|
|
"wikidata:" + tags.wikidata,
|
|
|
|
"osmid:" + tags.id,
|
|
|
|
"name:" + tags.name
|
|
|
|
].join("\n");
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
Imgur.uploadMultiple(title,
|
|
|
|
description,
|
|
|
|
files,
|
|
|
|
function (url) {
|
|
|
|
console.log("File saved at", url);
|
|
|
|
self._isUploading.setData(self._isUploading.data - 1);
|
|
|
|
self.handleSuccessfulUpload(url);
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
console.log("All uploads completed");
|
|
|
|
self._allDone.setData(true);
|
|
|
|
},
|
|
|
|
function (failReason) {
|
|
|
|
console.log("Upload failed due to ", failReason)
|
|
|
|
// No need to call something from the options -> we handle this here
|
|
|
|
self._didFail.setData(true);
|
|
|
|
self._isUploading.data--;
|
|
|
|
self._isUploading.ping();
|
|
|
|
}, 0
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-25 03:39:31 +02:00
|
|
|
InnerUpdate(htmlElement: HTMLElement) {
|
|
|
|
super.InnerUpdate(htmlElement);
|
2020-06-28 00:06:23 +02:00
|
|
|
|
2020-07-27 13:04:38 +02:00
|
|
|
this._licensePicker.Update()
|
|
|
|
const form = document.getElementById('fileselector-form-' + this.id) as HTMLFormElement
|
|
|
|
const selector = document.getElementById('fileselector-' + this.id)
|
|
|
|
const self = this
|
|
|
|
|
|
|
|
function submitHandler() {
|
2020-10-17 02:37:53 +02:00
|
|
|
self.handleFiles($(selector).prop('files'))
|
2020-07-27 13:04:38 +02:00
|
|
|
}
|
2020-06-28 00:06:23 +02:00
|
|
|
|
2020-07-27 13:04:38 +02:00
|
|
|
if (selector != null && form != null) {
|
2020-06-28 00:06:23 +02:00
|
|
|
selector.onchange = function () {
|
2020-07-27 13:04:38 +02:00
|
|
|
submitHandler()
|
2020-06-25 03:39:31 +02:00
|
|
|
}
|
2020-07-27 13:04:38 +02:00
|
|
|
form.addEventListener('submit', e => {
|
|
|
|
e.preventDefault()
|
|
|
|
submitHandler()
|
|
|
|
})
|
2020-06-25 03:39:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|