1
Fork 0
rust/src/librustdoc/html/static/js/storage.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

253 lines
7.6 KiB
JavaScript
Raw Normal View History

2022-05-02 14:32:56 +02:00
"use strict";
2022-04-25 14:33:53 +02:00
const darkThemes = ["dark", "ayu"];
window.currentTheme = document.getElementById("themeStyle");
window.mainTheme = document.getElementById("mainThemeStyle");
const settingsDataset = (function() {
2022-04-25 14:33:53 +02:00
const settingsElement = document.getElementById("default-settings");
if (settingsElement === null) {
return null;
}
2022-04-25 14:33:53 +02:00
const dataset = settingsElement.dataset;
if (dataset === undefined) {
return null;
}
return dataset;
})();
function getSettingValue(settingName) {
2022-04-25 14:33:53 +02:00
const current = getCurrentValue(settingName);
if (current !== null) {
return current;
}
if (settingsDataset !== null) {
// See the comment for `default_settings.into_iter()` etc. in
// `Options::from_matches` in `librustdoc/config.rs`.
2022-05-07 20:18:23 +02:00
const def = settingsDataset[settingName.replace(/-/g,"_")];
if (def !== undefined) {
return def;
}
}
return null;
}
2022-04-25 14:33:53 +02:00
const localStoredTheme = getSettingValue("theme");
2018-01-22 23:44:08 +01:00
2022-04-25 14:33:53 +02:00
const savedHref = [];
2021-01-23 14:55:24 +01:00
// eslint-disable-next-line no-unused-vars
2018-11-06 01:40:12 +01:00
function hasClass(elem, className) {
2018-12-02 01:22:44 +01:00
return elem && elem.classList && elem.classList.contains(className);
2018-11-06 01:40:12 +01:00
}
2021-01-23 14:55:24 +01:00
// eslint-disable-next-line no-unused-vars
2018-11-06 01:40:12 +01:00
function addClass(elem, className) {
if (!elem || !elem.classList) {
return;
2018-11-06 01:40:12 +01:00
}
elem.classList.add(className);
2018-11-06 01:40:12 +01:00
}
2021-01-23 14:55:24 +01:00
// eslint-disable-next-line no-unused-vars
2018-11-06 01:40:12 +01:00
function removeClass(elem, className) {
if (!elem || !elem.classList) {
return;
2018-11-06 01:40:12 +01:00
}
elem.classList.remove(className);
2018-11-06 01:40:12 +01:00
}
/**
* Run a callback for every element of an Array.
* @param {Array<?>} arr - The array to iterate over
* @param {function(?)} func - The callback
* @param {boolean} [reversed] - Whether to iterate in reverse
*/
2018-09-12 23:31:21 +02:00
function onEach(arr, func, reversed) {
if (arr && arr.length > 0 && func) {
if (reversed) {
2022-04-25 14:33:53 +02:00
const length = arr.length;
for (let i = length - 1; i >= 0; --i) {
if (func(arr[i])) {
2018-09-12 23:31:21 +02:00
return true;
}
}
} else {
2022-04-25 14:33:53 +02:00
for (const elem of arr) {
if (func(elem)) {
2018-09-12 23:31:21 +02:00
return true;
}
}
}
}
return false;
}
/**
* Turn an HTMLCollection or a NodeList into an Array, then run a callback
* for every element. This is useful because iterating over an HTMLCollection
* or a "live" NodeList while modifying it can be very slow.
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection
* https://developer.mozilla.org/en-US/docs/Web/API/NodeList
* @param {NodeList<?>|HTMLCollection<?>} lazyArray - An array to iterate over
* @param {function(?)} func - The callback
* @param {boolean} [reversed] - Whether to iterate in reverse
*/
2018-12-02 01:22:44 +01:00
function onEachLazy(lazyArray, func, reversed) {
return onEach(
Array.prototype.slice.call(lazyArray),
func,
reversed);
}
function updateLocalStorage(name, value) {
try {
window.localStorage.setItem("rustdoc-" + name, value);
} catch (e) {
// localStorage is not accessible, do nothing
}
}
function getCurrentValue(name) {
try {
return window.localStorage.getItem("rustdoc-" + name);
} catch (e) {
return null;
}
}
function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
2022-04-25 14:33:53 +02:00
const newHref = mainStyleElem.href.replace(
/\/rustdoc([^/]*)\.css/, "/" + newTheme + "$1" + ".css");
// If this new value comes from a system setting or from the previously
// saved theme, no need to save it.
if (saveTheme) {
updateLocalStorage("theme", newTheme);
}
2018-03-08 00:11:17 +01:00
if (styleElem.href === newHref) {
return;
}
2022-04-25 14:33:53 +02:00
let found = false;
if (savedHref.length === 0) {
onEachLazy(document.getElementsByTagName("link"), el => {
savedHref.push(el.href);
});
}
onEach(savedHref, el => {
if (el === newHref) {
found = true;
return true;
}
});
if (found) {
styleElem.href = newHref;
}
2018-01-22 23:44:08 +01:00
}
// This function is called from "main.js".
2021-01-23 14:55:24 +01:00
// eslint-disable-next-line no-unused-vars
2020-10-11 02:53:37 +02:00
function useSystemTheme(value) {
if (value === undefined) {
value = true;
}
updateLocalStorage("use-system-theme", value);
2020-10-11 02:53:37 +02:00
// update the toggle if we're on the settings page
2022-04-25 14:33:53 +02:00
const toggle = document.getElementById("use-system-theme");
2020-10-11 02:53:37 +02:00
if (toggle && toggle instanceof HTMLInputElement) {
toggle.checked = value;
}
2019-05-27 15:57:44 +02:00
}
const updateSystemTheme = (function() {
2020-10-11 02:53:37 +02:00
if (!window.matchMedia) {
// fallback to the CSS computed value
return () => {
2022-04-25 14:33:53 +02:00
const cssTheme = getComputedStyle(document.documentElement)
2022-05-07 20:18:23 +02:00
.getPropertyValue("content");
2020-10-11 02:53:37 +02:00
switchTheme(
window.currentTheme,
window.mainTheme,
2021-01-23 14:55:24 +01:00
JSON.parse(cssTheme) || "light",
2020-10-11 02:53:37 +02:00
true
);
};
}
// only listen to (prefers-color-scheme: dark) because light is the default
2022-04-25 14:33:53 +02:00
const mql = window.matchMedia("(prefers-color-scheme: dark)");
2020-10-11 02:53:37 +02:00
function handlePreferenceChange(mql) {
const use = theme => {
switchTheme(window.currentTheme, window.mainTheme, theme, true);
};
2020-10-11 02:53:37 +02:00
// maybe the user has disabled the setting in the meantime!
if (getSettingValue("use-system-theme") !== "false") {
2022-04-25 14:33:53 +02:00
const lightTheme = getSettingValue("preferred-light-theme") || "light";
const darkTheme = getSettingValue("preferred-dark-theme") || "dark";
2020-10-11 02:53:37 +02:00
if (mql.matches) {
use(darkTheme);
2020-10-11 02:53:37 +02:00
} else {
// prefers a light theme, or has no preference
use(lightTheme);
2020-10-11 02:53:37 +02:00
}
// note: we save the theme so that it doesn't suddenly change when
// the user disables "use-system-theme" and reloads the page or
// navigates to another page
} else {
use(getSettingValue("theme"));
2020-10-11 02:53:37 +02:00
}
}
mql.addListener(handlePreferenceChange);
return () => {
2020-10-11 02:53:37 +02:00
handlePreferenceChange(mql);
};
})();
function switchToSavedTheme() {
switchTheme(
window.currentTheme,
window.mainTheme,
getSettingValue("theme") || "light",
false
);
}
if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
// update the preferred dark theme if the user is already using a dark theme
// See https://github.com/rust-lang/rust/pull/77809#issuecomment-707875732
if (getSettingValue("use-system-theme") === null
&& getSettingValue("preferred-dark-theme") === null
&& darkThemes.indexOf(localStoredTheme) >= 0) {
updateLocalStorage("preferred-dark-theme", localStoredTheme);
}
2020-10-11 02:53:37 +02:00
// call the function to initialize the theme at least once!
updateSystemTheme();
} else {
switchToSavedTheme();
2020-10-11 02:53:37 +02:00
}
// If we navigate away (for example to a settings page), and then use the back or
// forward button to get back to a page, the theme may have changed in the meantime.
// But scripts may not be re-loaded in such a case due to the bfcache
// (https://web.dev/bfcache/). The "pageshow" event triggers on such navigations.
// Use that opportunity to update the theme.
// We use a setTimeout with a 0 timeout here to put the change on the event queue.
// For some reason, if we try to change the theme while the `pageshow` event is
// running, it sometimes fails to take effect. The problem manifests on Chrome,
// specifically when talking to a remote website with no caching.
window.addEventListener("pageshow", ev => {
if (ev.persisted) {
setTimeout(switchToSavedTheme, 0);
}
});