From bfeee8a1d3b45703af9fba0c708d23b219113a9c Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Sat, 27 Jun 2026 18:00:12 +0200 Subject: [PATCH] Move feature labeling code to its own module to be ready for a second connsumer in maplibres data layer --- app/assets/javascripts/feature_label.js | 39 ++++++++++++ app/assets/javascripts/index_modules/query.js | 60 ++----------------- 2 files changed, 43 insertions(+), 56 deletions(-) create mode 100644 app/assets/javascripts/feature_label.js diff --git a/app/assets/javascripts/feature_label.js b/app/assets/javascripts/feature_label.js new file mode 100644 index 000000000..490b5e285 --- /dev/null +++ b/app/assets/javascripts/feature_label.js @@ -0,0 +1,39 @@ +OSM.featurePrefix = function (feature) { + const tags = feature.tags || {}; + + if (tags.boundary === "administrative" && (tags.border_type || tags.admin_level)) { + return OSM.i18n.t("geocoder.search_osm_nominatim.border_types." + tags.border_type, { + defaultValue: OSM.i18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level, { + defaultValue: OSM.i18n.t("geocoder.search_osm_nominatim.prefix.boundary.administrative") + }) + }); + } + + const prefixes = OSM.i18n.t("geocoder.search_osm_nominatim.prefix"); + + // Prefer an exact value mapping from any tag before falling back to a + // title-cased value of the first tag that has a prefix group. + for (const key in tags) { + if (prefixes[key]?.[tags[key]]) return prefixes[key][tags[key]]; + } + for (const key in tags) { + if (prefixes[key]) { + const value = tags[key]; + return value.slice(0, 1).toUpperCase() + value.slice(1).replace(/_/g, " "); + } + } + + return OSM.i18n.t("javascripts.query." + feature.type); +}; + +OSM.featureName = function (feature) { + const tags = feature.tags || {}, + localeKeys = (OSM.preferred_languages || []).map(locale => `name:${locale}`); + + for (const key of [...localeKeys, "name", "ref", "addr:housename"]) { + if (tags[key]) return tags[key]; + } + if (tags["addr:housenumber"] && tags["addr:street"]) return `${tags["addr:housenumber"]} ${tags["addr:street"]}`; + + return "#" + feature.id; +}; diff --git a/app/assets/javascripts/index_modules/query.js b/app/assets/javascripts/index_modules/query.js index f3a3f9250..b31ae3d61 100644 --- a/app/assets/javascripts/index_modules/query.js +++ b/app/assets/javascripts/index_modules/query.js @@ -1,3 +1,5 @@ +//= require feature_label + export default function (map) { const uninterestingTags = ["source", "source_ref", "source:ref", "history", "attribution", "created_by", "tiger:county", "tiger:tlid", "tiger:upload_uuid", "KSJ2:curve_id", "KSJ2:lat", "KSJ2:lon", "KSJ2:coordinate", "KSJ2:filename", "note:ja"]; let marker; @@ -38,60 +40,6 @@ export default function (map) { return false; } - function featurePrefix(feature) { - const tags = feature.tags; - let prefix = ""; - - if (tags.boundary === "administrative" && (tags.border_type || tags.admin_level)) { - prefix = OSM.i18n.t("geocoder.search_osm_nominatim.border_types." + tags.border_type, { - defaultValue: OSM.i18n.t("geocoder.search_osm_nominatim.admin_levels.level" + tags.admin_level, { - defaultValue: OSM.i18n.t("geocoder.search_osm_nominatim.prefix.boundary.administrative") - }) - }); - } else { - const prefixes = OSM.i18n.t("geocoder.search_osm_nominatim.prefix"); - - for (const key in tags) { - const value = tags[key]; - - if (prefixes[key]) { - if (prefixes[key][value]) { - return prefixes[key][value]; - } - } - } - - for (const key in tags) { - const value = tags[key]; - - if (prefixes[key]) { - const first = value.slice(0, 1).toUpperCase(), - rest = value.slice(1).replace(/_/g, " "); - - return first + rest; - } - } - } - - if (!prefix) { - prefix = OSM.i18n.t("javascripts.query." + feature.type); - } - - return prefix; - } - - function featureName(feature) { - const tags = feature.tags, - localeKeys = OSM.preferred_languages.map(locale => `name:${locale}`); - - for (const key of [...localeKeys, "name", "ref", "addr:housename"]) { - if (tags[key]) return tags[key]; - } - if (tags["addr:housenumber"] && tags["addr:street"]) return `${tags["addr:housenumber"]} ${tags["addr:street"]}`; - - return "#" + feature.id; - } - function featureGeometry(feature) { switch (feature.type) { case "node": @@ -161,14 +109,14 @@ export default function (map) { const $li = $("
  • ") .addClass("list-group-item list-group-item-action") - .text(featurePrefix(element) + " ") + .text(OSM.featurePrefix(element) + " ") .appendTo($ul); $("") .addClass("stretched-link") .attr("href", "/" + element.type + "/" + element.id) .data("geometry", featureGeometry(element)) - .text(featureName(element)) + .text(OSM.featureName(element)) .appendTo($li); } -- 2.47.3