Allow to not switch to a theme if it doesn't exist

This commit is contained in:
Guillaume Gomez 2018-02-20 20:11:58 +01:00
parent 2d3f31df8b
commit 5b61b615f5
2 changed files with 31 additions and 11 deletions

View File

@ -96,14 +96,6 @@
}
}
function onEach(arr, func) {
if (arr && arr.length > 0 && func) {
for (var i = 0; i < arr.length; i++) {
func(arr[i]);
}
}
}
function isHidden(elem) {
return (elem.offsetParent === null)
}

View File

@ -13,6 +13,18 @@
var currentTheme = document.getElementById("themeStyle");
var mainTheme = document.getElementById("mainThemeStyle");
var savedHref = [];
function onEach(arr, func) {
if (arr && arr.length > 0 && func) {
for (var i = 0; i < arr.length; i++) {
if (func(arr[i]) === true) {
break;
}
}
}
}
function updateLocalStorage(name, value) {
if (typeof(Storage) !== "undefined") {
localStorage[name] = value;
@ -29,8 +41,24 @@ function getCurrentValue(name) {
}
function switchTheme(styleElem, mainStyleElem, newTheme) {
styleElem.href = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
updateLocalStorage('rustdoc-theme', newTheme);
var newHref = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
var found = false;
if (savedHref.length === 0) {
onEach(document.getElementsByTagName("link"), function(el) {
savedHref.push(el.href);
});
}
onEach(savedHref, function(el) {
if (el === newHref) {
found = true;
return true;
}
});
if (found === true) {
styleElem.href = newHref;
updateLocalStorage('rustdoc-theme', newTheme);
}
}
switchTheme(currentTheme, mainTheme, getCurrentValue('theme') || 'main');
switchTheme(currentTheme, mainTheme, getCurrentValue('rustdoc-theme') || 'main');