Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions assets/js/component.loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,61 @@
document.head.appendChild(s);
});

// --- Map-Story Widget Helper ---
const loadMapStoryWidget = () => {
loadMapStoryWidget.cache ??= null;
if (loadMapStoryWidget.cache) return loadMapStoryWidget.cache;

loadMapStoryWidget.cache = new Promise((resolve, reject) => {
const container = document.querySelector('[data-script-loader="component.map-box"]');
if (!container) return resolve();

const src =
"https://static.solcast.com/2025-global-interactive-story/js/map-story-widget.iife.js";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GF-D6 @Rework-Digital this might work fine, but remember about the strict dynamic directive and how loading scripts needs a nonce:

j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=


// Prevent double-injection
if (document.querySelector(`script[src="${src}"]`)) {
return resolve();
}

const mapboxToken = container.getAttribute("component-data-mapbox-token");

// Fill in component-data-container from the dynamically set ID
if (container.id) {
container.setAttribute("component-data-container", `#${container.id}`);
}

const dataContainer = container.getAttribute("component-data-container");

if (!mapboxToken || !dataContainer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The widget expects both to be present or it will reject.

console.warn("[MapStory] Missing mapbox token or container selector", {
mapboxToken,
dataContainer,
});
}

const s = document.createElement("script");
s.src = src;
s.async = true;

if (dataContainer) {
s.setAttribute("data-container", dataContainer);
}

if (mapboxToken) {
s.setAttribute("data-mapbox-token", mapboxToken);
}

s.onload = resolve;
s.onerror = () => reject(new Error("Failed to load Map Story widget"));

document.head.appendChild(s);
});

return loadMapStoryWidget.cache;
};


// Function to allow loading of External JS
const loadExternalScript = (src, label) => {
loadExternalScript.cache ??= new Map();
Expand Down Expand Up @@ -267,6 +322,11 @@
if (has(".embed-graph")) {
tasks.push(loadModule("component.dw-graph.js", "graph"));
}

// ---------- Map-box Solar Anomaly ----------
if (has('[data-script-loader="component.map-box"]')) {
tasks.push(loadMapStoryWidget());
}

// ---------- Per-page modules ----------

Expand Down