Added begining of XHR way of doing stuff (#3)

This commit is contained in:
Thomas Forgione 2017-09-25 15:26:06 +02:00
parent b893d8933c
commit 5e63027352
No known key found for this signature in database
GPG Key ID: C75CD416BD1FFCE1
5 changed files with 82 additions and 28 deletions

View File

@ -1,26 +1,8 @@
extends ../../../templates/base.pug
block content
table.table.table-bordered.table-striped.table-hover
thead
tr
th Course name
th Detected type
th Count in hours
th Count in TD equivalent
tbody
each course in courses
tr
td #{course.name}
td #{course.type}
td #{course.time}
td #{course.tdEquivalent}
tr.table-active
td
strong #{total.name}
td
strong #{total.type}
td
strong #{total.time}
td
strong #{total.tdEquivalent}
#loading Loading... please wait
#result
block extrajs
script(src="/static/js/totalTable.js")

View File

@ -0,0 +1,23 @@
table.table.table-bordered.table-striped.table-hover
thead
tr
th Course name
th Detected type
th Count in hours
th Count in TD equivalent
tbody
each course in courses
tr
td #{course.name}
td #{course.type}
td #{course.time}
td #{course.tdEquivalent}
tr.table-active
td
strong #{total.name}
td
strong #{total.type}
td
strong #{total.time}
td
strong #{total.tdEquivalent}

View File

@ -2,4 +2,5 @@ const url = require('create-url').url;
module.exports = [
url('/total/', 'total', 'total'),
url('/total-table', 'totalTable', 'totalTable'),
]

View File

@ -1,7 +1,10 @@
const cal = require('./calendar');
module.exports.total = function(req, res, render) {
cal.getTotal(req.session.user, (table) => {
function computeUserTable(user, callback) {
let result = {};
cal.getTotal(user, (table) => {
let courses = [];
let total = {
name: "Total",
@ -22,10 +25,22 @@ module.exports.total = function(req, res, render) {
total.tdEquivalent += table[key].tdEquivalent;
}
res.locals.courses = courses;
res.locals.total = total;
result.courses = courses;
result.total = total;
callback(result);
render('total.pug');
});
}
module.exports.total = function(req, res, render) {
render('total.pug');
}
module.exports.totalTable = function(req, res, render) {
computeUserTable(req.session.user, (result) => {
res.locals.courses = result.courses;
res.locals.total = total;
render('totalTable.pug');
});
}

33
static/js/totalTable.js Normal file
View File

@ -0,0 +1,33 @@
let loadingDiv = document.getElementById('loading');
let resultDiv = document.getElementById('result');
const timeout = 30000;
function getTable() {
// Send XHR to totalTable
let xhr = new XMLHttpRequest();
xhr.timeout = timeout;
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
setData(xhr.responseText);
} else {
setError();
}
}
};
xhr.open('GET', '/total-table', true);
xhr.send();
window.xhr = xhr;
}
function setData(text) {
loadingDiv.style.display = "none";
resultDiv.innerHTML = text;
}
function setError() {
loadingDiv.style.display = "none";
resultDiv.innerHTML = "An error occured";
}
getTable();