diff --git a/js/main.js b/js/main.js index b5ea1599d225f73a5792f5bff69837c146c4a612..164985f622b9b02b1a30081b3a2b44d13c973dea 100644 --- a/js/main.js +++ b/js/main.js @@ -15,7 +15,7 @@ export function mainFirstLoad(idPrefix) { if (!firstElement) throw "no elements yet"; //Listener for voting form - baseElement.querySelectorAll("solid-form.poll-votes-form")[0].addEventListener("populate",() => { + baseElement.querySelector("solid-form.poll-votes-form").addEventListener("populate",() => { setTimeout( function() { disableVotes(baseElement); }); @@ -93,7 +93,7 @@ function disableVotes(baseElement) { //get the submit button - const votingSection = baseElement.querySelectorAll(".poll-votes-form")[0]; + const votingSection = baseElement.querySelector(".poll-votes-form"); const submitButton = votingSection.querySelector("input[type=submit]"); if (comparisonStartDate > date) { @@ -110,8 +110,8 @@ function disableVotes(baseElement) { activateButton(submitButton, errorMessage); } - const pollPage = baseElement.parentElement.querySelectorAll(".pollPage")[0]; - const pageTitle = pollPage.querySelectorAll("solid-display[class=title]")[0]; + const pollPage = baseElement.parentElement.querySelector(".pollPage"); + const pageTitle = pollPage.querySelector("solid-display[class=title]"); //get the data src node of first sib display & extract the url const dataSrc = pageTitle.dataset.src || (pageTitle.component.resource && await pageTitle.component.resource['@id']); diff --git a/js/math.js b/js/math.js index 8c4d78da67446f5e8d90b89a9edc9a16031eced6..9f71b78c2adae59e0b2ff2031e3f8f44fb79d7b2 100644 --- a/js/math.js +++ b/js/math.js @@ -18,7 +18,7 @@ export function mathFirstLoad(baseElementId){ mathFirstLoad(baseElementId); }, 500); } else { - let elementsLoaded = baseElement.querySelectorAll("solid-display")[0]; + let elementsLoaded = baseElement.querySelector("solid-display"); console.log("first base element is ", baseElement); const dataSourceURI = elementsLoaded.dataset.src; @@ -45,7 +45,7 @@ export function mathFirstLoad(baseElementId){ }); //refresh display of total votes on a poll - baseElement.parentElement.querySelectorAll(".poll-votes-values")[0].addEventListener("populate",() => { + baseElement.parentElement.querySelector(".poll-votes-values").addEventListener("populate",() => { //console.log("populating"); if(!baseElement.parentElement) { setTimeout(function() { @@ -57,7 +57,7 @@ export function mathFirstLoad(baseElementId){ }); //refresh total votes when user votes - baseElement.parentElement.querySelectorAll("solid-form.poll-votes-form")[0].addEventListener("save",()=> { + baseElement.parentElement.querySelector("solid-form.poll-votes-form").addEventListener("save",()=> { refreshVoteDataSrc(baseElement); }); @@ -67,8 +67,8 @@ export function mathFirstLoad(baseElementId){ async function getPollTotalInfo(baseElement, dataSourceDomain){ //get value of data-scr linked to poll page if (baseElement.parentElement) { - const pollPage = baseElement.parentElement.querySelectorAll(".pollPage")[0]; - const pageTitle = pollPage.querySelectorAll("solid-display[class=title]")[0]; + const pollPage = baseElement.parentElement.querySelector(".pollPage"); + const pageTitle = pollPage.querySelector("solid-display[class=title]"); //get the data src node of first sib display & extract the url const dataSrc = pageTitle.dataset.src || (pageTitle.component.resource && await pageTitle.component.resource['@id']); @@ -92,7 +92,7 @@ export function mathFirstLoad(baseElementId){ function setTotalResultId(dataSourceDomain,currentPage,currentId){ //get the vote blocks //console.log("setting currnt source"); - const currentPollValues = currentPage.querySelectorAll("[class=poll-votes-values]")[0]; + const currentPollValues = currentPage.querySelector("[class=poll-votes-values]"); //add the poll number const currentPollSrc = dataSourceDomain + "/polls/total_votes/" + currentId + "/"; //push the reconstructed data src to the HTML @@ -105,7 +105,7 @@ export function mathFirstLoad(baseElementId){ function setCurrentPollOptions(dataSourceDomain, currentPage, currentId){ //add the poll option number within the URL of the options //get the form block - const formContent = currentPage.querySelectorAll("[class=poll-votes-form]")[0]; + const formContent = currentPage.querySelector("[class=poll-votes-form]"); if (formContent == null) { throw "there's no formContent with the id poll-votes-form"; } else { @@ -162,9 +162,7 @@ export function mathFirstLoad(baseElementId){ if (!percentageBarField) continue; const progressBar = percentageBarField.querySelector(".progressBarValue"); if (!progressBar) continue; - let percent = currentValue / totalCount * 100; - if (Number.isNaN(percent)) percent = 0; - percent = percent.toFixed(2) + "%"; + let percent = (totalCount && currentValue / totalCount * 100).toFixed(2) + "%"; progressBar.innerHTML = percent; progressBar.style.width = percent; setChoiceColor(percentageBarField, choiceNumber); @@ -236,7 +234,7 @@ function displayTotalVotes(poll,totalCount){ const totalVoteContent = totalCount + " votes"; //update value in total - poll.previousElementSibling.querySelectorAll(".totalVotesDisplay")[0].textContent = totalVoteContent; + poll.previousElementSibling.querySelector(".totalVotesDisplay").textContent = totalVoteContent; } @@ -248,7 +246,7 @@ function displayTotalVotes(poll,totalCount){ //change content of button function newOptionContent(baseElement){ //get options section - let options = baseElement.querySelectorAll("solid-multiple-form[name=pollOptions]")[0]; + let options = baseElement.querySelector("solid-multiple-form[name=pollOptions]"); //console.log("options : ",options); for(let i = 0; i < options.length; i++) { let button = options[i].querySelectorAll("button");