/* OPENING AND CLOSING THE ARCADE * * The games never run inside the page. They take the whole screen, swallow * every touch and stop the page scrolling, so one running inline would hijack * whatever it was sitting in. They run in a pop-up, in an iframe, and the * iframe's src is only set when somebody actually asks — nothing downloads * for a visitor who never presses play. * * Three ways out, because a child on a phone will find only one of them: * the big X, the Escape key, and a tap on the dark outside. The game also has * its own X, which posts a message back here. */ (function () { 'use strict'; var modal = document.getElementById('gameModal'); var frame = document.getElementById('gameFrame'); if (!modal || !frame) return; var lastFocus = null; function open(slug) { lastFocus = document.activeElement; // ?embed=1 tells the arcade it is inside a pop-up, so its own close button // reaches out to us instead of going back to its title screen var src = '/play/index.html?embed=1' + (slug ? '&game=' + encodeURIComponent(slug) : ''); frame.src = src; modal.hidden = false; document.body.classList.add('gmodal-open'); var x = modal.querySelector('.gmodal__x'); if (x) x.focus(); } function close() { if (modal.hidden) return; modal.hidden = true; document.body.classList.remove('gmodal-open'); /* Blanking the src is what actually stops the game: an iframe left loaded keeps its audio going and keeps burning a frame loop behind the page. */ frame.src = 'about:blank'; if (lastFocus && lastFocus.focus) lastFocus.focus(); } document.addEventListener('click', function (e) { var play = e.target.closest('[data-play]'); if (play) { e.preventDefault(); open(play.getAttribute('data-play')); return; } if (e.target.closest('[data-gclose]')) { e.preventDefault(); close(); } }); document.addEventListener('keydown', function (e) { if (e.key === 'Escape' && !modal.hidden) close(); }); /* the arcade's own X, from inside the frame */ window.addEventListener('message', function (e) { if (e.source !== frame.contentWindow) return; // only our own game if (e.data && e.data.neuon === 'close') close(); }); /* Keep the tab focus inside the pop-up while it is open, or Tab walks off into the page underneath and the X stops being reachable. */ modal.addEventListener('keydown', function (e) { if (e.key !== 'Tab') return; var x = modal.querySelector('.gmodal__x'); if (x) { e.preventDefault(); x.focus(); } }); })();