diff --git a/voting.html b/voting.html index b5d34fb..5e78e34 100644 --- a/voting.html +++ b/voting.html @@ -351,64 +351,68 @@

Your RCV selection:

} } + // The RCV heavy lifter + function updateRCVContest(selectedItem, thisContestName, thisContestValue) { + // Create a new selected item with a remove button + const selectedText = selectedItem.textContent; + const newItem = document.createElement("li"); + newItem.classList.add("flex-item"); // Apply a class for styling + + // Create a remove button + const newButton = document.createElement("button"); + newButton.innerText = "remove"; + newButton.classList.add("remove"); + // add an event listener to the button + newButton.addEventListener("click", function (e) { + let itemName = e.target.parentNode.textContent.trim().replace(/\s+remove$/, ""); + e.target.parentNode.classList.remove("selected"); + console.log("Remove button eventListener: removing '" + itemName + "' (parentNode=" + e.target.parentNode.innerText + ")"); + // remove it from sortableList + e.target.parentNode.remove(); + // add to choiceList + setupChoiceList(thisContestName, thisContestValue, itemName); + }); + + // Create a re-order glyph + const svgIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg"); + svgIcon.setAttribute("width", "20"); + svgIcon.setAttribute("height", "20"); + svgIcon.setAttribute("viewBox", "0 -4 16 16"); + // svgIcon.setAttribute("preserveAspectRatio", "xMidYMid meet"); + svgIcon.setAttribute("fill", "currentColor"); + svgIcon.setAttribute("fill-rule", "evenodd"); + svgIcon.setAttribute("clip-rule", "evenodd"); + // the actual svg + svgIcon.innerHTML = ``; + + // Create the text element + const textElement = document.createElement("span"); + textElement.innerText = selectedText; + textElement.innerHTML += extraSpace; + + // Append everything to the li node + newItem.appendChild(svgIcon); + newItem.appendChild(textElement); + newItem.appendChild(newButton); + + // Add the class to the li node + newItem.classList.add("selected"); + // ... and append that to the bottom of sortableList + sortableList.appendChild(newItem); + // Remove the containing li for the selected item from the first list + selectedItem.remove(); + // Initialize the sortable list + initSortableList(document.getElementById("sortableList")); + } + // RCV event listeners function setupRCVEventListeners(thisContestName, thisContestValue) { // Event listener for selection in the first list (when a candidate is selected) choiceList.addEventListener("click", (event) => { if (event.target.closest("li") && event.target.closest("li").classList.contains("unselected")) { console.log("Running RCV eventListener:"); - // Create a new selected item with a remove button - const selectedText = event.target.closest("li").textContent; - const newItem = document.createElement("li"); - newItem.classList.add("flex-item"); // Apply a class for styling - - // Create a remove button - const newButton = document.createElement("button"); - newButton.innerText = "remove"; - newButton.classList.add("remove"); - // add an event listener to the button - newButton.addEventListener("click", function (e) { - let itemName = e.target.parentNode.textContent.trim().replace(/\s+remove$/, ""); - e.target.parentNode.classList.remove("selected"); - console.log("Remove button eventListener: removing '" + itemName + "' (parentNode=" + e.target.parentNode.innerText + ")"); - // remove it from sortableList - e.target.parentNode.remove(); - // add to choiceList - setupChoiceList(thisContestName, thisContestValue, itemName); - }); - - // Create a re-order glyph - const svgIcon = document.createElementNS("http://www.w3.org/2000/svg", "svg"); - svgIcon.setAttribute("width", "20"); - svgIcon.setAttribute("height", "20"); - svgIcon.setAttribute("viewBox", "0 -4 16 16"); - // svgIcon.setAttribute("preserveAspectRatio", "xMidYMid meet"); - svgIcon.setAttribute("fill", "currentColor"); - svgIcon.setAttribute("fill-rule", "evenodd"); - svgIcon.setAttribute("clip-rule", "evenodd"); - // the actual svg - svgIcon.innerHTML = ``; - - // Create the text element - const textElement = document.createElement("span"); - textElement.innerText = selectedText; - textElement.innerHTML += extraSpace; - - // Append everything to the li node - newItem.appendChild(svgIcon); - newItem.appendChild(textElement); - newItem.appendChild(newButton); - - // Add the class to the li node - newItem.classList.add("selected"); - // ... and append that to the bottom of sortableList - sortableList.appendChild(newItem); - - // Remove the containing li for the selected item from the first list - event.target.closest("li").remove(); - - // Initialize the sortable list - initSortableList(document.getElementById("sortableList")); + // Update the RCV contest + updateRCVContest(event.target.closest("li"), thisContestName, thisContestValue); } }); } @@ -660,16 +664,23 @@

Your RCV selection:

// everything out, so to restore we only need to add the classes // to the correct li if (thisContestValue.selection) { + // loop over selection in order for (const selection of thisContestValue.selection) { const indexName = splitSelection(selection); // Find the choice in choiceList that matches for (const choice of choiceList.children) { if (choice.getAttribute("thename") == indexName[1]) { - choice.classList.add("selected"); - choice.classList.remove("unselected"); - // get the svg (first child) and set fill to on (black) - choice.firstElementChild.style.fill = "black" - selectedCount++; + if (thisContestValue.tally == "plurality") { + // plurality is easier enough to manually update + choice.classList.add("selected"); + choice.classList.remove("unselected"); + // get the svg (first child) and set fill to on (black) + choice.firstElementChild.style.fill = "black" + selectedCount++; + } else { + // Need to manually update the RCV contest + updateRCVContest(choice, thisContestName, thisContestValue) + } console.log("restored " + indexName[0] + ", " + indexName[1]); } }