From acbdba98a7ab37e76c856cc751b78e2a974036b9 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Sun, 29 Jan 2017 22:08:16 +0100 Subject: Update the page URL when selecting user When you select a user and press the back button of your browser, it will exit out of schedule page instead of going back inside of the app. This is unexpected behavior. This commit will update the URL using history.pushState() when a user is selected. Allowing the browser navigation buttons to nicely integrate with the schedule. --- public/javascripts/url.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 public/javascripts/url.js (limited to 'public/javascripts/url.js') diff --git a/public/javascripts/url.js b/public/javascripts/url.js new file mode 100644 index 0000000..a5877c6 --- /dev/null +++ b/public/javascripts/url.js @@ -0,0 +1,38 @@ +/* global USERS */ + +const EventEmitter = require('events') + +const self = new EventEmitter() + +self.update = function (selectedItem) { + const pageTitle = `Metis Rooster - ${selectedItem.value}` + const pageUrl = `/${selectedItem.type}/${selectedItem.value}` + window.history.pushState(selectedItem, pageTitle, pageUrl) +} + +self.hasSelectedItem = function () { + const pageUrl = window.location.pathname + return /^\/s\/|^\/t\/|^\/r\/|^\/c\//.test(pageUrl) +} + +self.getSelectedItem = function () { + const pageUrl = window.location.pathname + const pageUrlData = pageUrl.split('/') + const type = pageUrlData[1] + const value = pageUrlData[2] + + const user = USERS.filter(function (user) { + return user.type === type && + user.value === value + })[0] + + return user +} + +self._handleUpdate = function (event) { + self.emit('update', event.state) +} + +window.addEventListener('popstate', self._handleUpdate) + +module.exports = self -- cgit v1.1 From 25fbba2084a23fb62ab0e7fdf2dd47eadc28a967 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Mon, 30 Jan 2017 14:04:20 +0100 Subject: Fix bugs in url update - Document title doesn't update - Page crashes when going back to front page --- public/javascripts/url.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'public/javascripts/url.js') diff --git a/public/javascripts/url.js b/public/javascripts/url.js index a5877c6..b856285 100644 --- a/public/javascripts/url.js +++ b/public/javascripts/url.js @@ -4,10 +4,31 @@ const EventEmitter = require('events') const self = new EventEmitter() +self._getPageTitle = function (selectedItem) { + if (selectedItem == null) { + return `Metis Rooster` + } else { + return `Metis Rooster - ${selectedItem.value}` + } +} + +self._getPageURL = function (selectedItem) { + return `/${selectedItem.type}/${selectedItem.value}` +} + +self.push = function (selectedItem, push) { + if (push == null) push = true + const pageTitle = self._getPageTitle(selectedItem) + const pageURL = self._getPageURL(selectedItem) + if (push) { + window.history.pushState(selectedItem, pageTitle, pageURL) + } else { + window.history.replaceState(selectedItem, pageTitle, pageURL) + } +} + self.update = function (selectedItem) { - const pageTitle = `Metis Rooster - ${selectedItem.value}` - const pageUrl = `/${selectedItem.type}/${selectedItem.value}` - window.history.pushState(selectedItem, pageTitle, pageUrl) + document.title = self._getPageTitle(selectedItem) } self.hasSelectedItem = function () { -- cgit v1.1