Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
komejo committed Dec 11, 2024
1 parent dbc1f48 commit d1e115e
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions themes/custom/ts_wrin/js/components/wri_maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
*
* WRI Maps with SVG loading and resize debounce.
*/
export default function (context) {
export default function(context) {
const $ = jQuery;

// SVG loading logic with debounce.
const mapContainer = document.getElementById('interactive-map');
const svgUrl = mapContainer?.getAttribute('data-svg-url');
const mapContainer = document.getElementById("interactive-map");
const svgUrl = mapContainer?.getAttribute("data-svg-url");

let debounceTimer;

Expand All @@ -19,18 +19,18 @@ export default function (context) {

if (window.innerWidth >= 765 && !mapContainer.dataset.loaded) {
fetch(svgUrl)
.then((response) => {
.then(response => {
if (response.ok) {
return response.text();
}
throw new Error('SVG could not be loaded.');
throw new Error("SVG could not be loaded.");
})
.then((svgContent) => {
.then(svgContent => {
mapContainer.innerHTML = svgContent;
mapContainer.dataset.loaded = true;
processRegionMapNodes(context);
})
.catch((error) => {
.catch(error => {
console.error(error);
});
}
Expand All @@ -43,12 +43,14 @@ export default function (context) {

// Function to process .wri-region-map nodes.
function processRegionMapNodes(context) {
$(".wri-region-map", context).each(function () {
$(".wri-region-map", context).each(function() {
let $map = $(this);
let nids = [];

$map.find("svg > g").each(function () {
let matches = $(this).attr("id").match(/^node-(\d+)/, "");
$map.find("svg > g").each(function() {
let matches = $(this)
.attr("id")
.match(/^node-(\d+)/, "");
if (matches && matches.length > 1) {
nids.push(matches[1]);
}
Expand All @@ -57,18 +59,20 @@ export default function (context) {
if (nids.length) {
$.ajax("/wri_maps/region-map/json", {
data: { nids: nids },
success: function (result) {
$.each(result, function (nid, data) {
success: function(result) {
$.each(result, function(nid, data) {
$map
.find("svg > g[id='node-" + nid + "']")
.addClass(data.type)
.attr("aria-label", data.title)
.attr("tabindex", "0")
.on("click keypress", function (event) {
.on("click keypress", function(event) {
if (event.type == "click" || event.keyCode == 13) {
if ($(window).width() < 768) {
// Open region map popup on small screens.
$map.find(".wri-region-map-popup-button").trigger("click");
$map
.find(".wri-region-map-popup-button")
.trigger("click");
$map.find(".wri-region-map-popup").html(data.popup);
} else {
// Go directly to the node on larger screens.
Expand All @@ -77,7 +81,7 @@ export default function (context) {
}
});
});
},
}
});
}
});
Expand Down

0 comments on commit d1e115e

Please sign in to comment.