From 59d53e5ec5111397e38bca6a689f6af76eb78af6 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Mon, 22 Mar 2021 02:45:22 +0100 Subject: [PATCH] Fix generation of docs by injecting dependency --- Logic/MetaTagging.ts | 37 +++++++++++++++++++++++-------------- index.ts | 11 ++++++++++- scripts/generateDocs.ts | 2 +- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/Logic/MetaTagging.ts b/Logic/MetaTagging.ts index c0e4e22..c95c2ff 100644 --- a/Logic/MetaTagging.ts +++ b/Logic/MetaTagging.ts @@ -5,7 +5,6 @@ import {And, Or, Tag} from "./Tags"; import {Utils} from "../Utils"; import {UIElement} from "../UI/UIElement"; import Combine from "../UI/Base/Combine"; -import CountryCoder from "latlon2country" class SimpleMetaTagger { public readonly keys: string[]; @@ -42,6 +41,7 @@ class SimpleMetaTagger { export default class MetaTagging { + static coder: any; private static latlon = new SimpleMetaTagger(["_lat", "_lon"], "The latitude and longitude of the point (or centerpoint in the case of a way/area)", (feature => { const centerPoint = GeoOperations.centerpoint(feature); @@ -64,12 +64,12 @@ export default class MetaTagging { ["_country"], "The country code of the property (with latlon2country)", feature => { - const coder = new CountryCoder("https://pietervdvn.github.io/latlon2country/"); let centerPoint: any = GeoOperations.centerpoint(feature); const lat = centerPoint.geometry.coordinates[1]; const lon = centerPoint.geometry.coordinates[0]; - coder.GetCountryCodeFor(lon, lat, (countries) => { + + MetaTagging.GetCountryCodeFor(lon, lat, (countries) => { try { feature.properties["_country"] = countries[0].trim().toLowerCase(); const tagsSource = State.state.allElements.getEventSourceFor(feature); @@ -143,7 +143,6 @@ export default class MetaTagging { }) }) ) - private static directionSimplified = new SimpleMetaTagger( ["_direction:simplified", "_direction:leftright"], "_direction:simplified turns 'camera:direction' and 'direction' into either 0, 45, 90, 135, 180, 225, 270 or 315, whichever is closest. _direction:leftright is either 'left' or 'right', which is left-looking on the map or 'right-looking' on the map", (feature => { @@ -167,7 +166,6 @@ export default class MetaTagging { }) ) - private static carriageWayWidth = new SimpleMetaTagger( ["_width:needed", "_width:needed:no_pedestrians", "_width:difference"], "Legacy for a specific project calculating the needed width for safe traffic on a road. Only activated if 'width:carriageway' is present", @@ -276,14 +274,13 @@ export default class MetaTagging { } ); - private static currentTime = new SimpleMetaTagger( ["_now:date", "_now:datetime", "_loaded:date", "_loaded:_datetime"], "Adds the time that the data got loaded - pretty much the time of downloading from overpass. The format is YYYY-MM-DD hh:mm, aka 'sortable' aka ISO-8601-but-not-entirely", (feature, _, freshness) => { const now = new Date(); - - if(typeof freshness === "string" ){ + + if (typeof freshness === "string") { freshness = new Date(freshness) } @@ -294,6 +291,7 @@ export default class MetaTagging { function datetime(d: Date) { return d.toISOString().slice(0, -5).replace("T", " "); } + feature.properties["_now:date"] = date(now); feature.properties["_now:datetime"] = datetime(now); feature.properties["_loaded:date"] = date(freshness); @@ -301,7 +299,6 @@ export default class MetaTagging { } ) - private static metatags = [ MetaTagging.latlon, MetaTagging.surfaceArea, @@ -317,7 +314,7 @@ export default class MetaTagging { * An actor which adds metatags on every feature in the given object * The features are a list of geojson-features, with a "properties"-field and geometry */ - static addMetatags(features: {feature: any, freshness: Date}[]) { + static addMetatags(features: { feature: any, freshness: Date }[]) { for (const metatag of MetaTagging.metatags) { try { @@ -329,9 +326,21 @@ export default class MetaTagging { } } - - static HelpText() : UIElement{ - const subElements: UIElement[] = []; + + static GetCountryCodeFor(lon: number, lat: number, callback: (country: string) => void) { + MetaTagging.coder.GetCountryCodeFor(lon, lat, callback) + } + + static HelpText(): UIElement { + const subElements: UIElement[] = [ + new Combine([ + "

Metatags

", + "Metatags are extra tags available, in order to display more data or to give better questions.", + "The are calculated when the data arrives in the webbrowser. This document gives an overview of the available metatags" + ]) + + + ]; for (const metatag of MetaTagging.metatags) { subElements.push( @@ -341,7 +350,7 @@ export default class MetaTagging { ) ) } - + return new Combine(subElements) } } diff --git a/index.ts b/index.ts index 0a912a3..bf4843b 100644 --- a/index.ts +++ b/index.ts @@ -11,6 +11,15 @@ import State from "./State"; import Combine from "./UI/Base/Combine"; import Translations from "./UI/i18n/Translations"; + +import CountryCoder from "latlon2country" + +import MetaTagging from "./Logic/MetaTagging"; + +// Workaround for a stupid crash: inject the function +MetaTagging.coder = new CountryCoder("https://pietervdvn.github.io/latlon2country/"); + + let defaultLayout = "" // --------------------- Special actions based on the parameters ----------------- // @ts-ignore @@ -73,7 +82,7 @@ if (layoutFromBase64.startsWith("http")) { $.ajax({ url: link, success: function (data) { - + try { const parsed = JSON.parse(data); // Overwrite the id to the wiki:value diff --git a/scripts/generateDocs.ts b/scripts/generateDocs.ts index ac8dd5e..efa40b9 100644 --- a/scripts/generateDocs.ts +++ b/scripts/generateDocs.ts @@ -14,7 +14,7 @@ function WriteFile(filename, html: UIElement) : void { } WriteFile("./Docs/SpecialRenderings.md", SpecialVisualizations.HelpMessage) -// WriteFile("./Docs/CalculatedTags.md", MetaTagging.HelpText()) +WriteFile("./Docs/CalculatedTags.md", MetaTagging.HelpText()) console.log("Generated docs")