aboutsummaryrefslogtreecommitdiff
path: root/src/client/javascript/url.js
blob: 17ab7c8101fec760f1a4549ba89e8dfb69382e1b (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* global USERS FLAGS */

const EventEmitter = require('events')

const self = new EventEmitter()

self._getPageTitle = function (selectedUser) {
  let ret

  if (selectedUser == null) {
    ret = `Metis Rooster`
  } else {
    ret = `Metis Rooster - ${selectedUser.value}`
  }

  if (FLAGS.indexOf('BETA') !== -1) {
    ret = `BETA ${ret}`
  }

  return ret
}

self._getPageURL = function (selectedUser) {
  return `/${selectedUser.type}/${selectedUser.value}`
}

self.push = function (selectedUser, push) {
  if (push == null) push = true
  const pageTitle = self._getPageTitle(selectedUser)
  const pageURL = self._getPageURL(selectedUser)
  if (push) {
    window.history.pushState(selectedUser, pageTitle, pageURL)
  } else {
    window.history.replaceState(selectedUser, pageTitle, pageURL)
  }
}

self.update = function (selectedUser) {
  document.title = self._getPageTitle(selectedUser)
}

self.hasSelectedUser = function () {
  const pageUrl = window.location.pathname
  return /^\/s\/|^\/t\/|^\/r\/|^\/c\//.test(pageUrl)
}

self.getSelectedUser = 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