2020-07-31 01:45:54 +02:00
|
|
|
import {UIElement} from "./UI/UIElement";
|
|
|
|
import {Layout} from "./Customizations/Layout";
|
|
|
|
import {Utils} from "./Utils";
|
2020-08-30 01:13:18 +02:00
|
|
|
import {Preset} from "./Customizations/LayerDefinition";
|
2020-07-31 01:45:54 +02:00
|
|
|
import {ElementStorage} from "./Logic/ElementStorage";
|
|
|
|
import {Changes} from "./Logic/Osm/Changes";
|
|
|
|
import {OsmConnection} from "./Logic/Osm/OsmConnection";
|
2020-07-31 04:58:58 +02:00
|
|
|
import Locale from "./UI/i18n/Locale";
|
|
|
|
import Translations from "./UI/i18n/Translations";
|
2020-07-31 16:17:16 +02:00
|
|
|
import {FilteredLayer} from "./Logic/FilteredLayer";
|
|
|
|
import {LayerUpdater} from "./Logic/LayerUpdater";
|
2020-08-17 17:23:15 +02:00
|
|
|
import {UIEventSource} from "./Logic/UIEventSource";
|
|
|
|
import {LocalStorageSource} from "./Logic/Web/LocalStorageSource";
|
|
|
|
import {QueryParameters} from "./Logic/Web/QueryParameters";
|
2020-07-31 01:45:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains the global state: a bunch of UI-event sources
|
|
|
|
*/
|
|
|
|
|
|
|
|
export class State {
|
|
|
|
|
|
|
|
// The singleton of the global state
|
|
|
|
public static state: State;
|
2020-08-06 23:49:35 +02:00
|
|
|
|
2020-09-25 23:11:01 +02:00
|
|
|
public static vNumber = "0.0.8f";
|
2020-08-22 18:57:27 +02:00
|
|
|
|
|
|
|
// The user journey states thresholds when a new feature gets unlocked
|
|
|
|
public static userJourney = {
|
2020-09-17 13:13:02 +02:00
|
|
|
addNewPointsUnlock: 1,
|
2020-09-05 01:40:43 +02:00
|
|
|
moreScreenUnlock: 5,
|
2020-09-03 16:44:48 +02:00
|
|
|
personalLayoutUnlock: 20,
|
|
|
|
tagsVisibleAt: 100,
|
2020-09-05 01:40:43 +02:00
|
|
|
mapCompleteHelpUnlock: 200,
|
2020-09-03 16:44:48 +02:00
|
|
|
tagsVisibleAndWikiLinked: 150,
|
|
|
|
themeGeneratorReadOnlyUnlock: 200,
|
2020-09-05 01:40:43 +02:00
|
|
|
themeGeneratorFullUnlock: 500,
|
2020-09-17 13:13:02 +02:00
|
|
|
addNewPointWithUnreadMessagesUnlock: 500
|
2020-08-22 18:57:27 +02:00
|
|
|
};
|
2020-07-31 01:45:54 +02:00
|
|
|
|
2020-07-31 17:11:44 +02:00
|
|
|
public static runningFromConsole: boolean = false;
|
2020-07-31 01:45:54 +02:00
|
|
|
|
|
|
|
public readonly layoutToUse = new UIEventSource<Layout>(undefined);
|
|
|
|
|
|
|
|
/**
|
|
|
|
The mapping from id -> UIEventSource<properties>
|
|
|
|
*/
|
|
|
|
public allElements: ElementStorage;
|
|
|
|
/**
|
|
|
|
THe change handler
|
|
|
|
*/
|
|
|
|
public changes: Changes;
|
|
|
|
/**
|
|
|
|
THe basemap with leaflet instance
|
|
|
|
*/
|
2020-07-31 17:38:03 +02:00
|
|
|
public bm;
|
2020-07-31 01:45:54 +02:00
|
|
|
/**
|
2020-08-22 16:00:33 +02:00
|
|
|
The user credentials
|
2020-07-31 01:45:54 +02:00
|
|
|
*/
|
|
|
|
public osmConnection: OsmConnection;
|
2020-08-26 15:36:04 +02:00
|
|
|
|
|
|
|
public favouriteLayers: UIEventSource<string[]>;
|
|
|
|
|
|
|
|
public layerUpdater: LayerUpdater;
|
|
|
|
|
|
|
|
|
2020-07-31 16:17:16 +02:00
|
|
|
public filteredLayers: UIEventSource<FilteredLayer[]> = new UIEventSource<FilteredLayer[]>([])
|
|
|
|
public presets: UIEventSource<Preset[]> = new UIEventSource<Preset[]>([])
|
2020-08-26 15:36:04 +02:00
|
|
|
|
2020-07-31 01:45:54 +02:00
|
|
|
/**
|
|
|
|
* The message that should be shown at the center of the screen
|
|
|
|
*/
|
|
|
|
public readonly centerMessage = new UIEventSource<string>("");
|
|
|
|
/**
|
|
|
|
This message is shown full screen on mobile devices
|
|
|
|
*/
|
|
|
|
public readonly fullScreenMessage = new UIEventSource<UIElement>(undefined);
|
|
|
|
|
|
|
|
/**
|
|
|
|
The latest element that was selected - used to generate the right UI at the right place
|
|
|
|
*/
|
|
|
|
public readonly selectedElement = new UIEventSource<{ feature: any }>(undefined);
|
|
|
|
|
2020-09-18 00:31:54 +02:00
|
|
|
public readonly zoom: UIEventSource<number>;
|
|
|
|
public readonly lat: UIEventSource<number>;
|
|
|
|
public readonly lon: UIEventSource<number>;
|
2020-07-31 01:45:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
public readonly featureSwitchUserbadge: UIEventSource<boolean>;
|
|
|
|
public readonly featureSwitchSearch: UIEventSource<boolean>;
|
|
|
|
public readonly featureSwitchLayers: UIEventSource<boolean>;
|
|
|
|
public readonly featureSwitchAddNew: UIEventSource<boolean>;
|
|
|
|
public readonly featureSwitchWelcomeMessage: UIEventSource<boolean>;
|
|
|
|
public readonly featureSwitchIframe: UIEventSource<boolean>;
|
2020-08-07 00:45:33 +02:00
|
|
|
public readonly featureSwitchMoreQuests: UIEventSource<boolean>;
|
|
|
|
public readonly featureSwitchShareScreen: UIEventSource<boolean>;
|
2020-08-08 14:43:48 +02:00
|
|
|
public readonly featureSwitchGeolocation: UIEventSource<boolean>;
|
2020-07-31 01:45:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The map location: currently centered lat, lon and zoom
|
|
|
|
*/
|
|
|
|
public readonly locationControl = new UIEventSource<{ lat: number, lon: number, zoom: number }>(undefined);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The location as delivered by the GPS
|
|
|
|
*/
|
|
|
|
public currentGPSLocation: UIEventSource<{
|
|
|
|
latlng: number,
|
|
|
|
accuracy: number
|
|
|
|
}> = new UIEventSource<{ latlng: number, accuracy: number }>(undefined);
|
2020-08-26 15:36:04 +02:00
|
|
|
public layoutDefinition: string;
|
2020-08-26 20:11:43 +02:00
|
|
|
public installedThemes: UIEventSource<{ layout: Layout; definition: string }[]>;
|
2020-07-31 04:58:58 +02:00
|
|
|
|
2020-09-17 23:53:57 +02:00
|
|
|
public layerControlIsOpened: UIEventSource<boolean> = QueryParameters.GetQueryParameter("layer-control-toggle", "false")
|
|
|
|
.map<boolean>((str) => str !== "false", [], b => "" + b)
|
|
|
|
|
|
|
|
public welcomeMessageOpenedTab = QueryParameters.GetQueryParameter("tab", "0").map<number>(
|
|
|
|
str => isNaN(Number(str)) ? 0 : Number(str), [], n => "" + n
|
2020-08-30 01:13:18 +02:00
|
|
|
);
|
2020-07-31 01:45:54 +02:00
|
|
|
|
2020-09-16 01:29:38 +02:00
|
|
|
constructor(layoutToUse: Layout) {
|
2020-08-26 15:36:04 +02:00
|
|
|
const self = this;
|
2020-09-18 00:31:54 +02:00
|
|
|
this.layoutToUse.setData(layoutToUse);
|
|
|
|
|
|
|
|
function asFloat(source: UIEventSource<string>): UIEventSource<number> {
|
|
|
|
return source.map(str => {
|
|
|
|
let parsed = parseFloat(str);
|
|
|
|
return isNaN(parsed) ? undefined : parsed;
|
|
|
|
}, [], fl => {
|
|
|
|
if (fl === undefined || isNaN(fl)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return ("" + fl).substr(0, 6);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-18 12:00:38 +02:00
|
|
|
this.zoom = asFloat(
|
|
|
|
QueryParameters.GetQueryParameter("z", "" + layoutToUse.startzoom)
|
|
|
|
.syncWith(LocalStorageSource.Get("zoom"), true));
|
2020-09-18 00:31:54 +02:00
|
|
|
this.lat = asFloat(QueryParameters.GetQueryParameter("lat", "" + layoutToUse.startLat)
|
2020-09-18 12:00:38 +02:00
|
|
|
.syncWith(LocalStorageSource.Get("lat"), true));
|
2020-09-18 00:31:54 +02:00
|
|
|
this.lon = asFloat(QueryParameters.GetQueryParameter("lon", "" + layoutToUse.startLon)
|
2020-09-18 12:00:38 +02:00
|
|
|
.syncWith(LocalStorageSource.Get("lon"), true));
|
2020-09-18 00:31:54 +02:00
|
|
|
|
|
|
|
|
2020-07-31 01:45:54 +02:00
|
|
|
this.locationControl = new UIEventSource<{ lat: number, lon: number, zoom: number }>({
|
2020-08-26 15:36:04 +02:00
|
|
|
zoom: Utils.asFloat(this.zoom.data),
|
|
|
|
lat: Utils.asFloat(this.lat.data),
|
|
|
|
lon: Utils.asFloat(this.lon.data),
|
2020-07-31 01:45:54 +02:00
|
|
|
}).addCallback((latlonz) => {
|
2020-09-18 00:31:54 +02:00
|
|
|
this.zoom.setData(latlonz.zoom);
|
|
|
|
this.lat.setData(latlonz.lat);
|
|
|
|
this.lon.setData(latlonz.lon);
|
2020-08-26 15:36:04 +02:00
|
|
|
});
|
2020-07-31 01:45:54 +02:00
|
|
|
|
2020-08-26 15:36:04 +02:00
|
|
|
this.layoutToUse.addCallback(layoutToUse => {
|
|
|
|
const lcd = self.locationControl.data;
|
|
|
|
lcd.zoom = lcd.zoom ?? layoutToUse?.startzoom;
|
|
|
|
lcd.lat = lcd.lat ?? layoutToUse?.startLat;
|
|
|
|
lcd.lon = lcd.lon ?? layoutToUse?.startLon;
|
|
|
|
self.locationControl.ping();
|
|
|
|
});
|
2020-07-31 01:45:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
function featSw(key: string, deflt: (layout: Layout) => boolean): UIEventSource<boolean> {
|
|
|
|
const queryParameterSource = QueryParameters.GetQueryParameter(key, undefined);
|
|
|
|
// I'm so sorry about someone trying to decipher this
|
2020-08-26 15:36:04 +02:00
|
|
|
|
2020-07-31 01:45:54 +02:00
|
|
|
// It takes the current layout, extracts the default value for this query paramter. A query parameter event source is then retreived and flattened
|
|
|
|
return UIEventSource.flatten(
|
2020-08-26 15:36:04 +02:00
|
|
|
self.layoutToUse.map((layout) => {
|
|
|
|
const defaultValue = deflt(layout);
|
|
|
|
const queryParam = QueryParameters.GetQueryParameter(key, "" + defaultValue)
|
|
|
|
return queryParam.map((str) => str === undefined ? defaultValue : (str !== "false"));
|
|
|
|
}), [queryParameterSource]);
|
2020-07-31 01:45:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-26 15:36:04 +02:00
|
|
|
this.featureSwitchUserbadge = featSw("fs-userbadge", (layoutToUse) => layoutToUse?.enableUserBadge ?? true);
|
|
|
|
this.featureSwitchSearch = featSw("fs-search", (layoutToUse) => layoutToUse?.enableSearch ?? true);
|
|
|
|
this.featureSwitchLayers = featSw("fs-layers", (layoutToUse) => layoutToUse?.enableLayers ?? true);
|
|
|
|
this.featureSwitchAddNew = featSw("fs-add-new", (layoutToUse) => layoutToUse?.enableAdd ?? true);
|
2020-07-31 01:45:54 +02:00
|
|
|
this.featureSwitchWelcomeMessage = featSw("fs-welcome-message", () => true);
|
|
|
|
this.featureSwitchIframe = featSw("fs-iframe", () => false);
|
2020-08-26 15:36:04 +02:00
|
|
|
this.featureSwitchMoreQuests = featSw("fs-more-quests", (layoutToUse) => layoutToUse?.enableMoreQuests ?? true);
|
|
|
|
this.featureSwitchShareScreen = featSw("fs-share-screen", (layoutToUse) => layoutToUse?.enableShareScreen ?? true);
|
|
|
|
this.featureSwitchGeolocation = featSw("fs-geolocation", (layoutToUse) => layoutToUse?.enableGeolocation ?? true);
|
2020-07-31 01:45:54 +02:00
|
|
|
|
2020-08-27 11:11:20 +02:00
|
|
|
const testParam = QueryParameters.GetQueryParameter("test", "false").data;
|
2020-07-31 04:58:58 +02:00
|
|
|
this.osmConnection = new OsmConnection(
|
2020-08-27 11:11:20 +02:00
|
|
|
testParam === "true",
|
|
|
|
QueryParameters.GetQueryParameter("oauth_token", undefined),
|
2020-08-30 01:13:18 +02:00
|
|
|
layoutToUse.id,
|
|
|
|
true
|
2020-07-31 04:58:58 +02:00
|
|
|
);
|
2020-08-26 15:36:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-27 00:08:00 +02:00
|
|
|
this.installedThemes = this.osmConnection.preferencesHandler.preferences.map<{ layout: Layout, definition: string }[]>(allPreferences => {
|
2020-08-26 20:11:43 +02:00
|
|
|
const installedThemes: { layout: Layout, definition: string }[] = [];
|
|
|
|
if (allPreferences === undefined) {
|
|
|
|
return installedThemes;
|
|
|
|
}
|
|
|
|
for (const allPreferencesKey in allPreferences) {
|
|
|
|
const themename = allPreferencesKey.match(/^mapcomplete-installed-theme-(.*)-combined-length$/);
|
|
|
|
if (themename && themename[1] !== "") {
|
|
|
|
const customLayout = State.state.osmConnection.GetLongPreference("installed-theme-" + themename[1]);
|
|
|
|
if(customLayout.data === undefined){
|
|
|
|
console.log("No data defined for ", themename[1]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
2020-09-10 19:33:06 +02:00
|
|
|
const layout = State.FromBase64(customLayout.data);
|
2020-08-31 02:59:47 +02:00
|
|
|
if(layout.id === undefined){
|
|
|
|
// This is an old style theme
|
|
|
|
// We remove it
|
|
|
|
customLayout.setData(undefined);
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-26 20:11:43 +02:00
|
|
|
installedThemes.push({
|
2020-08-31 02:59:47 +02:00
|
|
|
layout: layout,
|
2020-08-26 20:11:43 +02:00
|
|
|
definition: customLayout.data
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.warn("Could not parse custom layout from preferences: ", allPreferencesKey, e, customLayout.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return installedThemes;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2020-09-15 14:00:31 +02:00
|
|
|
|
|
|
|
// IMportant: the favourite layers are initiliazed _after_ the installed themes, as these might contain an installedTheme
|
|
|
|
this.favouriteLayers = this.osmConnection.GetLongPreference("favouriteLayers").map(
|
|
|
|
str => Utils.Dedup(str?.split(";")) ?? [],
|
|
|
|
[], layers => Utils.Dedup(layers)?.join(";")
|
|
|
|
);
|
|
|
|
|
2020-07-31 04:58:58 +02:00
|
|
|
Locale.language.syncWith(this.osmConnection.GetPreference("language"));
|
|
|
|
|
|
|
|
|
|
|
|
Locale.language.addCallback((currentLanguage) => {
|
2020-08-26 15:36:04 +02:00
|
|
|
const layoutToUse = self.layoutToUse.data;
|
|
|
|
if (layoutToUse === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.layoutToUse.data.supportedLanguages.indexOf(currentLanguage) < 0) {
|
2020-08-07 20:50:46 +02:00
|
|
|
console.log("Resetting language to", layoutToUse.supportedLanguages[0], "as", currentLanguage, " is unsupported")
|
2020-07-31 04:58:58 +02:00
|
|
|
// The current language is not supported -> switch to a supported one
|
|
|
|
Locale.language.setData(layoutToUse.supportedLanguages[0]);
|
|
|
|
}
|
|
|
|
}).ping()
|
|
|
|
|
2020-08-26 15:36:04 +02:00
|
|
|
this.layoutToUse.map((layoutToUse) => {
|
2020-09-03 03:16:43 +02:00
|
|
|
return Translations.WT(layoutToUse?.title)?.txt ?? "MapComplete"
|
2020-08-26 15:36:04 +02:00
|
|
|
}, [Locale.language]
|
2020-09-03 03:16:43 +02:00
|
|
|
).addCallbackAndRun((title) => {
|
2020-08-26 15:36:04 +02:00
|
|
|
document.title = title
|
|
|
|
});
|
2020-07-31 04:58:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
this.allElements = new ElementStorage();
|
2020-08-27 11:11:20 +02:00
|
|
|
this.changes = new Changes();
|
2020-07-31 04:58:58 +02:00
|
|
|
|
2020-08-26 15:36:04 +02:00
|
|
|
if (State.runningFromConsole) {
|
2020-07-31 17:11:44 +02:00
|
|
|
console.warn("running from console - not initializing map. Assuming test.html");
|
|
|
|
return;
|
|
|
|
}
|
2020-07-31 04:58:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (document.getElementById("leafletDiv") === null) {
|
|
|
|
console.warn("leafletDiv not found - not initializing map. Assuming test.html");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-31 16:17:16 +02:00
|
|
|
}
|
2020-09-10 19:33:06 +02:00
|
|
|
|
|
|
|
public static FromBase64 : (data: string) => Layout = undefined;
|
2020-07-31 16:17:16 +02:00
|
|
|
}
|