From 9af9946eae3dffe68d8e8caa808d1dd20a8bfb42 Mon Sep 17 00:00:00 2001 From: Marwin Hochfelsner <50826859+hlfan@users.noreply.github.com> Date: Mon, 22 Jun 2026 03:24:08 +0200 Subject: [PATCH] Remove dangling top-level block --- .../javascripts/index_modules/element.js | 196 +++++++++--------- 1 file changed, 97 insertions(+), 99 deletions(-) diff --git a/app/assets/javascripts/index_modules/element.js b/app/assets/javascripts/index_modules/element.js index 6bf51dfef..cb3f208c9 100644 --- a/app/assets/javascripts/index_modules/element.js +++ b/app/assets/javascripts/index_modules/element.js @@ -58,114 +58,112 @@ export function mappedElement(type) { }; }; -{ - $(document).on("click", "button.wdt-preview", e => previewWikidataValue($(e.currentTarget))); +$(document).on("click", "button.wdt-preview", e => previewWikidataValue($(e.currentTarget))); - function previewWikidataValue($btn) { - if (!OSM.WIKIDATA_API_URL) return; - const items = $btn.data("qids"); - if (!items?.length) return; - $btn.prop("disabled", true); - fetch(OSM.WIKIDATA_API_URL + "?" + new URLSearchParams({ - action: "wbgetentities", - format: "json", - origin: "*", - ids: items.join("|"), - props: "labels|sitelinks/urls|claims|descriptions", - languages: languagesToRequest.join("|"), - languagefallback: 1, - sitefilter: wikisToRequest.join("|") - }), { - headers: { "Api-User-Agent": "OSM-TagPreview (https://github.com/openstreetmap/openstreetmap-website)" }, - signal: abortController?.signal +function previewWikidataValue($btn) { + if (!OSM.WIKIDATA_API_URL) return; + const items = $btn.data("qids"); + if (!items?.length) return; + $btn.prop("disabled", true); + fetch(OSM.WIKIDATA_API_URL + "?" + new URLSearchParams({ + action: "wbgetentities", + format: "json", + origin: "*", + ids: items.join("|"), + props: "labels|sitelinks/urls|claims|descriptions", + languages: languagesToRequest.join("|"), + languagefallback: 1, + sitefilter: wikisToRequest.join("|") + }), { + headers: { "Api-User-Agent": "OSM-TagPreview (https://github.com/openstreetmap/openstreetmap-website)" }, + signal: abortController?.signal + }) + .then(response => response.ok ? response.json() : Promise.reject(response)) + .then(({ entities }) => { + if (!entities) return Promise.reject(entities); + $btn + .closest("tr") + .after( + items + .filter(qid => entities[qid]) + .map(qid => getLocalizedResponse(entities[qid])) + .filter(data => data.label || data.icon || data.description || data.article) + .map(data => renderWikidataResponse(data, $btn.siblings(`a[href*="wikidata.org/entity/${data.qid}"]`))) + ); }) - .then(response => response.ok ? response.json() : Promise.reject(response)) - .then(({ entities }) => { - if (!entities) return Promise.reject(entities); - $btn - .closest("tr") - .after( - items - .filter(qid => entities[qid]) - .map(qid => getLocalizedResponse(entities[qid])) - .filter(data => data.label || data.icon || data.description || data.article) - .map(data => renderWikidataResponse(data, $btn.siblings(`a[href*="wikidata.org/entity/${data.qid}"]`))) - ); - }) - .catch(() => $btn.prop("disabled", false)); - } + .catch(() => $btn.prop("disabled", false)); +} - function getLocalizedResponse(entity) { - const siteScheme = OSM.isDark("bs") ? "Q6545942" : "Q101608434"; - const scheme = ({ qualifiers }) => qualifiers?.P8798?.some(q => q?.datavalue?.value?.id === siteScheme) ?? 0; - const rank = ({ rank }) => ({ preferred: 2, normal: 0, deprecated: -2 })[rank] ?? 0; - const toBestClaim = (out, claim) => (rank(claim) + scheme(claim) > rank(out) + scheme(out)) ? claim : out; - const toFirstOf = (property) => (out, localization) => out ?? property[localization]; - const data = { - qid: entity.id, - label: languagesToRequest.reduce(toFirstOf(entity.labels), null), - icon: [ - "P8972", // small logo or icon - "P154", // logo image - "P14" // traffic sign - ].reduce((out, prop) => out ?? entity.claims[prop]?.reduce(toBestClaim)?.mainsnak?.datavalue?.value, null), - description: languagesToRequest.reduce(toFirstOf(entity.descriptions), null), - article: wikisToRequest.reduce(toFirstOf(entity.sitelinks), null) - }; - if (data.article) data.article.language = data.article.site.replace("wiki", ""); - return data; - } +function getLocalizedResponse(entity) { + const siteScheme = OSM.isDark("bs") ? "Q6545942" : "Q101608434"; + const scheme = ({ qualifiers }) => qualifiers?.P8798?.some(q => q?.datavalue?.value?.id === siteScheme) ?? 0; + const rank = ({ rank }) => ({ preferred: 2, normal: 0, deprecated: -2 })[rank] ?? 0; + const toBestClaim = (out, claim) => (rank(claim) + scheme(claim) > rank(out) + scheme(out)) ? claim : out; + const toFirstOf = (property) => (out, localization) => out ?? property[localization]; + const data = { + qid: entity.id, + label: languagesToRequest.reduce(toFirstOf(entity.labels), null), + icon: [ + "P8972", // small logo or icon + "P154", // logo image + "P14" // traffic sign + ].reduce((out, prop) => out ?? entity.claims[prop]?.reduce(toBestClaim)?.mainsnak?.datavalue?.value, null), + description: languagesToRequest.reduce(toFirstOf(entity.descriptions), null), + article: wikisToRequest.reduce(toFirstOf(entity.sitelinks), null) + }; + if (data.article) data.article.language = data.article.site.replace("wiki", ""); + return data; +} - function renderWikidataResponse({ icon, label, article, description }, $link) { - const localeName = new Intl.DisplayNames(OSM.preferred_languages, { type: "language" }); - const cell = $("") - .attr("colspan", 2) - .addClass("bg-body-tertiary"); +function renderWikidataResponse({ icon, label, article, description }, $link) { + const localeName = new Intl.DisplayNames(OSM.preferred_languages, { type: "language" }); + const cell = $("") + .attr("colspan", 2) + .addClass("bg-body-tertiary"); - if (icon && OSM.WIKIMEDIA_COMMONS_URL) { - let src = OSM.WIKIMEDIA_COMMONS_URL + "Special:Redirect/file/" + encodeURIComponent(icon) + "?mobileaction=toggle_view_desktop"; - if (!icon.endsWith(".svg")) src += "&width=128"; - $("") - .attr("href", OSM.WIKIMEDIA_COMMONS_URL + "File:" + encodeURIComponent(icon) + `?uselang=${OSM.i18n.locale}`) - .append($("").attr({ src, height: "32" })) - .addClass("float-end mb-1 ms-2") - .appendTo(cell); + if (icon && OSM.WIKIMEDIA_COMMONS_URL) { + let src = OSM.WIKIMEDIA_COMMONS_URL + "Special:Redirect/file/" + encodeURIComponent(icon) + "?mobileaction=toggle_view_desktop"; + if (!icon.endsWith(".svg")) src += "&width=128"; + $("") + .attr("href", OSM.WIKIMEDIA_COMMONS_URL + "File:" + encodeURIComponent(icon) + `?uselang=${OSM.i18n.locale}`) + .append($("").attr({ src, height: "32" })) + .addClass("float-end mb-1 ms-2") + .appendTo(cell); + } + if (label) { + const link = $link.clone() + .text(label.value) + .attr("dir", "auto") + .appendTo(cell); + if (!isOfExpectedLanguage(label)) { + link.attr("lang", label.language); + link.after($("").text(" " + localeName.of(label.language))); } + } + if (article) { + const link = $("") + .attr("href", article.url + `?uselang=${OSM.i18n.locale}`) + .text(label ? OSM.i18n.t("javascripts.element.wikipedia") : article.title) + .attr("dir", "auto") + .appendTo(cell); if (label) { - const link = $link.clone() - .text(label.value) - .attr("dir", "auto") - .appendTo(cell); - if (!isOfExpectedLanguage(label)) { - link.attr("lang", label.language); - link.after($("").text(" " + localeName.of(label.language))); - } + link.before(" ("); + link.after(")"); } - if (article) { - const link = $("") - .attr("href", article.url + `?uselang=${OSM.i18n.locale}`) - .text(label ? OSM.i18n.t("javascripts.element.wikipedia") : article.title) - .attr("dir", "auto") - .appendTo(cell); - if (label) { - link.before(" ("); - link.after(")"); - } - if (!isOfExpectedLanguage(article)) { - link.attr("lang", article.language); - link.after($("").text(" " + localeName.of(article.language))); - } + if (!isOfExpectedLanguage(article)) { + link.attr("lang", article.language); + link.after($("").text(" " + localeName.of(article.language))); } - if (description) { - const text = $("
") - .text(description.value) - .addClass("small") - .attr("dir", "auto") - .appendTo(cell); - if (!isOfExpectedLanguage(description)) { - text.attr("lang", description.language); - } + } + if (description) { + const text = $("
") + .text(description.value) + .addClass("small") + .attr("dir", "auto") + .appendTo(cell); + if (!isOfExpectedLanguage(description)) { + text.attr("lang", description.language); } - return $("").append(cell); } + return $("").append(cell); } -- 2.47.3