aboutsummaryrefslogtreecommitdiff
path: root/layouts/partials/js.html
blob: c4c5a859e4e8549aae5c9e963f66f5155924e842 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<script type="text/javascript">
  /**
   * Dev toolset
   */

  console.log('cState v4.4 - https://github.com/cstate/cstate');
  document.getElementsByTagName('html')[0].className = 'js';

  /**
   * Make theme color pretty
   */

  if (document.body.className === 'change-header-color') {
    if (document.body.className === 'status-down') {
      document.querySelector('meta[name=theme-color]').setAttribute('content', themeDownColor);
    } else if (document.body.className === 'status-disrupted') {
      document.querySelector('meta[name=theme-color]').setAttribute('content', themeDisruptedColor);
    } else {
      document.querySelector('meta[name=theme-color]').setAttribute('content', themeNoticeColor);
    }
  }

  /**
   * Admin redirect
   */

  if (window.location.hash.match('access_token')) {
    document.location.pathname = '/admin';
  }
  if (window.location.hash.match('invite_token')) {
    document.location.pathname = '/admin';
  }
  if (window.location.hash.match('confirmation_token')) {
    document.location.pathname = '/admin';
  }
  if (window.location.hash.match('email_change_token')) {
    document.location.pathname = '/admin';
  }
  if (window.location.hash.match('recovery_token')) {
    document.location.pathname = '/admin';
  }

  /**
   * Returns a relative date string for the given date.
   */
  function timeSince(date) {
    var seconds = Math.floor((new Date() - date) / 1000);

    var interval = Math.floor(seconds / 31536000);

    if (interval > 1) {
      return interval + ' {{ T "yearsAgo" }}';
    }
    interval = Math.floor(seconds / 2592000);
    if (interval > 1) {
      return interval + ' {{ T "monthsAgo" }}';
    }
    interval = Math.floor(seconds / 86400);
    if (interval > 1) {
      return interval + '{{ T "daysAgo" }}';
    }
    interval = Math.floor(seconds / 3600);
    if (interval > 1) {
      return interval + '{{ T "hoursAgo" }}';
    }
    interval = Math.floor(seconds / 60);
    if (interval > 1) {
      return interval + ' {{ T "minsAgo" }}';
    }
    return Math.floor(seconds) + '{{ T "secondsAgo" }}';
  }

  /**
   * Changes elements with the class 'relative-time' into relative times and
   * moves the timestamp to a title attribute tooltip.
   */
  function updateRelativeTimes() {
    var elements = document.querySelectorAll('.relative-time');
    for (var i = 0; i < elements.length; i++) {
      var element = elements[i];
      var time = Date.parse(element.getAttribute('title'));
      var html = element.getAttribute('data-time-prefix') || '';
      if (!time) {
        time = element.innerText;
        element.setAttribute('title', time || new Date);
        html += '{{ T "justNow" }}';
      } else {
        html += timeSince(time) + ' {{ T "someTimeAgo" }}';
      }
      html += element.getAttribute('data-time-suffix') || '';
      element.innerHTML = html.trim();
    }
  }
  updateRelativeTimes();
  setInterval(updateRelativeTimes, 5000);

  /**
   * Reload homepage after 290 seconds.
   */
  if (document.querySelector('body.status-homepage')) {
    setTimeout(location.reload, 290000)
  }
</script>


{{ if ne .Site.Params.googleAnalytics "UA-00000000-1" }}
  <!-- Global site tag (gtag.js) - Google Analytics -->
  <script type="text/javascript" defer src="https://www.googletagmanager.com/gtag/js?id=UA-109775664-1"></script>
  <script type="text/javascript" defer>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', '{{ .Site.Params.googleAnalytics }}');
  </script>
{{ end }}