-
Notifications
You must be signed in to change notification settings - Fork 26k
/
Copy path_main.js
234 lines (209 loc) · 7.71 KB
/
_main.js
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
/* ==========================================================================
jQuery plugin settings and other scripts
========================================================================== */
$(document).ready(function () {
// FitVids init
$("#main").fitVids();
// Follow menu drop down
$(".author__urls-wrapper button").on("click", function () {
$(".author__urls").toggleClass("is--visible");
$(".author__urls-wrapper").find("button").toggleClass("open");
});
// Close search screen with Esc key
$(document).keyup(function (e) {
if (e.keyCode === 27) {
if ($(".initial-content").hasClass("is--hidden")) {
$(".search-content").toggleClass("is--visible");
$(".initial-content").toggleClass("is--hidden");
}
}
});
// Search toggle
$(".search__toggle").on("click", function () {
$(".search-content").toggleClass("is--visible");
$(".initial-content").toggleClass("is--hidden");
// set focus on input
setTimeout(function () {
$(".search-content input").focus();
}, 400);
});
// Smooth scrolling
var scroll = new SmoothScroll('a[href*="#"]', {
offset: 20,
speed: 400,
speedAsDuration: true,
durationMax: 500,
});
// Gumshoe scroll spy init
if ($("nav.toc").length > 0) {
var spy = new Gumshoe("nav.toc a", {
// Active classes
navClass: "active", // applied to the nav list item
contentClass: "active", // applied to the content
// Nested navigation
nested: false, // if true, add classes to parents of active link
nestedClass: "active", // applied to the parent items
// Offset & reflow
offset: 20, // how far from the top of the page to activate a content area
reflow: true, // if true, listen for reflows
// Event support
events: true, // if true, emit custom events
});
}
// Auto scroll sticky ToC with content
const scrollTocToContent = function (event) {
var target = event.target;
var scrollOptions = { behavior: "auto", block: "nearest", inline: "start" };
var tocElement = document.querySelector("aside.sidebar__right.sticky");
if (!tocElement) return;
if (window.getComputedStyle(tocElement).position !== "sticky") return;
if (target.parentElement.classList.contains("toc__menu") && target == target.parentElement.firstElementChild) {
// Scroll to top instead
document.querySelector("nav.toc header").scrollIntoView(scrollOptions);
} else {
target.scrollIntoView(scrollOptions);
}
};
// Has issues on Firefox, whitelist Chrome for now
if (!!window.chrome) {
document.addEventListener("gumshoeActivate", scrollTocToContent);
}
// add lightbox class to all image links
$(
"a[href$='.jpg'],a[href$='.jpeg'],a[href$='.JPG'],a[href$='.png'],a[href$='.gif'],a[href$='.webp']"
).has("> img").addClass("image-popup");
// Magnific-Popup options
$(".image-popup").magnificPopup({
// disableOn: function() {
// if( $(window).width() < 500 ) {
// return false;
// }
// return true;
// },
type: "image",
tLoading: "Loading image #%curr%...",
gallery: {
enabled: true,
navigateByImgClick: true,
preload: [0, 1], // Will preload 0 - before current, and 1 after the current image
},
image: {
tError: '<a href="%url%">Image #%curr%</a> could not be loaded.',
},
removalDelay: 500, // Delay in milliseconds before popup is removed
// Class that is added to body when popup is open.
// make it unique to apply your CSS animations just to this exact popup
mainClass: "mfp-zoom-in",
callbacks: {
beforeOpen: function () {
// just a hack that adds mfp-anim class to markup
this.st.image.markup = this.st.image.markup.replace(
"mfp-figure",
"mfp-figure mfp-with-anim"
);
},
},
closeOnContentClick: true,
midClick: true, // allow opening popup on middle mouse click. Always set it to true if you don't provide alternative source.
});
// Add anchors for headings
(function () {
var pageContentElement = document.querySelector(".page__content");
if (!pageContentElement) return;
pageContentElement
.querySelectorAll("h1, h2, h3, h4, h5, h6")
.forEach(function (element) {
var id = element.getAttribute("id");
if (id) {
var anchor = document.createElement("a");
anchor.className = "header-link";
anchor.href = "#" + id;
anchor.innerHTML =
'<span class="sr-only">Permalink</span><i class="fas fa-link"></i>';
anchor.title = "Permalink";
element.appendChild(anchor);
}
});
})();
// Add copy button for <pre> blocks
var copyText = function (text) {
if (document.queryCommandEnabled("copy") && navigator.clipboard) {
navigator.clipboard.writeText(text).then(
() => true,
() => console.error("Failed to copy text to clipboard: " + text)
);
return true;
} else {
var isRTL = document.documentElement.getAttribute("dir") === "rtl";
var textarea = document.createElement("textarea");
textarea.className = "clipboard-helper";
textarea.style[isRTL ? "right" : "left"] = "-9999px";
// Move element to the same position vertically
var yPosition = window.pageYOffset || document.documentElement.scrollTop;
textarea.style.top = yPosition + "px";
textarea.setAttribute("readonly", "");
textarea.value = text;
document.body.appendChild(textarea);
var success = true;
try {
textarea.select();
success = document.execCommand("copy");
} catch (e) {
success = false;
}
textarea.parentNode.removeChild(textarea);
return success;
}
};
var copyButtonEventListener = function (event) {
var thisButton = event.target;
// Locate the <code> element
var codeBlock = thisButton.nextElementSibling;
while (codeBlock && codeBlock.tagName.toLowerCase() !== "code") {
codeBlock = codeBlock.nextElementSibling;
}
if (!codeBlock) {
// No <code> found - wtf?
console.warn(thisButton);
throw new Error("No code block found for this button.");
}
// Skip line numbers if present (i.e. {% highlight lineno %})
var realCodeBlock = codeBlock.querySelector("td.code, td.rouge-code");
if (realCodeBlock) {
codeBlock = realCodeBlock;
}
var result = copyText(codeBlock.innerText);
// Restore the focus to the button
thisButton.focus();
if (result) {
if (thisButton.interval !== null) {
clearInterval(thisButton.interval);
}
thisButton.classList.add('copied');
thisButton.interval = setTimeout(function () {
thisButton.classList.remove('copied');
clearInterval(thisButton.interval);
thisButton.interval = null;
}, 1500);
}
return result;
};
if (window.enable_copy_code_button) {
document
.querySelectorAll(".page__content pre.highlight > code")
.forEach(function (element, index, parentList) {
// Locate the <pre> element
var container = element.parentElement;
// Sanity check - don't add an extra button if there's already one
if (container.firstElementChild.tagName.toLowerCase() !== "code") {
return;
}
var copyButton = document.createElement("button");
copyButton.title = "Copy to clipboard";
copyButton.className = "clipboard-copy-button";
copyButton.innerHTML = '<span class="sr-only">Copy code</span><i class="far fa-fw fa-copy"></i><i class="fas fa-fw fa-check copied"></i>';
copyButton.addEventListener("click", copyButtonEventListener);
container.prepend(copyButton);
});
}
});