forked from ehmorris/lunar-lander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructions.js
93 lines (82 loc) · 2.52 KB
/
instructions.js
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
export const manageInstructions = (onCloseInstructions) => {
let _engineDone = false;
let _leftRotationDone = false;
let _rightRotationDone = false;
let _engineAndRotationDone = false;
let _hasClosedInstructionsVar = (() => {
try {
return localStorage.getItem("closedInstructions");
} catch {
return false;
}
})();
// Using an approximation. controls.getHasKeyboard() can't be used here
// because the user is unlikely to have used the keyboard when the page has
// just loaded and is showing instructions
const likelyTouchDevice = window.matchMedia("(any-pointer: coarse)").matches;
const show = () => {
document.querySelector("#instructions").classList.add("show");
if (likelyTouchDevice) {
document.querySelector("#forKeyboard").remove();
} else {
document.querySelector("#forTouch").remove();
}
};
function close() {
// May be called twice in checkDone() on devices with both touch support
// and a keyboard
if (!_hasClosedInstructionsVar) {
document.querySelector("#instructions").classList.remove("show");
try {
localStorage.setItem("closedInstructions", true);
} catch {}
_hasClosedInstructionsVar = true;
onCloseInstructions();
}
}
const checkDone = () => {
if (
_engineDone &&
_leftRotationDone &&
_rightRotationDone &&
_engineAndRotationDone
) {
const closeTimeout = () => setTimeout(close, 1000);
const options = { once: true };
document.addEventListener("touchend", closeTimeout, options);
document.addEventListener("keyup", closeTimeout, options);
}
};
const setEngineDone = () => {
_engineDone = true;
document.querySelector("#engineCheck").classList.add("strikethrough");
checkDone();
};
const setLeftRotationDone = () => {
_leftRotationDone = true;
document
.querySelector("#rightRotationCheck")
.classList.add("strikethrough");
checkDone();
};
const setRightRotationDone = () => {
_rightRotationDone = true;
document.querySelector("#leftRotationCheck").classList.add("strikethrough");
checkDone();
};
const setEngineAndRotationDone = () => {
_engineAndRotationDone = true;
document
.querySelector("#engineAndRotationCheck")
.classList.add("strikethrough");
checkDone();
};
return {
show,
hasClosedInstructions: () => _hasClosedInstructionsVar,
setEngineDone,
setLeftRotationDone,
setRightRotationDone,
setEngineAndRotationDone,
};
};