mapcomplete/src/index.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
3 KiB
TypeScript
Raw Normal View History

import DetermineLayout from "./Logic/DetermineLayout"
2023-04-13 23:40:28 +02:00
import ThemeViewState from "./Models/ThemeViewState"
import SvelteUIElement from "./UI/Base/SvelteUIElement"
import ThemeViewGUI from "./UI/ThemeViewGUI.svelte"
import { FixedUiElement } from "./UI/Base/FixedUiElement"
import Combine from "./UI/Base/Combine"
import { SubtleButton } from "./UI/Base/SubtleButton"
import { Utils } from "./Utils"
import Download from "./assets/svg/Download.svelte"
2024-02-18 15:59:28 +01:00
import Constants from "./Models/Constants"
function webgl_support() {
try {
const canvas = document.createElement("canvas")
return (
!!window.WebGLRenderingContext &&
(canvas.getContext("webgl") || canvas.getContext("experimental-webgl"))
)
} catch (e) {
return false
}
}
async function timeout(timeMS: number): Promise<{ layers: string[] }> {
await Utils.waitFor(timeMS)
return { layers: [] }
}
2024-02-19 15:38:46 +01:00
async function getAvailableLayers(): Promise<Set<string>> {
try {
const host = new URL(Constants.VectorTileServer).host
const status: { layers: string[] } = await Promise.any([
2024-04-30 23:14:57 +02:00
Utils.downloadJson<{layers}>("https://" + host + "/summary/status.json"),
timeout(2500),
])
2024-02-19 15:38:46 +01:00
return new Set<string>(status.layers)
} catch (e) {
console.error("Could not get MVT available layers due to", e)
return new Set<string>()
}
2024-02-18 15:59:28 +01:00
}
2024-02-18 15:59:28 +01:00
async function main() {
// @ts-ignore
try {
if (!webgl_support()) {
throw "WebGL is not supported or not enabled. This is essential for MapComplete to function, please enable this."
}
const [layout, availableLayers] = await Promise.all([
DetermineLayout.GetLayout(),
2024-04-13 02:40:21 +02:00
await getAvailableLayers(),
2024-02-18 15:59:28 +01:00
])
2024-02-19 15:38:46 +01:00
console.log("The available layers on server are", Array.from(availableLayers))
const state = new ThemeViewState(layout, availableLayers)
2024-02-18 15:59:28 +01:00
const main = new SvelteUIElement(ThemeViewGUI, { state })
main.AttachTo("maindiv")
2024-04-13 02:40:21 +02:00
Array.from(document.getElementsByClassName("delete-on-load")).forEach((el) => {
2024-03-21 22:39:36 +01:00
el.parentElement.removeChild(el)
})
2024-02-18 15:59:28 +01:00
} catch (err) {
console.error("Error while initializing: ", err, err.stack)
const customDefinition = DetermineLayout.getCustomDefinition()
new Combine([
new FixedUiElement(err.toString().split("\n").join("<br/>")).SetClass("block alert"),
2024-02-18 15:59:28 +01:00
customDefinition?.length > 0
? new SubtleButton(new SvelteUIElement(Download), "Download the raw file").onClick(
2024-04-13 02:40:21 +02:00
() =>
Utils.offerContentsAsDownloadableFile(
DetermineLayout.getCustomDefinition(),
"mapcomplete-theme.json",
{ mimetype: "application/json" }
)
)
: undefined,
2024-02-18 15:59:28 +01:00
]).AttachTo("maindiv")
}
}
2024-02-18 15:59:28 +01:00
2024-04-13 02:40:21 +02:00
main().then((_) => {})