Skip to content

Commit 2e10475

Browse files
committed
rustdoc: js: Use getSettingValue for all rustdoc-* values
Currently, storage.js and main.js have many open-coded calls to getCurrentValue for "rustdoc-" values, but these are settings and should be handled by getSettingValue. So make getSettingValue part of storage.js (where everyone can call it) and use it everywhere. No functional change yet. We are going to make getSettingValue do something more sophisticated in a moment. Signed-off-by: Ian Jackson <[email protected]>
1 parent 2eb4fc8 commit 2e10475

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

src/librustdoc/html/static/main.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function defocusSearchBar() {
8989
"derive",
9090
"traitalias"];
9191

92-
var disableShortcuts = getCurrentValue("rustdoc-disable-shortcuts") === "true";
92+
var disableShortcuts = getSettingValue("disable-shortcuts") === "true";
9393
var search_input = getSearchInput();
9494
var searchTimeout = null;
9595
var toggleAllDocsId = "toggle-all-docs";
@@ -1580,7 +1580,7 @@ function defocusSearchBar() {
15801580
function showResults(results) {
15811581
var search = getSearchElement();
15821582
if (results.others.length === 1
1583-
&& getCurrentValue("rustdoc-go-to-only-result") === "true"
1583+
&& getSettingValue("go-to-only-result") === "true"
15841584
// By default, the search DOM element is "empty" (meaning it has no children not
15851585
// text content). Once a search has been run, it won't be empty, even if you press
15861586
// ESC or empty the search input (which also "cancels" the search).
@@ -2296,7 +2296,7 @@ function defocusSearchBar() {
22962296
function autoCollapse(pageId, collapse) {
22972297
if (collapse) {
22982298
toggleAllDocs(pageId, true);
2299-
} else if (getCurrentValue("rustdoc-auto-hide-trait-implementations") !== "false") {
2299+
} else if (getSettingValue("auto-hide-trait-implementations") !== "false") {
23002300
var impl_list = document.getElementById("trait-implementations-list");
23012301

23022302
if (impl_list !== null) {
@@ -2370,8 +2370,8 @@ function defocusSearchBar() {
23702370
}
23712371

23722372
var toggle = createSimpleToggle(false);
2373-
var hideMethodDocs = getCurrentValue("rustdoc-auto-hide-method-docs") === "true";
2374-
var hideImplementors = getCurrentValue("rustdoc-auto-collapse-implementors") !== "false";
2373+
var hideMethodDocs = getSettingValue("auto-hide-method-docs") === "true";
2374+
var hideImplementors = getSettingValue("auto-collapse-implementors") !== "false";
23752375
var pageId = getPageId();
23762376

23772377
var func = function(e) {
@@ -2487,15 +2487,15 @@ function defocusSearchBar() {
24872487
});
24882488
}
24892489
}
2490-
var showItemDeclarations = getCurrentValue("rustdoc-auto-hide-" + className);
2490+
var showItemDeclarations = getSettingValue("auto-hide-" + className);
24912491
if (showItemDeclarations === null) {
24922492
if (className === "enum" || className === "macro") {
24932493
showItemDeclarations = "false";
24942494
} else if (className === "struct" || className === "union" || className === "trait") {
24952495
showItemDeclarations = "true";
24962496
} else {
24972497
// In case we found an unknown type, we just use the "parent" value.
2498-
showItemDeclarations = getCurrentValue("rustdoc-auto-hide-declarations");
2498+
showItemDeclarations = getSettingValue("auto-hide-declarations");
24992499
}
25002500
}
25012501
showItemDeclarations = showItemDeclarations === "false";
@@ -2569,7 +2569,7 @@ function defocusSearchBar() {
25692569
onEachLazy(document.getElementsByClassName("sub-variant"), buildToggleWrapper);
25702570
var pageId = getPageId();
25712571

2572-
autoCollapse(pageId, getCurrentValue("rustdoc-collapse") === "true");
2572+
autoCollapse(pageId, getSettingValue("collapse") === "true");
25732573

25742574
if (pageId !== null) {
25752575
expandSection(pageId);
@@ -2592,7 +2592,7 @@ function defocusSearchBar() {
25922592
(function() {
25932593
// To avoid checking on "rustdoc-item-attributes" value on every loop...
25942594
var itemAttributesFunc = function() {};
2595-
if (getCurrentValue("rustdoc-auto-hide-attributes") !== "false") {
2595+
if (getSettingValue("auto-hide-attributes") !== "false") {
25962596
itemAttributesFunc = function(x) {
25972597
collapseDocs(x.previousSibling.childNodes[0], "toggle");
25982598
};
@@ -2611,7 +2611,7 @@ function defocusSearchBar() {
26112611
(function() {
26122612
// To avoid checking on "rustdoc-line-numbers" value on every loop...
26132613
var lineNumbersFunc = function() {};
2614-
if (getCurrentValue("rustdoc-line-numbers") === "true") {
2614+
if (getSettingValue("line-numbers") === "true") {
26152615
lineNumbersFunc = function(x) {
26162616
var count = x.textContent.split("\n").length;
26172617
var elems = [];
@@ -2768,7 +2768,7 @@ function defocusSearchBar() {
27682768
}
27692769
return 0;
27702770
});
2771-
var savedCrate = getCurrentValue("rustdoc-saved-filter-crate");
2771+
var savedCrate = getSettingValue("saved-filter-crate");
27722772
for (var i = 0; i < crates_text.length; ++i) {
27732773
var option = document.createElement("option");
27742774
option.value = crates_text[i];

src/librustdoc/html/static/settings.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
}
1515
}
1616

17-
function getSettingValue(settingName) {
18-
return getCurrentValue("rustdoc-" + settingName);
19-
}
20-
2117
function setEvents() {
2218
var elems = {
2319
toggles: document.getElementsByClassName("slider"),

src/librustdoc/html/static/storage.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
// From rust:
2-
/* global resourcesSuffix */
2+
/* global resourcesSuffix, getSettingValue */
33

44
var darkThemes = ["dark", "ayu"];
55
var currentTheme = document.getElementById("themeStyle");
66
var mainTheme = document.getElementById("mainThemeStyle");
7-
var localStoredTheme = getCurrentValue("rustdoc-theme");
7+
8+
function getSettingValue(settingName) {
9+
return getCurrentValue('rustdoc-' + settingName);
10+
}
11+
12+
var localStoredTheme = getSettingValue("theme");
813

914
var savedHref = [];
1015

@@ -156,9 +161,9 @@ var updateSystemTheme = (function() {
156161

157162
function handlePreferenceChange(mql) {
158163
// maybe the user has disabled the setting in the meantime!
159-
if (getCurrentValue("rustdoc-use-system-theme") !== "false") {
160-
var lightTheme = getCurrentValue("rustdoc-preferred-light-theme") || "light";
161-
var darkTheme = getCurrentValue("rustdoc-preferred-dark-theme") || "dark";
164+
if (getSettingValue("use-system-theme") !== "false") {
165+
var lightTheme = getSettingValue("preferred-light-theme") || "light";
166+
var darkTheme = getSettingValue("preferred-dark-theme") || "dark";
162167

163168
if (mql.matches) {
164169
// prefers a dark theme
@@ -181,11 +186,11 @@ var updateSystemTheme = (function() {
181186
};
182187
})();
183188

184-
if (getCurrentValue("rustdoc-use-system-theme") !== "false" && window.matchMedia) {
189+
if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
185190
// update the preferred dark theme if the user is already using a dark theme
186191
// See https://github.com/rust-lang/rust/pull/77809#issuecomment-707875732
187-
if (getCurrentValue("rustdoc-use-system-theme") === null
188-
&& getCurrentValue("rustdoc-preferred-dark-theme") === null
192+
if (getSettingValue("use-system-theme") === null
193+
&& getSettingValue("preferred-dark-theme") === null
189194
&& darkThemes.indexOf(localStoredTheme) >= 0) {
190195
updateLocalStorage("rustdoc-preferred-dark-theme", localStoredTheme);
191196
}
@@ -196,7 +201,7 @@ if (getCurrentValue("rustdoc-use-system-theme") !== "false" && window.matchMedia
196201
switchTheme(
197202
currentTheme,
198203
mainTheme,
199-
getCurrentValue("rustdoc-theme") || "light",
204+
getSettingValue("theme") || "light",
200205
false
201206
);
202207
}

0 commit comments

Comments
 (0)