]>
git.proxmox.com Git - rustc.git/blob - src/librustdoc/html/static/storage.js
2 /* global resourcesSuffix */
4 var darkThemes
= ["dark", "ayu"];
5 var currentTheme
= document
.getElementById("themeStyle");
6 var mainTheme
= document
.getElementById("mainThemeStyle");
8 var settingsDataset
= (function () {
9 var settingsElement
= document
.getElementById("default-settings");
10 if (settingsElement
=== null) {
13 var dataset
= settingsElement
.dataset
;
14 if (dataset
=== undefined) {
20 function getSettingValue(settingName
) {
21 var current
= getCurrentValue('rustdoc-' + settingName
);
22 if (current
!== null) {
25 if (settingsDataset
!== null) {
26 var def
= settingsDataset
[settingName
.replace(/-/g
,'_')];
27 if (def
!== undefined) {
34 var localStoredTheme
= getSettingValue("theme");
38 // eslint-disable-next-line no-unused-vars
39 function hasClass(elem
, className
) {
40 return elem
&& elem
.classList
&& elem
.classList
.contains(className
);
43 // eslint-disable-next-line no-unused-vars
44 function addClass(elem
, className
) {
45 if (!elem
|| !elem
.classList
) {
48 elem
.classList
.add(className
);
51 // eslint-disable-next-line no-unused-vars
52 function removeClass(elem
, className
) {
53 if (!elem
|| !elem
.classList
) {
56 elem
.classList
.remove(className
);
59 function onEach(arr
, func
, reversed
) {
60 if (arr
&& arr
.length
> 0 && func
) {
61 var length
= arr
.length
;
63 if (reversed
!== true) {
64 for (i
= 0; i
< length
; ++i
) {
65 if (func(arr
[i
]) === true) {
70 for (i
= length
- 1; i
>= 0; --i
) {
71 if (func(arr
[i
]) === true) {
80 function onEachLazy(lazyArray
, func
, reversed
) {
82 Array
.prototype.slice
.call(lazyArray
),
87 // eslint-disable-next-line no-unused-vars
88 function hasOwnProperty(obj
, property
) {
89 return Object
.prototype.hasOwnProperty
.call(obj
, property
);
92 function updateLocalStorage(name
, value
) {
94 window
.localStorage
.setItem(name
, value
);
96 // localStorage is not accessible, do nothing
100 function getCurrentValue(name
) {
102 return window
.localStorage
.getItem(name
);
108 function switchTheme(styleElem
, mainStyleElem
, newTheme
, saveTheme
) {
109 var fullBasicCss
= "rustdoc" + resourcesSuffix
+ ".css";
110 var fullNewTheme
= newTheme
+ resourcesSuffix
+ ".css";
111 var newHref
= mainStyleElem
.href
.replace(fullBasicCss
, fullNewTheme
);
113 // If this new value comes from a system setting or from the previously
114 // saved theme, no need to save it.
115 if (saveTheme
=== true) {
116 updateLocalStorage("rustdoc-theme", newTheme
);
119 if (styleElem
.href
=== newHref
) {
124 if (savedHref
.length
=== 0) {
125 onEachLazy(document
.getElementsByTagName("link"), function(el
) {
126 savedHref
.push(el
.href
);
129 onEach(savedHref
, function(el
) {
130 if (el
=== newHref
) {
135 if (found
=== true) {
136 styleElem
.href
= newHref
;
140 // This function is called from "theme.js", generated in `html/render/mod.rs`.
141 // eslint-disable-next-line no-unused-vars
142 function useSystemTheme(value
) {
143 if (value
=== undefined) {
147 updateLocalStorage("rustdoc-use-system-theme", value
);
149 // update the toggle if we're on the settings page
150 var toggle
= document
.getElementById("use-system-theme");
151 if (toggle
&& toggle
instanceof HTMLInputElement
) {
152 toggle
.checked
= value
;
156 var updateSystemTheme
= (function() {
157 if (!window
.matchMedia
) {
158 // fallback to the CSS computed value
160 var cssTheme
= getComputedStyle(document
.documentElement
)
161 .getPropertyValue('content');
166 JSON
.parse(cssTheme
) || "light",
172 // only listen to (prefers-color-scheme: dark) because light is the default
173 var mql
= window
.matchMedia("(prefers-color-scheme: dark)");
175 function handlePreferenceChange(mql
) {
176 // maybe the user has disabled the setting in the meantime!
177 if (getSettingValue("use-system-theme") !== "false") {
178 var lightTheme
= getSettingValue("preferred-light-theme") || "light";
179 var darkTheme
= getSettingValue("preferred-dark-theme") || "dark";
182 // prefers a dark theme
183 switchTheme(currentTheme
, mainTheme
, darkTheme
, true);
185 // prefers a light theme, or has no preference
186 switchTheme(currentTheme
, mainTheme
, lightTheme
, true);
189 // note: we save the theme so that it doesn't suddenly change when
190 // the user disables "use-system-theme" and reloads the page or
191 // navigates to another page
195 mql
.addListener(handlePreferenceChange
);
198 handlePreferenceChange(mql
);
202 if (getSettingValue("use-system-theme") !== "false" && window
.matchMedia
) {
203 // update the preferred dark theme if the user is already using a dark theme
204 // See https://github.com/rust-lang/rust/pull/77809#issuecomment-707875732
205 if (getSettingValue("use-system-theme") === null
206 && getSettingValue("preferred-dark-theme") === null
207 && darkThemes
.indexOf(localStoredTheme
) >= 0) {
208 updateLocalStorage("rustdoc-preferred-dark-theme", localStoredTheme
);
211 // call the function to initialize the theme at least once!
217 getSettingValue("theme") || "light",