]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/static/storage.js
Rollup merge of #74444 - Alexendoo:test-69414, r=nikomatsakis
[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 onEach(arr, func, reversed) {
28     if (arr && arr.length > 0 && func) {
29         var length = arr.length;
30         var i;
31         if (reversed !== true) {
32             for (i = 0; i < length; ++i) {
33                 if (func(arr[i]) === true) {
34                     return true;
35                 }
36             }
37         } else {
38             for (i = length - 1; i >= 0; --i) {
39                 if (func(arr[i]) === true) {
40                     return true;
41                 }
42             }
43         }
44     }
45     return false;
46 }
47
48 function onEachLazy(lazyArray, func, reversed) {
49     return onEach(
50         Array.prototype.slice.call(lazyArray),
51         func,
52         reversed);
53 }
54
55 function hasOwnProperty(obj, property) {
56     return Object.prototype.hasOwnProperty.call(obj, property);
57 }
58
59 function usableLocalStorage() {
60     // Check if the browser supports localStorage at all:
61     if (typeof Storage === "undefined") {
62         return false;
63     }
64     // Check if we can access it; this access will fail if the browser
65     // preferences deny access to localStorage, e.g., to prevent storage of
66     // "cookies" (or cookie-likes, as is the case here).
67     try {
68         return window.localStorage !== null && window.localStorage !== undefined;
69     } catch(err) {
70         // Storage is supported, but browser preferences deny access to it.
71         return false;
72     }
73 }
74
75 function updateLocalStorage(name, value) {
76     if (usableLocalStorage()) {
77         localStorage[name] = value;
78     } else {
79         // No Web Storage support so we do nothing
80     }
81 }
82
83 function getCurrentValue(name) {
84     if (usableLocalStorage() && localStorage[name] !== undefined) {
85         return localStorage[name];
86     }
87     return null;
88 }
89
90 function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
91     var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
92     var fullNewTheme = newTheme + resourcesSuffix + ".css";
93     var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
94
95     if (styleElem.href === newHref) {
96         return;
97     }
98
99     var found = false;
100     if (savedHref.length === 0) {
101         onEachLazy(document.getElementsByTagName("link"), function(el) {
102             savedHref.push(el.href);
103         });
104     }
105     onEach(savedHref, function(el) {
106         if (el === newHref) {
107             found = true;
108             return true;
109         }
110     });
111     if (found === true) {
112         styleElem.href = newHref;
113         // If this new value comes from a system setting or from the previously saved theme, no
114         // need to save it.
115         if (saveTheme === true) {
116             updateLocalStorage("rustdoc-theme", newTheme);
117         }
118     }
119 }
120
121 function getSystemValue() {
122     var property = getComputedStyle(document.documentElement).getPropertyValue('content');
123     return property.replace(/[\"\']/g, "");
124 }
125
126 switchTheme(currentTheme, mainTheme,
127             getCurrentValue("rustdoc-theme") || getSystemValue() || "light",
128             false);