Skip to content

Commit

Permalink
Merge pull request #338 from wri/WRI-305--map-and-webp
Browse files Browse the repository at this point in the history
#305: Hide giant SVG map for mobile.
  • Loading branch information
komejo authored Dec 17, 2024
2 parents 70a39d0 + c5bf5ff commit 1f10561
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 40 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
"drupal/views_extras": "^3.0",
"drupal/webform": "^6.1",
"drupal/webform_views": "^5.2",
"drupal/webp": "^1.0@RC",
"drupal/xmlsitemap": "^1.2",
"mglaman/composer-drupal-lenient": "*",
"npm-asset/jquery.toc": "^0.4.0",
Expand Down
1 change: 1 addition & 0 deletions modules/wri_maps/src/Plugin/Block/RegionMapBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function build() {

$attributes = new Attribute();
$attributes->addClass('wri-region-map');
$attributes->addClass('hidden');
$attributes->addClass($map_style);

$build['#attributes']['class'][] = $map_style;
Expand Down
2 changes: 1 addition & 1 deletion modules/wri_maps/templates/wri-region-map.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
#}
<div{{ attributes }}>
{% include svg_url ignore missing %}
<div id="interactive-map" data-svg-url="{{ svg_url }}" class="interactive-map"></div>
<div class="wri-region-map-popup"></div>
<button class="wri-region-map-popup-button"></button>
</div>
125 changes: 86 additions & 39 deletions themes/custom/ts_wrin/js/components/wri_maps.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,98 @@
/**
* @file
*
* WRI Maps
* WRI Maps with SVG loading and resize debounce.
*/
export default function(context) {
const $ = jQuery;

$(".wri-region-map", context).each(function() {
let $map = $(this);
let nids = [];
// SVG loading logic with debounce.
const mapContainer = document.getElementById("interactive-map");
const svgUrl = mapContainer?.getAttribute("data-svg-url");

$map.find("svg > g").each(function() {
let matches = $(this)
.attr("id")
.match(/^node-(\d+)/, "");
if (matches && matches.length > 1) {
nids.push(matches[1]);
}
});
let debounceTimer;

if (nids.length) {
$.ajax("/wri_maps/region-map/json", {
data: {
nids: nids
},
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) {
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").html(data.popup);
} else {
// Go directly to the node on larger screens.
window.location = data.url;
}
}
});
});
function loadSVG() {
if (!mapContainer || !svgUrl) {
return;
}

if (window.innerWidth >= 765 && !mapContainer.dataset.loaded) {
fetch(svgUrl)
.then(response => {
if (response.ok) {
return response.text();
}
throw new Error("SVG could not be loaded.");
})
.then(svgContent => {
mapContainer.innerHTML = svgContent;
mapContainer.dataset.loaded = true;
processRegionMapNodes(context);
})
.catch(error => {
console.error(error);
});
}
}

function debounceResize() {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(loadSVG, 200);
}

// Function to process .wri-region-map nodes.
function processRegionMapNodes(context) {
$(".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+)/, "");
if (matches && matches.length > 1) {
nids.push(matches[1]);
}
});
}
});

if (nids.length) {
$.ajax("/wri_maps/region-map/json", {
data: { nids: nids },
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) {
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").html(data.popup);
} else {
// Go directly to the node on larger screens.
window.location = data.url;
}
}
});
});
}
});
}
});
}

// Initial SVG loading and setup.
loadSVG();

// Add debounce for window resize.
window.addEventListener("resize", debounceResize);

// Ensure node processing on AJAX or content updates.
processRegionMapNodes(context);
}
23 changes: 23 additions & 0 deletions themes/custom/ts_wrin/sass/components/_region-map.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
/* line text up with border. */
margin-top: -0.5rem;

&.margin-bottom-md {
@media (max-width: rem(767)) {
margin-bottom: 0;
}
}

@include mq($ph) {
@include center-columns($grid-columns, $grid-columns);
}
Expand Down Expand Up @@ -46,13 +52,30 @@
}
}

.interactive-map {
width: 100%;
height: auto;
background-color: $white;
text-align: center;
aspect-ratio: 923.2/448;
}
.has-light-gray-background-color .interactive-map {
background-color: $light-grey;
}

.wri-region-map {
@include center-columns($grid-columns-mobile, $grid-columns-mobile);

@include mq($ph) {
@include center-columns($grid-columns, $grid-columns);
}

@include mq($md) {
&.hidden {
display: block;
}
}

svg > g.region,
svg > g.international_office {
outline: 0;
Expand Down

0 comments on commit 1f10561

Please sign in to comment.