forked from SheepTester/hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelements.html
More file actions
211 lines (204 loc) · 7.27 KB
/
elements.html
File metadata and controls
211 lines (204 loc) · 7.27 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>
<html>
<head>
<title>OC's Basement</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="description" content="Element name to atomic symbol practice!"/>
<meta property="og:title" content="Elements"/>
<meta property="og:description" content="One way to remember the atomic symbols and elements."/>
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="../sheep2.css">
<script src="../sheep2.js" charset="utf-8"></script>
<style>
#loading-msg {
display: none;
}
.loading #loading-msg {
display: flex;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
justify-content: center;
align-items: center;
color: white;
background-color: #202225;
font-size: 5vh;
}
#answer {
display: none;
}
.show-answer #show-answer {
display: none;
}
.show-answer #answer {
display: block;
}
body {
font-family: 'Open Sans', sans-serif;
color: #dcddde;
background-color: #36393F;
}
hr {
border: none;
border-top: 1px solid rgba(255, 255, 255, 0.04);
}
a {
color: #0096cf;
}
table {
border-collapse: collapse;
}
th, td {
border: 1px solid rgba(255, 255, 255, 0.04);
padding: 5px 10px;
}
body:not(.flipped) #elem {
font-family: serif;
}
.flipped #answer {
font-family: serif;
}
</style>
</head>
<body class="loading">
<span id="loading-msg">Fetching element data</span>
<p>Status: <strong id="status"></strong></p>
<p>What is <strong id="elem"></strong>?</p>
<p><button id="show-answer">reveal answer</button><strong id="answer"></strong></p>
<p><button id="new">I was right</button> <button id="wrong">I was wrong</button></p>
<hr>
<ul>
<li><a href="?range=1-18&mode=name">Elements 1–18</a> (<a href="?range=1-18&mode=symbol">by name</a>)</li>
<li><a href="?range=1-36&mode=name">Elements 1–36</a> (<a href="?range=1-36&mode=symbol">by name</a>)</li>
<li><a href="?range=1-38,47,50,53-56&mode=name">Most elements 1–56</a> (<a href="?range=1-38,47,50,53-56&mode=symbol">by name</a>)</li>
<li><a href="?range=1-38,47,50,53-56,78-80,82,86,92,94&mode=name">Elements you should know</a> (<a href="?range=1-38,47,50,53-56,78-80,82,86,92,94&mode=symbol">by name</a>)</li>
<li><a href="?range=1-38,46-56,78-98&mode=name" id="oc-fetish">OC's fetishes</a> (<a href="?range=1-38,46-56,78-98&mode=symbol">by name</a>)</li>
<li><a href="?range=*&mode=name">All elements</a> (<a href="?range=*&mode=symbol">by name</a>)</li>
</ul>
<h3>Keyboard shortcuts</h3>
<table>
<tr><th>space</th><td>reveal answer</td></tr>
<tr><th>F</th><td>I was right/next element</td></tr>
<tr><th>J</th><td>I was wrong/save for later</td></tr>
<tr id="oc-keyboard-shortcut"><th>1</th><td>OC is near me!</td></tr>
</table>
<button id="hide-oc">OC is near me!</button>
<script>
function getParams(elementCount) {
const params = {};
const paramString = window.location.search.slice(1);
if (paramString) {
paramString.split('&').forEach(param => {
if (param.includes('=')) {
params[param.slice(0, param.indexOf('='))] = param.slice(param.indexOf('=') + 1);
}
});
}
const range = [];
params.range = params.range === '*' || !params.range ? '1-' + elementCount : params.range;
params.range.split(',').forEach(r => {
if (r.includes('-')) {
const end = +r.slice(r.indexOf('-') + 1);
for (let i = +r.slice(0, r.indexOf('-')); i <= end; i++) {
range.push(i);
}
} else {
range.push(+r);
}
});
return {
showNameMode: params.mode === 'symbol',
elementRange: range,
hideOC: params.safe === 'true'
};
}
function shuffle(arr) {
// https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
let currentIndex = arr.length;
while (0 !== currentIndex) {
const randomIndex = Math.floor(Math.random() * currentIndex--);
const temporaryValue = arr[currentIndex];
arr[currentIndex] = arr[randomIndex];
arr[randomIndex] = temporaryValue;
}
return arr;
}
fetch('elements.json') // https://sheeptester.github.io/hello-world/elements.json
.then(res => res.json())
.then(elements => {
document.body.classList.remove('loading');
const {showNameMode, elementRange, hideOC} = getParams(elements.length);
elements = elements.filter(({number}) => elementRange.includes(number));
if (showNameMode) document.body.classList.add('flipped');
const question = document.getElementById('elem');
const answer = document.getElementById('answer');
const answerShower = document.getElementById('show-answer');
const newElem = document.getElementById('new');
const statusIndicator = document.getElementById('status');
const wasWrong = document.getElementById('wrong');
const OCHider = document.getElementById('hide-oc');
answerShower.addEventListener('click', () => {
document.body.classList.add('show-answer');
newElem.textContent = 'I was right';
wasWrong.textContent = 'I was wrong';
});
let queue = [], queueLength = 0, wrongList = [], currentElem = null,
status = 'all';
function genQueue(src) {
queue = shuffle(JSON.parse(JSON.stringify(src)));
queueLength = queue.length;
if (src === wrongList) {
wrongList = [];
status = 'wrong';
} else {
status = 'all';
}
}
function generate() {
currentElem = queue.pop() || (wrongList.length ? genQueue(wrongList) : genQueue(elements), queue.pop());
question.textContent = showNameMode ? currentElem.name : currentElem.symbol;
answer.textContent = showNameMode ? currentElem.symbol : currentElem.name;
newElem.textContent = 'next element';
wasWrong.textContent = 'save for later';
document.body.classList.remove('show-answer');
statusIndicator.innerHTML = (status === 'all' ? 'All selected elements' + (wrongList.length ? ' — ' + wrongList.length + ' marked wrong' : '') : 'Reviewing mistakes') + ' (' + (queueLength - queue.length) + '/' + queueLength + ')';
}
newElem.addEventListener('click', generate);
wasWrong.addEventListener('click', () => {
wrongList.push(currentElem);
generate();
});
generate();
document.addEventListener('keydown', e => {
switch (e.keyCode) {
case 32:
answerShower.click();
e.preventDefault(); // prevent pressing focused button
break;
case 70:
newElem.click();
break;
case 74:
wasWrong.click();
break;
case 49:
if (deOCify) deOCify();
break;
}
});
function deOCify() {
OCHider.remove();
document.getElementById('oc-keyboard-shortcut').remove();
document.getElementById('oc-fetish').textContent = 'A few more everyday elements';
document.title = 'Elements';
deOCify = null;
}
OCHider.addEventListener('click', deOCify);
if (hideOC) deOCify();
});
</script>
</body>
</html>