2 /* global resourcesSuffix */
4 var currentTheme = document.getElementById("themeStyle");
5 var mainTheme = document.getElementById("mainThemeStyle");
9 function hasClass(elem, className) {
10 return elem && elem.classList && elem.classList.contains(className);
13 function addClass(elem, className) {
14 if (!elem || !elem.classList) {
17 elem.classList.add(className);
20 function removeClass(elem, className) {
21 if (!elem || !elem.classList) {
24 elem.classList.remove(className);
27 function isHidden(elem) {
28 return elem.offsetParent === null;
31 function onEach(arr, func, reversed) {
32 if (arr && arr.length > 0 && func) {
33 var length = arr.length;
34 if (reversed !== true) {
35 for (var i = 0; i < length; ++i) {
36 if (func(arr[i]) === true) {
41 for (var i = length - 1; i >= 0; --i) {
42 if (func(arr[i]) === true) {
51 function onEachLazy(lazyArray, func, reversed) {
53 Array.prototype.slice.call(lazyArray),
58 function usableLocalStorage() {
59 // Check if the browser supports localStorage at all:
60 if (typeof(Storage) === "undefined") {
63 // Check if we can access it; this access will fail if the browser
64 // preferences deny access to localStorage, e.g., to prevent storage of
65 // "cookies" (or cookie-likes, as is the case here).
67 return window.localStorage !== null && window.localStorage !== undefined;
69 // Storage is supported, but browser preferences deny access to it.
74 function updateLocalStorage(name, value) {
75 if (usableLocalStorage()) {
76 localStorage[name] = value;
78 // No Web Storage support so we do nothing
82 function getCurrentValue(name) {
83 if (usableLocalStorage() && localStorage[name] !== undefined) {
84 return localStorage[name];
89 function switchTheme(styleElem, mainStyleElem, newTheme) {
90 var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
91 var fullNewTheme = newTheme + resourcesSuffix + ".css";
92 var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
94 if (styleElem.href === newHref) {
99 if (savedHref.length === 0) {
100 onEachLazy(document.getElementsByTagName("link"), function(el) {
101 savedHref.push(el.href);
104 onEach(savedHref, function(el) {
105 if (el === newHref) {
110 if (found === true) {
111 styleElem.href = newHref;
112 updateLocalStorage("rustdoc-theme", newTheme);
116 switchTheme(currentTheme, mainTheme, getCurrentValue("rustdoc-theme") || "light");