forked from MapComplete/MapComplete
First part of a huge refactoring
This commit is contained in:
parent
99a38f2b10
commit
0c22b15c8d
3 changed files with 205 additions and 131 deletions
|
@ -1,131 +0,0 @@
|
||||||
import * as known_layers from "../assets/generated/known_layers_and_themes.json"
|
|
||||||
import {Utils} from "../Utils";
|
|
||||||
import LayerConfig from "../Models/ThemeConfig/LayerConfig";
|
|
||||||
import {TagRenderingConfigJson} from "../Models/ThemeConfig/Json/TagRenderingConfigJson";
|
|
||||||
import SharedTagRenderings from "./SharedTagRenderings";
|
|
||||||
import {LayerConfigJson} from "../Models/ThemeConfig/Json/LayerConfigJson";
|
|
||||||
import WithContextLoader from "../Models/ThemeConfig/WithContextLoader";
|
|
||||||
|
|
||||||
export default class AllKnownLayers {
|
|
||||||
|
|
||||||
public static inited = (_ => {
|
|
||||||
WithContextLoader.getKnownTagRenderings = (id => AllKnownLayers.getTagRendering(id))
|
|
||||||
return true
|
|
||||||
})()
|
|
||||||
|
|
||||||
public static runningGenerateScript = false;
|
|
||||||
|
|
||||||
// Must be below the list...
|
|
||||||
public static sharedLayers: Map<string, LayerConfig> = AllKnownLayers.getSharedLayers();
|
|
||||||
public static sharedLayersJson: Map<string, any> = AllKnownLayers.getSharedLayersJson();
|
|
||||||
|
|
||||||
|
|
||||||
public static added_by_default: string[] = ["gps_location", "gps_location_history", "home_location", "gps_track"]
|
|
||||||
public static no_include: string[] = ["conflation", "left_right_style", "split_point","current_view"]
|
|
||||||
/**
|
|
||||||
* Layer IDs of layers which have special properties through built-in hooks
|
|
||||||
*/
|
|
||||||
public static priviliged_layers: string[] = [...AllKnownLayers.added_by_default, "type_node", ...AllKnownLayers.no_include]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the appropriate tagRenderingJSON
|
|
||||||
* Allows to steal them from other layers.
|
|
||||||
* This will add the tags of the layer to the configuration though!
|
|
||||||
* @param renderingId
|
|
||||||
*/
|
|
||||||
static getTagRendering(renderingId: string): TagRenderingConfigJson[] {
|
|
||||||
if (renderingId.indexOf(".") < 0) {
|
|
||||||
const found = SharedTagRenderings.SharedTagRenderingJson.get(renderingId)
|
|
||||||
if(found === undefined){
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
return [found]
|
|
||||||
}
|
|
||||||
|
|
||||||
const [layerId, id] = renderingId.split(".")
|
|
||||||
const layer = AllKnownLayers.getSharedLayersJson().get(layerId)
|
|
||||||
if (layer === undefined) {
|
|
||||||
if (AllKnownLayers.runningGenerateScript) {
|
|
||||||
// Probably generating the layer overview
|
|
||||||
return <TagRenderingConfigJson[]>[{
|
|
||||||
id: "dummy"
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
throw "Builtin layer " + layerId + " not found"
|
|
||||||
}
|
|
||||||
|
|
||||||
const renderings = layer?.tagRenderings ?? []
|
|
||||||
if (id === "*") {
|
|
||||||
return <TagRenderingConfigJson[]>JSON.parse(JSON.stringify(renderings))
|
|
||||||
}
|
|
||||||
|
|
||||||
const selectByGroup = id.startsWith("*")
|
|
||||||
const expectedGroupName = id.substring(1)
|
|
||||||
|
|
||||||
const allValidValues = []
|
|
||||||
for (const rendering of renderings) {
|
|
||||||
if ((!selectByGroup && rendering["id"] === id) || (selectByGroup && rendering["group"] === expectedGroupName)) {
|
|
||||||
const found = <TagRenderingConfigJson>JSON.parse(JSON.stringify(rendering))
|
|
||||||
if (found.condition === undefined) {
|
|
||||||
found.condition = layer.source.osmTags
|
|
||||||
} else {
|
|
||||||
found.condition = {and: [found.condition, layer.source.osmTags]}
|
|
||||||
}
|
|
||||||
allValidValues.push(found)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(allValidValues.length === 0){
|
|
||||||
|
|
||||||
throw `The rendering with id ${id} was not found in the builtin layer ${layerId}. Try one of ${Utils.NoNull(renderings.map(r => r["id"])).join(", ")}`
|
|
||||||
}
|
|
||||||
return allValidValues
|
|
||||||
}
|
|
||||||
|
|
||||||
private static getSharedLayers(): Map<string, LayerConfig> {
|
|
||||||
const sharedLayers = new Map<string, LayerConfig>();
|
|
||||||
for (const layer of known_layers.layers) {
|
|
||||||
try {
|
|
||||||
// @ts-ignore
|
|
||||||
const parsed = new LayerConfig(layer, "shared_layers")
|
|
||||||
sharedLayers.set(layer.id, parsed);
|
|
||||||
} catch (e) {
|
|
||||||
if (!Utils.runningFromConsole) {
|
|
||||||
console.error("CRITICAL: Could not parse a layer configuration!", layer.id, " due to", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const layout of known_layers.themes) {
|
|
||||||
for (const layer of layout.layers) {
|
|
||||||
if (typeof layer === "string") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (layer.builtin !== undefined) {
|
|
||||||
// This is a builtin layer of which stuff is overridden - skip
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const parsed = new LayerConfig(layer, "shared_layer_in_theme")
|
|
||||||
sharedLayers.set(layer.id, parsed);
|
|
||||||
sharedLayers[layer.id] = parsed;
|
|
||||||
} catch (e) {
|
|
||||||
if (!Utils.runningFromConsole) {
|
|
||||||
console.error("Could not parse a layer in theme ", layout.id, "due to", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sharedLayers;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static getSharedLayersJson(): Map<string, LayerConfigJson> {
|
|
||||||
const sharedLayers = new Map<string, any>();
|
|
||||||
for (const layer of known_layers.layers) {
|
|
||||||
sharedLayers.set(layer.id, layer);
|
|
||||||
sharedLayers[layer.id] = layer;
|
|
||||||
}
|
|
||||||
return sharedLayers;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
104
all_themes_index.ts
Normal file
104
all_themes_index.ts
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
import {FixedUiElement} from "./UI/Base/FixedUiElement";
|
||||||
|
import {QueryParameters} from "./Logic/Web/QueryParameters";
|
||||||
|
import Combine from "./UI/Base/Combine";
|
||||||
|
import AvailableBaseLayers from "./Logic/Actors/AvailableBaseLayers";
|
||||||
|
import MinimapImplementation from "./UI/Base/MinimapImplementation";
|
||||||
|
import {Utils} from "./Utils";
|
||||||
|
import AllThemesGui from "./UI/AllThemesGui";
|
||||||
|
import DetermineLayout from "./Logic/DetermineLayout";
|
||||||
|
import LayoutConfig from "./Models/ThemeConfig/LayoutConfig";
|
||||||
|
import DefaultGUI from "./UI/DefaultGUI";
|
||||||
|
import State from "./State";
|
||||||
|
import AvailableBaseLayersImplementation from "./Logic/Actors/AvailableBaseLayersImplementation";
|
||||||
|
import ShowOverlayLayerImplementation from "./UI/ShowDataLayer/ShowOverlayLayerImplementation";
|
||||||
|
import {DefaultGuiState} from "./UI/DefaultGuiState";
|
||||||
|
|
||||||
|
// Workaround for a stupid crash: inject some functions which would give stupid circular dependencies or crash the other nodejs scripts running from console
|
||||||
|
MinimapImplementation.initialize()
|
||||||
|
AvailableBaseLayers.implement(new AvailableBaseLayersImplementation())
|
||||||
|
ShowOverlayLayerImplementation.Implement();
|
||||||
|
// Miscelleanous
|
||||||
|
Utils.DisableLongPresses()
|
||||||
|
|
||||||
|
// --------------------- Special actions based on the parameters -----------------
|
||||||
|
// @ts-ignore
|
||||||
|
if (location.href.startsWith("http://buurtnatuur.be")) {
|
||||||
|
// Reload the https version. This is important for the 'locate me' button
|
||||||
|
window.location.replace("https://buurtnatuur.be");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Init {
|
||||||
|
public static Init(layoutToUse: LayoutConfig, encoded: string) {
|
||||||
|
|
||||||
|
if (layoutToUse === null) {
|
||||||
|
// Something went wrong, error message is already on screen
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (layoutToUse === undefined) {
|
||||||
|
// No layout found
|
||||||
|
new AllThemesGui()
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Workaround/legacy to keep the old paramters working as I renamed some of them
|
||||||
|
if (layoutToUse?.id === "cyclofix") {
|
||||||
|
const legacy = QueryParameters.GetQueryParameter("layer-bike_shops", "true", "Legacy - keep De Fietsambassade working");
|
||||||
|
const correct = QueryParameters.GetQueryParameter("layer-bike_shop", "true", "Legacy - keep De Fietsambassade working")
|
||||||
|
if (legacy.data !== "true") {
|
||||||
|
correct.setData(legacy.data)
|
||||||
|
}
|
||||||
|
console.log("layer-bike_shop toggles: legacy:", legacy.data, "new:", correct.data)
|
||||||
|
|
||||||
|
const legacyCafe = QueryParameters.GetQueryParameter("layer-bike_cafes", "true", "Legacy - keep De Fietsambassade working")
|
||||||
|
const correctCafe = QueryParameters.GetQueryParameter("layer-bike_cafe", "true", "Legacy - keep De Fietsambassade working")
|
||||||
|
if (legacyCafe.data !== "true") {
|
||||||
|
correctCafe.setData(legacy.data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const guiState = new DefaultGuiState()
|
||||||
|
State.state = new State(layoutToUse);
|
||||||
|
DefaultGuiState.state = guiState;
|
||||||
|
// This 'leaks' the global state via the window object, useful for debugging
|
||||||
|
// @ts-ignore
|
||||||
|
window.mapcomplete_state = State.state;
|
||||||
|
|
||||||
|
new DefaultGUI(State.state, guiState)
|
||||||
|
|
||||||
|
if (encoded !== undefined && encoded.length > 10) {
|
||||||
|
// We save the layout to the user settings and local storage
|
||||||
|
State.state.osmConnection.OnLoggedIn(() => {
|
||||||
|
State.state.osmConnection
|
||||||
|
.GetLongPreference("installed-theme-" + layoutToUse.id)
|
||||||
|
.setData(encoded);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
document.getElementById("decoration-desktop").remove();
|
||||||
|
new Combine(["Initializing... <br/>",
|
||||||
|
new FixedUiElement("<a>If this message persist, something went wrong - click here to try again</a>")
|
||||||
|
.SetClass("link-underline small")
|
||||||
|
.onClick(() => {
|
||||||
|
localStorage.clear();
|
||||||
|
window.location.reload(true);
|
||||||
|
|
||||||
|
})])
|
||||||
|
.AttachTo("centermessage"); // Add an initialization and reset button if something goes wrong
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
if(typeof theme === "undefined"){
|
||||||
|
DetermineLayout.GetLayout().then(value => {
|
||||||
|
console.log("Got ", value)
|
||||||
|
Init.Init(value[0], value[1])
|
||||||
|
}).catch(err => {
|
||||||
|
console.error("Error while initializing: ", err, err.stack)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
101
index_theme.ts.template
Normal file
101
index_theme.ts.template
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
import {FixedUiElement} from "./UI/Base/FixedUiElement";
|
||||||
|
import {QueryParameters} from "./Logic/Web/QueryParameters";
|
||||||
|
import Combine from "./UI/Base/Combine";
|
||||||
|
import AvailableBaseLayers from "./Logic/Actors/AvailableBaseLayers";
|
||||||
|
import MinimapImplementation from "./UI/Base/MinimapImplementation";
|
||||||
|
import {Utils} from "./Utils";
|
||||||
|
import AllThemesGui from "./UI/AllThemesGui";
|
||||||
|
import DetermineLayout from "./Logic/DetermineLayout";
|
||||||
|
import LayoutConfig from "./Models/ThemeConfig/LayoutConfig";
|
||||||
|
import DefaultGUI from "./UI/DefaultGUI";
|
||||||
|
import State from "./State";
|
||||||
|
import AvailableBaseLayersImplementation from "./Logic/Actors/AvailableBaseLayersImplementation";
|
||||||
|
import ShowOverlayLayerImplementation from "./UI/ShowDataLayer/ShowOverlayLayerImplementation";
|
||||||
|
import {DefaultGuiState} from "./UI/DefaultGuiState";
|
||||||
|
|
||||||
|
// Workaround for a stupid crash: inject some functions which would give stupid circular dependencies or crash the other nodejs scripts running from console
|
||||||
|
MinimapImplementation.initialize()
|
||||||
|
AvailableBaseLayers.implement(new AvailableBaseLayersImplementation())
|
||||||
|
ShowOverlayLayerImplementation.Implement();
|
||||||
|
// Miscelleanous
|
||||||
|
Utils.DisableLongPresses()
|
||||||
|
|
||||||
|
// --------------------- Special actions based on the parameters -----------------
|
||||||
|
// @ts-ignore
|
||||||
|
if (location.href.startsWith("http://buurtnatuur.be")) {
|
||||||
|
// Reload the https version. This is important for the 'locate me' button
|
||||||
|
window.location.replace("https://buurtnatuur.be");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Init {
|
||||||
|
public static Init(layoutToUse: LayoutConfig, encoded: string) {
|
||||||
|
|
||||||
|
if (layoutToUse === null) {
|
||||||
|
// Something went wrong, error message is already on screen
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (layoutToUse === undefined) {
|
||||||
|
// No layout found
|
||||||
|
new AllThemesGui()
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Workaround/legacy to keep the old paramters working as I renamed some of them
|
||||||
|
if (layoutToUse?.id === "cyclofix") {
|
||||||
|
const legacy = QueryParameters.GetQueryParameter("layer-bike_shops", "true", "Legacy - keep De Fietsambassade working");
|
||||||
|
const correct = QueryParameters.GetQueryParameter("layer-bike_shop", "true", "Legacy - keep De Fietsambassade working")
|
||||||
|
if (legacy.data !== "true") {
|
||||||
|
correct.setData(legacy.data)
|
||||||
|
}
|
||||||
|
console.log("layer-bike_shop toggles: legacy:", legacy.data, "new:", correct.data)
|
||||||
|
|
||||||
|
const legacyCafe = QueryParameters.GetQueryParameter("layer-bike_cafes", "true", "Legacy - keep De Fietsambassade working")
|
||||||
|
const correctCafe = QueryParameters.GetQueryParameter("layer-bike_cafe", "true", "Legacy - keep De Fietsambassade working")
|
||||||
|
if (legacyCafe.data !== "true") {
|
||||||
|
correctCafe.setData(legacy.data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const guiState = new DefaultGuiState()
|
||||||
|
State.state = new State(layoutToUse);
|
||||||
|
DefaultGuiState.state = guiState;
|
||||||
|
// This 'leaks' the global state via the window object, useful for debugging
|
||||||
|
// @ts-ignore
|
||||||
|
window.mapcomplete_state = State.state;
|
||||||
|
new DefaultGUI(State.state, guiState)
|
||||||
|
|
||||||
|
if (encoded !== undefined && encoded.length > 10) {
|
||||||
|
// We save the layout to the user settings and local storage
|
||||||
|
State.state.osmConnection.OnLoggedIn(() => {
|
||||||
|
State.state.osmConnection
|
||||||
|
.GetLongPreference("installed-theme-" + layoutToUse.id)
|
||||||
|
.setData(encoded);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
document.getElementById("decoration-desktop").remove();
|
||||||
|
new Combine(["Initializing... <br/>",
|
||||||
|
new FixedUiElement("<a>If this message persist, something went wrong - click here to try again</a>")
|
||||||
|
.SetClass("link-underline small")
|
||||||
|
.onClick(() => {
|
||||||
|
localStorage.clear();
|
||||||
|
window.location.reload(true);
|
||||||
|
|
||||||
|
})])
|
||||||
|
.AttachTo("centermessage"); // Add an initialization and reset button if something goes wrong
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
DetermineLayout.GetLayout().then(value => {
|
||||||
|
console.log("Got ", value)
|
||||||
|
Init.Init(value[0], value[1])
|
||||||
|
}).catch(err => {
|
||||||
|
console.error("Error while initializing: ", err, err.stack)
|
||||||
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue