-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·144 lines (124 loc) · 4.58 KB
/
Copy pathmain.js
File metadata and controls
executable file
·144 lines (124 loc) · 4.58 KB
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
// =============================================================================
// Parallel Scripts
// =============================================================================
import '../../node_modules/babel-polyfill/dist/polyfill';
import getUser from './user';
import getChallenge from './challenge';
import '../../node_modules/@johmun/vue-tags-input/dist/vue-tags-input';
function removeStudent(id, index, level) {
if (window.confirm('Are you sure you want to remove this student?')) {
return fetch('/remove-student', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({id})
}).then((response) => {
if (!response.ok) return alert('Something went wrong. Please try again!');
const table = document.querySelector(`.dashboard-table[data-level='${level}']`);
table.querySelector(`.dashboard-names tr[data-index='${index}']`).remove();
table.querySelector(`.dashboard-data tr[data-index='${index}']`).remove();
});
}
}
function getRowValue(row, i) {
if (i === 0) return -row[0].dataset.index;
return +row[1].childNodes[i - 1].dataset.value;
}
function sortTable(i, level) {
const $table = document.querySelector(`.dashboard-table[data-level="${level}"]`);
const $active = $table.querySelector('.dashboard-active');
if ($active) $active.classList.remove('dashboard-active');
const $labels = $table.querySelectorAll('th');
$labels[i].classList.add('dashboard-active');
const $namesBox = $table.querySelector('.dashboard-names tbody');
const $rowsBox = $table.querySelector('.dashboard-data tbody');
const $names = $namesBox.childNodes;
const rows = Array.from($rowsBox.childNodes).map(($r, i) => [$names[i], $r]);
const ordered = rows.sort((a, b) => getRowValue(b, i) - getRowValue(a, i));
for (let row of ordered) {
$namesBox.appendChild(row[0]);
$rowsBox.appendChild(row[1]);
}
}
function returnLoginLink() {
try {
return 'https://parallel.org.uk/n/login?return=' + btoa(window.location.pathname);
} catch (error) {
return 'https://parallel.org.uk/n/login';
}
}
function initialiseGoogleFormLinks() {
try {
[...document.getElementsByClassName("googleform")].forEach((e) => {
if (!window.PARALLEL_USER_DATA.uid) {
e.outerHTML =
`<p class="googleform-error">Please <a href="${returnLoginLink()}">login</a> to access your Google Form</p>`;
} else {
e.href = e.dataset.href
.replace(
`{{name}}`,
window.PARALLEL_USER_DATA.googleFormName || "First Last",
)
.replace(
`{{email}}`,
window.PARALLEL_USER_DATA.googleFormEmail || "Email",
)
.replace(
`{{filename}}`,
window.PARALLEL_USER_DATA.googleFormFilename || "filename.pdf",
);
}
});
} catch (error) {
console.error('Error initialising Google Form links', error);
}
}
function initialiseGoogleFormSolution() {
try {
[...document.getElementsByClassName("googleformsolution")].forEach((e) => {
if (!window.PARALLEL_USER_DATA.uid) {
e.outerHTML =
`<p class="googleform-error">Please <a href="${returnLoginLink()}">login</a> to access your solutions</p>`;
} if (!window.PARALLEL_USER_DATA.answers.solution) {
e.outerHTML =
`<p class="googleform-error">No solution found</p>`;
} else {
e.href = window.PARALLEL_USER_DATA.answers.solution
}
});
} catch (error) {
console.error('Error initialising Google Form links', error);
}
}
// Disables scroll on number inputs to avoid accidental changes
document.addEventListener("wheel", function(event){
if(document.activeElement.type === "number"){
document.activeElement.blur();
}
});
document.addEventListener('DOMContentLoaded', function() {
firebase.initializeApp({
apiKey: "AIzaSyDk4_ME-Uy1D3Yjg94Af7Gzhg3I1xNYWp8",
projectId: "parallel-beta-31dc4",
authDomain: "parallel-beta-31dc4.firebaseapp.com",
});
const user = getUser();
const challenge = window.PARALLELOGRAM ? getChallenge() : null;
if (challenge) {
initialiseGoogleFormLinks();
initialiseGoogleFormSolution();
}
Vue.component('v-style', {
render: function (createElement) {
return createElement('style', this.$slots.default)
}
});
if (window.self !== window.top) {
document.body.className = 'insideIframe'
}
window.app = new Vue({
el: '#vue',
components: {VueTagsInput: window.vueTagsInput.default},
data: {showSidebar: false, showWelcomeMsg: true, user, c: challenge,
removeStudent, sortTable}
});
});