mirror of https://github.com/theNewDynamic/gohugo-theme-ananke.git

Patrick Kollitsch
9 hours ago c70b22c0c65d21fc25eda46a8182eb042da11da0
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
{{- /*
  Site scripts. Each block is progressive enhancement: its buttons are hidden
  in the markup and only revealed here, so a site with JavaScript disabled
  never shows an inert button.
 
  1. Copy-to-clipboard behaviour for code blocks rendered by
     layouts/_markup/render-codeblock.html. A single delegated listener
     handles every code block on the page. Disable the copy buttons entirely
     with `[ananke] copy_code = false`.
  2. Header navigation rendered by layouts/_partials/site-navigation.html:
     the menu button that collapses the menu below the Tachyons `-l`
     breakpoint, and the desktop submenu toggles. Only included if the site has a
     `main` menu.
 
  Override this partial in your own project to add or replace site scripts.
*/ -}}
{{- if ne false site.Params.ananke.copy_code -}}
<script>
  (function () {
    "use strict";
 
    // Reveal the copy buttons once the DOM is ready. This script is included in
    // the <head>, so the code blocks may not exist yet when it first runs.
    function revealButtons() {
      var buttons = document.querySelectorAll(".code-block .code-copy[hidden]");
      for (var i = 0; i < buttons.length; i++) {
        buttons[i].hidden = false;
      }
    }
    if (document.readyState === "loading") {
      document.addEventListener("DOMContentLoaded", revealButtons);
    } else {
      revealButtons();
    }
 
    function flash(button) {
      var label = button.querySelector(".code-copy-label");
      var previous = label ? label.textContent : null;
      button.classList.add("is-copied");
      if (label) label.textContent = "Copied";
      window.setTimeout(function () {
        button.classList.remove("is-copied");
        if (label && previous !== null) label.textContent = previous;
      }, 2000);
    }
 
    function legacyCopy(text) {
      var area = document.createElement("textarea");
      area.value = text;
      area.setAttribute("readonly", "");
      area.style.position = "absolute";
      area.style.left = "-9999px";
      document.body.appendChild(area);
      area.select();
      var ok = false;
      try {
        ok = document.execCommand("copy");
      } catch (e) {
        ok = false;
      }
      document.body.removeChild(area);
      return ok;
    }
 
    function copyFrom(button) {
      var container = button.closest(".code-block");
      if (!container) return;
      var pre = container.querySelector("pre");
      if (!pre) return;
      var source = pre.querySelector("code") || pre;
      var text = source.innerText;
 
      if (navigator.clipboard && navigator.clipboard.writeText) {
        navigator.clipboard.writeText(text).then(
          function () { flash(button); },
          function () { if (legacyCopy(text)) flash(button); }
        );
      } else if (legacyCopy(text)) {
        flash(button);
      }
    }
 
    document.addEventListener("click", function (event) {
      var button = event.target.closest(".code-copy");
      if (button) copyFrom(button);
    });
  })();
</script>
{{- end -}}
 
