aboutsummaryrefslogtreecommitdiff
path: root/layouts/partials/js.html
blob: 7740d85ccb8c592feb8c4b9d2043d09321c6a976 (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
<script type="text/javascript">
  /**
   * Dev toolset
   */

  console.log('cState v4.0.0 - rc1 2019-12-15 - 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';
  }

  /**
   * Timer
   */

  function hasClass(element, className) {
    return (' ' + element.className + ' ').indexOf(' ' + className+ ' ') > -1;
  }

  if (hasClass(document.querySelector('body'), 'status-homepage')) {
    var lastUpdated = document.querySelector('.summary__date');
    lastUpdated.innerHTML = '{{ T "lastChecked" }} {{ T "justNow" }}';

    var lastUpdate = new 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" }}';
    }
    var aDay = 24*60*60*1000;
  }

  window.setInterval(function() {
    if (hasClass(document.querySelector('body'), 'status-homepage')) {
      lastUpdated.innerHTML = '{{ T "lastChecked" }} ' + timeSince(lastUpdate) + ' {{ T "someTimeAgo" }}';

      // Refresh almost every 5m
      if (lastUpdate > 290000) {
        location.reload;
      }
    }
  }, 5000)
</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 }}