MCPcopy Index your code
hub / github.com/librespeed/speedtest / populateDropdown

Function populateDropdown

frontend/javascript/index.js:202–245  ·  view source on GitHub ↗

* Add all the servers to the server selection dropdown and make it actually * work. * @param {Array} servers - an array of server objects

(servers)

Source from the content-addressed store, hash-verified

200 * @param {Array} servers - an array of server objects
201 */
202function populateDropdown(servers) {
203 const serverSelector = document.querySelector("div.server-selector");
204 const serverList = serverSelector.querySelector("ul.servers");
205
206 // Reset previous state (populateDropdown can be called multiple times)
207 serverSelector.classList.remove("single-server");
208 serverSelector.classList.remove("active");
209 serverList.classList.remove("active");
210 serverList.innerHTML = "";
211
212 // If we have only a single server, just show it
213 if (servers.length === 1) {
214 serverSelector.classList.add("single-server");
215 selectServer(servers[0]);
216 return;
217 }
218 serverSelector.classList.add("active");
219
220 // Make the dropdown open and close (hook only once)
221 if (serverSelector.dataset.hooked !== "1") {
222 serverSelector.dataset.hooked = "1";
223
224 serverSelector.addEventListener("click", () => {
225 serverList.classList.toggle("active");
226 });
227 document.addEventListener("click", (e) => {
228 if (e.target.closest("div.server-selector") !== serverSelector)
229 serverList.classList.remove("active");
230 });
231 }
232
233 // Populate the list to choose from
234 servers.forEach((server) => {
235 const item = document.createElement("li");
236 const link = document.createElement("a");
237 link.href = "#";
238 link.innerHTML = `${server.name}${
239 server.sponsorName ? ` <span>(${server.sponsorName})</span>` : ""
240 }`;
241 link.addEventListener("click", () => selectServer(server));
242 item.appendChild(link);
243 serverList.appendChild(item);
244 });
245}
246
247/**
248 * Set the given server as the selected server for the speedtest

Callers 1

applyServerListJSONFunction · 0.85

Calls 1

selectServerFunction · 0.85

Tested by

no test coverage detected