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

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

  /**
   * Change header color based on status
   */

  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';
  }

  /** 
   * Category logic
   */

  function toggleCategoryHead(el) {
    if (el.parentNode.className == 'category category--open category--titled') {
      el.parentNode.className = 'category category--closed category--titled';
    } else {
      el.parentNode.className = 'category category--open category--titled';
    }
  }

  /**
   * 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" }}';
    }
    {{ if .Site.Params.skipSeconds }}
      return '<1 {{ T "minsAgo" }}'
    {{ else }}
      return Math.floor(seconds) + '{{ T "secondsAgo" }}';
    {{ end }}
  }

  /**
   * 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();

  // Update "time since" feature every 5s
  setInterval(updateRelativeTimes, 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={{ .Site.Params.googleAnalytics }}"></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 }}