]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/static/storage.js
Fix storage usage when disabled
[rust.git] / src / librustdoc / html / static / storage.js
1 // From rust:
2 /* global resourcesSuffix */
3
4 var currentTheme = document.getElementById("themeStyle");
5 var mainTheme = document.getElementById("mainThemeStyle");
6
7 var savedHref = [];
8
9 function hasClass(elem, className) {
10     return elem && elem.classList && elem.classList.contains(className);
11 }
12
13 function addClass(elem, className) {
14     if (!elem || !elem.classList) {
15         return;
16     }
17     elem.classList.add(className);
18 }
19
20 function removeClass(elem, className) {
21     if (!elem || !elem.classList) {
22         return;
23     }
24     elem.classList.remove(className);
25 }
26
27 function isHidden(elem) {
28     return elem.offsetParent === null;
29 }
30
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) {
37                     return true;
38                 }
39             }
40         } else {
41             for (var i = length - 1; i >= 0; --i) {
42                 if (func(arr[i]) === true) {
43                     return true;
44                 }
45             }
46         }
47     }
48     return false;
49 }
50
51 function onEachLazy(lazyArray, func, reversed) {
52     return onEach(
53         Array.prototype.slice.call(lazyArray),
54         func,
55         reversed);
56 }
57
58 function usableLocalStorage() {
59     // Check if the browser supports localStorage at all:
60     if (typeof(Storage) === "undefined") {
61         return false;
62     }
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).
66     try {
67         return window.localStorage !== null && window.localStorage !== undefined;
68     } catch(err) {
69         // Storage is supported, but browser preferences deny access to it.
70         return false;
71     }
72 }
73
74 function updateLocalStorage(name, value) {
75     if (usableLocalStorage()) {
76         localStorage[name] = value;
77     } else {
78         // No Web Storage support so we do nothing
79     }
80 }
81
82 function getCurrentValue(name) {
83     if (usableLocalStorage() && localStorage[name] !== undefined) {
84         return localStorage[name];
85     }
86     return null;
87 }
88
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);
93
94     if (styleElem.href === newHref) {
95         return;
96     }
97
98     var found = false;
99     if (savedHref.length === 0) {
100         onEachLazy(document.getElementsByTagName("link"), function(el) {
101             savedHref.push(el.href);
102         });
103     }
104     onEach(savedHref, function(el) {
105         if (el === newHref) {
106             found = true;
107             return true;
108         }
109     });
110     if (found === true) {
111         styleElem.href = newHref;
112         updateLocalStorage("rustdoc-theme", newTheme);
113     }
114 }
115
116 switchTheme(currentTheme, mainTheme, getCurrentValue("rustdoc-theme") || "light");