-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
426 lines (351 loc) · 11.5 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
display: flex;
justify-content: center;
text-align: center;
}
#loading, #authorize, #choose-course, #view-all, #flash-cards { display: none; }
.is-loading #loading { display: block; }
.is-authorizing #authorize { display: block; }
.is-choosing-course #choose-course { display: block; }
.is-playing #flash-cards { display: block; }
.is-viewing-all #view-all { display: block; }
#error {
display: none;
position: absolute;
z-index: 1;
width: 70vw;
max-width: 400px;
left: 50%;
transform: translate(-50%, 0);
top: 0;
background: #ff4f4f;
border-radius: 0 0 5px 5px;
padding: 10px 1.5vw;
text-align: center;
font-weight: bold;
color: white;
transition: opacity 100ms linear;
box-shadow: 0px 1px 5px darkred;
overflow-wrap: break-word;
}
#sign-in {
padding: 15px 30px;
margin: 50px;
text-transform: none;
font-size: 1rem;
font-weight: bold;
}
#flash-cards,
#choose-course { width: 100vw; }
#courses,
#options {
position: relative;
list-style: none;
padding: 0;
}
#student { width: 50%; max-width: 250px; min-width: 150px; }
#choose-course button { font-size: 1rem; padding: 10px 15px; }
#choose-course button,
#options .option { width: 75%; max-width: 500px; min-width: 200px; }
.back { position: absolute; left: 0; padding: 0 10px; cursor: pointer; }
.back:before { content: "⮐"; margin-right: 5px; }
#sign-out { position: absolute; right: 0; padding: 0 10px; cursor: pointer; }
#all-students { cursor: pointer; }
.classroom { display: flex; flex-wrap: wrap; justify-content: center; margin-bottom: 50px; }
.student {
border: 1px solid grey;
border-radius: 10px;
padding: 15px 10px 10px;
margin: 15px;
text-align: center;
width: 200px;
}
.student img { width: 150px; border-radius: 10px; }
.student span { font-weight: bold; display: block; margin-top: 5px; }
@media print {
#view-all .back { display: none; }
#view-all h2 { page-break-before: always; margin: 5px 0; }
.classroom { display: block; }
.student { display: inline-block; page-break-inside: avoid; width: 25vw; margin: 1vw; }
}
button {
text-transform: capitalize;
border: none;
border-radius: 5px;
padding: 10px;
text-decoration: none;
font-size: 1em;
margin: 0 0 10px 0;
color: #fff;
position: relative;
display: inline-block;
cursor: pointer;
text-align: center;
outline: none;
-webkit-transition: background-color 100ms linear;
-ms-transition: background-color 100ms linear;
transition: background-color 100ms linear;
}
button:active {
transform: translate(0, 5px);
-webkit-transform: translate(0, 5px);
box-shadow: 0 1px 0 0;
}
button { background-color: #55acee; box-shadow: 0 5px 0 0 #3C93D5; }
button:hover { background-color: #6FC6FF; }
.option-picked .correct-answer { background-color: #2ecc71; box-shadow: 0 5px 0 0 #15B358; }
.option-picked .correct-answer:hover { background-color: #48E68B; }
.wrong-answer.selected-option { background-color: #e74c3c; box-shadow: 0 5px 0 0 #CE3323; }
.wrong-answer.selected-option:hover { background-color: #FF6656; }
.click-catcher {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: none;
z-index: 1000;
}
.option-picked .click-catcher{ display: block; }
</style>
</head>
<body class="is-loading">
<div id="error"></div>
<div id="loading">
<h1>Loading data...</h1>
</div>
<div id="authorize">
<button id="sign-in">Sign in with Google Classroom</button>
</div>
<div id="choose-course">
<a id="sign-out">Sign out</a>
<h1>Choose a Classroom</h1>
<ul id="courses"></ul>
<a id="all-students">View all students</a>
</div>
<div id="view-all">
<a id="previous-screen" class="back">Back</a>
<div id="classrooms"></div>
</div>
</div>
<div id="flash-cards">
<a id="previous-screen" class="back">Back</a>
<img id="student" src="">
<ul id="options"></ul>
</div>
<script src="https://apis.google.com/js/api.js"></script>
<script>
function authorize(callback) {
const SCOPES = [
'https://www.googleapis.com/auth/classroom.courses.readonly',
'https://www.googleapis.com/auth/classroom.profile.photos',
'https://www.googleapis.com/auth/classroom.rosters.readonly',
].join(" ");
const config = {
'apiKey': 'AIzaSyDhP7cfYme795mDLjqBrS_lpovY4Imc0Oc',
'discoveryDocs': [ "https://www.googleapis.com/discovery/v1/apis/classroom/v1/rest" ],
'clientId': '885544404811-b1a3397hc05c9arjbkupaufg2no1ii8l.apps.googleusercontent.com',
'scope': SCOPES
};
function createClient(auth) {
return {
user() {
return auth.currentUser.get();
},
courses(callback) {
return gapi.client.classroom.courses.list({ pageSize: 100 }).then(
(res) => callback(null, res.result.courses),
(err) => callback(err)
);
},
profiles(courseId, callback) {
let params = { pageSize: 100, courseId: courseId };
let promise = gapi.client.classroom.courses.students.list(params);
return promise.then((res) => {
const students = res.result.students.map(student => {
return {
name: student.profile.name.fullName,
photo: student.profile.photoUrl
};
});
callback && callback(null, students);
return students;
}, (err) => callback && callback(err) );
},
signOut(callback) {
return auth.signOut().then(
(res) => callback(null),
(err) => callback(err)
);
},
};
}
// Load the API's client and auth2 modules.
// Call the initClient function after the modules load.
gapi.load('client:auth2', (err) => {
if(err) { return callback(err); }
gapi.client.init(config).then(() => {
let auth = gapi.auth2.getAuthInstance();
if (auth.isSignedIn.get()) { return callback(null, createClient(auth)); }
document.getElementsByTagName("body")[0].className = "is-authorizing";
document.getElementById("authorize").addEventListener("click", function(e) {
auth.signIn().then((res) => {
let user = auth.currentUser.get();
let isAuthorized = user.hasGrantedScopes(SCOPES);
if (!isAuthorized) { return callback(new Error("This app was not authorized")); }
return callback(null, createClient(auth));
}, (err) => callback(err));
});
}, (err) => callback(err));
});
}
function createGame(profiles, previousScreen) {
let students = profiles.map(p => p.name);
let pool = [];
function renderOptions(options, correctAnswer) {
document.getElementById("options").className = "";
let html = options.map(function(opt) {
let btnClass = (opt === correctAnswer ? "correct-answer" : "wrong-answer");
return `<li><button class="option ${btnClass}">${opt}</button></li>`;
}).join("") + "<div class='click-catcher'/>";
document.getElementById("options").innerHTML = html;
}
function nextQuestion() {
if(pool.length < 1) { pool = students.slice(0); /* clones array */ }
let idx = Math.random() * pool.length | 0;
let student = pool.splice(idx, 1)[0];
document.getElementById("student").src = profiles.find(p => p.name == student).photo;
let options = [ student ];
while(options.length < 5 && options.length < students.length) {
let option = students[Math.random() * students.length | 0];
if(options.indexOf(option) === -1) {
options.splice(Math.random() * (options.length + 1) | 0, 0, option); // Random insert
}
}
renderOptions(options, student);
}
function attachListeners() {
if(createGame.isAttached) { return; }
document.querySelector("#flash-cards .back").addEventListener("click", function(e) {
e.preventDefault();
previousScreen();
});
document.getElementById("options").addEventListener("click", function(e) {
let target = e.target.getElementsByClassName("option")[0] || e.target;
if(target.className.indexOf("option") === -1) { return; }
e.preventDefault();
target.className += " selected-option";
document.getElementById("options").className = "option-picked";
setTimeout(nextQuestion, 1000);
}, false);
createGame.isAttached = true;
}
document.getElementsByTagName("body")[0].className = "is-playing";
nextQuestion();
attachListeners();
return { nextQuestion };
}
function displayCourseChooser(courses, signOut, viewAll, callback) {
function renderCourse() {
document.getElementsByTagName("body")[0].className = "is-choosing-course";
let html = courses.map(course =>
`<li><button class="option" data-course="${course.id}">${course.name}</button></li>`
).join("");
document.getElementById("courses").innerHTML = html;
}
function attachListeners() {
if(displayCourseChooser.isAttached){ return; }
document.getElementById("sign-out").addEventListener("click", function(e) {
e.preventDefault();
signOut();
});
document.getElementById("all-students").addEventListener("click", function(e) {
e.preventDefault();
viewAll();
});
document.getElementById("courses").addEventListener("click", function(e) {
let target = e.target.getElementsByClassName("option")[0] || e.target;
if(target.className.indexOf("option") === -1) { return; }
e.preventDefault();
target.className += " selected-option";
callback(null, target.dataset["course"]);
}, false);
displayCourseChooser.isAttached = true;
}
renderCourse();
attachListeners();
}
function displayAllStudents(classrooms, previousScreen) {
function renderAll() {
document.getElementsByTagName("body")[0].className = "is-viewing-all";
let html = classrooms.map(classroom => {
let students = classroom.students.sort((a, b) => a.name.localeCompare(b.name));
let studentHtml = students.map(profile =>
`<div class="student"><img src="${profile.photo}"><span>${profile.name}</span></div>`
).join("");
return `<h2>${classroom.course.name}</h2><div class="classroom">${studentHtml}</div>`;
}).join("")
document.getElementById("classrooms").innerHTML = html;
}
function attachListeners() {
if(displayAllStudents.isAttached) { return; }
document.querySelector("#view-all .back").addEventListener("click", function(e) {
e.preventDefault();
previousScreen();
});
displayAllStudents.isAttached = true;
}
renderAll();
attachListeners();
}
function showError(where, err) {
let msg = `${where} error: ${err.details || err.message || err.error || err.toString()}`;
document.getElementById("error").textContent = msg;
document.getElementById("error").style.display = "block";
}
document.getElementsByTagName("body")[0].className = "is-loading";
authorize((err, client) => {
if(err) { return showError("Login", err); }
document.getElementsByTagName("body")[0].className = "is-loading";
client.courses((err, courses) => {
if(err) { return showError("Google API", err); }
let signOut = () => {
document.getElementsByTagName("body")[0].className = "is-loading";
client.signOut((err) => {
if(err) { return showError("Sign out", err); }
window.location.reload();
});
};
let viewAll = () => {
document.getElementsByTagName("body")[0].className = "is-loading";
Promise.all(courses.map(course => client.profiles(course.id))).then(
(profiles) => {
let classrooms = courses.map((course, idx) => {
return { course, students: profiles[idx] };
});
displayAllStudents(classrooms, displayScreen);
},
(err) => showError("Google API", err),
);
}
let displayScreen = () => {
displayCourseChooser(courses, signOut, viewAll, (err, courseId) => {
if(err) { return showError("Choose course", err); }
document.getElementsByTagName("body")[0].className = "is-loading";
client.profiles(courseId, (err, profiles) => {
createGame(profiles, displayScreen);
});
});
};
displayScreen();
});
});
</script>
</body>
</html>