]>
git.proxmox.com Git - rustc.git/blob - src/librustdoc/html/static/js/storage.js
1 var darkThemes
= ["dark", "ayu"];
2 window
.currentTheme
= document
.getElementById("themeStyle");
3 window
.mainTheme
= document
.getElementById("mainThemeStyle");
5 var settingsDataset
= (function () {
6 var settingsElement
= document
.getElementById("default-settings");
7 if (settingsElement
=== null) {
10 var dataset
= settingsElement
.dataset
;
11 if (dataset
=== undefined) {
17 function getSettingValue(settingName
) {
18 var current
= getCurrentValue(settingName
);
19 if (current
!== null) {
22 if (settingsDataset
!== null) {
23 // See the comment for `default_settings.into_iter()` etc. in
24 // `Options::from_matches` in `librustdoc/config.rs`.
25 var def
= settingsDataset
[settingName
.replace(/-/g
,'_')];
26 if (def
!== undefined) {
33 var localStoredTheme
= getSettingValue("theme");
37 // eslint-disable-next-line no-unused-vars
38 function hasClass(elem
, className
) {
39 return elem
&& elem
.classList
&& elem
.classList
.contains(className
);
42 // eslint-disable-next-line no-unused-vars
43 function addClass(elem
, className
) {
44 if (!elem
|| !elem
.classList
) {
47 elem
.classList
.add(className
);
50 // eslint-disable-next-line no-unused-vars
51 function removeClass(elem
, className
) {
52 if (!elem
|| !elem
.classList
) {
55 elem
.classList
.remove(className
);
59 * Run a callback for every element of an Array.
60 * @param {Array<?>} arr - The array to iterate over
61 * @param {function(?)} func - The callback
62 * @param {boolean} [reversed] - Whether to iterate in reverse
64 function onEach(arr
, func
, reversed
) {
65 if (arr
&& arr
.length
> 0 && func
) {
66 var length
= arr
.length
;
69 for (i
= length
- 1; i
>= 0; --i
) {
75 for (i
= 0; i
< length
; ++i
) {
86 * Turn an HTMLCollection or a NodeList into an Array, then run a callback
87 * for every element. This is useful because iterating over an HTMLCollection
88 * or a "live" NodeList while modifying it can be very slow.
89 * https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection
90 * https://developer.mozilla.org/en-US/docs/Web/API/NodeList
91 * @param {NodeList<?>|HTMLCollection<?>} lazyArray - An array to iterate over
92 * @param {function(?)} func - The callback
93 * @param {boolean} [reversed] - Whether to iterate in reverse
95 function onEachLazy(lazyArray
, func
, reversed
) {
97 Array
.prototype.slice
.call(lazyArray
),
102 // eslint-disable-next-line no-unused-vars
103 function hasOwnPropertyRustdoc(obj
, property
) {
104 return Object
.prototype.hasOwnProperty
.call(obj
, property
);
107 function updateLocalStorage(name
, value
) {
109 window
.localStorage
.setItem("rustdoc-" + name
, value
);
111 // localStorage is not accessible, do nothing
115 function getCurrentValue(name
) {
117 return window
.localStorage
.getItem("rustdoc-" + name
);
123 function switchTheme(styleElem
, mainStyleElem
, newTheme
, saveTheme
) {
124 var newHref
= mainStyleElem
.href
.replace(
125 /\/rustdoc([^/]*)\.css
/, "/" + newTheme + "$1" + ".css
");
127 // If this new value comes from a system setting or from the previously
128 // saved theme, no need to save it.
130 updateLocalStorage("theme
", newTheme);
133 if (styleElem.href === newHref) {
138 if (savedHref.length === 0) {
139 onEachLazy(document.getElementsByTagName("link
"), function(el) {
140 savedHref.push(el.href);
143 onEach(savedHref, function(el) {
144 if (el === newHref) {
150 styleElem.href = newHref;
154 // This function is called from "main
.js
".
155 // eslint-disable-next-line no-unused-vars
156 function useSystemTheme(value) {
157 if (value === undefined) {
161 updateLocalStorage("use-system
-theme
", value);
163 // update the toggle if we're on the settings page
164 var toggle = document.getElementById("use-system
-theme
");
165 if (toggle && toggle instanceof HTMLInputElement) {
166 toggle.checked = value;
170 var updateSystemTheme = (function() {
171 if (!window.matchMedia) {
172 // fallback to the CSS computed value
174 var cssTheme = getComputedStyle(document.documentElement)
175 .getPropertyValue('content');
180 JSON.parse(cssTheme) || "light
",
186 // only listen to (prefers-color-scheme: dark) because light is the default
187 var mql = window.matchMedia("(prefers
-color
-scheme
: dark
)");
189 function handlePreferenceChange(mql) {
190 let use = function(theme) {
191 switchTheme(window.currentTheme, window.mainTheme, theme, true);
193 // maybe the user has disabled the setting in the meantime!
194 if (getSettingValue("use-system
-theme
") !== "false") {
195 var lightTheme = getSettingValue("preferred
-light
-theme
") || "light
";
196 var darkTheme = getSettingValue("preferred
-dark
-theme
") || "dark
";
201 // prefers a light theme, or has no preference
204 // note: we save the theme so that it doesn't suddenly change when
205 // the user disables "use-system
-theme
" and reloads the page or
206 // navigates to another page
208 use(getSettingValue("theme
"));
212 mql.addListener(handlePreferenceChange);
215 handlePreferenceChange(mql);
219 function switchToSavedTheme() {
223 getSettingValue("theme
") || "light
",
228 if (getSettingValue("use-system
-theme
") !== "false" && window.matchMedia) {
229 // update the preferred dark theme if the user is already using a dark theme
230 // See https://github.com/rust-lang/rust/pull/77809#issuecomment-707875732
231 if (getSettingValue("use-system
-theme
") === null
232 && getSettingValue("preferred
-dark
-theme
") === null
233 && darkThemes.indexOf(localStoredTheme) >= 0) {
234 updateLocalStorage("preferred
-dark
-theme
", localStoredTheme);
237 // call the function to initialize the theme at least once!
240 switchToSavedTheme();
243 // If we navigate away (for example to a settings page), and then use the back or
244 // forward button to get back to a page, the theme may have changed in the meantime.
245 // But scripts may not be re-loaded in such a case due to the bfcache
246 // (https://web.dev/bfcache/). The "pageshow
" event triggers on such navigations.
247 // Use that opportunity to update the theme.
248 // We use a setTimeout with a 0 timeout here to put the change on the event queue.
249 // For some reason, if we try to change the theme while the `pageshow` event is
250 // running, it sometimes fails to take effect. The problem manifests on Chrome,
251 // specifically when talking to a remote website with no caching.
252 window.addEventListener("pageshow
", function(ev) {
254 setTimeout(switchToSavedTheme, 0);