mapcomplete/index.ts

261 lines
9.3 KiB
TypeScript
Raw Normal View History

2020-06-23 22:35:19 +00:00
import {ElementStorage} from "./Logic/ElementStorage";
import {UIEventSource} from "./UI/UIEventSource";
import {UserBadge} from "./UI/UserBadge";
import {PendingChanges} from "./UI/PendingChanges";
import {CenterMessageBox} from "./UI/CenterMessageBox";
import {Helpers} from "./Helpers";
2020-07-29 22:59:08 +00:00
import {TagUtils} from "./Logic/TagsFilter";
2020-06-23 22:35:19 +00:00
import {LayerUpdater} from "./Logic/LayerUpdater";
import {UIElement} from "./UI/UIElement";
import {FullScreenMessageBoxHandler} from "./UI/FullScreenMessageBoxHandler";
import {FeatureInfoBox} from "./UI/FeatureInfoBox";
2020-06-29 01:12:44 +00:00
import {SimpleAddUI} from "./UI/SimpleAddUI";
import {VariableUiElement} from "./UI/Base/VariableUIElement";
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-23 23:12:57 +00:00
import {CheckBox} from "./UI/Input/CheckBox";
2020-07-20 10:39:43 +00:00
import Translations from "./UI/i18n/Translations";
import Locale from "./UI/i18n/Locale";
2020-07-29 22:59:08 +00:00
import {Layout} from "./Customizations/Layout";
2020-07-20 22:07:04 +00:00
import {DropDown} from "./UI/Input/DropDown";
import {FixedUiElement} from "./UI/Base/FixedUiElement";
import {LayerSelection} from "./UI/LayerSelection";
2020-07-22 12:57:35 +00:00
import Combine from "./UI/Base/Combine";
2020-07-22 15:54:59 +00:00
import {Img} from "./UI/Img";
2020-07-22 21:47:04 +00:00
import {QueryParameters} from "./Logic/QueryParameters";
2020-07-23 23:12:57 +00:00
import {Utils} from "./Utils";
import {LocalStorageSource} from "./Logic/LocalStorageSource";
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 {BaseLayers, Basemap} from "./Logic/Leaflet/Basemap";
import {GeoLocationHandler} from "./Logic/Leaflet/GeoLocationHandler";
import {OsmConnection} from "./Logic/Osm/OsmConnection";
import {Changes} from "./Logic/Osm/Changes";
import {State} from "./State";
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 -----------------
2020-07-29 13:05:19 +00:00
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"];
2020-07-15 11:15:36 +00:00
console.log("Using layout: ", layoutToUse.name);
if (layoutToUse === undefined) {
console.log("Incorrect layout")
}
2020-07-15 11:15:36 +00:00
// Setup the global state
State.state = new State(layoutToUse);
const state = State.state;
2020-07-22 21:47:04 +00:00
2020-06-23 22:35:19 +00:00
// ----------------- Prepare the important objects -----------------
state.osmConnection = new OsmConnection(
QueryParameters.GetQueryParameter("test", "false").data === "true",
QueryParameters.GetQueryParameter("oauth_token", undefined)
2020-07-23 23:12:57 +00:00
);
2020-07-20 22:07:04 +00:00
Locale.language.syncWith(state.osmConnection.GetPreference("language"));
2020-07-20 22:07:04 +00:00
// @ts-ignore
2020-07-20 22:07:04 +00:00
window.setLanguage = function (language: string) {
Locale.language.setData(language)
}
Locale.language.addCallback((currentLanguage) => {
if (layoutToUse.supportedLanguages.indexOf(currentLanguage) < 0) {
2020-07-29 22:59:08 +00:00
console.log("Resetting languate to", layoutToUse.supportedLanguages[0], "as", currentLanguage, " is unsupported")
// The current language is not supported -> switch to a supported one
Locale.language.setData(layoutToUse.supportedLanguages[0]);
}
}).ping()
2020-07-20 22:07:04 +00:00
state.allElements = new ElementStorage();
state.changes = new Changes(
"Beantwoorden van vragen met #MapComplete voor vragenset #" + state.layoutToUse.data.name,
state.osmConnection, state.allElements);
state.bm = new Basemap("leafletDiv", state.locationControl, new VariableUiElement(
state.locationControl.map((location) => {
2020-06-29 01:12:44 +00:00
const mapComplete = "<a href='https://github.com/pietervdvn/MapComplete' target='_blank'>Mapcomple</a> " +
" " +
"<a href='https://github.com/pietervdvn/MapComplete/issues' target='_blank'><img src='./assets/bug.svg' alt='Report bug' class='small-userbadge-icon'></a>";
let editHere = "";
if (location !== undefined) {
editHere = " | " +
"<a href='https://www.openstreetmap.org/edit?editor=id#map=" + location.zoom + "/" + location.lat + "/" + location.lon + "' target='_blank'>" +
"<img src='./assets/pencil.svg' alt='edit here' class='small-userbadge-icon'>" +
"</a>"
}
return mapComplete + editHere;
})
));
2020-06-23 22:35:19 +00:00
2020-07-29 13:05:19 +00:00
2020-06-23 22:35:19 +00:00
// ------------- Setup the layers -------------------------------
2020-07-29 17:43:15 +00:00
const layerSetup = InitUiElements.InitLayers();
2020-06-23 22:35:19 +00:00
const layerUpdater = new LayerUpdater(layerSetup.minZoom, layoutToUse.widenFactor, layerSetup.flayers);
2020-06-23 22:35:19 +00:00
2020-07-23 23:12:57 +00:00
// --------------- Setting up layer selection ui --------
2020-07-22 15:54:59 +00:00
2020-07-25 16:00:08 +00:00
const closedFilterButton = `<button id="filter__button" class="filter__button shadow">${Img.closedFilterButton}</button>`;
2020-07-22 15:54:59 +00:00
const openFilterButton = `
<button id="filter__button" class="filter__button">${Img.openFilterButton}</button>`;
let baseLayerOptions = BaseLayers.baseLayers.map((layer) => {
return {value: layer, shown: layer.name}
});
const backgroundMapPicker = new Combine([new DropDown(`Background map`, baseLayerOptions, State.state.bm.CurrentLayer), openFilterButton]);
2020-07-29 22:59:08 +00:00
const layerSelection = new Combine([`<p class="filter__label">Maplayers</p>`, new LayerSelection(layerSetup.flayers)]);
2020-07-23 14:28:19 +00:00
let layerControl = backgroundMapPicker;
2020-07-29 22:59:08 +00:00
if (layerSetup.flayers.length > 1) {
2020-07-23 23:12:57 +00:00
layerControl = new Combine([layerSelection, backgroundMapPicker]);
2020-07-22 15:54:59 +00:00
}
InitUiElements.OnlyIf(State.state.featureSwitchLayers, () => {
const checkbox = new CheckBox(layerControl, closedFilterButton);
checkbox.AttachTo("filter__selection");
State.state.bm.Location.addCallback(() => {
checkbox.isEnabled.setData(false);
});
2020-07-29 13:05:19 +00:00
});
2020-06-23 22:35:19 +00:00
2020-07-23 14:00:49 +00:00
2020-07-23 23:12:57 +00:00
// ------------------ Setup various other UI elements ------------
2020-07-27 09:45:26 +00:00
document.title = Translations.W(layoutToUse.title).InnerRender();
2020-07-23 23:12:57 +00:00
Locale.language.addCallback(e => {
2020-07-27 09:45:26 +00:00
document.title = Translations.W(layoutToUse.title).InnerRender();
2020-07-23 23:12:57 +00:00
})
2020-07-20 22:07:04 +00:00
2020-06-29 01:12:44 +00:00
InitUiElements.OnlyIf(State.state.featureSwitchAddNew, () => {
new StrayClickHandler(() => {
return new SimpleAddUI(
2020-07-29 13:05:19 +00:00
layerUpdater.runningQuery,
2020-07-29 22:59:08 +00:00
layerSetup.presets);
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
console.log("Enable new:",State.state.featureSwitchAddNew.data,"deafult", layoutToUse.enableAdd)
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) || State.state.featureSwitchIframe.data) {
new FixedUiElement(`<a href='${window.location}' target='_blank'><span class='iframe-escape'><img src='assets/pop-out.svg'></span></a>`).AttachTo("top-right")
2020-07-29 13:05:19 +00:00
}
2020-06-23 22:35:19 +00:00
new CenterMessageBox(
2020-07-29 22:59:08 +00:00
layerSetup.minZoom,
2020-06-23 22:35:19 +00:00
layerUpdater.runningQuery)
.AttachTo("centermessage");
Helpers.SetupAutoSave();
Helpers.LastEffortSave();
2020-06-23 22:35:19 +00:00
new GeoLocationHandler().AttachTo("geolocate-button");
State.state.osmConnection.registerActivateOsmAUthenticationClass();
State.state.locationControl.ping()
2020-07-22 13:44:47 +00:00