diff options
author | Noah Loomans <noahloomans@gmail.com> | 2016-12-14 12:57:59 +0100 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2016-12-14 12:57:59 +0100 |
commit | 7c4d1ca2d771ee527201ccd6447e70da97ad33be (patch) | |
tree | e151354bb2182d9788c0e2e8e85d560ccb862f3f /public/javascripts/weekSelector.js | |
parent | a016343e0a88e1bb46a2deed0b27dda7b6bc7e44 (diff) | |
parent | b7027583efff12e1bd5acdbb4077ab36c5226839 (diff) |
Merge branch 'refactor' into beta
Diffstat (limited to 'public/javascripts/weekSelector.js')
-rw-r--r-- | public/javascripts/weekSelector.js | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/public/javascripts/weekSelector.js b/public/javascripts/weekSelector.js new file mode 100644 index 0000000..2a8bb62 --- /dev/null +++ b/public/javascripts/weekSelector.js @@ -0,0 +1,58 @@ +const EventEmitter = require('events') + +const self = new EventEmitter() + +self._nodes = { + prevButton: document.querySelectorAll('#week-selector button')[0], + nextButton: document.querySelectorAll('#week-selector button')[1], + currentWeekText: document.querySelector('#week-selector .current') +} + +self._weekOffset = 0 + +// copied from http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/untisscripts.js, +// were using the same code as they do to be sure that we always get the same +// week number. +self.getCurrentWeek = function (target) { + const dayNr = (target.getDay() + 6) % 7 + target.setDate(target.getDate() - dayNr + 3) + const firstThursday = target.valueOf() + target.setMonth(0, 1) + if (target.getDay() !== 4) { + target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7) + } + + return 1 + Math.ceil((firstThursday - target) / 604800000) +} + +self.getSelectedWeek = function () { + const now = new Date() + const targetDate = new Date(now.getTime() + self._weekOffset * 604800 * 1000) + return self.getCurrentWeek(targetDate) +} + +self.updateCurrentWeek = function () { + const selectedWeekNumber = self.getSelectedWeek() + self._nodes.currentWeekText.textContent = `Week ${selectedWeekNumber}` + if (self.getCurrentWeek(new Date()) !== selectedWeekNumber) { + self._nodes.currentWeekText.classList.add('changed') + } else { + self._nodes.currentWeekText.classList.remove('changed') + } + self.emit('weekChanged', selectedWeekNumber) +} + +self._handlePrevButtonClick = function () { + self._weekOffset -= 1 + self.updateCurrentWeek() +} + +self._handleNextButtonClick = function () { + self._weekOffset += 1 + self.updateCurrentWeek() +} + +self._nodes.prevButton.addEventListener('click', self._handlePrevButtonClick) +self._nodes.nextButton.addEventListener('click', self._handleNextButtonClick) + +module.exports = self |