|
1 | | -<div class="container"><div class="c-header__bottom"> |
| 1 | +<div class="container" id="apex-header-navigation"><div class="c-header__bottom"> |
2 | 2 | <a href="https://apex.esa.int/" class="c-header__logo"> |
3 | 3 | APEx |
4 | 4 | <span>Application Propagation Environments</span> |
@@ -254,3 +254,139 @@ <h6> |
254 | 254 | </div> |
255 | 255 |
|
256 | 256 | </div></div> |
| 257 | + |
| 258 | + <script type="application/javascript"> |
| 259 | + (function () { |
| 260 | + 'use strict'; |
| 261 | + |
| 262 | + const navigationRoot = document.getElementById('apex-header-navigation'); |
| 263 | + if (!navigationRoot) { |
| 264 | + return; |
| 265 | + } |
| 266 | + |
| 267 | + const NAV_SOURCES = [ |
| 268 | + { |
| 269 | + label: 'apex-primary', |
| 270 | + url: 'https://apex.esa.int/' |
| 271 | + }, |
| 272 | + { // This is a CORS proxy to fetch the navigation from apex.esa.int for local testing. |
| 273 | + label: 'apex-corsproxy', |
| 274 | + url: 'https://corsproxy.io/?https://apex.esa.int/' |
| 275 | + } |
| 276 | + ]; |
| 277 | + |
| 278 | + function absolutizeApexUrl(value) { |
| 279 | + if (!value || value.startsWith('#') || value.startsWith('javascript:')) { |
| 280 | + return value; |
| 281 | + } |
| 282 | + |
| 283 | + if (value.startsWith('//')) { |
| 284 | + return 'https:' + value; |
| 285 | + } |
| 286 | + |
| 287 | + if (value.startsWith('/')) { |
| 288 | + return 'https://apex.esa.int' + value; |
| 289 | + } |
| 290 | + |
| 291 | + return value; |
| 292 | + } |
| 293 | + |
| 294 | + function normalizeApexLinks(rootElement) { |
| 295 | + const attributeSelectors = [ |
| 296 | + ['a[href]', 'href'], |
| 297 | + ['img[src]', 'src'], |
| 298 | + ['source[src]', 'src'], |
| 299 | + ['source[srcset]', 'srcset'], |
| 300 | + ['img[srcset]', 'srcset'], |
| 301 | + ['link[href]', 'href'] |
| 302 | + ]; |
| 303 | + |
| 304 | + attributeSelectors.forEach(([selector, attribute]) => { |
| 305 | + rootElement.querySelectorAll(selector).forEach((element) => { |
| 306 | + const value = element.getAttribute(attribute); |
| 307 | + if (!value) { |
| 308 | + return; |
| 309 | + } |
| 310 | + |
| 311 | + if (attribute === 'srcset') { |
| 312 | + const normalizedSrcset = value |
| 313 | + .split(',') |
| 314 | + .map((candidate) => { |
| 315 | + const trimmedCandidate = candidate.trim(); |
| 316 | + if (!trimmedCandidate) { |
| 317 | + return trimmedCandidate; |
| 318 | + } |
| 319 | + |
| 320 | + const parts = trimmedCandidate.split(/\s+/); |
| 321 | + parts[0] = absolutizeApexUrl(parts[0]); |
| 322 | + return parts.join(' '); |
| 323 | + }) |
| 324 | + .join(', '); |
| 325 | + element.setAttribute(attribute, normalizedSrcset); |
| 326 | + return; |
| 327 | + } |
| 328 | + |
| 329 | + element.setAttribute(attribute, absolutizeApexUrl(value)); |
| 330 | + }); |
| 331 | + }); |
| 332 | + } |
| 333 | + |
| 334 | + function extractNavigationMarkup(pageHtml) { |
| 335 | + const parser = new DOMParser(); |
| 336 | + const pageDoc = parser.parseFromString(pageHtml, 'text/html'); |
| 337 | + const navigation = pageDoc.querySelector('.c-header__bottom'); |
| 338 | + return navigation ? navigation.outerHTML : null; |
| 339 | + } |
| 340 | + |
| 341 | + async function fetchSourceMarkup(source) { |
| 342 | + const response = await fetch(source.url, { |
| 343 | + method: 'GET', |
| 344 | + credentials: 'omit', |
| 345 | + mode: 'cors' |
| 346 | + }); |
| 347 | + |
| 348 | + if (!response.ok) { |
| 349 | + throw new Error('Navigation request failed with status ' + response.status); |
| 350 | + } |
| 351 | + |
| 352 | + const pageHtml = await response.text(); |
| 353 | + return extractNavigationMarkup(pageHtml); |
| 354 | + } |
| 355 | + |
| 356 | + async function loadRemoteNavigation() { |
| 357 | + for (const source of NAV_SOURCES) { |
| 358 | + try { |
| 359 | + const markup = await fetchSourceMarkup(source); |
| 360 | + if (!markup) { |
| 361 | + continue; |
| 362 | + } |
| 363 | + |
| 364 | + navigationRoot.innerHTML = markup; |
| 365 | + normalizeApexLinks(navigationRoot); |
| 366 | + navigationRoot.setAttribute('data-navigation-source', source.label); |
| 367 | + document.dispatchEvent(new CustomEvent('apex:navigation:loaded', { |
| 368 | + detail: { |
| 369 | + source: source.label |
| 370 | + } |
| 371 | + })); |
| 372 | + return; |
| 373 | + } catch (error) { |
| 374 | + // Keep fallback navigation if remote source is unavailable. |
| 375 | + } |
| 376 | + } |
| 377 | + |
| 378 | + navigationRoot.setAttribute('data-navigation-source', 'fallback'); |
| 379 | + document.dispatchEvent(new CustomEvent('apex:navigation:loaded', { |
| 380 | + detail: { |
| 381 | + source: 'fallback' |
| 382 | + } |
| 383 | + })); |
| 384 | + } |
| 385 | + |
| 386 | + if (document.readyState === 'loading') { |
| 387 | + document.addEventListener('DOMContentLoaded', loadRemoteNavigation, { once: true }); |
| 388 | + } else { |
| 389 | + loadRemoteNavigation(); |
| 390 | + } |
| 391 | + })(); |
| 392 | + </script> |
0 commit comments