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

  console.log('Welcome to cState v2.0! https://github.com/cstate/cstate');
  document.querySelector('html').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);
    }
  }

  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 = 'Last checked just now';

    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 + ' years';
      }
      interval = Math.floor(seconds / 2592000);
      if (interval > 1) {
        return interval + ' months';
      }
      interval = Math.floor(seconds / 86400);
      if (interval > 1) {
        return interval + 'd';
      }
      interval = Math.floor(seconds / 3600);
      if (interval > 1) {
        return interval + 'h';
      }
      interval = Math.floor(seconds / 60);
      if (interval > 1) {
        return interval + ' min';
      }
      return Math.floor(seconds) + 's';
    }
    var aDay = 24*60*60*1000;
  }

  window.setInterval(function() {
    if (hasClass(document.querySelector('body'), 'status-homepage')) {
      lastUpdated.innerHTML = 'Last checked ' + timeSince(lastUpdate) + ' ago';

      var lastUpdateTime = new Date() - lastUpdate;
      // Refresh every 4m 30s
      if (lastUpdateTime > 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 }}