diff options
Diffstat (limited to 'target/doc/storage.js')
-rw-r--r-- | target/doc/storage.js | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/target/doc/storage.js b/target/doc/storage.js index 0aa1065..09511fa 100644 --- a/target/doc/storage.js +++ b/target/doc/storage.js @@ -1,3 +1,4 @@ +var resourcesSuffix = ""; /*! * Copyright 2018 The Rust Project Developers. See the COPYRIGHT * file at the top-level directory of this distribution and at @@ -13,6 +14,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 +42,30 @@ function getCurrentValue(name) { } function switchTheme(styleElem, mainStyleElem, newTheme) { - styleElem.href = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css"); - updateLocalStorage('theme', newTheme); + var fullBasicCss = "rustdoc" + resourcesSuffix + ".css"; + var fullNewTheme = newTheme + resourcesSuffix + ".css"; + var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme); + + if (styleElem.href === newHref) { + return; + } + + 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') || 'light'); |