mapcomplete/index.ts

180 lines
6.3 KiB
TypeScript
Raw Normal View History

2020-06-23 22:35:19 +00:00
import {UserBadge} from "./UI/UserBadge";
import {CenterMessageBox} from "./UI/CenterMessageBox";
2020-07-29 22:59:08 +00:00
import {TagUtils} from "./Logic/TagsFilter";
import {FullScreenMessageBoxHandler} from "./UI/FullScreenMessageBoxHandler";
import {FeatureInfoBox} from "./UI/FeatureInfoBox";
2020-06-29 01:12:44 +00:00
import {SimpleAddUI} from "./UI/SimpleAddUI";
2020-07-01 00:12:33 +00:00
import {SearchAndGo} from "./UI/SearchAndGo";
2020-07-05 16:59:47 +00:00
import {AllKnownLayouts} from "./Customizations/AllKnownLayouts";
2020-07-29 22:59:08 +00:00
import {Layout} from "./Customizations/Layout";
2020-07-20 22:07:04 +00:00
import {FixedUiElement} from "./UI/Base/FixedUiElement";
2020-07-22 21:47:04 +00:00
import {QueryParameters} from "./Logic/QueryParameters";
2020-07-29 13:05:19 +00:00
import {InitUiElements} from "./InitUiElements";
2020-07-29 22:59:08 +00:00
import {StrayClickHandler} from "./Logic/Leaflet/StrayClickHandler";
import {GeoLocationHandler} from "./Logic/Leaflet/GeoLocationHandler";
import {State} from "./State";
2020-07-31 14:17:16 +00:00
import {CustomLayout} from "./Logic/CustomLayers";
2020-07-31 15:47:43 +00:00
import {TagRenderingOptions} from "./Customizations/TagRenderingOptions";
import {TagRendering} from "./Customizations/TagRendering";
2020-07-31 16:48:37 +00:00
import {Img} from "./UI/Img";
import Combine from "./UI/Base/Combine";
2020-06-23 22:35:19 +00:00
2020-07-11 09:50:03 +00:00
2020-07-23 23:12:57 +00:00
// --------------------- Special actions based on the parameters -----------------
2020-07-11 09:50:03 +00:00
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
}
if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
// Set to true if testing and changes should NOT be saved
2020-07-29 13:05:19 +00:00
const testing = QueryParameters.GetQueryParameter("test", "true");
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
2020-07-17 16:57:07 +00:00
//Overpass.testUrl = "http://127.0.0.1:8080/streetwidths.geojson";
}
2020-06-23 22:35:19 +00:00
// ----------------- SELECT THE RIGHT QUESTSET -----------------
let defaultLayout = "bookcases"
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 11:15:36 +00: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 11:15:36 +00:00
if (locationMatch === "") {
continue
}
2020-07-15 11:15:36 +00:00
if (window.location.href.toLowerCase().indexOf(locationMatch.toLowerCase()) >= 0) {
defaultLayout = layout.name;
}
}
}
2020-07-29 13:05:19 +00:00
defaultLayout = QueryParameters.GetQueryParameter("layout", defaultLayout).data;
2020-07-05 16:59:47 +00:00
2020-07-23 23:12:57 +00:00
const layoutToUse: Layout = AllKnownLayouts.allSets[defaultLayout] ?? AllKnownLayouts["all"];
if (layoutToUse === undefined) {
console.log("Incorrect layout")
2020-07-31 16:07:55 +00: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 02:58:58 +00:00
});
throw "Incorrect layout"
}
2020-07-15 11:15:36 +00:00
2020-07-31 02:58:58 +00:00
console.log("Using layout: ", layoutToUse.name);
2020-07-31 15:47:43 +00:00
TagRendering.injectFunction();
2020-06-23 22:35:19 +00:00
2020-07-31 02:58:58 +00:00
State.state = new State(layoutToUse);
2020-07-31 15:38:03 +00:00
InitUiElements.InitBaseMap();
2020-06-23 22:35:19 +00:00
2020-07-31 16:48:37 +00:00
new FixedUiElement("").AttachTo("decoration"); // Remove the decoration
2020-07-31 02:58:58 +00:00
function setupAllLayerElements() {
2020-07-29 13:05:19 +00:00
2020-07-31 16:48:37 +00:00
// ------------- Setup the layers -------------------------------
2020-07-29 17:43:15 +00:00
2020-07-31 14:17:16 +00:00
InitUiElements.InitLayers();
InitUiElements.InitLayerSelection();
2020-06-23 22:35:19 +00:00
2020-07-22 15:54:59 +00:00
2020-07-31 16:48:37 +00:00
// ------------------ Setup various other UI elements ------------
2020-07-22 15:54:59 +00:00
2020-07-31 02:58:58 +00:00
InitUiElements.OnlyIf(State.state.featureSwitchAddNew, () => {
new StrayClickHandler(() => {
2020-07-31 14:17:16 +00:00
return new SimpleAddUI();
2020-07-31 02:58:58 +00:00
}
);
});
2020-07-31 14:17:16 +00:00
new CenterMessageBox() .AttachTo("centermessage");
2020-07-23 23:12:57 +00:00
2020-07-31 02:58:58 +00:00
}
2020-07-20 22:07:04 +00:00
2020-07-31 02:58:58 +00:00
setupAllLayerElements();
2020-06-29 01:12:44 +00:00
2020-07-31 14:17:16 +00:00
if (layoutToUse === AllKnownLayouts.allSets[CustomLayout.NAME]) {
2020-07-31 02:58:58 +00:00
State.state.favourteLayers.addCallback((favs) => {
2020-07-31 14:17:16 +00:00
layoutToUse.layers = [];
2020-07-31 02:58:58 +00:00
for (const fav of favs) {
const layer = AllKnownLayouts.allLayers[fav];
if (!!layer) {
layoutToUse.layers.push(layer);
}
setupAllLayerElements();
2020-07-31 14:17:16 +00:00
};
State.state.locationControl.ping();
2020-07-31 02:58:58 +00:00
})
}
2020-07-29 13:05:19 +00:00
/**
2020-07-23 23:12:57 +00: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 17:43:15 +00:00
if (feature?.feature?.properties === undefined) {
return;
}
const data = feature.feature.properties;
2020-06-29 01:12:44 +00:00
// Which is the applicable set?
2020-07-15 11:15:36 +00:00
for (const layer of layoutToUse.layers) {
2020-06-29 01:12:44 +00:00
const applicable = layer.overpassFilter.matches(TagUtils.proprtiesToKV(data));
if (applicable) {
// This layer is the layer that gives the questions
2020-07-20 22:07:04 +00:00
const featureBox = new FeatureInfoBox(
feature.feature,
State.state.allElements.getElement(data.id),
2020-07-20 22:07:04 +00:00
layer.title,
layer.elementsToShow,
);
State.state.fullScreenMessage.setData(featureBox);
2020-06-29 01:12:44 +00:00
break;
}
2020-06-29 01:12:44 +00:00
}
}
);
2020-06-23 22:35:19 +00:00
InitUiElements.OnlyIf(State.state.featureSwitchUserbadge, () => {
new UserBadge().AttachTo('userbadge');
2020-07-25 16:00:08 +00:00
});
2020-07-23 23:12:57 +00:00
InitUiElements.OnlyIf((State.state.featureSwitchSearch), () => {
new SearchAndGo().AttachTo("searchbox");
2020-07-29 13:05:19 +00:00
});
2020-06-23 22:35:19 +00:00
new FullScreenMessageBoxHandler(() => {
State.state.selectedElement.setData(undefined)
}).update();
InitUiElements.OnlyIf(State.state.featureSwitchWelcomeMessage, () => {
InitUiElements.InitWelcomeMessage()
2020-07-29 13:05:19 +00: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 13:21:16 +00: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 13:05:19 +00:00
}
2020-06-23 22:35:19 +00:00
new GeoLocationHandler().AttachTo("geolocate-button");