mapcomplete/index.ts

130 lines
5.6 KiB
TypeScript
Raw Normal View History

2020-07-05 16:59:47 +00:00
import {AllKnownLayouts} from "./Customizations/AllKnownLayouts";
2020-07-20 22:07:04 +00:00
import {FixedUiElement} from "./UI/Base/FixedUiElement";
2020-07-29 13:05:19 +00:00
import {InitUiElements} from "./InitUiElements";
import {QueryParameters} from "./Logic/Web/QueryParameters";
import {UIEventSource} from "./Logic/UIEventSource";
import * as $ from "jquery";
2020-11-11 15:23:49 +00:00
import LayoutConfig from "./Customizations/JSON/LayoutConfig";
2020-11-14 02:26:09 +00:00
import {Utils} from "./Utils";
import MoreScreen from "./UI/BigComponents/MoreScreen";
import State from "./State";
import Combine from "./UI/Base/Combine";
import Translations from "./UI/i18n/Translations";
let defaultLayout = ""
2020-07-23 23:12:57 +00:00
// --------------------- Special actions based on the parameters -----------------
2020-07-12 21:19:05 +00:00
// @ts-ignore
2020-07-15 12:03:44 +00:00
if (location.href.startsWith("http://buurtnatuur.be")) {
2020-07-11 09:50:03 +00:00
// Reload the https version. This is important for the 'locate me' button
2020-07-15 12:03:44 +00:00
window.location.replace("https://buurtnatuur.be");
2020-07-11 09:50:03 +00:00
}
2020-11-06 00:58:26 +00:00
2020-09-18 20:23:49 +00:00
if (location.href.indexOf("buurtnatuur.be") >= 0) {
defaultLayout = "buurtnatuur"
}
2020-11-14 02:26:09 +00: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 !== "") {
2020-11-14 02:26:09 +00:00
Utils.LoadCustomCss(customCssQP.data);
}
2020-09-18 20:23:49 +00:00
2020-09-17 11:13:02 +00: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 11:46:03 +00: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 20:03:40 +00:00
// Overpass.testUrl = "http://127.0.0.1:8080/streetwidths.geojson";
} else {
testing = QueryParameters.GetQueryParameter("test", "false");
}
2020-06-23 22:35:19 +00:00
// ----------------- SELECT THE RIGHT QUESTSET -----------------
2020-09-18 20:23:49 +00: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;
let layoutToUse: LayoutConfig = AllKnownLayouts.allSets[defaultLayout.toLowerCase()];
const userLayoutParam = QueryParameters.GetQueryParameter("userlayout", "false");
const layoutFromBase64 = decodeURIComponent(userLayoutParam.data);
document.getElementById('centermessage').innerText = '';
document.getElementById("decoration-desktop").remove();
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-14 22:25:25 +00:00
const cleanUrl = `https://wiki.openstreetmap.org/wiki/${themeName}`;
2021-01-04 18:08:22 +00:00
const url = `https://cors-anywhere.herokuapp.com/` + cleanUrl; // ~NOT~ VERY SAFE AND HACKER-PROOF!
// We use cors-anywhere because the wiki from openstreetmap is locked-down :(
2021-01-04 18:08:22 +00:00
/*/
const url = cleanUrl; // MUCH SAFER! //*/
$.ajax({
url: url,
success: function (data) {
2021-01-02 20:03:40 +00:00
// Hacky McHackFace has been working here. This'll probably break in the future
2020-11-17 17:23:40 +00:00
const startTrigger = "<div class=\"mw-parser-output\">";
const start = data.indexOf(startTrigger);
data = data.substr(start,
2020-11-17 17:23:40 +00:00
data.indexOf("<div class=\"printfooter\">") - start)
data = data.substr(0, data.lastIndexOf("</p>"))
data = data.substr(data.indexOf("<p>") + 3)
2021-01-08 23:29:12 +00:00
console.log(data)
try {
2020-11-17 17:23:40 +00:00
const parsed = JSON.parse(data);
// Overwrite the id to the wiki:value
2021-01-17 20:02:56 +00:00
parsed.id = layoutFromBase64.replace(/[: \/]/g, '-')
2020-11-17 17:23:40 +00: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");
2021-01-08 23:29:12 +00:00
console.error("Could not parse the text", data)
throw e;
2020-07-31 02:58:58 +00:00
}
},
}).fail((_, textstatus, error) => {
console.error("Could not download the wiki theme:", textstatus, error)
new FixedUiElement(`<a href="${cleanUrl}">${themeName}</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-14 22:25:25 +00:00
} else if (layoutFromBase64 !== "false") {
layoutToUse = InitUiElements.LoadLayoutFromHash(userLayoutParam);
2020-09-15 00:29:31 +00:00
InitUiElements.InitAll(layoutToUse, layoutFromBase64, testing, defaultLayout, location.hash.substr(1));
} 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 a few buttons
State.state = new State(undefined);
2021-01-27 02:08:46 +00:00
new Combine([new MoreScreen(true),
Translations.t.general.aboutMapcomplete
2021-01-25 02:12:09 +00:00
]).SetClass("block m-5 lg:w-3/4 lg:ml-40")
2021-01-27 02:08:46 +00:00
.SetStyle("pointer-events: all;")
.AttachTo("topleft-tools");
2020-07-29 13:05:19 +00:00
}
window.addEventListener('contextmenu', function (e) { // Not compatible with IE < 9
e.preventDefault();
}, false);