-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
235 lines (187 loc) · 6.56 KB
/
script.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
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
// DOM elements
const mainContainer = document.querySelector('.main');
const startLogger = document.getElementById('start-logger');
const loadLogs = document.getElementById('load-logs');
const logsContainer = document.querySelector('.logs');
const stopLogger = document.getElementById('stop-logger');
const clearLogger = document.getElementById('clear-logger');
const backToMain = document.getElementById('back-to-main');
const logsModal = document.getElementById('logs-modal');
const logsSelect = document.getElementById('logs-select');
const closeModalButton = document.getElementById('close-modal');
const loadLog = document.getElementById('load-log');
const loadError = document.getElementById('load-error');
const scrollUpButton = document.getElementById('scroll-up');
// Logger variables
let isLoggingEnabled = false;
let logsDate = null;
let isLoadError = false;
Edrys.onReady(() => {
console.log('Code Logger is ready!');
});
let studentsSubmissions = []; // To store the code submissions
// Listen for code submissions
Edrys.onMessage(({ from, subject, body }) => {
if (subject === 'execute' && isLoggingEnabled) {
const newEntry = {
From: from,
Date: new Date().toLocaleString(),
Code: body
};
studentsSubmissions.push(newEntry);
renderSubmissions();
storeLogInIndexedDB();
}
}, (promiscuous = true));
// Display the code submissions
function renderSubmissions() {
const submissionsContainer = document.querySelector('.students-submissions');
submissionsContainer.innerHTML = '';
studentsSubmissions.forEach((submission) => {
const submissionElement = document.createElement('div');
submissionElement.classList.add('submission-wrapper');
submissionElement.innerHTML = `
<div class="student-info">
<div>
<p>From: <span>${submission.From}</span></p>
</div>
<div>
<p>Date: <span>${submission.Date}</span></p>
</div>
</div>
<div class="student-code">
<p>Code:</p>
<pre><code class="language-cpp">${escapeHTML(submission.Code)}</code></pre>
</div>
`;
submissionsContainer.appendChild(submissionElement);
});
// Highlight the code
Prism.highlightAll();
};
// Escape HTML characters
function escapeHTML(code) {
return code
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
};
// Logger Main Buttons Handlers
startLogger.onclick = () => {
isLoggingEnabled = true;
logsDate = new Date().toLocaleString();
stopLogger.disabled = false;
clearLogger.disabled = true;
backToMain.disabled = true;
mainContainer.classList.add('hidden');
logsContainer.classList.remove('hidden');
}
stopLogger.onclick = () => {
isLoggingEnabled = false;
clearLogger.disabled = false;
backToMain.disabled = false;
};
clearLogger.onclick = () => {
studentsSubmissions = [];
renderSubmissions();
};
backToMain.onclick = () => {
studentsSubmissions = [];
renderSubmissions();
mainContainer.classList.remove('hidden');
logsContainer.classList.add('hidden');
};
// Store log in IndexedDB
function storeLogInIndexedDB() {
const username = Edrys.username.split('_')[0];
const stationName = Edrys.liveUser.room;
db.logs.put({ id: `User: ${username}_${stationName}_Date: ${logsDate}`, studentsSubmissions: studentsSubmissions });
};
// Load logs from IndexedDB for the select field
function loadLogsIntoModal() {
const username = Edrys.username;
// Create the unique prefix for the current user
const currentUserPrefix = `User: ${username}`.split('_')[0];
db.logs.toArray().then((allLogs) => {
logsSelect.innerHTML = '<option value="" disabled selected>Choose a log</option>';
const userLogs = allLogs.filter(log => log.id.startsWith(currentUserPrefix));
userLogs.forEach((log, index) => {
const option = document.createElement('option');
// Extract station and date from the id
const [_, stationAndDate] = log.id.split(`${currentUserPrefix}_`);
option.value = log.id;
option.textContent = stationAndDate;
logsSelect.appendChild(option);
});
if (userLogs.length === 0) {
const noLogsOption = document.createElement('option');
noLogsOption.textContent = "No logs available for this user.";
noLogsOption.disabled = true;
logsSelect.appendChild(noLogsOption);
}
});
};
// Load log from IndexedDB
function loadLogFromDB() {
const selectedLogId = logsSelect.value;
// Check if a log is selected
if (!selectedLogId) {
isLoadError = true;
loadError.textContent = "Please select a log.";
return;
}
// Fetch the log from IndexedDB
db.logs.get(selectedLogId)
.then((log) => {
if (!log) {
isLoadError = true;
loadError.textContent = "Log not found.";
return;
}
// Update submissions array and render
studentsSubmissions = log.studentsSubmissions;
renderSubmissions();
// No errors, hide modal and show logs container
isLoadError = false;
logsModal.classList.add('hidden');
mainContainer.classList.add('hidden');
logsContainer.classList.remove('hidden');
loadError.textContent = "";
stopLogger.disabled = true;
})
.catch((error) => {
isLoadError = true;
loadError.textContent = "An error occurred while loading the log.";
console.error("Error loading log:", error);
});
};
// Load Modal Handlers
loadLogs.onclick = () => {
loadLogsIntoModal();
logsModal.classList.remove('hidden');
};
closeModalButton.onclick = () => {
loadError.textContent = "";
logsModal.classList.add('hidden');
};
loadLog.onclick = () => {
loadLogFromDB();
};
// Handle scroll up
window.onscroll = () => {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
scrollUpButton.style.display = 'block';
} else {
scrollUpButton.style.display = 'none';
}
};
scrollUpButton.onclick = () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
};
// Database implementation
var db = new Dexie("CodeLogger");
db.version(1).stores({
logs: 'id'
});