2022-11-02 14:44:06 +01:00
|
|
|
import Toggle from "../Input/Toggle"
|
|
|
|
import { RadioButton } from "../Input/RadioButton"
|
|
|
|
import { FixedInputElement } from "../Input/FixedInputElement"
|
|
|
|
import Combine from "../Base/Combine"
|
|
|
|
import Translations from "../i18n/Translations"
|
|
|
|
import { TextField } from "../Input/TextField"
|
2023-01-06 03:42:12 +01:00
|
|
|
import { Store, UIEventSource } from "../../Logic/UIEventSource"
|
2022-11-02 14:44:06 +01:00
|
|
|
import Title from "../Base/Title"
|
|
|
|
import { SubtleButton } from "../Base/SubtleButton"
|
|
|
|
import Svg from "../../Svg"
|
|
|
|
import { OsmConnection } from "../../Logic/Osm/OsmConnection"
|
|
|
|
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"
|
|
|
|
import { Translation } from "../i18n/Translation"
|
2023-01-06 03:42:12 +01:00
|
|
|
import { LoginToggle } from "../Popup/LoginButton"
|
2022-08-05 12:39:02 +02:00
|
|
|
|
2023-01-06 03:42:12 +01:00
|
|
|
export default class UploadTraceToOsmUI extends LoginToggle {
|
2022-08-05 12:39:02 +02:00
|
|
|
constructor(
|
2022-08-05 19:47:24 +02:00
|
|
|
trace: (title: string) => string,
|
2022-08-05 12:39:02 +02:00
|
|
|
state: {
|
2022-11-02 14:44:06 +01:00
|
|
|
layoutToUse: LayoutConfig
|
2022-08-05 12:39:02 +02:00
|
|
|
osmConnection: OsmConnection
|
2023-01-06 03:42:12 +01:00
|
|
|
readonly featureSwitchUserbadge: Store<boolean>
|
2022-11-02 14:44:06 +01:00
|
|
|
},
|
|
|
|
options?: {
|
2022-08-05 12:39:02 +02:00
|
|
|
whenUploaded?: () => void | Promise<void>
|
2022-11-02 14:44:06 +01:00
|
|
|
}
|
|
|
|
) {
|
2022-08-05 12:39:02 +02:00
|
|
|
const t = Translations.t.general.uploadGpx
|
2022-08-05 19:47:24 +02:00
|
|
|
const uploadFinished = new UIEventSource(false)
|
2022-08-05 12:39:02 +02:00
|
|
|
const traceVisibilities: {
|
2022-11-02 14:44:06 +01:00
|
|
|
key: "private" | "public"
|
|
|
|
name: Translation
|
2022-08-05 12:39:02 +02:00
|
|
|
docs: Translation
|
|
|
|
}[] = [
|
|
|
|
{
|
|
|
|
key: "private",
|
2022-11-02 14:44:06 +01:00
|
|
|
...t.modes.private,
|
2022-08-05 12:39:02 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "public",
|
2022-11-02 14:44:06 +01:00
|
|
|
...t.modes.public,
|
|
|
|
},
|
2022-08-05 12:39:02 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
const dropdown = new RadioButton<"private" | "public">(
|
2022-11-02 14:44:06 +01:00
|
|
|
traceVisibilities.map(
|
|
|
|
(tv) =>
|
|
|
|
new FixedInputElement<"private" | "public">(
|
|
|
|
new Combine([
|
|
|
|
Translations.W(tv.name).SetClass("font-bold"),
|
|
|
|
tv.docs,
|
|
|
|
]).SetClass("flex flex-col"),
|
|
|
|
tv.key
|
|
|
|
)
|
|
|
|
),
|
2022-08-05 12:39:02 +02:00
|
|
|
{
|
2022-11-02 14:44:06 +01:00
|
|
|
value: <any>state?.osmConnection?.GetPreference("gps.trace.visibility"),
|
2022-08-05 12:39:02 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
const description = new TextField({
|
2022-11-02 14:44:06 +01:00
|
|
|
placeholder: t.meta.descriptionPlaceHolder,
|
2022-08-05 19:47:24 +02:00
|
|
|
})
|
|
|
|
const title = new TextField({
|
2022-11-02 14:44:06 +01:00
|
|
|
placeholder: t.meta.titlePlaceholder,
|
2022-08-05 12:39:02 +02:00
|
|
|
})
|
|
|
|
const clicked = new UIEventSource<boolean>(false)
|
|
|
|
|
|
|
|
const confirmPanel = new Combine([
|
|
|
|
new Title(t.title),
|
|
|
|
t.intro0,
|
|
|
|
t.intro1,
|
|
|
|
|
|
|
|
t.choosePermission,
|
|
|
|
dropdown,
|
2022-08-05 19:47:24 +02:00
|
|
|
new Title(t.meta.title, 4),
|
|
|
|
t.meta.intro,
|
|
|
|
title,
|
|
|
|
t.meta.descriptionIntro,
|
2022-08-05 12:39:02 +02:00
|
|
|
description,
|
|
|
|
new Combine([
|
2022-11-02 14:44:06 +01:00
|
|
|
new SubtleButton(Svg.close_svg(), Translations.t.general.cancel)
|
|
|
|
.onClick(() => {
|
|
|
|
clicked.setData(false)
|
2022-08-05 12:39:02 +02:00
|
|
|
})
|
2022-11-02 14:44:06 +01:00
|
|
|
.SetClass(""),
|
|
|
|
new SubtleButton(Svg.upload_svg(), t.confirm).OnClickWithLoading(
|
|
|
|
t.uploading,
|
|
|
|
async () => {
|
|
|
|
const titleStr = UploadTraceToOsmUI.createDefault(
|
|
|
|
title.GetValue().data,
|
|
|
|
"Track with mapcomplete"
|
|
|
|
)
|
|
|
|
const descriptionStr = UploadTraceToOsmUI.createDefault(
|
|
|
|
description.GetValue().data,
|
|
|
|
"Track created with MapComplete with theme " + state?.layoutToUse?.id
|
|
|
|
)
|
|
|
|
await state?.osmConnection?.uploadGpxTrack(trace(title.GetValue().data), {
|
|
|
|
visibility: dropdown.GetValue().data,
|
|
|
|
description: descriptionStr,
|
|
|
|
filename: titleStr + ".gpx",
|
|
|
|
labels: ["MapComplete", state?.layoutToUse?.id],
|
|
|
|
})
|
2022-08-05 12:39:02 +02:00
|
|
|
|
2022-11-02 14:44:06 +01:00
|
|
|
if (options?.whenUploaded !== undefined) {
|
|
|
|
await options.whenUploaded()
|
|
|
|
}
|
|
|
|
uploadFinished.setData(true)
|
2022-08-05 12:39:02 +02:00
|
|
|
}
|
2022-11-02 14:44:06 +01:00
|
|
|
),
|
|
|
|
]).SetClass("flex flex-wrap flex-wrap-reverse justify-between items-stretch"),
|
2022-08-05 12:39:02 +02:00
|
|
|
]).SetClass("flex flex-col p-4 rounded border-2 m-2 border-subtle")
|
|
|
|
|
|
|
|
super(
|
2022-08-05 19:47:24 +02:00
|
|
|
new Toggle(
|
2023-01-06 03:42:12 +01:00
|
|
|
new Toggle(
|
|
|
|
new Combine([
|
|
|
|
Svg.confirm_svg().SetClass("w-12 h-12 mr-2"),
|
|
|
|
t.uploadFinished,
|
|
|
|
]).SetClass("flex p-2 rounded-xl border-2 subtle-border items-center"),
|
|
|
|
new Toggle(
|
|
|
|
confirmPanel,
|
|
|
|
new SubtleButton(Svg.upload_svg(), t.title).onClick(() =>
|
|
|
|
clicked.setData(true)
|
|
|
|
),
|
|
|
|
clicked
|
|
|
|
),
|
|
|
|
uploadFinished
|
|
|
|
),
|
|
|
|
new Combine([
|
|
|
|
Svg.invalid_ui().SetClass("w-8 h-8 m-2"),
|
|
|
|
t.gpxServiceOffline.SetClass("p-2"),
|
|
|
|
]).SetClass("flex border alert items-center"),
|
|
|
|
state.osmConnection.gpxServiceIsOnline.map(
|
|
|
|
(serviceState) => serviceState === "online"
|
|
|
|
)
|
2022-11-02 14:44:06 +01:00
|
|
|
),
|
2023-01-06 03:42:12 +01:00
|
|
|
undefined,
|
|
|
|
state
|
2022-11-02 14:44:06 +01:00
|
|
|
)
|
2022-08-05 12:39:02 +02:00
|
|
|
}
|
2023-01-06 03:42:12 +01:00
|
|
|
|
|
|
|
private static createDefault(s: string, defaultValue: string) {
|
|
|
|
if (defaultValue.length < 1) {
|
|
|
|
throw "Default value should have some characters"
|
|
|
|
}
|
|
|
|
if (s === undefined || s === null || s === "") {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2022-10-28 04:38:10 +02:00
|
|
|
}
|