Cleanup of code

This commit is contained in:
Pieter Vander Vennet 2020-07-24 01:12:57 +02:00
parent dd91e11bd0
commit e0f9a93468
12 changed files with 193 additions and 369 deletions

107
index.ts
View file

@ -20,7 +20,7 @@ import {VariableUiElement} from "./UI/Base/VariableUIElement";
import {SearchAndGo} from "./UI/SearchAndGo";
import {CollapseButton} from "./UI/Base/CollapseButton";
import {AllKnownLayouts} from "./Customizations/AllKnownLayouts";
import {CheckBox} from "./UI/Base/CheckBox";
import {CheckBox} from "./UI/Input/CheckBox";
import Translations from "./UI/i18n/Translations";
import Locale from "./UI/i18n/Locale";
import {Layout, WelcomeMessage} from "./Customizations/Layout";
@ -30,9 +30,10 @@ import {LayerSelection} from "./UI/LayerSelection";
import Combine from "./UI/Base/Combine";
import {Img} from "./UI/Img";
import {QueryParameters} from "./Logic/QueryParameters";
import {Utils} from "./Utils";
// --------------------- Read the URL parameters -----------------
// --------------------- Special actions based on the parameters -----------------
// @ts-ignore
if (location.href.startsWith("http://buurtnatuur.be")) {
@ -40,13 +41,11 @@ if (location.href.startsWith("http://buurtnatuur.be")) {
window.location.replace("https://buurtnatuur.be");
}
let dryRun = false;
if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
// Set to true if testing and changes should NOT be saved
dryRun = true;
const testing = QueryParameters.GetQueryParameter("test");
testing.setData((testing.data === undefined) + "")
// 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
//Overpass.testUrl = "http://127.0.0.1:8080/streetwidths.geojson";
@ -55,7 +54,6 @@ if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
// ----------------- SELECT THE RIGHT QUESTSET -----------------
let defaultLayout = "walkbybrussels"
@ -74,16 +72,10 @@ for (const k in AllKnownLayouts.allSets) {
}
defaultLayout = QueryParameters.GetQueryParameter("layout").data ?? defaultLayout;
dryRun = QueryParameters.GetQueryParameter("test").data === "true";
const layoutToUse: Layout = AllKnownLayouts.allSets[defaultLayout];
const layoutToUse: Layout = AllKnownLayouts.allSets[defaultLayout] ?? AllKnownLayouts["all"];
console.log("Using layout: ", layoutToUse.name);
document.title = layoutToUse.title.InnerRender();
Locale.language.addCallback(e => {
document.title = layoutToUse.title.InnerRender();
})
// ----------------- Setup a few event sources -------------
@ -102,31 +94,19 @@ const fullScreenMessage = new UIEventSource<UIElement>(undefined);
// The latest element that was selected - used to generate the right UI at the right place
const selectedElement = new UIEventSource<{ feature: any }>(undefined);
function clean(str) : number{
if (str) {
const i = parseFloat(str);
if (isNaN(i)) {
return undefined;
}
return i;
}
return undefined;
}
const zoom = QueryParameters.GetQueryParameter("z");
const lat = QueryParameters.GetQueryParameter("lat");
const lon = QueryParameters.GetQueryParameter("lon");
const locationControl = new UIEventSource<{ lat: number, lon: number, zoom: number }>({
zoom: clean(zoom.data) ?? layoutToUse.startzoom,
lat: clean(lat.data) ?? layoutToUse.startLat,
lon: clean(lon.data) ?? layoutToUse.startLon
zoom: Utils.asFloat(zoom.data) ?? layoutToUse.startzoom,
lat: Utils.asFloat(lat.data) ?? layoutToUse.startLat,
lon: Utils.asFloat(lon.data) ?? layoutToUse.startLon
});
locationControl.addCallback((latlonz) => {
zoom.setData(latlonz.zoom.toString());
lat.setData(latlonz.lat.toString().substr(0,6));
lon.setData(latlonz.lon.toString().substr(0,6));
})
@ -134,7 +114,9 @@ locationControl.addCallback((latlonz) => {
// ----------------- Prepare the important objects -----------------
const osmConnection = new OsmConnection(dryRun);
const osmConnection = new OsmConnection(
QueryParameters.GetQueryParameter("test").data === "true"
);
Locale.language.syncWith(osmConnection.GetPreference("language"));
@ -212,7 +194,7 @@ for (const layer of layoutToUse.layers) {
const layerUpdater = new LayerUpdater(bm, minZoom, flayers);
// --------------- Setting up filter ui --------
// --------------- Setting up layer selection ui --------
const closedFilterButton = `<button id="filter__button" class="filter__button filter__button--shadow">${Img.closedFilterButton}</button>`;
@ -229,28 +211,39 @@ const backgroundMapPicker = new Combine([new DropDown(`Background map`, baseLaye
const layerSelection = new Combine([`<p class="filter__label">Maplayers</p>`, new LayerSelection(flayers)]);
let layerControl = backgroundMapPicker;
if (flayers.length > 1) {
layerControl = new Combine([layerSelection, backgroundMapPicker);
layerControl = new Combine([layerSelection, backgroundMapPicker]);
}
new CheckBox(layerControl, closedFilterButton).AttachTo("filter__selection");
// ------------------ Setup various UI elements ------------
// ------------------ Setup various other UI elements ------------
document.title = layoutToUse.title.InnerRender();
Locale.language.addCallback(e => {
document.title = layoutToUse.title.InnerRender();
})
let languagePicker = new DropDown("", layoutToUse.supportedLanguages.map(lang => {
return {value: lang, shown: lang}
}
), Locale.language);
new StrayClickHandler(bm, selectedElement, fullScreenMessage, () => {
return new SimpleAddUI(bm.Location,
bm.LastClickLocation,
changes,
selectedElement,
layerUpdater.runningQuery,
osmConnection.userDetails,
addButtons);
return new SimpleAddUI(bm.Location,
bm.LastClickLocation,
changes,
selectedElement,
layerUpdater.runningQuery,
osmConnection.userDetails,
addButtons);
}
);
/**
* Show the questions and information for the selected element on the fullScreen
* Show the questions and information for the selected element
* This is given to the div which renders fullscreen on mobile devices
*/
selectedElement.addCallback((feature) => {
const data = feature.feature.properties;
@ -278,13 +271,7 @@ selectedElement.addCallback((feature) => {
);
const pendingChanges = new PendingChanges(
changes, secondsTillChangesAreSaved,);
let languagePicker = new DropDown("", layoutToUse.supportedLanguages.map(lang => {
return {value: lang, shown: lang}
}
), Locale.language);
const pendingChanges = new PendingChanges(changes, secondsTillChangesAreSaved,);
new UserBadge(osmConnection.userDetails,
pendingChanges,
@ -294,12 +281,20 @@ new UserBadge(osmConnection.userDetails,
new SearchAndGo(bm).AttachTo("searchbox");
/*
new CollapseButton("messagesbox")
.AttachTo("collapseButton");
new WelcomeMessage(layoutToUse, osmConnection).AttachTo("messagesbox");
fullScreenMessage.setData(
new WelcomeMessage(layoutToUse, osmConnection)
);
.AttachTo("collapseButton");*/
const welcome = new WelcomeMessage(layoutToUse, osmConnection).onClick(() => {
});
const help = new FixedUiElement(`<div class='collapse-button-img'><img src='assets/help.svg' alt='collapse'></div>`);
new CheckBox(
new Combine([
new Combine(["<span class='collapse-button'>", help, "</span>"]),
welcome]),
new Combine(["<span class='open-button'>", help, "</span>"])
, true
).AttachTo("messagesbox")
new FullScreenMessageBoxHandler(fullScreenMessage, () => {
@ -324,9 +319,5 @@ osmConnection.registerActivateOsmAUthenticationClass();
new GeoLocationHandler(bm).AttachTo("geolocate-button");
// --------------- Send a ping to start various action --------
locationControl.ping();