From c25d2e85ecaeecfb480a8e76b31b9fad826c8a60 Mon Sep 17 00:00:00 2001 From: senza <maximesenzamici@gmail.com> Date: Fri, 28 Aug 2020 12:51:54 +0200 Subject: [PATCH] clean console logs && replace any try catch's --- js/main.js | 112 ++++++++++++----------------------------------- js/polls-math.js | 35 +++++---------- 2 files changed, 38 insertions(+), 109 deletions(-) diff --git a/js/main.js b/js/main.js index bb41838..c033281 100644 --- a/js/main.js +++ b/js/main.js @@ -14,69 +14,33 @@ function firstLoad(){ //listeners for all forms const allForms = document.querySelectorAll("solid-form[id]"); - allForms.forEach(form => { form.addEventListener("populate",()=> { - console.log("form loaded",form); - globalFormListener(); + //console.log("form loaded",form); + globalFormSumbitListener(); }); }); - //Scripts dedicated to voting form + //Listener for voting form document.querySelector("solid-form#poll-votes-form").addEventListener("populate",()=> { //console.log("Vote form is populating"); disableVotes(); }); - //const mainPollSection = document.querySelectorAll("#poll-listing,#poll-page,#add-survey"); - //console.log("section is",mainPollSection); - //console.log("Main.js : Page",mainPollSection, "is populating"); - - - - //loadFormValidityScript(); - ////console.log("about to clear form"); - //clearFormErrors(mainPollSection); - - - function globalFormListener(){ + function globalFormSumbitListener(){ //event listener on all forms let submitButtons = document.querySelectorAll("input[type=submit]"); - console.log("setting event listener for", submitButtons); submitButtons.forEach(button => { + //listen on click & not submit to be able to check the data in the form button.addEventListener("click",()=>{ - //console.log("submit button clicked"); - let clickedFormId = button.parentElement.parentElement.id; - //console.log("form id of click is :",clickedFormId); + let clickedFormId = button.parentElement.parentElement.id; + //console.log("form id of click is :",clickedFormId); checkFormValidity(clickedFormId); }); - - //event listener on voting forms specifically if needed - /* - let voteForms = document.querySelectorAll("solid-form[id=poll-votes-form]"); - voteForms.forEach(voteForm => { - //console.log("listing for votes on : ",voteForm); - - try { - let voteButtons = voteForm.querySelector("input[type=submit]"); - //console.log("the buttons are",voteButtons); - - let clickedFormId = voteButtons.parentElement.parentElement.id; - - voteButtons.addEventListener("click",()=>{ - //console.log("voting button clicked"); - checkFormValidity(clickedFormId); - }); - } catch (error) { - //console.log(error); - } - }); - */ }); } }; - /* =================== JS For Tabs ================== */ @@ -113,8 +77,7 @@ function showHideBlocks(activeCTAClass,inactiveCTAClass,diplayBlockClass,hideBlo for (m = 0; m < inactiveCTAClass.length; m++) { inactiveCTAClass[m].classList.remove("active-tab"); } - } - +} /* =================== Add survey form @@ -133,30 +96,14 @@ function newOptionContent(){ for(y = 0; y < button.length; y++) { let buttonContent = button[i].textContent; //console.log("the content",buttonContent ); - button[i].textContent = "Add option"; //open 2 first options - //console.log("Open first option"); button[i].click(); - //console.log("Open second option"); button[i].click(); } } } -/* - SHARE POLL BY EMAIL -*/ -function sharePollEmail(){ - let shareEmails = document.querySelector("#share-emails").value; - let emailTitle = document.querySelector("h1").innerText; - let emailSubject = "Donnez votre avis sur " + emailTitle; - let pollURL = window.location.href; - let emailContent = "Bonjour, je vous invite à participer au vote sur " + emailTitle + ". \n Venez sur la page suivante pour voter : "+ pollURL; - emailContent = encodeURIComponent(emailContent); - window.open("mailto:"+shareEmails+"?subject="+emailSubject+"&body="+emailContent); -} - /*======================================= Form validation scripts =======================================*/ @@ -165,15 +112,12 @@ function sharePollEmail(){ // select all forms ***********/ function checkFormValidity(clickedFormId){ - let formId = clickedFormId; let form = document.querySelector("solid-form[id="+formId+"]"); - - console.log("current form id is : ",formId); + //console.log("current form id is : ",formId); var currenErrors = []; - //console.log("errors are", currenErrors); + //console.log("errors are", currenErrors); var errorDiv = form.nextElementSibling; - console.log("error div is", errorDiv); //check the fiels of the wanted forms switch(formId){ case "add-survey-form" : @@ -188,11 +132,8 @@ function sharePollEmail(){ CheckMandatoryRadioField("Choose an option to vote"); break; default: - //console.log("form not taken into account"); + console.log("form not taken into account"); } - - // check if any email fields & if format ok - //display errors showErrors(currenErrors,errorDiv,formId); @@ -265,7 +206,6 @@ function sharePollEmail(){ this.ErrorValue = ErrorValue; //select all inputs let formInput = form.querySelectorAll("input[type=radio]"); - console.log(formInput); let checkedButton = false; //see if any are checked buttons formInput.forEach(input => { @@ -287,7 +227,7 @@ function sharePollEmail(){ ***********/ function showErrors(errors,formId){ this.formId = formId; - console.log("herher",errors); + //console.log("errors are",errors); if (errors.length==1) { errorDiv.innerHTML = "The field \"" + currenErrors.join(", ") + "\" is mandatory"; errorDiv.classList.add("show","validation-error-style"); @@ -304,7 +244,7 @@ function sharePollEmail(){ else{ errorDiv.classList.remove("show","validation-error-style"); errorDiv.classList.add("validate"); - console.log("the form ID is",formId); + //console.log("the form ID is",formId); //personnalise confimation message depending on the form if (formId = "poll-votes-form" ){ @@ -320,17 +260,6 @@ function sharePollEmail(){ }, 10000); } } -function clearFormErrors(mainPollSection){ - //console.log("clear form started"); - try { - let mainPollSection = mainPollSection; - mainPollSection.forEach(section => { - - }); - } catch (error) { - //console.log("clearing error div's went wrong : ",error); - } -} /*********** disable voting if the startdate hasn't arrived or if the enddate has passed @@ -382,4 +311,17 @@ function disableVotes(){ submitButton.nextElementSibling.remove(); } } -} \ No newline at end of file +} + +/* + SHARE POLL BY EMAIL +*/ +function sharePollEmail(){ + let shareEmails = document.querySelector("#share-emails").value; + let emailTitle = document.querySelector("h1").innerText; + let emailSubject = "Donnez votre avis sur " + emailTitle; + let pollURL = window.location.href; + let emailContent = "Bonjour, je vous invite à participer au vote sur " + emailTitle + ". \n Venez sur la page suivante pour voter : "+ pollURL; + emailContent = encodeURIComponent(emailContent); + window.open("mailto:"+shareEmails+"?subject="+emailSubject+"&body="+emailContent); +} diff --git a/js/polls-math.js b/js/polls-math.js index d5c4bab..8026a8d 100644 --- a/js/polls-math.js +++ b/js/polls-math.js @@ -34,27 +34,18 @@ function firstLoad(){ Updates on navigate */ document.querySelector("solid-router").addEventListener("navigate",()=> { - - ////console.log("updating on navigation - math.js"); - try { - getPollTotalInfo(dataSourceDomain); - updateFormSource(); - }catch (error) { - //console.log("error on navigation : ",error); - } + getPollTotalInfo(dataSourceDomain); + updateFormSource(); + }); //refresh display of total votes on a poll document.querySelector("#poll-votes-values").addEventListener("populate",() => { - //console.log("refreshing total vote now"); refreshTotalVotes(); }); //refresh total votes when user votes document.querySelector("solid-form#poll-votes-form").addEventListener("save",()=> { - ////console.log("submitted new vote"); - setTimeout(function(){ - refreshVoteDataSrc(); - },500); + refreshVoteDataSrc(); }); /********* @@ -62,13 +53,15 @@ function firstLoad(){ *********/ function getPollTotalInfo(dataSourceDomain){ //get value of data-scr linked to poll page - const pollPage = document.querySelectorAll("#pollPage")[0]; + const pollPage = document.querySelector("#pollPage"); const pageTitle = pollPage.querySelectorAll("solid-display[id=title]"); ////console.log("page title is",pageTitle); //console.log("getting total poll info for the domaine",dataSourceDomain); for (let i = 0; i < pageTitle.length; i++){ - try { + //if to deal with error + if(pageTitle[i] == "" || null) continue + //get the data src node of first sib display & extract the url const dataSrc = pageTitle[i].dataset.src; //extract current poll ID from the data src @@ -79,11 +72,7 @@ function firstLoad(){ push it to the total polls data-src */ setCurrentPollOptions(dataSourceDomain,pollPage,pollId); - setTotalResultId(dataSourceDomain,pollPage,pollId); - - } catch (error) { - ////console.log("Poll total cannot be display due to : ",error); - } + setTotalResultId(dataSourceDomain,pollPage,pollId); } } /** @@ -107,9 +96,7 @@ function firstLoad(){ //add the poll option number within the URL of the options //get the form block const formContent = currentPage.querySelector("[id=poll-votes-form]"); - if(formContent == null){ - throw "there's no formContent with the id poll-votes-form"; - } + if(formContent == null) throw "there's no formContent with the id poll-votes-form"; //get the range for option choices const newOptionRange = dataSourceDomain+"/polls/"+currentId+"/pollOptions/"; //push new @@ -141,7 +128,7 @@ function firstLoad(){ totalCount = totalCount + currentValue; } else{ - ////console.log("it's Cheese NaN"); + console.log("it's Cheese NaN"); } } //launch function to display the number of votes in HTML -- GitLab