2022-10-27 01:50:41 +02:00
|
|
|
import Combine from "../Base/Combine"
|
2023-05-06 12:35:02 +02:00
|
|
|
import {FlowPanelFactory, FlowStep} from "../ImportFlow/FlowStep"
|
|
|
|
import {ImmutableStore, Store, UIEventSource} from "../../Logic/UIEventSource"
|
|
|
|
import {InputElement} from "../Input/InputElement"
|
|
|
|
import {SvgToPdf, SvgToPdfOptions} from "../../Utils/svgToPdf"
|
|
|
|
import {FixedInputElement} from "../Input/FixedInputElement"
|
|
|
|
import {FixedUiElement} from "../Base/FixedUiElement"
|
2022-10-27 01:50:41 +02:00
|
|
|
import FileSelectorButton from "../Input/FileSelectorButton"
|
|
|
|
import InputElementMap from "../Input/InputElementMap"
|
2023-05-06 12:35:02 +02:00
|
|
|
import {RadioButton} from "../Input/RadioButton"
|
|
|
|
import {Utils} from "../../Utils"
|
|
|
|
import {VariableUiElement} from "../Base/VariableUIElement"
|
2022-10-27 01:50:41 +02:00
|
|
|
import Loading from "../Base/Loading"
|
|
|
|
import BaseUIElement from "../BaseUIElement"
|
|
|
|
import Img from "../Base/Img"
|
|
|
|
import Title from "../Base/Title"
|
2023-05-06 12:35:02 +02:00
|
|
|
import {CheckBox} from "../Input/Checkboxes"
|
2022-10-27 01:50:41 +02:00
|
|
|
import Toggle from "../Input/Toggle"
|
|
|
|
import List from "../Base/List"
|
|
|
|
import LeftIndex from "../Base/LeftIndex"
|
|
|
|
import Constants from "../../Models/Constants"
|
|
|
|
import Toggleable from "../Base/Toggleable"
|
|
|
|
import Lazy from "../Base/Lazy"
|
|
|
|
import LinkToWeblate from "../Base/LinkToWeblate"
|
|
|
|
import Link from "../Base/Link"
|
2023-05-06 12:35:02 +02:00
|
|
|
import {AllLanguagesSelector} from "../Popup/AllLanguagesSelector"
|
2022-09-18 12:45:02 +02:00
|
|
|
|
2022-10-27 01:50:41 +02:00
|
|
|
class SelectTemplate extends Combine implements FlowStep<{ title: string; pages: string[] }> {
|
|
|
|
readonly IsValid: Store<boolean>
|
|
|
|
readonly Value: Store<{ title: string; pages: string[] }>
|
2022-09-18 12:45:02 +02:00
|
|
|
|
|
|
|
constructor() {
|
2022-10-27 01:50:41 +02:00
|
|
|
const elements: InputElement<{ templateName: string; pages: string[] }>[] = []
|
2022-09-18 12:45:02 +02:00
|
|
|
for (const templateName in SvgToPdf.templates) {
|
|
|
|
const template = SvgToPdf.templates[templateName]
|
2022-10-27 01:50:41 +02:00
|
|
|
elements.push(
|
|
|
|
new FixedInputElement(
|
|
|
|
new Combine([
|
|
|
|
new FixedUiElement(templateName).SetClass("font-bold pr-2"),
|
|
|
|
template.description,
|
|
|
|
]),
|
|
|
|
new UIEventSource({ templateName, pages: template.pages })
|
|
|
|
)
|
|
|
|
)
|
2022-09-18 12:45:02 +02:00
|
|
|
}
|
|
|
|
|
2022-10-27 01:50:41 +02:00
|
|
|
const file = new FileSelectorButton(
|
|
|
|
new FixedUiElement("Select an svg image which acts as template"),
|
|
|
|
{
|
|
|
|
acceptType: "image/svg+xml",
|
|
|
|
allowMultiple: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
const fileMapped = new InputElementMap<
|
|
|
|
FileList,
|
|
|
|
{ templateName: string; pages: string[]; fromFile: true }
|
|
|
|
>(
|
|
|
|
file,
|
|
|
|
(x0, x1) => x0 === x1,
|
2022-09-18 12:45:02 +02:00
|
|
|
(filelist) => {
|
|
|
|
if (filelist === undefined) {
|
2022-10-27 01:50:41 +02:00
|
|
|
return undefined
|
2022-09-18 12:45:02 +02:00
|
|
|
}
|
|
|
|
const pages = []
|
2022-10-27 01:50:41 +02:00
|
|
|
let templateName: string = undefined
|
2022-09-18 12:45:02 +02:00
|
|
|
for (const file of Array.from(filelist)) {
|
|
|
|
if (templateName == undefined) {
|
|
|
|
templateName = file.name.substring(file.name.lastIndexOf("/") + 1)
|
|
|
|
templateName = templateName.substring(0, templateName.lastIndexOf("."))
|
|
|
|
}
|
|
|
|
pages.push(file.text())
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
templateName,
|
|
|
|
pages,
|
2022-10-27 01:50:41 +02:00
|
|
|
fromFile: true,
|
2022-09-18 12:45:02 +02:00
|
|
|
}
|
|
|
|
},
|
2022-10-27 01:50:41 +02:00
|
|
|
(_) => undefined
|
2022-09-18 12:45:02 +02:00
|
|
|
)
|
|
|
|
elements.push(fileMapped)
|
2022-10-27 01:50:41 +02:00
|
|
|
const radio = new RadioButton(elements, { selectFirstAsDefault: true })
|
2022-09-18 12:45:02 +02:00
|
|
|
|
2022-10-27 01:50:41 +02:00
|
|
|
const loaded: Store<{ success: { title: string; pages: string[] } } | { error: any }> =
|
|
|
|
radio.GetValue().bind((template) => {
|
|
|
|
if (template === undefined) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
if (template["fromFile"]) {
|
|
|
|
return UIEventSource.FromPromiseWithErr(
|
|
|
|
Promise.all(template.pages).then((pages) => ({
|
|
|
|
title: template.templateName,
|
|
|
|
pages,
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const urls = template.pages.map((p) => SelectTemplate.ToUrl(p))
|
|
|
|
const dloadAll: Promise<{ title: string; pages: string[] }> = Promise.all(
|
|
|
|
urls.map((url) => Utils.download(url))
|
|
|
|
).then((pages) => ({
|
|
|
|
pages,
|
2022-09-18 12:45:02 +02:00
|
|
|
title: template.templateName,
|
2022-10-27 01:50:41 +02:00
|
|
|
}))
|
2022-09-18 12:45:02 +02:00
|
|
|
|
2022-10-27 01:50:41 +02:00
|
|
|
return UIEventSource.FromPromiseWithErr(dloadAll)
|
|
|
|
})
|
2022-09-18 12:45:02 +02:00
|
|
|
const preview = new VariableUiElement(
|
2022-10-27 01:50:41 +02:00
|
|
|
loaded.map((pages) => {
|
2022-09-18 12:45:02 +02:00
|
|
|
if (pages === undefined) {
|
|
|
|
return new Loading()
|
|
|
|
}
|
2022-09-21 00:37:05 +02:00
|
|
|
if (pages["error"] !== undefined) {
|
2022-10-27 01:50:41 +02:00
|
|
|
return new FixedUiElement("Loading preview failed: " + pages["err"]).SetClass(
|
|
|
|
"alert"
|
|
|
|
)
|
2022-09-18 12:45:02 +02:00
|
|
|
}
|
|
|
|
const svgs = pages["success"].pages
|
|
|
|
if (svgs.length === 0) {
|
|
|
|
return new FixedUiElement("No pages loaded...").SetClass("alert")
|
|
|
|
}
|
|
|
|
const els: BaseUIElement[] = []
|
|
|
|
for (const pageSrc of svgs) {
|
2022-10-27 01:50:41 +02:00
|
|
|
const el = new Img(pageSrc, true).SetClass("w-96 m-2 border-black border-2")
|
2022-09-18 12:45:02 +02:00
|
|
|
els.push(el)
|
|
|
|
}
|
2022-10-27 01:50:41 +02:00
|
|
|
return new Combine(els).SetClass("flex border border-subtle rounded-xl")
|
2022-09-18 12:45:02 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2022-10-27 01:50:41 +02:00
|
|
|
super([new Title("Select template"), radio, new Title("Preview"), preview])
|
|
|
|
this.Value = loaded.map((l) => (l === undefined ? undefined : l["success"]))
|
|
|
|
this.IsValid = this.Value.map((v) => v !== undefined)
|
2022-09-18 12:45:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static ToUrl(spec: string) {
|
|
|
|
if (spec.startsWith("http")) {
|
|
|
|
return spec
|
|
|
|
}
|
2022-09-21 00:37:05 +02:00
|
|
|
let path = window.location.pathname
|
|
|
|
path = path.substring(0, path.lastIndexOf("/"))
|
2022-09-21 02:23:19 +02:00
|
|
|
return window.location.protocol + "//" + window.location.host + path + "/" + spec
|
2022-09-18 12:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-27 01:50:41 +02:00
|
|
|
class SelectPdfOptions
|
|
|
|
extends Combine
|
|
|
|
implements FlowStep<{ title: string; pages: string[]; options: SvgToPdfOptions }>
|
|
|
|
{
|
|
|
|
readonly IsValid: Store<boolean>
|
|
|
|
readonly Value: Store<{ title: string; pages: string[]; options: SvgToPdfOptions }>
|
2022-09-18 12:45:02 +02:00
|
|
|
|
|
|
|
constructor(title: string, pages: string[], getFreeDiv: () => string) {
|
|
|
|
const dummy = new CheckBox("Don't add data to the map (to quickly preview the PDF)", false)
|
2022-10-27 01:50:41 +02:00
|
|
|
const overrideMapLocation = new CheckBox(
|
|
|
|
"Override map location: use a selected location instead of the location set in the template",
|
|
|
|
false
|
|
|
|
)
|
2022-09-18 12:45:02 +02:00
|
|
|
const locationInput = Minimap.createMiniMap().SetClass("block w-full")
|
2023-05-06 12:35:02 +02:00
|
|
|
const searchField = undefined // new SearchAndGo({ leafletMap: locationInput.leafletMap })
|
2022-10-27 01:50:41 +02:00
|
|
|
const selectLocation = new Combine([
|
|
|
|
new Toggle(
|
|
|
|
new Combine([new Title("Select override location"), searchField]).SetClass("flex"),
|
|
|
|
undefined,
|
|
|
|
overrideMapLocation.GetValue()
|
|
|
|
),
|
|
|
|
new Toggle(
|
|
|
|
locationInput.SetStyle("height: 20rem"),
|
|
|
|
undefined,
|
|
|
|
overrideMapLocation.GetValue()
|
|
|
|
).SetStyle("height: 20rem"),
|
|
|
|
])
|
|
|
|
.SetClass("block")
|
|
|
|
.SetStyle("height: 25rem")
|
|
|
|
super([new Title("Select options"), dummy, overrideMapLocation, selectLocation])
|
|
|
|
this.Value = dummy.GetValue().map(
|
|
|
|
(disableMaps) => {
|
|
|
|
return {
|
|
|
|
pages,
|
|
|
|
title,
|
|
|
|
options: <SvgToPdfOptions>{
|
|
|
|
disableMaps,
|
|
|
|
getFreeDiv,
|
|
|
|
overrideLocation: overrideMapLocation.GetValue().data
|
|
|
|
? locationInput.location.data
|
|
|
|
: undefined,
|
|
|
|
},
|
2022-09-18 12:45:02 +02:00
|
|
|
}
|
2022-10-27 01:50:41 +02:00
|
|
|
},
|
|
|
|
[overrideMapLocation.GetValue(), locationInput.location]
|
|
|
|
)
|
2022-09-18 12:45:02 +02:00
|
|
|
this.IsValid = new ImmutableStore(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-27 01:50:41 +02:00
|
|
|
class PreparePdf extends Combine implements FlowStep<{ svgToPdf: SvgToPdf; languages: string[] }> {
|
|
|
|
readonly IsValid: Store<boolean>
|
|
|
|
readonly Value: Store<{ svgToPdf: SvgToPdf; languages: string[] }>
|
2022-09-18 12:45:02 +02:00
|
|
|
|
|
|
|
constructor(title: string, pages: string[], options: SvgToPdfOptions) {
|
|
|
|
const svgToPdf = new SvgToPdf(title, pages, options)
|
2022-11-02 14:44:06 +01:00
|
|
|
const languageSelector = new AllLanguagesSelector()
|
2022-09-18 12:45:02 +02:00
|
|
|
const isPrepared = UIEventSource.FromPromiseWithErr(svgToPdf.Prepare())
|
|
|
|
|
|
|
|
super([
|
|
|
|
new Title("Select languages..."),
|
2022-09-22 16:21:07 +02:00
|
|
|
languageSelector,
|
2022-09-18 12:45:02 +02:00
|
|
|
new Toggle(
|
|
|
|
new Loading("Preparing maps..."),
|
|
|
|
undefined,
|
2022-10-27 01:50:41 +02:00
|
|
|
isPrepared.map((p) => p === undefined)
|
|
|
|
),
|
|
|
|
])
|
|
|
|
this.Value = isPrepared.map(
|
|
|
|
(isPrepped) => {
|
|
|
|
if (isPrepped === undefined) {
|
2022-09-18 12:45:02 +02:00
|
|
|
return undefined
|
|
|
|
}
|
2022-10-27 01:50:41 +02:00
|
|
|
if (isPrepped["success"] !== undefined) {
|
|
|
|
const svgToPdf = isPrepped["success"]
|
|
|
|
const langs = languageSelector.GetValue().data
|
|
|
|
console.log("Languages are", langs)
|
|
|
|
if (langs.length === 0) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
return { svgToPdf, languages: langs }
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
},
|
|
|
|
[languageSelector.GetValue()]
|
|
|
|
)
|
|
|
|
this.IsValid = this.Value.map((v) => v !== undefined)
|
2022-09-18 12:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-27 01:50:41 +02:00
|
|
|
class InspectStrings
|
|
|
|
extends Toggle
|
|
|
|
implements FlowStep<{ svgToPdf: SvgToPdf; languages: string[] }>
|
|
|
|
{
|
|
|
|
readonly IsValid: Store<boolean>
|
|
|
|
readonly Value: Store<{ svgToPdf: SvgToPdf; languages: string[] }>
|
2022-09-22 16:21:07 +02:00
|
|
|
|
|
|
|
constructor(svgToPdf: SvgToPdf, languages: string[]) {
|
2022-10-27 01:50:41 +02:00
|
|
|
const didLoadLanguages = UIEventSource.FromPromiseWithErr(
|
|
|
|
svgToPdf.PrepareLanguages(languages)
|
|
|
|
).map((l) => l !== undefined && l["success"] !== undefined)
|
2022-09-22 16:21:07 +02:00
|
|
|
|
2022-10-27 01:50:41 +02:00
|
|
|
super(
|
|
|
|
new Combine([
|
2022-09-22 16:21:07 +02:00
|
|
|
new Title("Inspect translation strings"),
|
2022-10-27 01:50:41 +02:00
|
|
|
...languages.map(
|
|
|
|
(l) => new Lazy(() => InspectStrings.createOverviewPanel(svgToPdf, l))
|
|
|
|
),
|
2022-09-22 16:21:07 +02:00
|
|
|
]),
|
|
|
|
new Loading(),
|
|
|
|
didLoadLanguages
|
2022-10-27 01:50:41 +02:00
|
|
|
)
|
|
|
|
this.Value = new ImmutableStore({ svgToPdf, languages })
|
2022-09-22 16:21:07 +02:00
|
|
|
this.IsValid = didLoadLanguages
|
|
|
|
}
|
|
|
|
|
|
|
|
private static createOverviewPanel(svgToPdf: SvgToPdf, language: string): BaseUIElement {
|
|
|
|
const elements: BaseUIElement[] = []
|
2022-09-23 01:28:53 +02:00
|
|
|
let foundTranslations = 0
|
|
|
|
const allKeys = Array.from(svgToPdf.translationKeys())
|
|
|
|
for (const translationKey of allKeys) {
|
2022-09-22 16:21:07 +02:00
|
|
|
let spec = translationKey
|
|
|
|
if (translationKey.startsWith("layer.")) {
|
|
|
|
spec = "layers:" + translationKey.substring(6)
|
|
|
|
} else {
|
|
|
|
spec = "core:" + translationKey
|
|
|
|
}
|
2022-09-23 01:28:53 +02:00
|
|
|
const translated = svgToPdf.getTranslation("$" + translationKey, language, true)
|
|
|
|
if (translated) {
|
|
|
|
foundTranslations++
|
|
|
|
}
|
2022-10-27 01:50:41 +02:00
|
|
|
const linkToWeblate = new Link(
|
|
|
|
spec,
|
|
|
|
LinkToWeblate.hrefToWeblate(language, spec),
|
|
|
|
true
|
|
|
|
).SetClass("font-bold link-underline")
|
|
|
|
elements.push(
|
|
|
|
new Combine([
|
|
|
|
linkToWeblate,
|
|
|
|
" ",
|
|
|
|
translated ?? new FixedUiElement("No translation found!").SetClass("alert"),
|
|
|
|
])
|
|
|
|
)
|
2022-09-22 16:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Toggleable(
|
|
|
|
new Title("Translations for " + language),
|
2022-09-23 01:28:53 +02:00
|
|
|
new Combine([
|
2022-10-27 01:50:41 +02:00
|
|
|
`${foundTranslations}/${allKeys.length} of translations are found (${Math.floor(
|
|
|
|
(100 * foundTranslations) / allKeys.length
|
|
|
|
)}%)`,
|
2022-09-23 01:28:53 +02:00
|
|
|
"The following keys are used:",
|
2022-10-27 01:50:41 +02:00
|
|
|
new List(elements),
|
2022-09-22 16:21:07 +02:00
|
|
|
]),
|
2022-10-27 01:50:41 +02:00
|
|
|
{ closeOnClick: false, height: "15rem" }
|
|
|
|
)
|
2022-09-22 16:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-18 12:45:02 +02:00
|
|
|
|
|
|
|
class SavePdf extends Combine {
|
|
|
|
constructor(svgToPdf: SvgToPdf, languages: string[]) {
|
|
|
|
super([
|
|
|
|
new Title("Generating your pdfs..."),
|
2022-10-27 01:50:41 +02:00
|
|
|
new List(
|
|
|
|
languages.map(
|
|
|
|
(lng) =>
|
|
|
|
new Toggle(
|
|
|
|
lng + " is done!",
|
|
|
|
new Loading("Creating pdf for " + lng),
|
|
|
|
UIEventSource.FromPromiseWithErr(
|
|
|
|
svgToPdf.ConvertSvg(lng).then(() => true)
|
|
|
|
).map((x) => x !== undefined && x["success"] === true)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
])
|
2022-09-18 12:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PdfExportGui extends LeftIndex {
|
|
|
|
constructor(freeDivId: string) {
|
|
|
|
let i = 0
|
|
|
|
const createDiv = (): string => {
|
|
|
|
const div = document.createElement("div")
|
2022-10-27 01:50:41 +02:00
|
|
|
div.id = "freediv-" + i++
|
2022-09-18 12:45:02 +02:00
|
|
|
document.getElementById(freeDivId).append(div)
|
|
|
|
return div.id
|
|
|
|
}
|
|
|
|
|
|
|
|
Constants.defaultOverpassUrls.splice(0, 1)
|
2022-10-27 01:50:41 +02:00
|
|
|
const { flow, furthestStep, titles } = FlowPanelFactory.start(
|
|
|
|
new Title("Select template"),
|
|
|
|
new SelectTemplate()
|
|
|
|
)
|
|
|
|
.then(
|
|
|
|
new Title("Select options"),
|
|
|
|
({ title, pages }) => new SelectPdfOptions(title, pages, createDiv)
|
|
|
|
)
|
|
|
|
.then(
|
|
|
|
"Generate maps...",
|
|
|
|
({ title, pages, options }) => new PreparePdf(title, pages, options)
|
|
|
|
)
|
|
|
|
.then(
|
|
|
|
"Inspect translations",
|
|
|
|
({ svgToPdf, languages }) => new InspectStrings(svgToPdf, languages)
|
|
|
|
)
|
|
|
|
.finish("Generating...", ({ svgToPdf, languages }) => new SavePdf(svgToPdf, languages))
|
2022-09-18 12:45:02 +02:00
|
|
|
|
|
|
|
const toc = new List(
|
|
|
|
titles.map(
|
|
|
|
(title, i) =>
|
|
|
|
new VariableUiElement(
|
|
|
|
furthestStep.map((currentStep) => {
|
|
|
|
if (i > currentStep) {
|
|
|
|
return new Combine([title]).SetClass("subtle")
|
|
|
|
}
|
|
|
|
if (i == currentStep) {
|
|
|
|
return new Combine([title]).SetClass("font-bold")
|
|
|
|
}
|
|
|
|
if (i < currentStep) {
|
|
|
|
return title
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
),
|
|
|
|
true
|
|
|
|
)
|
|
|
|
|
2022-10-27 01:50:41 +02:00
|
|
|
const leftContents: BaseUIElement[] = [toc].map((el) => el?.SetClass("pl-4"))
|
2022-09-18 12:45:02 +02:00
|
|
|
|
|
|
|
super(leftContents, flow)
|
|
|
|
}
|
|
|
|
}
|