-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
206 lines (202 loc) · 6.36 KB
/
index.html
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
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width">
<title>SDGs GOALQUIZ / ゴールクイズ</title>
<meta property="og:title" content="SDGs GOALQUIZ / ゴールクイズ">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://code4sabae.github.io/goalquiz/goalquiz.png">
<meta property="og:image" content="https://code4sabae.github.io/goalquiz/goalquiz.png">
<style>
body {
text-align: center;
margin: 0;
padding: 0;
font-family: sans-serif;
}
h1 {
margin: 0;
padding: .8em;
background-color: #f1f1f1;
margin-bottom: 1vw;
}
.result {
font-size: 3vw;
height: 2em;
}
.quiz {
display: inline-block;
text-align: left;
padding: 2vw;
font-size: 4vw;
margin: 0;
min-height: 16em;
box-sizing: border-box;
border: .3vw solid gray;
width: 93vw;
}
.logos {
display: inline-block;
text-align: left;
margin-top: 1vw;
width: 93vw;
line-height: 0;
}
img {
width: 15vw;
margin: .2vw .2vw;
}
a {
color: gray !important;
}
@media (min-aspect-ratio: 1/1) {
.result {
font-size: 3vh;
}
.quiz {
font-size: 3vh;
min-height: 12em;
}
img {
width: 9.8vw;
}
}
</style>
<script type="module">
import { CSV } from "https://code4sabae.github.io/js/CSV.js";
import { shuffle } from "https://code4sabae.github.io/js/shuffle.js";
import { sleep } from "https://code4sabae.github.io/js/sleep.js";
import {} from "https://code4sabae.github.io/leaflet-mjs/qr-code.mjs";
window.onload = async () => {
const baseurl = "https://code4sabae.github.io/SDGs_ja/";
const goals = CSV.toJSON(await CSV.fetch(baseurl + "SDG-goals.csv"));
const targets = CSV.toJSON(await CSV.fetch(baseurl + "SDG-targets.csv"));
const body = document.body;
add(body, "h1", "SDGs " + lang("ゴールクイズ", "GOALQUIZ"));
const result = add(body, "div");
result.className = "result";
const quiz = add(body, "div");
quiz.className = "quiz";
const logos = add(body, "div");
logos.className = "logos";
const sels = [];
for (const goal of goals) {
const img = new Image();
img.src = baseurl + goal[lang("logo_ja", "logo")];
logos.appendChild(img);
sels.push(img);
}
const credit = add(body, "div");
credit.className = "credit";
add(credit, "span", "App: CC BY @taisukef", "https://fukuno.jig.jp/3146");
add(credit, "span", " ");
add(credit, "span", "(src on GitHub)", "https://github.com/code4sabae/goalquiz");
add(credit, "br");
add(credit, "div", "Data: SDGs_ja on GitHub", "https://github.com/code4sabae/SDGs_ja");
add(credit, "span", "Japanese", "#ja");
add(credit, "span", " / ");
add(credit, "span", "English", "#en");
add(body, "qr-code");
shuffle(targets);
let point = 0;
for (let i = 0; i < targets.length; i++) {
quiz.innerHTML = " ";
quiz.style.backgroundColor = "white";
quiz.style.color = "black";
const target = targets[i];
const goal = goals[target.goal - 1];
const desc = lang("description_ja", "description");
const title = lang("title_ja", "title");
const tid = startShowText(quiz, target[desc], lang(50, 25));
const ans = await waitClick(sels) + 1;
clearInterval(tid);
quiz.textContent = target[desc];
quiz.innerHTML += "<br>" + lang("あなたの回答", "Your Choice") + ":" + ans + " 「" + goals[ans - 1][title] + "」";
await sleep(300);
quiz.style.backgroundColor = goal.color;
quiz.style.color = "white";
if (ans == target.goal) {
point++;
}
result.textContent = lang(
(i + 1) + "問中" + point + "問正解 正解率" + (point / (i + 1) * 100).toFixed(1) + "%",
"Q cnt " + (i + 1) + ", Corrects " + point + ", Rate " + (point / (i + 1) * 100).toFixed(1) + "%"
);
if (ans == target.goal) {
quiz.innerHTML += "<br>" + lang("正解!", "Correct!");
await sleep(1000);
} else {
quiz.innerHTML += "<br>" + lang("まちがい! 正解は ", "Wrong.. Correct answer is ") + target.goal + " 「" + goal[title] + "」";
quiz.innerHTML += "<br>" + lang("正解をタップして次の問題へ", "Go to click the correct answer.");
while (await waitClick(sels) + 1 != target.goal);
}
}
alert(lang("おめでとうございます!全169ターゲットのクイズを終えました!", "Congratulations!! You've finished quiz of 169 targets."))
};
const lang = (ja, en) => {
return getLang() == "ja" ? ja : en;
};
const getLang = () => {
const hash = document.location.hash;
if (hash.indexOf('en') >= 0) {
return "en";
}
if (hash.indexOf('ja') >= 0) {
return "ja";
}
const lang = (window.navigator.languages && window.navigator.languages[0]) || window.navigator.language || window.navigator.userLanguage || window.navigator.browserLanguage;
return lang;
};
const waitClick = async (sels) => {
return new Promise(resolve => {
if (Array.isArray(sels)) {
for (let i = 0; i < sels.length; i++) {
const sel = sels[i];
sel.wcid = i;
sel.onclick = function() {
resolve(this.wcid);
};
}
} else {
sels.onclick = function() {
resolve();
};
}
});
};
const startShowText = (div, text, waitms) => {
let n = 1;
return setInterval(() => {
if (n <= text.length) {
div.textContent = text.substring(0, n);
n++;
}
}, waitms);
};
const add = (parent, tag, txt, link) => {
const ele = (() => {
if (tag.indexOf("-") < 0) {
return document.createElement(tag);
} else {
return new (customElements.get(tag))();
}
})();
if (txt) {
if (link) {
const a = add(ele, "a", txt);
a.href = link;
if (link.startsWith("#")) {
a.onclick = () => {
location.href = a.href;
location.reload();
};
}
} else {
ele.textContent = txt;
}
}
parent.appendChild(ele);
return ele;
};
</script>
</head>
<body>
</body>
</html>