aboutsummaryrefslogtreecommitdiff
path: root/layouts/partials/js.html
blob: a26d36cbe67ce6c5179f45453675af7829e0c1df (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<script type="text/javascript">
  /**
   * Dev toolset
   */

  console.log('Welcome to cState! https://github.com/mistermantas/cstate');
  document.querySelector('html').className = 'js';


  /**
   * Make theme color pretty
   */

  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);
  }
</script>

<!--
<script>
  /**
   * Check for internet
   */

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

  if (hasClass(document.querySelector('body'), 'status-homepage')) {
    var lastUpdated = document.querySelector('.summary__date');
    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;
  }

  function checkCurrentStatus() {
    // Check for each status
    if (hasClass(document.querySelector('body'), 'status-ok')) {
      window.currentState = 'Operational';
    }
    if (hasClass(document.querySelector('body'), 'status-disrupted')) {
      window.currentState = 'Disrupted';
    }
    if (hasClass(document.querySelector('body'), 'status-down')) {
      window.currentState = 'Down';
    }
    if (hasClass(document.querySelector('body'), 'status-notice')) {
      window.currentState = 'Maintenance';
    }
  }
  checkCurrentStatus();

  // Set for first time use
  if (typeof(Storage) !== 'undefined') {
    window.sessionStorage.setItem('priorState', currentState);
  }

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

    checkCurrentStatus();

    if (typeof(Storage) !== "undefined") {
      if (window.sessionStorage.getItem('priorState') !== currentState) {
        displayNotification('Status changed from ' + window.sessionStorage.getItem('priorState') + ' to ' + currentState);
      }

      window.sessionStorage.setItem('priorState', currentState);
    } else {
      console.log('UNSUPPORTED - SMART NOTIFICATIONS');
    }
  }, 1000);


  /**
   * Modern browser shiz
   */

  if (navigator.serviceWorker) {
    console.log('Service Worker and Push is supported');

    navigator.serviceWorker.register('sw.js')
    .then(function(swReg) {
      console.log('Service Worker is registered', swReg);

      swRegistration = swReg;
    })
    .catch(function(error) {
      console.error('Service Worker Error', error);
    });
  } else {
    console.log('Push messaging is not supported');
  }


  Notification.requestPermission(function(status) {
    console.log('Notification permission status:', status);
  });


  function displayNotification(title, body) {
    if (Notification.permission == 'granted') {
      navigator.serviceWorker.getRegistration().then(function(reg) {
        var options = {
          body: body,
          icon: '/favicon.png',
          vibrate: [100, 50, 100],
          data: {
            dateOfArrival: Date.now(),
            primaryKey: 1
          },
          actions: [
            {action: 'view', title: 'View incident'},
            {action: 'close', title: 'Dismiss'},
          ]
        };
        reg.showNotification(title, options);
      });
    }
  }
</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 }}