-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdist.js
76 lines (67 loc) · 2.79 KB
/
dist.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
let sections = [];
function getGroup(name) {
const index = name.lastIndexOf('[');
if (index === -1 || !name.endsWith(']')) return null;
return name.substring(index + 1, name.length - 1);
}
fetch(`/api/v1/courses/${ENV.course_id}/sections`).then(c => c.json()).then(c => {
sections = c;
const sectionFilter = $(`<select>
<option>Show all sections</option>
${sections.map(s => `<option value=${s.id}>${s.name}</option>`)}
</select>`)[0];
$('div.header-bar').append(sectionFilter);
function updateModules() {
const modules = document.querySelectorAll('div.context_module');
for (const mod of modules) {
const name = mod.attributes["aria-label"].value;
if (!name) continue;
const group = getGroup(name);
mod.style.display = sectionFilter.selectedIndex === 0
|| !group
|| group === sections[sectionFilter.selectedIndex - 1].name
? 'block' : 'none';
}
}
sectionFilter.addEventListener('change', updateModules);
fetch(`/api/v1/courses/${ENV.course_id}/enrollments?user_id=${ENV.current_user_id}`)
.then(c => c.json())
.then(c => {
const secId = c[0].course_section_id;
const index = sections.findIndex(s => s.id === secId);
if (index >= 0) {
sectionFilter.selectedIndex = index + 1;
updateModules();
}
});
});
let sectionSelect = null;
const links = document.querySelectorAll('.edit_module_link,.add_module_link');
for (const link of links) {
link.addEventListener('click', () => {
const nameBox = document.querySelector('#context_module_name');
if (!sectionSelect) {
sectionSelect = $(`<select>
<option>All sections</option>
${sections.map(s => `<option value=${s.id}>${s.name}</option>`)}
</select>`)[0];
$('div.module_name')
.append(`<div style='margin-top: 10px'><label>Section:</label> </div>`)
.append(sectionSelect);
sectionSelect.addEventListener('change', () => {
const part = getGroup(nameBox.value) ? nameBox.value.substring(0, nameBox.value.lastIndexOf('[')).trim() : nameBox.value;
nameBox.value = sectionSelect.selectedIndex === 0 ? part : `${part} [${sections[sectionSelect.selectedIndex - 1].name}]`;
});
}
setTimeout(() => {
sectionSelect.selectedIndex = 0;
const group = getGroup(nameBox.value);
if (group) {
const section = sections.findIndex(z => z.name === group);
if (section >= 0) {
sectionSelect.selectedIndex = section + 1;
}
}
}, 10);
})
}