Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an IIFE to Reduce Global Scope Pollution and some refactors #2116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions server/fishtest/static/js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,7 @@ function padDotVersionStr(dn) {
}

// Filters tables client-side
function filterTable(inputValue, tableId, originalRows, predicate) {
const table = document.getElementById(tableId);
function filterTable(inputValue, table, originalRows, predicate) {
const tbody = table.querySelector("tbody");
let noDataRow = table.querySelector(".no-data");
inputValue = inputValue.toLowerCase();
Expand Down Expand Up @@ -442,13 +441,12 @@ function filterTable(inputValue, tableId, originalRows, predicate) {
});
};

const originalTable = document
.getElementById("my_table")
.cloneNode(true);
const originalTable = document.getElementById("my_table").cloneNode(true);
const originalRows = Array.from(originalTable.querySelectorAll("tbody tr"));

const searchInput = document.getElementById("my_input");

searchInput.addEventListener("input", (e) => {
const myTable = document.getElementById("my_table");
filterTable(e.target.value, "my_table", originalRows, myPredicate);
});
})();
Expand Down
2 changes: 1 addition & 1 deletion server/fishtest/static/js/spsa.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
async function handleSPSA() {
async function handleSPSA(spsaData) {
let raw = [],
chart_object,
chart_data,
Expand Down
3 changes: 2 additions & 1 deletion server/fishtest/templates/contributors.mak
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@

const searchInput = document.getElementById("search_contributors");
searchInput.addEventListener("input", (e) => {
filterTable(e.target.value, "contributors_table", originalRows, includes);
const contributorsTable = document.getElementById("contributors_table");
filterTable(e.target.value, contributorsTable, originalRows, includes);
});
})();
</script>
Expand Down
134 changes: 68 additions & 66 deletions server/fishtest/templates/tests.mak
Original file line number Diff line number Diff line change
Expand Up @@ -47,72 +47,6 @@
</div>
</div>

<script>
let fetchedMachinesBefore = false;
let machinesSkeleton = null;
let machinesBody = null;
async function handleRenderMachines(){
await DOMContentLoaded();
machinesBody = document.getElementById("machines");
if (!machinesSkeleton) {
machinesSkeleton = document.querySelector("#machines .ssc-wrapper").cloneNode(true);
}
const machinesButton = document.getElementById("machines-button");
machinesButton?.addEventListener("click", async () => {
await toggleMachines();
})
if (${str(machines_shown).lower()})
await renderMachines();
}

async function renderMachines() {
await DOMContentLoaded();
if (fetchedMachinesBefore) {
return Promise.resolve();
}
try {
if (document.querySelector("#machines .retry")) {
machinesBody.replaceChildren(machinesSkeleton);
}
const html = await fetchText("/tests/machines");
machinesBody.replaceChildren();
machinesBody.insertAdjacentHTML("beforeend", html);
const machinesTbody = document.querySelector("#machines tbody");
let newMachinesCount = machinesTbody?.childElementCount;

if (newMachinesCount === 1) {
const noMachines = document.getElementById("no-machines");
if (noMachines) newMachinesCount = 0;
}

const countSpan = document.getElementById("workers-count");
countSpan.textContent = `Workers - ${"${newMachinesCount}"} machines`;
fetchedMachinesBefore = true;
} catch (error) {
console.log("Request failed: " + error);
machinesBody.replaceChildren();
createRetryMessage(machinesBody, renderMachines);
}
}

async function toggleMachines() {
const button = document.getElementById("machines-button");
const active = button.textContent.trim() === "Hide";
if (active) {
button.textContent = "Show";
}
else {
button.textContent = "Hide";
await renderMachines();
}

document.cookie =
"machines_state" + "=" + button.textContent.trim() + "; max-age=${60 * 60}; SameSite=Lax";
}

handleRenderMachines();
</script>

<h4>
<a id="machines-button" class="btn btn-sm btn-light border"
data-bs-toggle="collapse" href="#machines" role="button" aria-expanded="false"
Expand All @@ -138,6 +72,74 @@
</div>
</div>
</div>

<script>
(() => {
let fetchedMachinesBefore = false;
let machinesSkeleton = null;
let machinesBody = null;
async function handleRenderMachines(){
await DOMContentLoaded();
machinesBody = document.getElementById("machines");
if (!machinesSkeleton) {
machinesSkeleton = document.querySelector("#machines .ssc-wrapper").cloneNode(true);
}
const machinesButton = document.getElementById("machines-button");
machinesButton?.addEventListener("click", async () => {
await toggleMachines();
})
if (${str(machines_shown).lower()})
await renderMachines();
}

async function renderMachines() {
await DOMContentLoaded();
if (fetchedMachinesBefore) {
return Promise.resolve();
}
try {
if (document.querySelector("#machines .retry")) {
machinesBody.replaceChildren(machinesSkeleton);
}
const html = await fetchText("/tests/machines");
machinesBody.replaceChildren();
machinesBody.insertAdjacentHTML("beforeend", html);
const machinesTbody = document.querySelector("#machines tbody");
let newMachinesCount = machinesTbody?.childElementCount;

if (newMachinesCount === 1) {
const noMachines = document.getElementById("no-machines");
if (noMachines) newMachinesCount = 0;
}

const countSpan = document.getElementById("workers-count");
countSpan.textContent = `Workers - ${"${newMachinesCount}"} machines`;
fetchedMachinesBefore = true;
} catch (error) {
console.log("Request failed: " + error);
machinesBody.replaceChildren();
createRetryMessage(machinesBody, renderMachines);
}
}

async function toggleMachines() {
const button = document.getElementById("machines-button");
const active = button.textContent.trim() === "Hide";
if (active) {
button.textContent = "Show";
}
else {
button.textContent = "Hide";
await renderMachines();
}

document.cookie =
"machines_state" + "=" + button.textContent.trim() + "; max-age=${60 * 60}; SameSite=Lax";
}

handleRenderMachines();
})();
</script>
% endif

<%include file="run_tables.mak"/>
8 changes: 5 additions & 3 deletions server/fishtest/templates/tests_live_elo.mak
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
></script>

<script>
document.title = "Live Elo - ${page_title} | Stockfish Testing";
const testId = "${str(run['_id'])}";
followLive(testId);
(() => {
document.title = "Live Elo - ${page_title} | Stockfish Testing";
const testId = "${str(run['_id'])}";
followLive(testId);
})();
</script>

<h2>Live Elo for SPRT test <a href="/tests/view/${str(run['_id'])}">${str(run['_id'])}</a></h2>
Expand Down
Loading
Loading