mapcomplete/index.ts

164 lines
7.5 KiB
TypeScript
Raw Normal View History

2020-07-05 18:59:47 +02:00
import {AllKnownLayouts} from "./Customizations/AllKnownLayouts";
2020-07-21 00:07:04 +02:00
import {FixedUiElement} from "./UI/Base/FixedUiElement";
2020-07-29 15:05:19 +02:00
import {InitUiElements} from "./InitUiElements";
import {QueryParameters} from "./Logic/Web/QueryParameters";
import {UIEventSource} from "./Logic/UIEventSource";
import * as $ from "jquery";
import MoreScreen from "./UI/BigComponents/MoreScreen";
import State from "./State";
import Combine from "./UI/Base/Combine";
import Translations from "./UI/i18n/Translations";
import ValidatedTextField from "./UI/Input/ValidatedTextField";
import AvailableBaseLayers from "./Logic/Actors/AvailableBaseLayers";
import LayoutConfig from "./Models/ThemeConfig/LayoutConfig";
2021-09-02 21:22:34 +02:00
import Constants from "./Models/Constants";
import MinimapImplementation from "./UI/Base/MinimapImplementation";
import CountryCoder from "latlon2country/index";
import SimpleMetaTagger from "./Logic/SimpleMetaTagger";
MinimapImplementation.initialize()
// Workaround for a stupid crash: inject some functions which would give stupid circular dependencies or crash the other nodejs scripts
ValidatedTextField.bestLayerAt = (location, layerPref) => AvailableBaseLayers.SelectBestLayerAccordingTo(location, layerPref)
SimpleMetaTagger.coder = new CountryCoder("https://pietervdvn.github.io/latlon2country/");
let defaultLayout = ""
2020-07-24 01:12:57 +02:00
// --------------------- Special actions based on the parameters -----------------
2020-07-12 23:19:05 +02:00
// @ts-ignore
2020-07-15 14:03:44 +02:00
if (location.href.startsWith("http://buurtnatuur.be")) {
2020-07-11 11:50:03 +02:00
// Reload the https version. This is important for the 'locate me' button
2020-07-15 14:03:44 +02:00
window.location.replace("https://buurtnatuur.be");
2020-07-11 11:50:03 +02:00
}
2020-11-06 01:58:26 +01:00
2020-09-18 22:23:49 +02:00
if (location.href.indexOf("buurtnatuur.be") >= 0) {
defaultLayout = "buurtnatuur"
}
let testing: UIEventSource<string>;
if (QueryParameters.GetQueryParameter("backend", undefined).data !== "osm-test" &&
(location.hostname === "localhost" || location.hostname === "127.0.0.1")) {
testing = QueryParameters.GetQueryParameter("test", "true");
// Set to true if testing and changes should NOT be saved
2020-07-24 13:46:03 +02:00
testing.setData(testing.data ?? "true")
// If you have a testfile somewhere, enable this to spoof overpass
// This should be hosted independantly, e.g. with `cd assets; webfsd -p 8080` + a CORS plugin to disable cors rules
2021-01-02 21:03:40 +01:00
// Overpass.testUrl = "http://127.0.0.1:8080/streetwidths.geojson";
} else {
testing = QueryParameters.GetQueryParameter("test", "false");
}
2020-06-24 00:35:19 +02:00
2021-04-10 03:50:44 +02:00
// ----------------- SELECT THE RIGHT Theme -----------------
2020-06-24 00:35:19 +02:00
2020-09-18 22:23:49 +02:00
const path = window.location.pathname.split("/").slice(-1)[0];
if (path !== "index.html" && path !== "") {
defaultLayout = path;
if (path.endsWith(".html")) {
defaultLayout = path.substr(0, path.length - 5);
}
console.log("Using layout", defaultLayout);
}
defaultLayout = QueryParameters.GetQueryParameter("layout", defaultLayout, "The layout to load into MapComplete").data;
2021-04-10 03:50:44 +02:00
let layoutToUse: LayoutConfig = AllKnownLayouts.allKnownLayouts.get(defaultLayout.toLowerCase());
const userLayoutParam = QueryParameters.GetQueryParameter("userlayout", "false", "If not 'false', a custom (non-official) theme is loaded. This custom layout can be done in multiple ways: \n\n- The hash of the URL contains a base64-encoded .json-file containing the theme definition\n- The hash of the URL contains a lz-compressed .json-file, as generated by the custom theme generator\n- The parameter itself is an URL, in which case that URL will be downloaded. It should point to a .json of a theme");
2021-04-22 03:54:14 +02:00
// Workaround/legacy to keep the old paramters working as I renamed some of them
if (layoutToUse?.id === "cyclofix") {
const legacy = QueryParameters.GetQueryParameter("layer-bike_shops", "true", "Legacy - keep De Fietsambassade working");
const correct = QueryParameters.GetQueryParameter("layer-bike_shop", "true", "Legacy - keep De Fietsambassade working")
if (legacy.data !== "true") {
correct.setData(legacy.data)
}
console.log("layer-bike_shop toggles: legacy:", legacy.data, "new:", correct.data)
const legacyCafe = QueryParameters.GetQueryParameter("layer-bike_cafes", "true", "Legacy - keep De Fietsambassade working")
const correctCafe = QueryParameters.GetQueryParameter("layer-bike_cafe", "true", "Legacy - keep De Fietsambassade working")
if (legacyCafe.data !== "true") {
correctCafe.setData(legacy.data)
}
}
const layoutFromBase64 = decodeURIComponent(userLayoutParam.data);
new Combine(["Initializing... <br/>",
new FixedUiElement("<a>If this message persist, something went wrong - click here to try again</a>")
.SetClass("link-underline small")
.onClick(() => {
localStorage.clear();
window.location.reload(true);
})])
.AttachTo("centermessage"); // Add an initialization and reset button if something goes wrong
document.getElementById("decoration-desktop").remove();
if (layoutFromBase64.startsWith("http")) {
const link = layoutFromBase64;
console.log("Downloading map theme from ", link);
new FixedUiElement(`Downloading the theme from the <a href="${link}">link</a>...`)
.AttachTo("centermessage");
$.ajax({
url: link,
2021-04-10 03:50:44 +02:00
success: (data) => {
try {
console.log("Received ", data)
let parsed = data;
if (typeof parsed == "string") {
parsed = JSON.parse(data);
} else {
2021-05-20 12:27:33 +02:00
data = JSON.stringify(parsed) // De wereld op zijn kop
}
// Overwrite the id to the wiki:value
parsed.id = link;
const layout = new LayoutConfig(parsed, false).patchImages(link, data);
2020-11-17 18:23:40 +01:00
InitUiElements.InitAll(layout, layoutFromBase64, testing, layoutFromBase64, btoa(data));
} catch (e) {
new FixedUiElement(`<a href="${link}">${link}</a> is invalid:<br/>${e}<br/> <a href='https://${window.location.host}/'>Go back</a>`)
.SetClass("clickable")
.AttachTo("centermessage");
2021-01-09 00:29:12 +01:00
console.error("Could not parse the text", data)
throw e;
2020-07-31 04:58:58 +02:00
}
},
}).fail((_, textstatus, error) => {
console.error("Could not download the wiki theme:", textstatus, error)
new FixedUiElement(`<a href="${link}">${link}</a> is invalid:<br/>Could not download - wrong URL?<br/>` +
error +
"<a href='https://${window.location.host}/'>Go back</a>")
.SetClass("clickable")
.AttachTo("centermessage");
});
2020-09-15 00:25:25 +02:00
} else if (layoutFromBase64 !== "false") {
let [layoutToUse, encoded] = InitUiElements.LoadLayoutFromHash(userLayoutParam);
InitUiElements.InitAll(layoutToUse, layoutFromBase64, testing, defaultLayout, encoded);
} else if (layoutToUse !== undefined) {
// This is the default case: a builtin theme
InitUiElements.InitAll(layoutToUse, layoutFromBase64, testing, defaultLayout);
} else {
// We fall through: no theme loaded: just show an overview of layouts
2021-05-30 00:17:31 +02:00
new FixedUiElement("").AttachTo("centermessage")
State.state = new State(undefined);
2021-01-27 03:08:46 +01:00
new Combine([new MoreScreen(true),
2021-09-02 21:22:34 +02:00
Translations.t.general.aboutMapcomplete.SetClass("link-underline"),
new FixedUiElement("v" + Constants.vNumber)
2021-01-25 03:12:09 +01:00
]).SetClass("block m-5 lg:w-3/4 lg:ml-40")
2021-01-27 03:08:46 +01:00
.SetStyle("pointer-events: all;")
.AttachTo("topleft-tools");
2020-07-29 15:05:19 +02:00
}
// Remove all context event listeners on mobile to prevent long presses
2021-04-10 03:50:44 +02:00
window.addEventListener('contextmenu', (e) => { // Not compatible with IE < 9
if (e.target["nodeName"] === "INPUT") {
return;
}
e.preventDefault();
return false;
}, false);