diff options
author | Noah Loomans <noahloomans@gmail.com> | 2017-01-08 11:48:13 +0100 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2017-01-08 11:48:13 +0100 |
commit | 320c18af3ee9cbeaaae3d4796dd7f15a5ac90889 (patch) | |
tree | d63339d425a2a6a20c883d896b9ed492272d6085 /public/javascripts/schedule.js | |
parent | 4e8da42863406764a659a7337e774ad216d356c9 (diff) | |
parent | dea89e1ec600b302a8db33dd48080b901aee7c7e (diff) |
Merge branch 'master' of github.com:nloomans/rooster-mml
Diffstat (limited to 'public/javascripts/schedule.js')
-rw-r--r-- | public/javascripts/schedule.js | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/public/javascripts/schedule.js b/public/javascripts/schedule.js new file mode 100644 index 0000000..a080ace --- /dev/null +++ b/public/javascripts/schedule.js @@ -0,0 +1,71 @@ +const EventEmitter = require('events') +const leftPad = require('left-pad') +const search = require('./search') + +const self = new EventEmitter() + +self._nodes = { + schedule: document.querySelector('#schedule') +} + +self._parseMeetingpointHTML = function (htmlStr) { + const html = document.createElement('html') + html.innerHTML = htmlStr + const centerNode = html.querySelector('center') + return centerNode +} + +self._handleLoad = function (event) { + const request = event.target + if (request.status < 200 || request.status >= 400) { + self._handleError(event) + return + } + const document = self._parseMeetingpointHTML(request.response) + self._removeChilds() + self._nodes.schedule.appendChild(document) + self._nodes.schedule.classList.remove('error') + self.emit('load') +} + +self._handleError = function (event) { + const request = event.target + let error + if (request.status === 404) { + error = 'Sorry, er is (nog) geen rooster voor deze week.' + } else { + error = 'Sorry, er is iets mis gegaan tijdens het laden van deze week.' + } + self._removeChilds() + self._nodes.schedule.textContent = error + self._nodes.schedule.classList.add('error') + self.emit('load') +} + +self._getURLOfUsers = function (week, type, index) { + const id = index + 1 + return '//' + window.location.host + '/meetingpointProxy/Roosters-AL%2Fdoc%2Fdagroosters%2F' + + week + '%2F' + type + '%2F' + type + leftPad(id, 5, '0') + '.htm' +} + +self._removeChilds = function () { + while (self._nodes.schedule.firstChild) { + self._nodes.schedule.removeChild(self._nodes.schedule.firstChild) + } +} + +self.viewItem = function (week, selectedUser) { + const url = self._getURLOfUsers(week, selectedUser.type, selectedUser.index) + + self._removeChilds() + + const request = new window.XMLHttpRequest() + request.addEventListener('load', self._handleLoad) + request.addEventListener('error', self._handleError) + request.open('GET', url, true) + request.send() + + search.updateDom(selectedUser) +} + +module.exports = self |