-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_main.js
More file actions
143 lines (123 loc) · 5.25 KB
/
Copy path_main.js
File metadata and controls
143 lines (123 loc) · 5.25 KB
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
/* ==========================================================================
Various functions that we want to use within the template
========================================================================== */
// Determine the expected state of the theme toggle, which can be "dark", "light", or
// "system". Default is "system".
let determineThemeSetting = () => {
let themeSetting = localStorage.getItem("theme");
return (themeSetting != "dark" && themeSetting != "light" && themeSetting != "system") ? "system" : themeSetting;
};
// Determine the computed theme, which can be "dark" or "light". If the theme setting is
// "system", the computed theme is determined based on the user's system preference.
let determineComputedTheme = () => {
let themeSetting = determineThemeSetting();
if (themeSetting != "system") {
return themeSetting;
}
return (userPref && userPref("(prefers-color-scheme: dark)").matches) ? "dark" : "light";
};
// detect OS/browser preference
const browserPref = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
// Set the theme on page load or when explicitly called
let setTheme = (theme) => {
const use_theme =
theme ||
localStorage.getItem("theme") ||
$("html").attr("data-theme") ||
browserPref;
if (use_theme === "dark") {
$("html").attr("data-theme", "dark");
$("#theme-icon").removeClass("fa-sun").addClass("fa-moon");
} else if (use_theme === "light") {
$("html").removeAttr("data-theme");
$("#theme-icon").removeClass("fa-moon").addClass("fa-sun");
}
};
// Toggle the theme manually
var toggleTheme = () => {
const current_theme = $("html").attr("data-theme");
const new_theme = current_theme === "dark" ? "light" : "dark";
localStorage.setItem("theme", new_theme);
setTheme(new_theme);
};
/* ==========================================================================
Plotly integration script so that Markdown codeblocks will be rendered
========================================================================== */
// Read the Plotly data from the code block, hide it, and render the chart as new node. This allows for the
// JSON data to be retrieve when the theme is switched. The listener should only be added if the data is
// actually present on the page.
import { plotlyDarkLayout, plotlyLightLayout } from './theme.js';
let plotlyElements = document.querySelectorAll("pre>code.language-plotly");
if (plotlyElements.length > 0) {
document.addEventListener("readystatechange", () => {
if (document.readyState === "complete") {
plotlyElements.forEach((elem) => {
// Parse the Plotly JSON data and hide it
var jsonData = JSON.parse(elem.textContent);
elem.parentElement.classList.add("hidden");
// Add the Plotly node
let chartElement = document.createElement("div");
elem.parentElement.after(chartElement);
// Set the theme for the plot and render it
const theme = (determineComputedTheme() === "dark") ? plotlyDarkLayout : plotlyLightLayout;
if (jsonData.layout) {
jsonData.layout.template = (jsonData.layout.template) ? { ...theme, ...jsonData.layout.template } : theme;
} else {
jsonData.layout = { template: theme };
}
Plotly.react(chartElement, jsonData.data, jsonData.layout);
});
}
});
}
/* ==========================================================================
Actions that should occur when the page has been fully loaded
========================================================================== */
$(document).ready(function () {
// SCSS SETTINGS - These should be the same as the settings in the relevant files
const scssLarge = 925; // pixels, from /_sass/_themes.scss
const scssMastheadHeight = 70; // pixels, from the current theme (e.g., /_sass/theme/_default.scss)
// If the user hasn't chosen a theme, follow the OS preference
setTheme();
window.matchMedia('(prefers-color-scheme: dark)')
.addEventListener("change", (e) => {
if (!localStorage.getItem("theme")) {
setTheme(e.matches ? "dark" : "light");
}
});
// Enable the theme toggle
$('#theme-toggle').on('click', toggleTheme);
// Enable the sticky footer
var bumpIt = function () {
$("body").css("padding-bottom", "0");
$("body").css("margin-bottom", $(".page__footer").outerHeight(true));
}
$(window).resize(function () {
didResize = true;
});
setInterval(function () {
if (didResize) {
didResize = false;
bumpIt();
}}, 250);
var didResize = false;
bumpIt();
// FitVids init
fitvids();
// Follow menu drop down
$(".author__urls-wrapper button").on("click", function () {
$(".author__urls").fadeToggle("fast", function () { });
$(".author__urls-wrapper button").toggleClass("open");
});
// Restore the follow menu if toggled on a window resize
jQuery(window).on('resize', function () {
if ($('.author__urls.social-icons').css('display') == 'none' && $(window).width() >= scssLarge) {
$(".author__urls").css('display', 'block')
}
});
// Init smooth scroll, this needs to be slightly more than then fixed masthead height
$("a").smoothScroll({
offset: -scssMastheadHeight,
preventDefault: false,
});
});