{{- if site.Menus.main -}}
<script>
  (function () {
    "use strict";
 
    // Apply the mobile layout before navigation markup can paint.
    document.documentElement.classList.add("ananke-js");
 
    // The toggle button of the open submenu. Only one submenu is open at a time.
    var openButton = null;
    var hoverCloseTimer = null;
    var lastNavigationFocus = null;
    var desktopMedia = window.matchMedia("(min-width: 60em)");
    var hoverMedia = window.matchMedia("(min-width: 60em) and (hover: hover) and (pointer: fine)");
 
    function controlled(button) {
      return document.getElementById(button.getAttribute("aria-controls"));
    }
 
    function setSubmenu(button, expanded) {
      var submenu = controlled(button);
      if (!submenu || !desktopMedia.matches) return;
      window.clearTimeout(hoverCloseTimer);
      button.setAttribute("aria-expanded", expanded ? "true" : "false");
      submenu.hidden = !expanded;
      submenu.classList.remove("ananke-submenu-flip");
      if (expanded) {
        // Align the dropdown to the other edge of its parent if it would
        // leave the viewport.
        var rect = submenu.getBoundingClientRect();
        if (rect.right > document.documentElement.clientWidth || rect.left < 0) {
          submenu.classList.add("ananke-submenu-flip");
        }
      }
      openButton = expanded ? button : null;
    }
 
    function closeSubmenu(returnFocus) {
      var button = openButton;
      if (!button) return;
      setSubmenu(button, false);
      if (returnFocus) button.focus();
    }
 
    function setMenu(button, expanded) {
      var nav = button.closest(".ananke-nav");
      if (!nav) return;
      button.setAttribute("aria-expanded", expanded ? "true" : "false");
      nav.classList.toggle("ananke-nav-collapsed", !expanded);
      nav.classList.toggle("ananke-nav-expanded", expanded);
    }
 
    function enhanceHover(button) {
      var item = button.parentNode;
      var menu = button.closest("[data-submenu-mode]");
      if (!menu || menu.dataset.submenuMode !== "hover") return;
      item.addEventListener("pointerenter", function (event) {
        if (event.pointerType !== "mouse" || !hoverMedia.matches) return;
        window.clearTimeout(hoverCloseTimer);
        if (openButton === button) return;
        // Hover must not hide a link that still has keyboard focus.
        if (openButton && controlled(openButton).contains(document.activeElement)) return;
        closeSubmenu(false);
        setSubmenu(button, true);
      });
      item.addEventListener("pointerleave", function (event) {
        if (event.pointerType !== "mouse" || !hoverMedia.matches) return;
        // Keep the dropdown open while the keyboard focus is inside it.
        hoverCloseTimer = window.setTimeout(function () {
          if (openButton === button && !item.contains(document.activeElement)) {
            closeSubmenu(false);
          }
        }, 200);
      });
    }
 
    hoverMedia.addEventListener("change", function () {
      if (openButton && openButton.closest("[data-submenu-mode]").dataset.submenuMode === "hover") {
        closeSubmenu(false);
      }
    });
 
    // Mobile has one disclosure: the hamburger. Every included child list
    // stays expanded, and parent entries keep their normal links or labels.
    function updateSubmenus() {
      var desktop = desktopMedia.matches;
      var active = document.activeElement;
      // A breakpoint can hide the focused element before this event runs.
      if (active === document.body && lastNavigationFocus) active = lastNavigationFocus;
      var focusTarget = null;
      window.clearTimeout(hoverCloseTimer);
      openButton = null;
      var buttons = document.querySelectorAll(".ananke-submenu-toggle");
      for (var i = 0; i < buttons.length; i++) {
        var button = buttons[i];
        var submenu = controlled(button);
        if (!submenu) continue;
        submenu.hidden = desktop;
        submenu.classList.remove("ananke-submenu-flip");
        button.hidden = !desktop;
        button.setAttribute("aria-expanded", "false");
        var label = button.parentNode.querySelector(".ananke-submenu-label");
        if (label) label.hidden = desktop;
        var parentLink = button.parentNode.querySelector("[data-submenu-parent]");
        if (parentLink) parentLink.hidden = desktop;
        if (desktop && submenu.contains(active)) focusTarget = button;
        if (!desktop && active === button) {
          focusTarget = parentLink || submenu.querySelector("a");
        }
      }
      var nav = active.closest(".ananke-nav");
      if (nav) {
        var menuButton = nav.querySelector(".ananke-nav-toggle");
        var panel = menuButton && controlled(menuButton);
        if (!desktop && panel && panel.contains(active) && nav.classList.contains("ananke-nav-collapsed")) {
          focusTarget = menuButton;
        } else if (desktop && active === menuButton && panel) {
          focusTarget = panel.querySelector('a:not([hidden]), button:not([hidden])');
        }
      }
      if (focusTarget) focusTarget.focus();
    }
 
    desktopMedia.addEventListener("change", updateSubmenus);
 
    // Without JavaScript, all links stay visible and controls stay hidden.
    function enhance() {
      var menuButtons = document.querySelectorAll(".ananke-nav-toggle[hidden]");
      for (var i = 0; i < menuButtons.length; i++) {
        if (!controlled(menuButtons[i])) continue;
        setMenu(menuButtons[i], false);
        menuButtons[i].hidden = false;
      }
      updateSubmenus();
      var buttons = document.querySelectorAll(".ananke-submenu-toggle");
      for (var j = 0; j < buttons.length; j++) {
        enhanceHover(buttons[j]);
      }
    }
    if (document.readyState === "loading") {
      document.addEventListener("DOMContentLoaded", enhance);
    } else {
      enhance();
    }
 
    document.addEventListener("click", function (event) {
      var menuButton = event.target.closest(".ananke-nav-toggle");
      if (menuButton) {
        setMenu(menuButton, menuButton.getAttribute("aria-expanded") !== "true");
        return;
      }
      var button = event.target.closest(".ananke-submenu-toggle");
      if (button && desktopMedia.matches) {
        var expand = button.getAttribute("aria-expanded") !== "true";
        if (openButton && openButton !== button) closeSubmenu(false);
        setSubmenu(button, expand);
      } else if (openButton && !openButton.parentNode.contains(event.target)) {
        closeSubmenu(false);
      }
    });
 
    document.addEventListener("keydown", function (event) {
      if (event.key !== "Escape") return;
      if (openButton) {
        // Return focus to the button only if focus was inside the menu item.
        closeSubmenu(openButton.parentNode.contains(document.activeElement));
        return;
      }
      var menuButton = document.querySelector('.ananke-nav-toggle[aria-expanded="true"]');
      if (!menuButton) return;
      var panel = controlled(menuButton);
      var inside = panel && panel.contains(document.activeElement);
      setMenu(menuButton, false);
      if (inside) menuButton.focus();
    });
 
    // Close a submenu when keyboard focus leaves its menu item. Pointer users
    // get the outside-click handler instead. Mobile child lists do not have
    // their own disclosure state.
    var keyboardFocus = false;
    document.addEventListener("keydown", function () {
      keyboardFocus = true;
    }, true);
    document.addEventListener("focusin", function (event) {
      lastNavigationFocus = event.target.closest(".ananke-nav") ? event.target : null;
    });
    document.addEventListener("pointerdown", function () {
      lastNavigationFocus = null;
      keyboardFocus = false;
    }, true);
    document.addEventListener("focusout", function (event) {
      if (!keyboardFocus || !openButton || !event.relatedTarget) return;
      var item = openButton.parentNode;
      if (item.contains(event.target) && !item.contains(event.relatedTarget)) {
        closeSubmenu(false);
      }
    });
  })();
</script>
{{- end -}}