|
96 | 96 | }); |
97 | 97 | } |
98 | 98 |
|
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) { |
100 | 109 | let output = ""; |
101 | 110 |
|
102 | | - onEach(settings, function(setting) { |
| 111 | + for (const setting of settings) { |
103 | 112 | output += `<div class="setting-line">`; |
104 | 113 | const js_data_name = setting["js_name"]; |
105 | 114 | const setting_name = setting["name"]; |
|
130 | 139 | <div>${setting_name}</div>`; |
131 | 140 | } |
132 | 141 | 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); |
169 | 142 | } |
170 | | - return el; |
| 143 | + return output; |
171 | 144 | } |
172 | 145 |
|
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() { |
174 | 152 | const themes = getVar("themes").split(","); |
175 | 153 | const settings = [ |
176 | 154 | { |
|
184 | 162 | "default": "light", |
185 | 163 | "options": themes, |
186 | 164 | }, |
187 | | - { |
188 | | - "name": "Preferred dark theme", |
189 | | - "js_name": "preferred-dark-theme", |
190 | | - "default": "dark", |
191 | | - "options": themes, |
192 | | - }, |
193 | 165 | { |
194 | 166 | "name": "Preferred light theme", |
195 | 167 | "js_name": "preferred-light-theme", |
196 | 168 | "default": "light", |
197 | 169 | "options": themes, |
198 | 170 | }, |
| 171 | + { |
| 172 | + "name": "Preferred dark theme", |
| 173 | + "js_name": "preferred-dark-theme", |
| 174 | + "default": "dark", |
| 175 | + "options": themes, |
| 176 | + }, |
199 | 177 | { |
200 | 178 | "name": "Auto-hide item contents for large items", |
201 | 179 | "js_name": "auto-hide-large-items", |
|
228 | 206 | }, |
229 | 207 | ]; |
230 | 208 |
|
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; |
232 | 242 | } |
233 | 243 |
|
234 | | - const settingsMenu = createSettingsPage(); |
| 244 | + const settingsMenu = buildSettingsPage(); |
235 | 245 |
|
236 | 246 | if (isSettingsPage) { |
237 | 247 | // We replace the existing "onclick" callback to do nothing if clicked. |
|
261 | 271 | if (!isSettingsPage) { |
262 | 272 | switchDisplayedElement(settingsMenu); |
263 | 273 | } |
264 | | - }, 10); |
| 274 | + }, 0); |
265 | 275 | })(); |
0 commit comments