1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| import Map from 'ol/Map.js'; import OSM from 'ol/source/OSM.js'; import TileLayer from 'ol/layer/Tile.js'; import View from 'ol/View.js'; import { Control, FullScreen, defaults as defaultControls, } from 'ol/control.js'; import {fromLonLat} from 'ol/proj.js';
class UnusableMask extends Control { constructor() { super({ element: document.createElement('div'), }); this.element.setAttribute('hidden', 'hidden'); this.element.className = 'ol-mask'; this.element.innerHTML = '<div>Map not usable</div>'; } }
const localMapTarget = document.getElementById('map');
const map = new Map({ target: localMapTarget, controls: defaultControls().extend([new FullScreen(), new UnusableMask()]), layers: [ new TileLayer({ source: new OSM(), }), ], view: new View({ center: fromLonLat([37.41, 8.82]), zoom: 4, }), });
let mapWindow; function closeMapWindow() { if (mapWindow) { mapWindow.close(); mapWindow = undefined; } }
window.addEventListener('pagehide', closeMapWindow);
const button = document.getElementById('external-map-button');
function resetMapTarget() { localMapTarget.style.height = ''; map.setTarget(localMapTarget); button.disabled = false; }
function updateOverlay() { if (!mapWindow) { return; } const externalMapTarget = mapWindow.document.getElementById('map'); if (!externalMapTarget) { return; } if (document.visibilityState === 'visible') { externalMapTarget.classList.remove('unusable'); externalMapTarget.setAttribute('tabindex', '0'); externalMapTarget.focus(); } else { externalMapTarget.removeAttribute('tabindex'); externalMapTarget.classList.add('unusable'); } } window.addEventListener('visibilitychange', updateOverlay);
button.addEventListener('click', function () { const blockerNotice = document.getElementById('blocker-notice'); blockerNotice.setAttribute('hidden', 'hidden'); button.disabled = true;
let timeoutKey = setTimeout(function () { closeMapWindow(); resetMapTarget(); blockerNotice.removeAttribute('hidden'); timeoutKey = undefined; }, 3000);
mapWindow = window.open( 'resources/external-map-map.html', 'MapWindow', 'toolbar=0,location=0,menubar=0,width=800,height=600' ); mapWindow.addEventListener('DOMContentLoaded', function () { const externalMapTarget = mapWindow.document.getElementById('map'); localMapTarget.style.height = '0px'; map.setTarget(externalMapTarget);
if (timeoutKey) { timeoutKey = clearTimeout(timeoutKey); } mapWindow.addEventListener('pagehide', function () { resetMapTarget(); closeMapWindow(); });
updateOverlay(); }); });
|