2018-12-04 13:59:27 -08:00
|
|
|
// From rust:
|
|
|
|
/* global resourcesSuffix */
|
|
|
|
|
2018-01-22 23:44:08 +01:00
|
|
|
var currentTheme = document.getElementById("themeStyle");
|
|
|
|
var mainTheme = document.getElementById("mainThemeStyle");
|
|
|
|
|
2018-02-20 20:11:58 +01:00
|
|
|
var savedHref = [];
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function addClass(elem, className) {
|
2018-11-16 16:31:07 +01:00
|
|
|
if (!elem || !elem.classList) {
|
|
|
|
return;
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
2018-11-16 16:31:07 +01:00
|
|
|
elem.classList.add(className);
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeClass(elem, className) {
|
2018-11-16 16:31:07 +01:00
|
|
|
if (!elem || !elem.classList) {
|
|
|
|
return;
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
2018-11-16 16:31:07 +01:00
|
|
|
elem.classList.remove(className);
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isHidden(elem) {
|
2018-12-02 01:22:44 +01:00
|
|
|
return elem.offsetParent === null;
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 23:31:21 +02:00
|
|
|
function onEach(arr, func, reversed) {
|
2018-02-20 20:11:58 +01:00
|
|
|
if (arr && arr.length > 0 && func) {
|
2018-11-16 16:31:07 +01:00
|
|
|
var length = arr.length;
|
2018-09-12 23:31:21 +02:00
|
|
|
if (reversed !== true) {
|
2018-11-16 16:31:07 +01:00
|
|
|
for (var i = 0; i < length; ++i) {
|
2018-09-12 23:31:21 +02:00
|
|
|
if (func(arr[i]) === true) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-16 16:31:07 +01:00
|
|
|
for (var i = length - 1; i >= 0; --i) {
|
2018-09-12 23:31:21 +02:00
|
|
|
if (func(arr[i]) === true) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-20 20:11:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-04 16:16:53 +02:00
|
|
|
return false;
|
2018-02-20 20:11:58 +01:00
|
|
|
}
|
|
|
|
|
2018-12-02 01:22:44 +01:00
|
|
|
function onEachLazy(lazyArray, func, reversed) {
|
|
|
|
return onEach(
|
|
|
|
Array.prototype.slice.call(lazyArray),
|
|
|
|
func,
|
|
|
|
reversed);
|
|
|
|
}
|
|
|
|
|
2018-10-15 20:38:51 -07:00
|
|
|
function usableLocalStorage() {
|
|
|
|
// Check if the browser supports localStorage at all:
|
|
|
|
if (typeof(Storage) === "undefined") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Check if we can access it; this access will fail if the browser
|
|
|
|
// preferences deny access to localStorage, e.g., to prevent storage of
|
|
|
|
// "cookies" (or cookie-likes, as is the case here).
|
|
|
|
try {
|
2019-06-02 16:57:26 +02:00
|
|
|
return window.localStorage !== null && window.localStorage !== undefined;
|
2018-10-15 20:38:51 -07:00
|
|
|
} catch(err) {
|
|
|
|
// Storage is supported, but browser preferences deny access to it.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 22:15:27 -07:00
|
|
|
function updateLocalStorage(name, value) {
|
|
|
|
if (usableLocalStorage()) {
|
|
|
|
localStorage[name] = value;
|
|
|
|
} else {
|
|
|
|
// No Web Storage support so we do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentValue(name) {
|
|
|
|
if (usableLocalStorage() && localStorage[name] !== undefined) {
|
|
|
|
return localStorage[name];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-22 23:44:08 +01:00
|
|
|
function switchTheme(styleElem, mainStyleElem, newTheme) {
|
2018-02-24 19:14:36 +01:00
|
|
|
var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
|
|
|
|
var fullNewTheme = newTheme + resourcesSuffix + ".css";
|
|
|
|
var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
|
2018-02-20 20:11:58 +01:00
|
|
|
|
2018-03-08 00:11:17 +01:00
|
|
|
if (styleElem.href === newHref) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var found = false;
|
2018-02-20 20:11:58 +01:00
|
|
|
if (savedHref.length === 0) {
|
2018-12-16 14:44:44 +01:00
|
|
|
onEachLazy(document.getElementsByTagName("link"), function(el) {
|
2018-02-20 20:11:58 +01:00
|
|
|
savedHref.push(el.href);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
onEach(savedHref, function(el) {
|
|
|
|
if (el === newHref) {
|
|
|
|
found = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (found === true) {
|
|
|
|
styleElem.href = newHref;
|
2018-11-16 16:31:07 +01:00
|
|
|
updateLocalStorage("rustdoc-theme", newTheme);
|
2018-02-20 20:11:58 +01:00
|
|
|
}
|
2018-01-22 23:44:08 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 16:31:07 +01:00
|
|
|
switchTheme(currentTheme, mainTheme, getCurrentValue("rustdoc-theme") || "light");
|