mapcomplete/UI/SimpleAddUI.ts

180 lines
6 KiB
TypeScript
Raw Normal View History

2020-06-29 01:12:44 +00:00
import {UIElement} from "./UIElement";
import {UIEventSource} from "./UIEventSource";
import {Tag} from "../Logic/TagsFilter";
import {FilteredLayer} from "../Logic/FilteredLayer";
import {FixedUiElement} from "./Base/FixedUiElement";
import {Button} from "./Base/Button";
import Translations from "./i18n/Translations";
import Combine from "./Base/Combine";
import {SubtleButton} from "./Base/SubtleButton";
import {VerticalCombine} from "./Base/VerticalCombine";
2020-07-29 19:50:19 +00:00
import Locale from "./i18n/Locale";
2020-07-29 22:59:08 +00:00
import {Changes} from "../Logic/Osm/Changes";
import {UserDetails} from "../Logic/Osm/OsmConnection";
import {State} from "../State";
2020-07-29 22:59:08 +00:00
export interface Preset {
description: string | UIElement,
name: string | UIElement,
icon: string,
tags: Tag[],
layerToAddTo: FilteredLayer
}
2020-06-29 01:12:44 +00:00
/**
* Asks to add a feature at the last clicked location, at least if zoom is sufficient
*/
export class SimpleAddUI extends UIElement {
private _addButtons: UIElement[];
2020-06-29 01:40:19 +00:00
private _dataIsLoading: UIEventSource<boolean>;
2020-06-29 01:12:44 +00:00
private _confirmPreset: UIEventSource<Preset>
= new UIEventSource<Preset>(undefined);
private confirmButton: UIElement = undefined;
private cancelButton: UIElement;
2020-07-29 19:48:41 +00:00
private goToInboxButton: UIElement = new SubtleButton("./assets/envelope.svg",
Translations.t.general.goToInbox, {url:"https://www.openstreetmap.org/messages/inbox", newTab: false});
constructor(
2020-06-29 01:40:19 +00:00
dataIsLoading: UIEventSource<boolean>,
addButtons: { description: string | UIElement, name: string | UIElement; icon: string; tags: Tag[]; layerToAddTo: FilteredLayer }[],
2020-06-29 01:12:44 +00:00
) {
super(State.state.locationControl);
2020-07-29 19:50:19 +00:00
this.ListenTo(Locale.language);
this.ListenTo(State.state.osmConnection.userDetails);
2020-06-29 01:40:19 +00:00
this._dataIsLoading = dataIsLoading;
2020-06-29 14:24:42 +00:00
this.ListenTo(dataIsLoading);
2020-06-29 01:12:44 +00:00
this._addButtons = [];
this.ListenTo(this._confirmPreset);
this.clss = "add-ui"
2020-06-29 01:12:44 +00:00
const self = this;
2020-06-29 01:12:44 +00:00
for (const option of addButtons) {
// <button type='button'> looks SO retarded
// the default type of button is 'submit', which performs a POST and page reload
const button =
new SubtleButton(
option.icon,
new Combine([
"<b>",
option.name,
"</b><br/>",
option.description !== undefined ? option.description : ""])
).onClick(
() => {
self.confirmButton = new SubtleButton(option.icon,
new Combine([
"<b>",
option.name,
"</b><br/>",
option.description !== undefined ? option.description : ""]));
self.confirmButton.onClick(self.CreatePoint(option));
self._confirmPreset.setData(option);
}
)
2020-06-29 01:12:44 +00:00
this._addButtons.push(button);
}
this.cancelButton = new SubtleButton(
2020-07-29 19:48:41 +00:00
"./assets/close.svg",
Translations.t.general.cancel
).onClick(() => {
self._confirmPreset.setData(undefined);
})
2020-06-29 01:12:44 +00:00
}
private CreatePoint(option: { tags: Tag[]; layerToAddTo: FilteredLayer }) {
2020-06-29 01:12:44 +00:00
const self = this;
return () => {
2020-07-31 02:58:58 +00:00
const loc = State.state.bm.LastClickLocation.data;
let feature = State.state.changes.createElement(option.tags, loc.lat, loc.lon);
2020-06-29 01:12:44 +00:00
option.layerToAddTo.AddNewElement(feature);
State.state.selectedElement.setData({feature: feature});
2020-06-29 01:12:44 +00:00
}
}
2020-07-20 16:24:00 +00:00
InnerRender(): string {
const userDetails = State.state.osmConnection.userDetails;
if (this._confirmPreset.data !== undefined) {
if(userDetails.data.dryRun){
2020-07-29 19:48:41 +00:00
this.CreatePoint(this._confirmPreset.data)();
2020-07-31 02:58:58 +00:00
return "";
2020-07-29 19:48:41 +00:00
}
return new Combine([
Translations.t.general.add.confirmIntro.Subs({title: this._confirmPreset.data.name}),
userDetails.data.dryRun ? "<span class='alert'>TESTING - changes won't be saved</span>":"",
this.confirmButton,
this.cancelButton
]).Render();
}
let header: UIElement = Translations.t.general.add.header;
2020-07-29 22:59:08 +00:00
if(userDetails === undefined){
2020-07-29 22:59:08 +00:00
return header.Render();
}
if (!userDetails.data.loggedIn) {
return new Combine([header, Translations.t.general.add.pleaseLogin]).Render()
2020-07-15 11:15:36 +00:00
}
if (userDetails.data.unreadMessages > 0) {
return new Combine([header, "<span class='alert'>",
Translations.t.general.readYourMessages,
2020-07-29 17:02:36 +00:00
"</span>",
this.goToInboxButton
]).Render();
}
if (userDetails.data.dryRun) {
header = new Combine([header,
"<span class='alert'>",
"Test mode - changes won't be saved",
"</span>"
]);
}
if (userDetails.data.csCount < 5) {
return new Combine([header, "<span class='alert'>",
Translations.t.general.fewChangesBefore,
"</span>"]).Render();
}
if (State.state.locationControl.data.zoom < 19) {
return new Combine([header, Translations.t.general.add.zoomInFurther]).Render()
2020-06-29 01:40:19 +00:00
}
2020-06-29 14:21:36 +00:00
if (this._dataIsLoading.data) {
return new Combine([header, Translations.t.general.add.stillLoading]).Render()
2020-06-29 01:12:44 +00:00
}
2020-06-29 01:12:44 +00:00
var html = "";
for (const button of this._addButtons) {
html += button.Render();
}
return header.Render() + html;
2020-06-29 01:12:44 +00:00
}
InnerUpdate(htmlElement: HTMLElement) {
State.state.osmConnection.registerActivateOsmAUthenticationClass();
2020-06-29 01:12:44 +00:00
}
}