Skip to content

Commit 73688e4

Browse files
* Add documentation for settings page rendering functions.
* Improve code. * Fix some documentation argument types. * Make settings order the same as before this PR. * Change timeout to 0 so that browser will render it as fast as possible.
1 parent 2f074de commit 73688e4

File tree

2 files changed

+65
-55
lines changed

2 files changed

+65
-55
lines changed

src/librustdoc/html/static/js/main.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ window.hideSettings = function() {
206206
* This function inserts `newNode` after `referenceNode`. It doesn't work if `referenceNode`
207207
* doesn't have a parent node.
208208
*
209-
* @param {DOM} newNode
210-
* @param {DOM} referenceNode
209+
* @param {HTMLElement} newNode
210+
* @param {HTMLElement} referenceNode
211211
*/
212212
function insertAfter(newNode, referenceNode) {
213213
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
@@ -237,7 +237,7 @@ function getOrCreateSection(id, classes) {
237237
/**
238238
* Returns the `<section>` element which contains the displayed element.
239239
*
240-
* @return {DOM}
240+
* @return {HTMLElement}
241241
*/
242242
function getAlternativeDisplayElem() {
243243
return getOrCreateSection(ALTERNATIVE_DISPLAY_ID, "content hidden");
@@ -246,7 +246,7 @@ function getAlternativeDisplayElem() {
246246
/**
247247
* Returns the `<section>` element which contains the not-displayed elements.
248248
*
249-
* @return {DOM}
249+
* @return {HTMLElement}
250250
*/
251251
function getNotDisplayedElem() {
252252
return getOrCreateSection(NOT_DISPLAYED_ID, "hidden");
@@ -255,11 +255,11 @@ function getNotDisplayedElem() {
255255
/**
256256
* To nicely switch between displayed "extra" elements (such as search results or settings menu)
257257
* and to alternate between the displayed and not displayed elements, we hold them in two different
258-
* `<section>` elements. They work in pair: one hold the not displayed elements while the other
258+
* `<section>` elements. They work in pair: one holds the hidden elements while the other
259259
* contains the displayed element (there can be only one at the same time!). So basically, we switch
260260
* elements between the two `<section>` elements.
261261
*
262-
* @param {DOM} elemToDisplay
262+
* @param {HTMLElement} elemToDisplay
263263
*/
264264
function switchDisplayedElement(elemToDisplay) {
265265
const el = getAlternativeDisplayElem();

src/librustdoc/html/static/js/settings.js

+59-49
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,19 @@
9696
});
9797
}
9898

99-
function buildSettings(settings) {
99+
/**
100+
* This function builds the sections inside the "settings page". It takes a `settings` list
101+
* as argument which describes each setting and how to render it. It returns a string
102+
* representing the raw HTML.
103+
*
104+
* @param {Array<Object>} settings
105+
*
106+
* @return {string}
107+
*/
108+
function buildSettingsPageSections(settings) {
100109
let output = "";
101110

102-
onEach(settings, function(setting) {
111+
for (const setting of settings) {
103112
output += `<div class="setting-line">`;
104113
const js_data_name = setting["js_name"];
105114
const setting_name = setting["name"];
@@ -130,47 +139,16 @@
130139
<div>${setting_name}</div>`;
131140
}
132141
output += "</div>";
133-
});
134-
return output;
135-
}
136-
137-
function buildSettingsPage(settings) {
138-
// First, we add the settings.css file.
139-
loadCss("settings");
140-
141-
// Then we build the DOM.
142-
const el = document.createElement("section");
143-
el.id = "settings";
144-
let innerHTML = `
145-
<div class="main-heading">
146-
<h1 class="fqn">
147-
<span class="in-band">Rustdoc settings</span>
148-
</h1>
149-
<span class="out-of-band">`;
150-
151-
if (isSettingsPage) {
152-
innerHTML +=
153-
`<a id="back" href="javascript:void(0)" onclick="history.back();">Back</a>`;
154-
} else {
155-
innerHTML +=
156-
`<a id="back" href="javascript:void(0)" onclick="switchDisplayedElement(null);">\
157-
Back</a>`;
158-
}
159-
innerHTML += `</span>
160-
</div>
161-
<div class="settings">${buildSettings(settings)}</div>`;
162-
163-
el.innerHTML = innerHTML;
164-
165-
if (isSettingsPage) {
166-
document.getElementById(MAIN_ID).appendChild(el);
167-
} else {
168-
getNotDisplayedElem().appendChild(el);
169142
}
170-
return el;
143+
return output;
171144
}
172145

173-
function createSettingsPage() {
146+
/**
147+
* This function builds the "settings page" and returns the generated HTML element.
148+
*
149+
* @return {HTMLElement}
150+
*/
151+
function buildSettingsPage() {
174152
const themes = getVar("themes").split(",");
175153
const settings = [
176154
{
@@ -184,18 +162,18 @@
184162
"default": "light",
185163
"options": themes,
186164
},
187-
{
188-
"name": "Preferred dark theme",
189-
"js_name": "preferred-dark-theme",
190-
"default": "dark",
191-
"options": themes,
192-
},
193165
{
194166
"name": "Preferred light theme",
195167
"js_name": "preferred-light-theme",
196168
"default": "light",
197169
"options": themes,
198170
},
171+
{
172+
"name": "Preferred dark theme",
173+
"js_name": "preferred-dark-theme",
174+
"default": "dark",
175+
"options": themes,
176+
},
199177
{
200178
"name": "Auto-hide item contents for large items",
201179
"js_name": "auto-hide-large-items",
@@ -228,10 +206,42 @@
228206
},
229207
];
230208

231-
return buildSettingsPage(settings);
209+
// First, we add the settings.css file.
210+
loadCss("settings");
211+
212+
// Then we build the DOM.
213+
const el = document.createElement("section");
214+
el.id = "settings";
215+
let innerHTML = `
216+
<div class="main-heading">
217+
<h1 class="fqn">
218+
<span class="in-band">Rustdoc settings</span>
219+
</h1>
220+
<span class="out-of-band">`;
221+
222+
if (isSettingsPage) {
223+
innerHTML +=
224+
`<a id="back" href="javascript:void(0)" onclick="history.back();">Back</a>`;
225+
} else {
226+
innerHTML +=
227+
`<a id="back" href="javascript:void(0)" onclick="switchDisplayedElement(null);">\
228+
Back</a>`;
229+
}
230+
innerHTML += `</span>
231+
</div>
232+
<div class="settings">${buildSettingsPageSections(settings)}</div>`;
233+
234+
el.innerHTML = innerHTML;
235+
236+
if (isSettingsPage) {
237+
document.getElementById(MAIN_ID).appendChild(el);
238+
} else {
239+
getNotDisplayedElem().appendChild(el);
240+
}
241+
return el;
232242
}
233243

234-
const settingsMenu = createSettingsPage();
244+
const settingsMenu = buildSettingsPage();
235245

236246
if (isSettingsPage) {
237247
// We replace the existing "onclick" callback to do nothing if clicked.
@@ -261,5 +271,5 @@
261271
if (!isSettingsPage) {
262272
switchDisplayedElement(settingsMenu);
263273
}
264-
}, 10);
274+
}, 0);
265275
})();

0 commit comments

Comments
 (0)