diff --git a/js/main.js b/js/main.js index 1a57b25b14487aebf915f0c072e46708fa9eb757..5a43980e24c9e8f44653d9f84ee380e1b4a24a6e 100644 --- a/js/main.js +++ b/js/main.js @@ -217,14 +217,13 @@ window.showHideBlocks = function(baseElementName, activeCTAClass,inactiveCTAClas disable voting if the startdate hasn't arrived or if the enddate has passed ***********/ function disableVotes(baseElement){ - let errorMessage = baseElement.querySelectorAll("div.unavailablePoll")[0]; - console.log('Error Message', baseElement, errorMessage); - if (errorMessage) return; //get start date & end date // let currentPoll = baseElement.querySelector("[id=pollPage]"); - console.log("current poll is :", baseElement); setTimeout(function() { + let errorMessage = baseElement.querySelectorAll("div.unavailablePoll")[0]; + if (errorMessage) throw "Not displaying message as it exists"; + let dateSeparator = "/"; let displayStartDate = baseElement.querySelector("solid-display-date[name=startDate]").textContent; let startDate = displayStartDate.split("/"); @@ -242,8 +241,6 @@ function disableVotes(baseElement){ let date = (today.getMonth()+1)+'/'+today.getDate()+'/'+today.getFullYear(); date = Date.parse(date); - console.log("startdate is",comparisonStartDate,'endate is',comparisonEndDate, "dateis",date); - //get the submit button let votingSection = baseElement.querySelectorAll(".poll-votes-form")[0]; let submitButton = votingSection.querySelector("input[type=submit]"); @@ -254,21 +251,22 @@ function disableVotes(baseElement){ } else if(comparisonEndDate < date) { let errorMessage = "This poll is closed since : " + displayEndDate+"."; deactivateButton(submitButton, errorMessage); - } else if(comparisonEndDate = date) { + } else if(comparisonEndDate === date) { let errorMessage = "Today is the last day to vote!"; - activateButton(submitButton); + activateButton(submitButton, errorMessage); } else { - activateButton(); + activateButton(submitButton); } //add deactivate attribute if needed function deactivateButton(submitButton, errorMessage){ submitButton.setAttribute("disabled",true); + submitButton.insertAdjacentHTML('afterend', '<div class="unavailablePoll">'+errorMessage+'</div>'); } //activate button & remove message - function activateButton(submitButton) { + function activateButton(submitButton, errorMessage) { submitButton.setAttribute("enabled",true); if (submitButton.nextElementSibling) { submitButton.nextElementSibling.remove(); diff --git a/js/math.js b/js/math.js index 994503379004030d1d663eb133d2ddc65353fd0f..f7fa6d2e703eb7766d1364e227775b229c87a6ab 100644 --- a/js/math.js +++ b/js/math.js @@ -48,8 +48,14 @@ export function mathFirstLoad(baseElementId){ //refresh display of total votes on a poll baseElement.parentElement.querySelectorAll(".poll-votes-values")[0].addEventListener("populate",() => { - //console.log("populating"); - refreshTotalVotes(baseElement.parentElement); + console.log("populating"); + if(!baseElement.parentElement) { + setTimeout(function() { + refreshTotalVotes(baseElement.parentElement); + }, 500); + } else { + refreshTotalVotes(baseElement.parentElement); + } }); //refresh total votes when user votes @@ -70,7 +76,6 @@ export function mathFirstLoad(baseElementId){ //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']); - console.log("data src is",dataSrc,"and title page is", pageTitle.textContent); if (dataSrc) { //console.log(pageTitle[i].dataset.src); //extract current poll ID from the data src @@ -116,7 +121,7 @@ export function mathFirstLoad(baseElementId){ //push new formContent.setAttribute('range-chosenoption',newOptionRange); console.log("form is now",formContent); - formContent.dataset.src += "?" + Math.random(); + formContent.dataset.src += "?" + Date.now(); } } @@ -132,56 +137,52 @@ export function mathFirstLoad(baseElementId){ ***********************************************************/ function refreshTotalVotes(baseElement){ //get all the polls - setTimeout(function() { - const pollVoteValues = baseElement.querySelectorAll(".poll-votes-values")[0]; - for (let i = 0; i < pollVoteValues.length; i++){ - //for each poll get the totals of each option - const voteOptionResults = pollVoteValues[i].querySelectorAll("[name=total_votes]"); - let totalCount = 0; - let choiceNumber = 0; - //add option total to the global tally - for(let y = 0; y < voteOptionResults.length; y++){ - const currentValue = parseInt(voteOptionResults[y].innerHTML); - if (!Number.isNaN(currentValue)){ - totalCount = totalCount + currentValue; - } - else{ - console.log("it's Cheese NaN"); - } - } - //launch function to display the number of votes in HTML - //console.log("about to refresh dsplayed total",totalCount); - displayTotalVotes(pollVoteValues[i],totalCount); + const pollVoteValues = baseElement.querySelectorAll(".poll-votes-values"); + for (let i = 0; i < pollVoteValues.length; i++) { + //for each poll get the totals of each option + const voteOptionResults = pollVoteValues[i].querySelectorAll("[name=total_votes]"); + let totalCount = 0; + let choiceNumber = 0; - /*now that we have the values & the totals we can calculate the %ge of each option - we will also take advantage of this step to launch the functions that set - the background color and the choice number*/ + //add option total to the global tally + for(let y = 0; y < voteOptionResults.length; y++){ + const currentValue = parseInt(voteOptionResults[y].innerHTML); + if (!Number.isNaN(currentValue)) { + totalCount = totalCount + currentValue; + } else { + console.log("it's Cheese NaN"); + } + } + //launch function to display the number of votes in HTML + //console.log("about to refresh dsplayed total",totalCount); + displayTotalVotes(pollVoteValues[i],totalCount); - for(let z = 0; z < voteOptionResults.length; z++){ - const currentValue = parseInt(voteOptionResults[z].innerHTML); + /*now that we have the values & the totals we can calculate the %ge of each option + we will also take advantage of this step to launch the functions that set + the background color and the choice number*/ - const percentageBarField = voteOptionResults[z].nextElementSibling; + for(let z = 0; z < voteOptionResults.length; z++){ + const currentValue = parseInt(voteOptionResults[z].innerHTML); - if (!Number.isNaN(currentValue)) { - const percentageOfTotal = ((currentValue/totalCount)*100).toFixed(2)+"%"; - percentageBarField.querySelector(".progressBarValue").innerHTML = percentageOfTotal; - percentageBarField.querySelector(".progressBarValue").style.width = percentageOfTotal; + const percentageBarField = voteOptionResults[z].nextElementSibling; - } - else{ - const percentageOfTotal = ""; - percentageBarField.querySelector(".progressBarValue").innerHTML = "0%"; - } - setChoiceColor(percentageBarField,choiceNumber); - choiceNumber = choiceNumber + 1; - if (choiceNumber > 3 ) { - choiceNumber = 1; - } + if (!Number.isNaN(currentValue)) { + const percentageOfTotal = ((currentValue/totalCount)*100).toFixed(2)+"%"; + percentageBarField.querySelector(".progressBarValue").innerHTML = percentageOfTotal; + percentageBarField.querySelector(".progressBarValue").style.width = percentageOfTotal; + } else { + const percentageOfTotal = ""; + percentageBarField.querySelector(".progressBarValue").innerHTML = "0%"; + } + setChoiceColor(percentageBarField,choiceNumber); + choiceNumber = choiceNumber + 1; + if (choiceNumber > 3 ) { + choiceNumber = 1; } - //launch the function that will display the choice number - setChoiceNumber(pollVoteValues[i]); } - }); + //launch the function that will display the choice number + setChoiceNumber(pollVoteValues[i]); + } } @@ -238,7 +239,7 @@ function setChoiceColor(currentProgressBar, choiceNumber){ function displayTotalVotes(poll,totalCount){ /*Add total votes values within the H3 prepared for the value*/ const totalVoteContent = totalCount + " votes"; - const previousSibling = poll.previousElementSibling.className; + //update value in total poll.previousElementSibling.querySelectorAll(".totalVotesDisplay")[0].textContent = totalVoteContent; } diff --git a/sib-polls.js b/sib-polls.js index 2ae6c3b3305d3384d8a9c69069661ae87e9e8032..021c76195207dd4aa35a64724e41222fca56b761 100644 --- a/sib-polls.js +++ b/sib-polls.js @@ -80,8 +80,6 @@ export class SolidPoll extends SolidTemplateElement { console.log(`Going through the template method X times with prefix being ${idPrefix}`); mainFirstLoad(idPrefix); mathFirstLoad(idPrefix + '-pollListing'); - // document.addEventListener("DOMContentLoaded", function() { - // }, idPrefix); return ` <solid-router use-hash default-route="${idPrefix}-pollListing">