-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
85 lines (72 loc) · 2.31 KB
/
Copy pathscript.js
File metadata and controls
85 lines (72 loc) · 2.31 KB
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
const apps = [
{ name: 'Space Invaders', path: 'games/space-invaders/index.html' },
{ name: 'Vesmírná mise', path: 'games/vesmirna-mise/index.html' },
{ name: 'Racing Game', path: 'games/racing-game/index.html' },
{ name: 'City Forge', path: 'games/city-forge/index.html' }
];
const appList = document.getElementById('app-list');
const modal = document.getElementById('game-modal');
const gameContainer = document.getElementById('game-container');
const closeBtn = document.querySelector('.close');
apps.forEach((app) => {
const li = document.createElement('li');
const button = document.createElement('button');
button.textContent = app.name;
button.addEventListener('click', () => {
openApp(app.path);
});
li.appendChild(button);
appList.appendChild(li);
});
let selectedIndex = 0;
const buttons = appList.querySelectorAll('button');
if (buttons.length > 0) {
buttons[selectedIndex].classList.add('selected');
}
window.addEventListener('message', (event) => {
if (event.data && event.data.action === 'closeModal') {
closeModal();
}
});
window.addEventListener('keydown', (event) => {
if (modal.style.display === 'block') {
if (event.key === 'Escape') {
closeModal();
}
return;
}
if (!buttons.length) {
return;
}
if (event.key === 'ArrowDown') {
event.preventDefault();
buttons[selectedIndex].classList.remove('selected');
selectedIndex = (selectedIndex + 1) % buttons.length;
buttons[selectedIndex].classList.add('selected');
}
if (event.key === 'ArrowUp') {
event.preventDefault();
buttons[selectedIndex].classList.remove('selected');
selectedIndex = (selectedIndex - 1 + buttons.length) % buttons.length;
buttons[selectedIndex].classList.add('selected');
}
if (event.key === 'Enter') {
event.preventDefault();
buttons[selectedIndex].click();
}
});
closeBtn.addEventListener('click', closeModal);
window.addEventListener('click', (event) => {
if (event.target === modal) {
closeModal();
}
});
function openApp(path) {
gameContainer.src = path;
modal.style.display = 'block';
gameContainer.focus();
}
function closeModal() {
modal.style.display = 'none';
gameContainer.src = '';
}