mapcomplete/index.ts

107 lines
4.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";
2020-11-11 16:23:49 +01:00
import LayoutConfig from "./Customizations/JSON/LayoutConfig";
2020-11-14 03:26:09 +01:00
import {Utils} from "./Utils";
2021-01-02 21:03:40 +01:00
import {Overpass} from "./Logic/Osm/Overpass";
2020-12-14 01:14:22 +01:00
let defaultLayout = "bookcases"
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"
}
2020-11-14 03:26:09 +01:00
const customCssQP = QueryParameters.GetQueryParameter("custom-css", "", "If specified, the custom css from the given link will be loaded additionaly");
if(customCssQP.data !== undefined && customCssQP.data !== ""){
Utils.LoadCustomCss(customCssQP.data);
}
2020-09-18 22:23:49 +02:00
2020-09-17 13:13:02 +02:00
let testing: UIEventSource<string>;
if (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
// ----------------- SELECT THE RIGHT QUESTSET -----------------
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);
}
2020-11-13 23:58:11 +01:00
defaultLayout = QueryParameters.GetQueryParameter("layout", defaultLayout,"The layout to load into MapComplete").data;
2020-11-11 16:23:49 +01:00
let layoutToUse: LayoutConfig = AllKnownLayouts.allSets[defaultLayout.toLowerCase()] ?? AllKnownLayouts["all"];
const userLayoutParam = QueryParameters.GetQueryParameter("userlayout", "false");
const layoutFromBase64 = decodeURIComponent(userLayoutParam.data);
if (layoutFromBase64.startsWith("wiki:")) {
console.log("Downloading map theme from the wiki");
const themeName = layoutFromBase64.substr("wiki:".length);
new FixedUiElement(`Downloading ${themeName} from the wiki...`)
.AttachTo("centermessage");
2020-09-15 00:25:25 +02:00
const cleanUrl = `https://wiki.openstreetmap.org/wiki/${themeName}`;
const url = `https://cors-anywhere.herokuapp.com/` + cleanUrl; // VERY SAFE AND HACKER-PROOF!
$.ajax({
url: url,
success: function (data) {
2021-01-02 21:03:40 +01:00
// Hacky McHackFace has been working here. This'll probably break in the future
2020-11-17 18:23:40 +01:00
const startTrigger = "<div class=\"mw-parser-output\">";
const start = data.indexOf(startTrigger);
data = data.substr(start,
data.indexOf("<div class=\"printfooter\">") - start)
data = data.substr(0, data.lastIndexOf("</p>"))
data = data.substr(startTrigger.length + 3);
try {
2020-11-17 18:23:40 +01:00
const parsed = JSON.parse(data);
2020-11-11 16:23:49 +01:00
parsed["id"] = layoutFromBase64
2020-11-17 18:23:40 +01:00
const layout = new LayoutConfig(parsed);
InitUiElements.InitAll(layout, layoutFromBase64, testing, layoutFromBase64, btoa(data));
} catch (e) {
new FixedUiElement(`<a href="${cleanUrl}">${themeName}</a> is invalid:<br/>${e}`)
.SetClass("clickable")
.AttachTo("centermessage");
throw e;
2020-07-31 04:58:58 +02:00
}
},
2020-09-20 19:11:32 +02:00
}).fail(() => {
new FixedUiElement(`<a href="${cleanUrl}">${themeName}</a> is invalid:<br/>Could not download - wrong URL?`)
.SetClass("clickable")
.AttachTo("centermessage");
});
2020-09-15 00:25:25 +02:00
} else if (layoutFromBase64 !== "false") {
layoutToUse = InitUiElements.LoadLayoutFromHash(userLayoutParam);
2020-09-15 02:29:31 +02:00
InitUiElements.InitAll(layoutToUse, layoutFromBase64, testing, defaultLayout, location.hash.substr(1));
} else {
InitUiElements.InitAll(layoutToUse, layoutFromBase64, testing, defaultLayout);
2020-07-29 15:05:19 +02:00
}
window.addEventListener('contextmenu', function (e) { // Not compatible with IE < 9
e.preventDefault();
}, false);