aboutsummaryrefslogtreecommitdiff
path: root/public/javascripts/url.js
blob: a5877c623494e0f0cab34f9a88a13b85817f4953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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