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
|
import { GraduationChange, fetchGraduation, submitGraduationChange } from "./graduation-ENV.ts";
import { fetchStudent, fetchStudents } from "./student-ENV.ts";
import { getName, DATETIME_FMT } from "./utils-ENV.js";
import { initSelection } from "./selection.js";
let name;
let student;
async function loadStudent(studentToLoad) {
student = studentToLoad;
await fetchAndUpdateStudentInfo();
// updateStudentCalendar();
// updateStudentProgress();
updateStudentGraduation();
}
function initStudentSelection() {
initSelection(
"student-selection",
fetchStudents,
(student) => `${student.name} ${student.getGroup()}`,
loadStudent,
);
}
function pointClickHandler(color) {
const textareaDiv = document.getElementById("student-info-point-reason-container");
const textarea = document.getElementById("student-info-point-reason");
const submitBtn = document.getElementById("student-info-point-reason-submit");
// TODO(muhq): Prefill the textarea with common reasons
switch (color) {
case "green":
textarea.style.background = "rgb(from green r g b / 20%)";
textarea.value = "";
break;
case "orange":
textarea.style.background = "rgb(from orange r g b / 20%)";
textarea.value = "";
break;
case "red":
textarea.style.background = "rgb(from red r g b / 20%)";
textarea.value = "";
break;
}
textareaDiv.style.display = "block";
textarea.focus();
const coach = name;
const submit = function () {
let change;
switch (color) {
case "green":
change = GraduationChange.newGreenPointChange(textarea.value, coach);
break;
case "orange":
change = GraduationChange.newOrangePointChange(textarea.value, coach);
break;
case "red":
change = GraduationChange.newRedPointChange(textarea.value, coach);
break;
default:
throw new Error("Invalid color");
}
submitGraduationChange(student, change);
textareaDiv.style.display = "none";
updateStudentInfo();
student.fetchGraduation().then(() => updateStudentGraduation());
};
// Use onEVENT to override any previous handler
textarea.onkeydown = function (e) {
if (e.shiftKey && e.key === "Enter") {
submit();
}
if (e.key === "Escape") {
textareaDiv.style.display = "none";
}
};
submitBtn.onclick = submit;
}
async function fetchAndUpdateStudentInfo() {
student.graduation = await fetchGraduation(student);
updateStudentInfo();
}
let DEFAULT_STUDENT_ICON;
function updateStudentInfo() {
// Reset the student icon
document.getElementById("student-info-icon").src = DEFAULT_STUDENT_ICON;
// Try to replace the generic icon with the user's icon
var img = document.createElement("img");
img.onload = function () {
document.getElementById("student-info-icon").src = img.src;
img = img.onload = null;
};
student.getIconUrl().then((url) => (img.src = url));
document.getElementById("student-info-name").innerHTML = `${student.name}`;
if (student.graduation) {
const tag = document.getElementById("student-info-tag");
tag.innerHTML = student.graduation.getLevelName();
tag.style.display = "block";
document.getElementById("student-info-points-green").innerHTML = student.graduation.green;
document.getElementById("student-info-points-orange").innerHTML = student.graduation.orange;
document.getElementById("student-info-points-red").innerHTML = student.graduation.red;
document.getElementById("student-info-points-sum").innerHTML =
student.graduation.getNettoPoints();
document
.getElementById("student-info-green")
.addEventListener("click", () => pointClickHandler("green"));
document
.getElementById("student-info-orange")
.addEventListener("click", () => pointClickHandler("orange"));
document
.getElementById("student-info-red")
.addEventListener("click", () => pointClickHandler("red"));
const icon = document.getElementById("student-level-icon");
icon.src = student.graduation.getIcon();
}
document.getElementById("student-content").style.display = "block";
}
async function updateStudentGraduation() {
console.assert(student.graduation, "Student has no graduation");
const graduation = student.graduation;
const levelChangeLvl = document.getElementById("graduation-level-change-level");
levelChangeLvl.value = Math.min(graduation.level + 1, 4);
const levelChangeBtn = document.getElementById("graduation-level-change-submit");
levelChangeBtn.onclick = () => {
const change = GraduationChange.newLevelChange(levelChangeLvl.value);
submitGraduationChange(student, change);
updateStudentInfo();
updateStudentGraduation();
};
const changesTbl = document.getElementById("graduation-changes");
changesTbl.innerHTML = "";
for (const change of graduation.changes) {
const changeTr = document.createElement("tr");
switch (change.change) {
case "Grün":
changeTr.style.background = "green";
break;
case "Rot":
changeTr.style.background = "red";
break;
case "Orange":
changeTr.style.background = "orange";
break;
}
const addTd = (s) => {
const changeTd = document.createElement("td");
changeTd.innerHTML = s;
changeTr.appendChild(changeTd);
};
addTd(change.change);
addTd(DATETIME_FMT.format(change.date));
addTd(change.reason);
addTd(change.coach);
addTd(JSON.stringify(change.context));
changesTbl.appendChild(changeTr);
}
}
async function initCockpit() {
initStudentSelection();
const params = new URLSearchParams(location.search);
const userId = params.get("userToLoad");
if (userId) {
student = await fetchStudent(userId);
await loadStudent(student);
}
}
window.onload = function () {
DEFAULT_STUDENT_ICON = document.getElementById("student-info-icon").src;
name = getName();
initCockpit();
};
|