aboutsummaryrefslogtreecommitdiff
path: root/public/javascripts/weekSelector.js
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2016-12-09 23:02:52 +0100
committerNoah Loomans <noahloomans@gmail.com>2016-12-09 23:02:52 +0100
commitcc60c30509e5b084f2b0c01ba22a6e43a446887f (patch)
tree0752308ba9835d9f84d694201f3c4088e91fa5b3 /public/javascripts/weekSelector.js
parent907e8c138ae890208b03b23a420b32290fc2ef88 (diff)
add week selector
Diffstat (limited to 'public/javascripts/weekSelector.js')
-rw-r--r--public/javascripts/weekSelector.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/public/javascripts/weekSelector.js b/public/javascripts/weekSelector.js
new file mode 100644
index 0000000..7ce0d68
--- /dev/null
+++ b/public/javascripts/weekSelector.js
@@ -0,0 +1,60 @@
+const EventEmitter = require('events')
+
+const self = {}
+
+self.events = 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.events.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