forked from MapComplete/MapComplete
Reformat all files with prettier
This commit is contained in:
parent
e22d189376
commit
b541d3eab4
382 changed files with 50893 additions and 35566 deletions
|
@ -1,8 +1,7 @@
|
|||
import SimpleMetaTaggers, {SimpleMetaTagger} from "./SimpleMetaTagger";
|
||||
import {ExtraFuncParams, ExtraFunctions} from "./ExtraFunctions";
|
||||
import LayerConfig from "../Models/ThemeConfig/LayerConfig";
|
||||
import {ElementStorage} from "./ElementStorage";
|
||||
|
||||
import SimpleMetaTaggers, { SimpleMetaTagger } from "./SimpleMetaTagger"
|
||||
import { ExtraFuncParams, ExtraFunctions } from "./ExtraFunctions"
|
||||
import LayerConfig from "../Models/ThemeConfig/LayerConfig"
|
||||
import { ElementStorage } from "./ElementStorage"
|
||||
|
||||
/**
|
||||
* Metatagging adds various tags to the elements, e.g. lat, lon, surface area, ...
|
||||
|
@ -10,10 +9,8 @@ import {ElementStorage} from "./ElementStorage";
|
|||
* All metatags start with an underscore
|
||||
*/
|
||||
export default class MetaTagging {
|
||||
|
||||
|
||||
private static errorPrintCount = 0;
|
||||
private static readonly stopErrorOutputAt = 10;
|
||||
private static errorPrintCount = 0
|
||||
private static readonly stopErrorOutputAt = 10
|
||||
private static retaggingFuncCache = new Map<string, ((feature: any) => void)[]>()
|
||||
|
||||
/**
|
||||
|
@ -22,17 +19,19 @@ export default class MetaTagging {
|
|||
*
|
||||
* Returns true if at least one feature has changed properties
|
||||
*/
|
||||
public static addMetatags(features: { feature: any; freshness: Date }[],
|
||||
params: ExtraFuncParams,
|
||||
layer: LayerConfig,
|
||||
state?: { allElements?: ElementStorage },
|
||||
options?: {
|
||||
includeDates?: true | boolean,
|
||||
includeNonDates?: true | boolean,
|
||||
evaluateStrict?: false | boolean
|
||||
}): boolean {
|
||||
public static addMetatags(
|
||||
features: { feature: any; freshness: Date }[],
|
||||
params: ExtraFuncParams,
|
||||
layer: LayerConfig,
|
||||
state?: { allElements?: ElementStorage },
|
||||
options?: {
|
||||
includeDates?: true | boolean
|
||||
includeNonDates?: true | boolean
|
||||
evaluateStrict?: false | boolean
|
||||
}
|
||||
): boolean {
|
||||
if (features === undefined || features.length === 0) {
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
console.log("Recalculating metatags...")
|
||||
|
@ -52,51 +51,62 @@ export default class MetaTagging {
|
|||
// The calculated functions - per layer - which add the new keys
|
||||
const layerFuncs = this.createRetaggingFunc(layer, state)
|
||||
|
||||
let atLeastOneFeatureChanged = false;
|
||||
let atLeastOneFeatureChanged = false
|
||||
|
||||
for (let i = 0; i < features.length; i++) {
|
||||
const ff = features[i];
|
||||
const ff = features[i]
|
||||
const feature = ff.feature
|
||||
const freshness = ff.freshness
|
||||
let somethingChanged = false
|
||||
let definedTags = new Set(Object.getOwnPropertyNames(feature.properties))
|
||||
for (const metatag of metatagsToApply) {
|
||||
try {
|
||||
if (!metatag.keys.some(key => feature.properties[key] === undefined)) {
|
||||
if (!metatag.keys.some((key) => feature.properties[key] === undefined)) {
|
||||
// All keys are already defined, we probably already ran this one
|
||||
continue
|
||||
}
|
||||
|
||||
if (metatag.isLazy) {
|
||||
if (!metatag.keys.some(key => !definedTags.has(key))) {
|
||||
if (!metatag.keys.some((key) => !definedTags.has(key))) {
|
||||
// All keys are defined - lets skip!
|
||||
continue
|
||||
}
|
||||
somethingChanged = true;
|
||||
somethingChanged = true
|
||||
metatag.applyMetaTagsOnFeature(feature, freshness, layer, state)
|
||||
if(options?.evaluateStrict){
|
||||
if (options?.evaluateStrict) {
|
||||
for (const key of metatag.keys) {
|
||||
feature.properties[key]
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const newValueAdded = metatag.applyMetaTagsOnFeature(feature, freshness, layer, state)
|
||||
const newValueAdded = metatag.applyMetaTagsOnFeature(
|
||||
feature,
|
||||
freshness,
|
||||
layer,
|
||||
state
|
||||
)
|
||||
/* Note that the expression:
|
||||
* `somethingChanged = newValueAdded || metatag.applyMetaTagsOnFeature(feature, freshness)`
|
||||
* Is WRONG
|
||||
*
|
||||
* IF something changed is `true` due to an earlier run, it will short-circuit and _not_ evaluate the right hand of the OR,
|
||||
* thus not running an update!
|
||||
*/
|
||||
* `somethingChanged = newValueAdded || metatag.applyMetaTagsOnFeature(feature, freshness)`
|
||||
* Is WRONG
|
||||
*
|
||||
* IF something changed is `true` due to an earlier run, it will short-circuit and _not_ evaluate the right hand of the OR,
|
||||
* thus not running an update!
|
||||
*/
|
||||
somethingChanged = newValueAdded || somethingChanged
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Could not calculate metatag for ", metatag.keys.join(","), ":", e, e.stack)
|
||||
console.error(
|
||||
"Could not calculate metatag for ",
|
||||
metatag.keys.join(","),
|
||||
":",
|
||||
e,
|
||||
e.stack
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (layerFuncs !== undefined) {
|
||||
let retaggingChanged = false;
|
||||
let retaggingChanged = false
|
||||
try {
|
||||
retaggingChanged = layerFuncs(params, feature)
|
||||
} catch (e) {
|
||||
|
@ -113,42 +123,62 @@ export default class MetaTagging {
|
|||
return atLeastOneFeatureChanged
|
||||
}
|
||||
|
||||
private static createFunctionsForFeature(layerId: string, calculatedTags: [string, string, boolean][]): ((feature: any) => void)[] {
|
||||
const functions: ((feature: any) => any)[] = [];
|
||||
private static createFunctionsForFeature(
|
||||
layerId: string,
|
||||
calculatedTags: [string, string, boolean][]
|
||||
): ((feature: any) => void)[] {
|
||||
const functions: ((feature: any) => any)[] = []
|
||||
for (const entry of calculatedTags) {
|
||||
const key = entry[0]
|
||||
const code = entry[1];
|
||||
const code = entry[1]
|
||||
const isStrict = entry[2]
|
||||
if (code === undefined) {
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
|
||||
const calculateAndAssign: ((feat: any) => any) = (feat) => {
|
||||
const calculateAndAssign: (feat: any) => any = (feat) => {
|
||||
try {
|
||||
let result = new Function("feat", "return " + code + ";")(feat);
|
||||
let result = new Function("feat", "return " + code + ";")(feat)
|
||||
if (result === "") {
|
||||
result === undefined
|
||||
}
|
||||
if (result !== undefined && typeof result !== "string") {
|
||||
// Make sure it is a string!
|
||||
result = JSON.stringify(result);
|
||||
result = JSON.stringify(result)
|
||||
}
|
||||
delete feat.properties[key]
|
||||
feat.properties[key] = result;
|
||||
feat.properties[key] = result
|
||||
return result
|
||||
} catch (e) {
|
||||
if (MetaTagging.errorPrintCount < MetaTagging.stopErrorOutputAt) {
|
||||
console.warn("Could not calculate a " + (isStrict ? "strict " : "") + " calculated tag for key " + key + " defined by " + code + " (in layer" + layerId + ") due to \n" + e + "\n. Are you the theme creator? Doublecheck your code. Note that the metatags might not be stable on new features", e, e.stack)
|
||||
MetaTagging.errorPrintCount++;
|
||||
console.warn(
|
||||
"Could not calculate a " +
|
||||
(isStrict ? "strict " : "") +
|
||||
" calculated tag for key " +
|
||||
key +
|
||||
" defined by " +
|
||||
code +
|
||||
" (in layer" +
|
||||
layerId +
|
||||
") due to \n" +
|
||||
e +
|
||||
"\n. Are you the theme creator? Doublecheck your code. Note that the metatags might not be stable on new features",
|
||||
e,
|
||||
e.stack
|
||||
)
|
||||
MetaTagging.errorPrintCount++
|
||||
if (MetaTagging.errorPrintCount == MetaTagging.stopErrorOutputAt) {
|
||||
console.error("Got ", MetaTagging.stopErrorOutputAt, " errors calculating this metatagging - stopping output now")
|
||||
console.error(
|
||||
"Got ",
|
||||
MetaTagging.stopErrorOutputAt,
|
||||
" errors calculating this metatagging - stopping output now"
|
||||
)
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (isStrict) {
|
||||
functions.push(calculateAndAssign)
|
||||
continue
|
||||
|
@ -162,15 +192,14 @@ export default class MetaTagging {
|
|||
enumerable: false, // By setting this as not enumerable, the localTileSaver will _not_ calculate this
|
||||
get: function () {
|
||||
return calculateAndAssign(feature)
|
||||
}
|
||||
},
|
||||
})
|
||||
return undefined
|
||||
}
|
||||
|
||||
|
||||
functions.push(f)
|
||||
}
|
||||
return functions;
|
||||
return functions
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -179,39 +208,37 @@ export default class MetaTagging {
|
|||
* @param state
|
||||
* @private
|
||||
*/
|
||||
private static createRetaggingFunc(layer: LayerConfig, state):
|
||||
((params: ExtraFuncParams, feature: any) => boolean) {
|
||||
|
||||
const calculatedTags: [string, string, boolean][] = layer.calculatedTags;
|
||||
private static createRetaggingFunc(
|
||||
layer: LayerConfig,
|
||||
state
|
||||
): (params: ExtraFuncParams, feature: any) => boolean {
|
||||
const calculatedTags: [string, string, boolean][] = layer.calculatedTags
|
||||
if (calculatedTags === undefined || calculatedTags.length === 0) {
|
||||
return undefined;
|
||||
return undefined
|
||||
}
|
||||
|
||||
let functions: ((feature: any) => void)[] = MetaTagging.retaggingFuncCache.get(layer.id);
|
||||
let functions: ((feature: any) => void)[] = MetaTagging.retaggingFuncCache.get(layer.id)
|
||||
if (functions === undefined) {
|
||||
functions = MetaTagging.createFunctionsForFeature(layer.id, calculatedTags)
|
||||
MetaTagging.retaggingFuncCache.set(layer.id, functions)
|
||||
}
|
||||
|
||||
|
||||
return (params: ExtraFuncParams, feature) => {
|
||||
const tags = feature.properties
|
||||
if (tags === undefined) {
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
ExtraFunctions.FullPatchFeature(params, feature);
|
||||
ExtraFunctions.FullPatchFeature(params, feature)
|
||||
for (const f of functions) {
|
||||
f(feature);
|
||||
f(feature)
|
||||
}
|
||||
state?.allElements?.getEventSourceById(feature.properties.id)?.ping();
|
||||
state?.allElements?.getEventSourceById(feature.properties.id)?.ping()
|
||||
} catch (e) {
|
||||
console.error("Invalid syntax in calculated tags or some other error: ", e)
|
||||
}
|
||||
return true; // Something changed
|
||||
return true // Something changed
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue