Skip to content

Commit fad4965

Browse files
committed
require content load for function execution
1 parent 61c99aa commit fad4965

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/checker/admin.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var responses = [];
1414
let draggedItem = null;
1515
var formData = new FormData();
1616
var polling = false;
17+
var active = false;
1718

1819
async function init() {
1920
formData = new FormData();
@@ -108,6 +109,8 @@ async function init() {
108109
if (document.querySelector('.responses.section')) {
109110
document.getElementById("sort-course-input").value = courses.find(c => c.id == String(responses.sort((a, b) => String(a.seatCode)[0] - String(b.seatCode)[0])[0].seatCode)[0]).id;
110111
updateResponses();
112+
} else {
113+
active = true;
111114
}
112115
})
113116
.catch((e) => {
@@ -157,28 +160,34 @@ if (document.querySelector('[data-select]')) document.querySelector('[data-delet
157160
if (document.querySelector('[data-polling]')) document.querySelector('[data-polling]').addEventListener("click", togglePolling);
158161

159162
function toggleSelecting() {
163+
if (!active) return;
160164
if (document.querySelector('.segments .section')) document.querySelector('.segments .section').classList.toggle('selecting');
161165
if (document.querySelector('.questions .section')) document.querySelector('.questions .section').classList.toggle('selecting');
162166
}
163167

164168
function removeSelecting() {
169+
if (!active) return;
165170
if (document.querySelector('.segments .section')) document.querySelector('.segments .section').classList.remove('selecting');
166171
if (document.querySelector('.questions .section')) document.querySelector('.questions .section').classList.remove('selecting');
167172
}
168173

169174
function deleteMultiple() {
175+
if (!active) return;
170176
document.querySelectorAll('.selected').forEach(e => e.remove());
171177
}
172178

173179
function toggleSelected() {
180+
if (!active) return;
174181
this.parentElement.parentElement.classList.toggle('selected');
175182
}
176183

177184
function removeAllSelected() {
185+
if (!active) return;
178186
document.querySelectorAll('.selected').forEach(e => e.classList.remove('.selected'));
179187
}
180188

181189
function togglePolling() {
190+
if (!active) return;
182191
if (polling) {
183192
polling = false;
184193
document.querySelector('[data-polling] .bi-skip-forward-circle-fill').style.display = "block";
@@ -198,12 +207,14 @@ function togglePolling() {
198207
}
199208

200209
function pollingOff() {
210+
if (!active) return;
201211
polling = false;
202212
document.querySelector('[data-polling] .bi-skip-forward-circle-fill').style.display = "block";
203213
document.querySelector('[data-polling] .bi-stop-circle-fill').style.display = "none";
204214
}
205215

206216
function updateSegments() {
217+
if (!active) return;
207218
const course = courses.find(c => c.id == document.getElementById("course-period-input").value);
208219
document.getElementById("course-input").value = course.name;
209220
var c = segments.filter(s => s.course == course.id);
@@ -240,10 +251,12 @@ function updateSegments() {
240251
}
241252

242253
function toggleSegment() {
254+
if (!active) return;
243255
this.parentElement.parentElement.classList.toggle('expanded');
244256
}
245257

246258
function calculateButtonHeights(container) {
259+
if (!active) return;
247260
let totalHeight = 0;
248261
const buttons = container.querySelectorAll('button');
249262
buttons.forEach(button => {
@@ -257,6 +270,7 @@ function calculateButtonHeights(container) {
257270

258271
if (document.getElementById("reorder-courses-button")) {
259272
document.getElementById("reorder-courses-button").addEventListener("click", () => {
273+
if (!active) return;
260274
const current = document.querySelector('.course-selector');
261275
const fromHeight = current?.getBoundingClientRect().height;
262276
document.querySelector('.course-reorder').style.display = 'flex';
@@ -281,6 +295,7 @@ if (document.getElementById("reorder-courses-button")) {
281295
});
282296

283297
document.getElementById("cancel-reorder-courses-button").addEventListener("click", () => {
298+
if (!active) return;
284299
const current = document.querySelector('.course-reorder');
285300
const fromHeight = current?.getBoundingClientRect().height;
286301
document.querySelector('.course-selector').style.display = 'flex';
@@ -305,6 +320,7 @@ if (document.getElementById("reorder-courses-button")) {
305320
});
306321

307322
document.getElementById("save-course-order-button").addEventListener("click", () => {
323+
if (!active) return;
308324
const current = document.querySelector('.course-reorder');
309325
const fromHeight = current?.getBoundingClientRect().height;
310326
document.querySelector('.course-selector').style.display = 'flex';
@@ -330,6 +346,7 @@ if (document.getElementById("reorder-courses-button")) {
330346

331347
// Save Course Order
332348
document.getElementById("save-course-order-button").addEventListener("click", () => {
349+
if (!active) return;
333350
var updatedCourses = [...document.querySelector(".reorder").children].map((course, i) => {
334351
const courseId = course.querySelector('input').id.split('-')[1];
335352
return {
@@ -384,6 +401,7 @@ if (document.getElementById("reorder-courses-button")) {
384401
document.querySelectorAll("#save-button").forEach(w => w.addEventListener("click", save));
385402

386403
async function save(hideResult) {
404+
if (!active) return;
387405
removeAllSelected();
388406
removeSelecting();
389407
var updatedInfo = {};
@@ -461,16 +479,19 @@ async function save(hideResult) {
461479
}
462480

463481
function handleDragStart(e) {
482+
if (!active) return;
464483
draggedItem = this.parentNode;
465484
e.dataTransfer.effectAllowed = 'move';
466485
}
467486

468487
function handleDragOver(e) {
488+
if (!active) return;
469489
e.preventDefault();
470490
e.dataTransfer.dropEffect = 'move';
471491
}
472492

473493
function handleDrop(e) {
494+
if (!active) return;
474495
e.stopPropagation();
475496
const targetItem = this.parentNode;
476497
if (draggedItem !== targetItem) {
@@ -486,6 +507,7 @@ function handleDrop(e) {
486507
}
487508

488509
function addSegment() {
510+
if (!active) return;
489511
var group = document.createElement('div');
490512
group.className = "section";
491513
group.id = 'segment-new';
@@ -507,10 +529,12 @@ function addSegment() {
507529
}
508530

509531
function removeSegment() {
532+
if (!active) return;
510533
this.parentElement.parentElement.remove();
511534
}
512535

513536
function addSegmentQuestion() {
537+
if (!active) return;
514538
var group = document.createElement('div');
515539
group.className = "input-group";
516540
group.innerHTML = `<input type="text" autocomplete="off" id="segment-question-name-input" value="" /><input type="number" autocomplete="off" id="segment-question-id-input" value="" />`;
@@ -520,6 +544,7 @@ function addSegmentQuestion() {
520544
}
521545

522546
function removeSegmentQuestion() {
547+
if (!active) return;
523548
this.parentElement.parentElement.children[this.parentElement.parentElement.children.length - 2].remove();
524549
if (this.parentElement.parentElement.children.length === 3) {
525550
this.parentElement.querySelector('[data-remove-segment-question-input]').disabled = true;
@@ -592,6 +617,7 @@ function updateQuestions() {
592617
}
593618

594619
function addCorrectAnswer() {
620+
if (!active) return;
595621
var input = document.createElement('div');
596622
input.className = "button-grid inputs";
597623
input.innerHTML = `<input type="text" autocomplete="off" id="question-correct-answer-input" value="" /><button data-remove-correct-answer-input square><i class="bi bi-dash"></i></button>`;
@@ -601,6 +627,7 @@ function addCorrectAnswer() {
601627
}
602628

603629
function addIncorrectAnswer() {
630+
if (!active) return;
604631
var input = document.createElement('div');
605632
input.className = "button-grid inputs";
606633
input.innerHTML = `<input type="text" autocomplete="off" id="question-incorrect-answer-input" value="" /><input type="text" autocomplete="off" id="question-incorrect-answer-reason-input" value="" /><button data-remove-incorrect-answer-input square><i class="bi bi-dash"></i></button>`;
@@ -610,14 +637,17 @@ function addIncorrectAnswer() {
610637
}
611638

612639
function removeCorrectAnswer() {
640+
if (!active) return;
613641
this.parentElement.remove();
614642
}
615643

616644
function removeIncorrectAnswer() {
645+
if (!active) return;
617646
this.parentElement.remove();
618647
}
619648

620649
function toggleQuestion() {
650+
if (!active) return;
621651
if (this.parentElement.parentElement.classList.contains('expanded')) {
622652
hideAllQuestions();
623653
} else {
@@ -629,12 +659,14 @@ function toggleQuestion() {
629659
}
630660

631661
function hideAllQuestions() {
662+
if (!active) return;
632663
document.querySelectorAll('.expanded').forEach(q => q.classList.remove('expanded'));
633664
document.querySelector('[data-add-question-input]').style.display = "block";
634665
document.querySelectorAll('#save-button').forEach(w => w.style.display = "block");
635666
}
636667

637668
function addQuestion() {
669+
if (!active) return;
638670
var group = document.createElement('div');
639671
group.className = "section";
640672
group.id = 'question-new';
@@ -652,11 +684,13 @@ function addQuestion() {
652684
}
653685

654686
function removeQuestion() {
687+
if (!active) return;
655688
this.parentElement.parentElement.remove();
656689
hideAllQuestions();
657690
}
658691

659692
async function renderPond() {
693+
if (!active) return;
660694
await save(true);
661695
const url = '/admin/upload.html?question=' + this.id;
662696
const width = 600;
@@ -684,6 +718,7 @@ async function renderPond() {
684718
}
685719

686720
async function removeImage(event) {
721+
if (!active) return;
687722
await save(true);
688723
const element = event.target;
689724
const rect = element.getBoundingClientRect();
@@ -770,9 +805,11 @@ function updateResponses() {
770805
document.querySelectorAll('#mark-incorrect-button').forEach(a => a.addEventListener('click', markIncorrect));
771806
document.querySelectorAll('[data-flag-response]').forEach(a => a.addEventListener('click', flagResponse));
772807
document.querySelectorAll('[data-unflag-response]').forEach(a => a.addEventListener('click', unflagResponse));
808+
active = true;
773809
}
774810

775811
function flagResponse() {
812+
if (!active) return;
776813
fetch(domain + '/flag', {
777814
method: "POST",
778815
headers: {
@@ -795,6 +832,7 @@ function flagResponse() {
795832
}
796833

797834
function unflagResponse() {
835+
if (!active) return;
798836
fetch(domain + '/unflag', {
799837
method: "POST",
800838
headers: {
@@ -817,6 +855,7 @@ function unflagResponse() {
817855
}
818856

819857
function markCorrect() {
858+
if (!active) return;
820859
fetch(domain + '/mark_correct', {
821860
method: "POST",
822861
headers: {
@@ -840,6 +879,7 @@ function markCorrect() {
840879
}
841880

842881
function markIncorrect() {
882+
if (!active) return;
843883
ui.modal({
844884
title: 'Add Reason',
845885
body: '<p>Add a reason that this response is incorrect.</p>',
@@ -867,6 +907,7 @@ function markIncorrect() {
867907
}
868908

869909
function markIncorrectConfirm(reason, e) {
910+
if (!active) return;
870911
fetch(domain + '/mark_incorrect', {
871912
method: "POST",
872913
headers: {

0 commit comments

Comments
 (0)