vertiefungs_scraper/main.js

111 lines
3 KiB
JavaScript
Raw Permalink Normal View History

2020-10-09 16:23:00 +02:00
function getEvents() {
return new Promise((resolve, reject) => {
$.getJSON("events.json", function(data) {
resolve(data);
});
});
}
db = new PouchDB("main");
subjects = {};
function setUpForm() {
for(let vert in subjects) {
$('#verts').append(`
<h4>${vert}</h4>
`);
2020-10-09 16:43:56 +02:00
2020-10-09 16:23:00 +02:00
for(let sub of subjects[vert].keys()) {
$('#verts').append(`
<label for="${sub}">${sub}</label>
<input class="subj" type="checkbox" id="${sub}" name="${sub}" value="${sub}">
<br>
`);
}
$('#verts').append(`<hr>`);
}
$('#settings').on('click', '*', e => {
2020-10-13 11:24:21 +02:00
renderTable('ugw');
renderTable('gw');
2020-10-09 16:23:00 +02:00
});
}
2020-10-13 11:24:21 +02:00
async function renderTable(week) {
let tbl = $(`#table-${week}`);
let checked_subj = new Set();
2020-10-09 16:23:00 +02:00
for(let subj of $('.subj'))
if($(subj).prop('checked'))
checked_subj.add(subj.value);
2020-10-13 11:24:21 +02:00
let types = $('#type').val();
2020-10-09 16:25:59 +02:00
if(types.length === 0)
types = ['tut', 'lect'];
2020-10-13 11:24:21 +02:00
let allsub = [];
2020-10-09 16:23:00 +02:00
for(let time of Array.from({length: 7}, (x, i) => i + 1)) {
allsub.push(db.find({
sort: ["day"],
selector:
{name: {$in:Array.from(checked_subj)},day: {$exists: true}, time: time, week: {$in:[week, 'both']}, type: {$in: types}}}));
}
2020-10-13 11:24:21 +02:00
let subs = await Promise.all(allsub);
2020-10-09 16:23:00 +02:00
let content = `<table><tr><th>DS</th>`;
for(let day of ["Mo", "Di", "Mi", "Do", "Fr"])
content += `<th>${day}</th>`;
content += `</tr>`;
for(let time in subs) {
content += `<tr><td>${parseInt(time) + 1}`;
2020-10-13 11:24:21 +02:00
let last_day = 0;
let weekday_subs = Array.from({length: 5}, (x, i) => []);
2020-10-09 16:23:00 +02:00
for(let sub of subs[time].docs) {
weekday_subs[sub.day].push(sub);
}
for (let day of weekday_subs) {
content += `<td class="${day.length > 1 ? "red" : ""}">`;
for(let sub of day) {
content += sub.name;
if(sub.type === "tut")
content += "(U)";
content += ",<br>";
}
content += `</td>`;
}
content += `</tr>`;
}
tbl.html(content);
}
$(document).ready(() => {
2020-10-09 16:43:56 +02:00
db.allDocs({include_docs: true}).then(({rows}) => {
return Promise.all(rows.map(row => db.remove(row.doc)));
}).then(() =>
getEvents()).then(events => {
2020-10-09 16:23:00 +02:00
allput=[];
for(let event of events) {
if(!subjects[event.vert_name])
subjects[event.vert_name] = new Set();
subjects[event.vert_name].add(event.name);
allput.push(db.put({_id: event.name + event.time.toString() + event.day.toString() + event.week,
...event}).catch(() => true));
}
return Promise.all(allput);
}).then(() => {
return db.createIndex({
index: {fields: ['day']}
});
}).then(() =>{
setUpForm();
});
});