mapcomplete/index.ts

261 lines
8.9 KiB
TypeScript
Raw Normal View History

import {TagRendering} from "./Customizations/TagRendering";
2020-06-23 22:35:19 +00:00
import {UserBadge} from "./UI/UserBadge";
import {CenterMessageBox} from "./UI/CenterMessageBox";
import {TagUtils} from "./Logic/Tags";
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-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";
import {QueryParameters} from "./Logic/Web/QueryParameters";
2020-08-22 23:49:19 +00:00
import {LocalStorageSource} from "./Logic/Web/LocalStorageSource";
import {PersonalLayout} from "./Logic/PersonalLayout";
import {FromJSON} from "./Customizations/JSON/FromJSON";
import {FullScreenMessageBox} from "./UI/FullScreenMessageBoxHandler";
2020-06-23 22:35:19 +00:00
TagRendering.injectFunction();
2020-07-11 09:50:03 +00:00
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-09-04 23:40:43 +00:00
let testing = QueryParameters.GetQueryParameter("test", "false");
if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
// 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
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"
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 layout", 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
let layoutToUse: Layout = AllKnownLayouts.allSets[defaultLayout.toLowerCase()] ?? AllKnownLayouts["all"];
const userLayoutParam = QueryParameters.GetQueryParameter("userlayout", "false");
const layoutFromBase64 = userLayoutParam.data;
if (layoutFromBase64 !== "false") {
try {
// layoutFromBase64 contains the name of the theme. This is partly to do tracking with goat counter
const dedicatedHashFromLocalStorage = LocalStorageSource.Get("user-layout-" + layoutFromBase64.replace(" ", "_"));
if(dedicatedHashFromLocalStorage.data?.length < 10){
dedicatedHashFromLocalStorage.setData(undefined);
}
2020-08-22 23:49:19 +00:00
const hashFromLocalStorage = LocalStorageSource.Get("last-loaded-user-layout");
if (hash.length < 10) {
hash = dedicatedHashFromLocalStorage.data ?? hashFromLocalStorage.data;
} else {
2020-08-22 23:49:19 +00:00
console.log("Saving hash to local storage")
hashFromLocalStorage.setData(hash);
dedicatedHashFromLocalStorage.setData(hash);
2020-08-22 23:49:19 +00:00
}
layoutToUse = FromJSON.FromBase64(hash.substr(1));
userLayoutParam.setData(layoutToUse.id);
} catch (e) {
new FixedUiElement("Error: could not parse the custom layout:<br/> "+e).AttachTo("centermessage");
throw e;
}
}
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
console.log("Using layout: ", layoutToUse.id);
2020-06-23 22:35:19 +00:00
2020-07-31 02:58:58 +00:00
State.state = new State(layoutToUse);
if (layoutToUse.hideFromOverview) {
State.state.osmConnection.GetPreference("hidden-theme-" + layoutToUse.id + "-enabled").setData("true");
}
if (layoutFromBase64 !== "false") {
2020-08-08 19:17:17 +00:00
State.state.layoutDefinition = hash.substr(1);
if (!testing) {
State.state.osmConnection.OnLoggedIn(() => {
State.state.osmConnection.GetLongPreference("installed-theme-" + layoutToUse.id).setData(State.state.layoutDefinition);
})
}else{
console.warn("NOT saving custom layout to OSM as we are tesing -> probably in an iFrame")
}
2020-08-08 19:17:17 +00:00
}
2020-07-31 15:38:03 +00:00
InitUiElements.InitBaseMap();
2020-06-23 22:35:19 +00:00
new FixedUiElement("").AttachTo("decoration-desktop"); // Remove the decoration
2020-07-31 16:48:37 +00:00
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, () => {
let presetCount = 0;
for (const layer of State.state.filteredLayers.data) {
for (const preset of layer.layerDef.presets) {
presetCount++;
}
}
if (presetCount == 0) {
return;
}
2020-07-31 02:58:58 +00:00
new StrayClickHandler(() => {
2020-07-31 14:17:16 +00:00
return new SimpleAddUI();
2020-07-31 02:58:58 +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
function updateFavs() {
const favs = State.state.favouriteLayers.data ?? [];
layoutToUse.layers = [];
for (const fav of favs) {
const layer = AllKnownLayouts.allLayers[fav];
if (!!layer) {
layoutToUse.layers.push(layer);
}
for (const layouts of State.state.installedThemes.data) {
for (const layer of layouts.layout.layers) {
if (typeof layer === "string") {
continue;
}
if (layer.id === fav) {
layoutToUse.layers.push(layer);
}
2020-07-31 02:58:58 +00:00
}
}
}
setupAllLayerElements();
State.state.layerUpdater.ForceRefresh();
State.state.locationControl.ping();
}
if (layoutToUse === AllKnownLayouts.allSets[PersonalLayout.NAME]) {
State.state.favouriteLayers.addCallback(updateFavs);
State.state.installedThemes.addCallback(updateFavs);
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) {
if (typeof layer === "string") {
continue;
}
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 FullScreenMessageBox(() => {
State.state.selectedElement.setData(undefined)
}).AttachTo("messagesboxmobile");
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");
State.state.locationControl.ping();