Allow to not switch to a theme if it doesn't exist
This commit is contained in:
parent
2d3f31df8b
commit
5b61b615f5
2 changed files with 31 additions and 11 deletions
|
@ -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) {
|
function isHidden(elem) {
|
||||||
return (elem.offsetParent === null)
|
return (elem.offsetParent === null)
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,18 @@
|
||||||
var currentTheme = document.getElementById("themeStyle");
|
var currentTheme = document.getElementById("themeStyle");
|
||||||
var mainTheme = document.getElementById("mainThemeStyle");
|
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) {
|
function updateLocalStorage(name, value) {
|
||||||
if (typeof(Storage) !== "undefined") {
|
if (typeof(Storage) !== "undefined") {
|
||||||
localStorage[name] = value;
|
localStorage[name] = value;
|
||||||
|
@ -29,8 +41,24 @@ function getCurrentValue(name) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function switchTheme(styleElem, mainStyleElem, newTheme) {
|
function switchTheme(styleElem, mainStyleElem, newTheme) {
|
||||||
styleElem.href = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
|
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);
|
updateLocalStorage('rustdoc-theme', newTheme);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switchTheme(currentTheme, mainTheme, getCurrentValue('theme') || 'main');
|
switchTheme(currentTheme, mainTheme, getCurrentValue('rustdoc-theme') || 'main');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue