mapcomplete/index.ts

193 lines
6.6 KiB
TypeScript
Raw Normal View History

2020-06-24 00:35:19 +02:00
import {UserBadge} from "./UI/UserBadge";
import {CenterMessageBox} from "./UI/CenterMessageBox";
2020-07-30 00:59:08 +02:00
import {TagUtils} from "./Logic/TagsFilter";
import {FullScreenMessageBoxHandler} from "./UI/FullScreenMessageBoxHandler";
import {FeatureInfoBox} from "./UI/FeatureInfoBox";
2020-06-29 03:12:44 +02:00
import {SimpleAddUI} from "./UI/SimpleAddUI";
2020-07-01 02:12:33 +02:00
import {SearchAndGo} from "./UI/SearchAndGo";
2020-07-05 18:59:47 +02:00
import {AllKnownLayouts} from "./Customizations/AllKnownLayouts";
2020-07-30 00:59:08 +02:00
import {Layout} from "./Customizations/Layout";
2020-07-21 00:07:04 +02:00
import {FixedUiElement} from "./UI/Base/FixedUiElement";
2020-07-22 23:47:04 +02:00
import {QueryParameters} from "./Logic/QueryParameters";
2020-07-29 15:05:19 +02:00
import {InitUiElements} from "./InitUiElements";
2020-07-30 00:59:08 +02:00
import {StrayClickHandler} from "./Logic/Leaflet/StrayClickHandler";
import {GeoLocationHandler} from "./Logic/Leaflet/GeoLocationHandler";
import {State} from "./State";
2020-07-31 16:17:16 +02:00
import {CustomLayout} from "./Logic/CustomLayers";
2020-07-31 17:47:43 +02:00
import {TagRenderingOptions} from "./Customizations/TagRenderingOptions";
import {TagRendering} from "./Customizations/TagRendering";
2020-07-31 18:48:37 +02:00
import {Img} from "./UI/Img";
import Combine from "./UI/Base/Combine";
import {CustomLayoutFromJSON} from "./Customizations/JSON/CustomLayoutFromJSON";
2020-06-24 00:35:19 +02:00
2020-07-11 11:50:03 +02:00
2020-07-24 01:12:57 +02:00
// --------------------- Special actions based on the parameters -----------------
2020-07-11 11:50:03 +02:00
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
}
if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
// Set to true if testing and changes should NOT be saved
2020-07-29 15:05:19 +02:00
const testing = QueryParameters.GetQueryParameter("test", "true");
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
2020-07-17 18:57:07 +02:00
//Overpass.testUrl = "http://127.0.0.1:8080/streetwidths.geojson";
}
2020-06-24 00:35:19 +02:00
// ----------------- SELECT THE RIGHT QUESTSET -----------------
let defaultLayout = "bookcases"
let hash = window.location.hash;
const path = window.location.pathname.split("/").slice(-1)[0];
if (path !== "index.html") {
defaultLayout = path.substr(0, path.length - 5);
console.log("Using", defaultLayout)
}
2020-07-15 13:15:36 +02:00
// Run over all questsets. If a part of the URL matches a searched-for part in the layout, it'll take that as the default
for (const k in AllKnownLayouts.allSets) {
const layout = AllKnownLayouts.allSets[k];
const possibleParts = layout.locationContains ?? [];
for (const locationMatch of possibleParts) {
2020-07-15 13:15:36 +02:00
if (locationMatch === "") {
continue
}
2020-07-15 13:15:36 +02:00
if (window.location.href.toLowerCase().indexOf(locationMatch.toLowerCase()) >= 0) {
defaultLayout = layout.name;
}
}
}
2020-07-29 15:05:19 +02:00
defaultLayout = QueryParameters.GetQueryParameter("layout", defaultLayout).data;
2020-07-05 18:59:47 +02:00
let layoutToUse: Layout = AllKnownLayouts.allSets[defaultLayout] ?? AllKnownLayouts["all"];
const layoutFromBase64 = QueryParameters.GetQueryParameter("userlayout", "false").data;
2020-08-08 21:17:17 +02:00
if(layoutFromBase64 !== "false"){
layoutToUse = CustomLayoutFromJSON.FromQueryParam(hash.substr(1));
}
if (layoutToUse === undefined) {
console.log("Incorrect layout")
2020-07-31 18:07:55 +02:00
new FixedUiElement("Error: incorrect layout <i>" + defaultLayout + "</i><br/><a href='https://pietervdvn.github.io/MapComplete/index.html'>Go back</a>").AttachTo("centermessage").onClick(() => {
2020-07-31 04:58:58 +02:00
});
throw "Incorrect layout"
}
2020-07-15 13:15:36 +02:00
2020-07-31 04:58:58 +02:00
console.log("Using layout: ", layoutToUse.name);
2020-06-24 00:35:19 +02:00
TagRendering.injectFunction();
2020-07-31 04:58:58 +02:00
State.state = new State(layoutToUse);
2020-08-08 21:17:17 +02:00
if(layoutFromBase64 !== "false"){
State.state.layoutDefinition = hash.substr(1);
}
2020-07-31 17:38:03 +02:00
InitUiElements.InitBaseMap();
2020-06-24 00:35:19 +02:00
2020-07-31 18:48:37 +02:00
new FixedUiElement("").AttachTo("decoration"); // Remove the decoration
2020-07-31 04:58:58 +02:00
function setupAllLayerElements() {
2020-07-29 15:05:19 +02:00
2020-07-31 18:48:37 +02:00
// ------------- Setup the layers -------------------------------
2020-07-29 19:43:15 +02:00
2020-07-31 16:17:16 +02:00
InitUiElements.InitLayers();
InitUiElements.InitLayerSelection();
2020-06-24 00:35:19 +02:00
2020-07-22 17:54:59 +02:00
2020-07-31 18:48:37 +02:00
// ------------------ Setup various other UI elements ------------
2020-07-22 17:54:59 +02:00
2020-07-31 04:58:58 +02:00
InitUiElements.OnlyIf(State.state.featureSwitchAddNew, () => {
new StrayClickHandler(() => {
2020-07-31 16:17:16 +02:00
return new SimpleAddUI();
2020-07-31 04:58:58 +02:00
}
);
});
2020-07-31 16:17:16 +02:00
new CenterMessageBox() .AttachTo("centermessage");
2020-07-24 01:12:57 +02:00
2020-07-31 04:58:58 +02:00
}
2020-07-21 00:07:04 +02:00
2020-07-31 04:58:58 +02:00
setupAllLayerElements();
2020-06-29 03:12:44 +02:00
2020-07-31 16:17:16 +02:00
if (layoutToUse === AllKnownLayouts.allSets[CustomLayout.NAME]) {
2020-07-31 04:58:58 +02:00
State.state.favourteLayers.addCallback((favs) => {
2020-07-31 16:17:16 +02:00
layoutToUse.layers = [];
2020-07-31 04:58:58 +02:00
for (const fav of favs) {
const layer = AllKnownLayouts.allLayers[fav];
if (!!layer) {
layoutToUse.layers.push(layer);
}
setupAllLayerElements();
2020-07-31 16:17:16 +02:00
};
State.state.locationControl.ping();
2020-07-31 04:58:58 +02:00
})
}
2020-07-29 15:05:19 +02:00
/**
2020-07-24 01:12:57 +02:00
* Show the questions and information for the selected element
* This is given to the div which renders fullscreen on mobile devices
*/
State.state.selectedElement.addCallback((feature) => {
2020-07-29 19:43:15 +02:00
if (feature?.feature?.properties === undefined) {
return;
}
const data = feature.feature.properties;
2020-06-29 03:12:44 +02:00
// Which is the applicable set?
2020-07-15 13:15:36 +02:00
for (const layer of layoutToUse.layers) {
2020-06-29 03:12:44 +02:00
const applicable = layer.overpassFilter.matches(TagUtils.proprtiesToKV(data));
if (applicable) {
// This layer is the layer that gives the questions
2020-07-21 00:07:04 +02:00
const featureBox = new FeatureInfoBox(
feature.feature,
State.state.allElements.getElement(data.id),
2020-07-21 00:07:04 +02:00
layer.title,
layer.elementsToShow,
);
State.state.fullScreenMessage.setData(featureBox);
2020-06-29 03:12:44 +02:00
break;
}
2020-06-29 03:12:44 +02:00
}
}
);
2020-06-24 00:35:19 +02:00
InitUiElements.OnlyIf(State.state.featureSwitchUserbadge, () => {
new UserBadge().AttachTo('userbadge');
2020-07-25 18:00:08 +02:00
});
2020-07-24 01:12:57 +02:00
InitUiElements.OnlyIf((State.state.featureSwitchSearch), () => {
new SearchAndGo().AttachTo("searchbox");
2020-07-29 15:05:19 +02:00
});
2020-06-24 00:35:19 +02:00
new FullScreenMessageBoxHandler(() => {
State.state.selectedElement.setData(undefined)
}).update();
InitUiElements.OnlyIf(State.state.featureSwitchWelcomeMessage, () => {
InitUiElements.InitWelcomeMessage()
2020-07-29 15:05:19 +02:00
});
if ((window != window.top && !State.state.featureSwitchWelcomeMessage.data) || State.state.featureSwitchIframe.data) {
const currentLocation = State.state.locationControl;
const url = `${window.location.origin}${window.location.pathname}?z=${currentLocation.data.zoom}&lat=${currentLocation.data.lat}&lon=${currentLocation.data.lon}`;
2020-08-08 15:21:16 +02:00
const content = `<a href='${url}' target='_blank'><span class='iframe-escape'><img src='assets/pop-out.svg'></span></a>`;
new FixedUiElement(content).AttachTo("messagesbox");
new FixedUiElement(content).AttachTo("help-button-mobile")
2020-07-29 15:05:19 +02:00
}
2020-06-24 00:35:19 +02:00
new GeoLocationHandler().AttachTo("geolocate-button");