2020-10-17 18:40:18 +11:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>pyRCV2 test</title>
|
2020-10-18 00:23:25 +11:00
|
|
|
<style type="text/css">
|
|
|
|
html, body {
|
|
|
|
font-family: 'Liberation Sans', FreeSans, Helvetica, Arial, sans-serif;
|
|
|
|
}
|
|
|
|
table {
|
|
|
|
border-collapse: collapse;
|
|
|
|
}
|
|
|
|
td {
|
|
|
|
padding: 0px 8px;
|
|
|
|
}
|
|
|
|
td.count {
|
|
|
|
text-align: right;
|
|
|
|
}
|
|
|
|
tr:first-child td {
|
|
|
|
vertical-align: bottom;
|
|
|
|
}
|
|
|
|
td.excluded {
|
|
|
|
background-color: #fecfcfff;
|
|
|
|
}
|
|
|
|
td.elected {
|
|
|
|
background-color: #d1fca7ff;
|
|
|
|
}
|
|
|
|
tr.info td {
|
|
|
|
background-color: #edededff;
|
|
|
|
}
|
|
|
|
</style>
|
2020-10-17 18:40:18 +11:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<input type="file" id="bltFile">
|
|
|
|
<button onclick="clickBtn();">OK</button>
|
2020-10-18 03:48:00 +11:00
|
|
|
<select id="numbers"><option value="native" selected>Native Float</option><option value="int">Native Integer</option><option value="rational">Rational</option><option value="fixed">Fixed (6 d.p.)</option></select>
|
2020-10-17 18:40:18 +11:00
|
|
|
|
2020-10-17 22:20:13 +11:00
|
|
|
<table id="result"></table>
|
|
|
|
|
2020-10-17 18:40:18 +11:00
|
|
|
<script src="http://peterolson.github.com/BigRational.js/BigInt_BigRat.min.js"></script>
|
2020-10-18 03:25:41 +11:00
|
|
|
<script src="https://cdn.jsdelivr.net/npm/big.js@6.0.0/big.min.js"></script>
|
2020-10-17 18:40:18 +11:00
|
|
|
<script src="bundle.js"></script>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
async function clickBtn() {
|
2020-10-17 22:20:13 +11:00
|
|
|
// Read BLT file
|
2020-10-17 18:40:18 +11:00
|
|
|
let bltFile = document.getElementById('bltFile').files[0];
|
|
|
|
let text = await bltFile.text();
|
2020-10-17 22:20:13 +11:00
|
|
|
|
|
|
|
// Initialise table rows
|
|
|
|
let tblResults = document.getElementById('result');
|
2020-10-17 23:09:29 +11:00
|
|
|
tblResults.innerHTML = '';
|
2020-10-17 23:38:42 +11:00
|
|
|
let candMap = {}; // candidate name -> rows
|
2020-10-17 22:20:13 +11:00
|
|
|
|
|
|
|
// Step election
|
2020-10-17 23:38:42 +11:00
|
|
|
let worker = new Worker('worker.js');
|
2020-10-18 03:48:00 +11:00
|
|
|
let election, elComment, elExhausted1, elExhausted2, elLTF1, elLTF2, elTotal, elQuota;
|
2020-10-17 22:20:13 +11:00
|
|
|
|
2020-10-17 23:38:42 +11:00
|
|
|
worker.onmessage = function(evt) {
|
|
|
|
if (evt.data.type == 'init') {
|
|
|
|
election = evt.data.election;
|
|
|
|
|
|
|
|
// Comment row
|
|
|
|
elComment = document.createElement('tr');
|
|
|
|
let elTd = document.createElement('td');
|
|
|
|
elComment.appendChild(elTd);
|
|
|
|
tblResults.appendChild(elComment);
|
|
|
|
|
|
|
|
// Candidates
|
|
|
|
for (let candidate of election.candidates) {
|
|
|
|
let elTr1 = document.createElement('tr');
|
|
|
|
let elTr2 = document.createElement('tr');
|
|
|
|
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.setAttribute('rowspan', '2');
|
|
|
|
elTd.style.borderTop = '1px solid black';
|
|
|
|
elTd.innerText = candidate;
|
|
|
|
elTr1.appendChild(elTd);
|
|
|
|
|
|
|
|
tblResults.appendChild(elTr1);
|
|
|
|
tblResults.appendChild(elTr2);
|
|
|
|
|
|
|
|
candMap[candidate] = [elTr1, elTr2];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exhausted votes row
|
|
|
|
elExhausted1 = document.createElement('tr');
|
2020-10-18 00:23:25 +11:00
|
|
|
elExhausted1.classList.add('info');
|
2020-10-17 23:38:42 +11:00
|
|
|
elExhausted2 = document.createElement('tr');
|
2020-10-18 00:23:25 +11:00
|
|
|
elExhausted2.classList.add('info');
|
2020-10-17 22:20:13 +11:00
|
|
|
|
|
|
|
elTd = document.createElement('td');
|
2020-10-17 23:38:42 +11:00
|
|
|
elTd.setAttribute('rowspan', '2');
|
2020-10-17 22:20:13 +11:00
|
|
|
elTd.style.borderTop = '1px solid black';
|
2020-10-17 23:38:42 +11:00
|
|
|
elTd.innerText = 'Exhausted';
|
|
|
|
elExhausted1.appendChild(elTd);
|
|
|
|
|
|
|
|
tblResults.appendChild(elExhausted1);
|
|
|
|
tblResults.appendChild(elExhausted2);
|
2020-10-17 22:20:13 +11:00
|
|
|
|
2020-10-18 03:48:00 +11:00
|
|
|
// Loss to fraction row
|
|
|
|
elLTF1 = document.createElement('tr');
|
|
|
|
elLTF1.classList.add('info');
|
|
|
|
elLTF2 = document.createElement('tr');
|
|
|
|
elLTF2.classList.add('info');
|
|
|
|
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.setAttribute('rowspan', '2');
|
|
|
|
elTd.style.borderTop = '1px solid black';
|
|
|
|
elTd.innerText = 'Loss to fraction';
|
|
|
|
elLTF1.appendChild(elTd);
|
|
|
|
|
|
|
|
tblResults.appendChild(elLTF1);
|
|
|
|
tblResults.appendChild(elLTF2);
|
|
|
|
|
2020-10-18 02:54:51 +11:00
|
|
|
// Total row
|
|
|
|
elTotal = document.createElement('tr');
|
|
|
|
elTotal.classList.add('info');
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.style.borderTop = '1px solid black';
|
|
|
|
elTd.innerText = 'Total';
|
|
|
|
elTotal.appendChild(elTd);
|
|
|
|
tblResults.appendChild(elTotal);
|
|
|
|
|
2020-10-17 23:38:42 +11:00
|
|
|
// Quota row
|
|
|
|
elQuota = document.createElement('tr');
|
2020-10-18 00:23:25 +11:00
|
|
|
elQuota.classList.add('info');
|
2020-10-17 22:20:13 +11:00
|
|
|
elTd = document.createElement('td');
|
2020-10-17 23:38:42 +11:00
|
|
|
elTd.style.borderTop = '1px solid black';
|
2020-10-18 00:23:25 +11:00
|
|
|
elTd.style.borderBottom = '1px solid black';
|
2020-10-17 23:38:42 +11:00
|
|
|
elTd.innerText = 'Quota';
|
|
|
|
elQuota.appendChild(elTd);
|
|
|
|
tblResults.appendChild(elQuota);
|
2020-10-17 22:20:13 +11:00
|
|
|
}
|
|
|
|
|
2020-10-17 23:38:42 +11:00
|
|
|
if (evt.data.type == 'result') {
|
|
|
|
let result = evt.data.result;
|
|
|
|
|
|
|
|
// Display results
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.innerText = result.comment;
|
|
|
|
elComment.appendChild(elTd);
|
|
|
|
|
|
|
|
for (let [candidate, countCard] of result.candidates) {
|
|
|
|
[elTr1, elTr2] = candMap[candidate];
|
|
|
|
|
|
|
|
elTd = document.createElement('td');
|
2020-10-18 00:23:25 +11:00
|
|
|
elTd.classList.add('count');
|
|
|
|
if (countCard.state == py.pyRCV2.model.CandidateState.WITHDRAWN || countCard.state == py.pyRCV2.model.CandidateState.EXCLUDED) {
|
|
|
|
elTd.classList.add('excluded');
|
|
|
|
} else if (countCard.state == py.pyRCV2.model.CandidateState.ELECTED || countCard.state == py.pyRCV2.model.CandidateState.PROVISIONALLY_ELECTED) {
|
|
|
|
elTd.classList.add('elected');
|
|
|
|
}
|
2020-10-17 23:38:42 +11:00
|
|
|
elTd.style.borderTop = '1px solid black';
|
|
|
|
if (countCard.transfers != '0.00') {
|
|
|
|
elTd.innerText = countCard.transfers;
|
|
|
|
}
|
|
|
|
elTr1.appendChild(elTd);
|
|
|
|
|
|
|
|
elTd = document.createElement('td');
|
2020-10-18 00:23:25 +11:00
|
|
|
elTd.classList.add('count');
|
2020-10-17 23:38:42 +11:00
|
|
|
if (countCard.state == py.pyRCV2.model.CandidateState.WITHDRAWN) {
|
2020-10-18 00:23:25 +11:00
|
|
|
elTd.classList.add('excluded');
|
2020-10-17 23:38:42 +11:00
|
|
|
elTd.innerText = 'WD';
|
|
|
|
} else if (countCard.state == py.pyRCV2.model.CandidateState.ELECTED || countCard.state == py.pyRCV2.model.CandidateState.PROVISIONALLY_ELECTED) {
|
2020-10-18 00:23:25 +11:00
|
|
|
elTd.classList.add('elected');
|
2020-10-17 23:38:42 +11:00
|
|
|
elTd.innerText = countCard.votes;
|
|
|
|
elTd.style.fontWeight = 'bold';
|
2020-10-18 00:23:25 +11:00
|
|
|
|
|
|
|
elTr1.querySelector('td:first-child').classList.add('elected');
|
2020-10-17 23:38:42 +11:00
|
|
|
} else if (countCard.state == py.pyRCV2.model.CandidateState.EXCLUDED) {
|
2020-10-18 00:23:25 +11:00
|
|
|
elTd.classList.add('excluded');
|
2020-10-17 23:38:42 +11:00
|
|
|
elTd.innerText = 'EX';
|
|
|
|
} else {
|
|
|
|
elTd.innerText = countCard.votes;
|
|
|
|
}
|
|
|
|
elTr2.appendChild(elTd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display exhausted votes
|
|
|
|
elTd = document.createElement('td');
|
2020-10-18 00:23:25 +11:00
|
|
|
elTd.classList.add('count');
|
2020-10-17 23:38:42 +11:00
|
|
|
elTd.style.borderTop = '1px solid black';
|
|
|
|
if (result.exhausted.transfers != '0.00') {
|
|
|
|
elTd.innerText = result.exhausted.transfers;
|
|
|
|
}
|
|
|
|
elExhausted1.appendChild(elTd);
|
|
|
|
|
|
|
|
elTd = document.createElement('td');
|
2020-10-18 00:23:25 +11:00
|
|
|
elTd.classList.add('count');
|
2020-10-17 23:38:42 +11:00
|
|
|
elTd.innerText = result.exhausted.votes;
|
|
|
|
elExhausted2.appendChild(elTd);
|
|
|
|
|
2020-10-18 03:48:00 +11:00
|
|
|
// Display loss to fraction
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.classList.add('count');
|
|
|
|
elTd.style.borderTop = '1px solid black';
|
|
|
|
if (result.loss_fraction.transfers != '0.00') {
|
|
|
|
elTd.innerText = result.loss_fraction.transfers;
|
|
|
|
}
|
|
|
|
elLTF1.appendChild(elTd);
|
|
|
|
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.classList.add('count');
|
|
|
|
elTd.innerText = result.loss_fraction.votes;
|
|
|
|
elLTF2.appendChild(elTd);
|
|
|
|
|
2020-10-18 02:54:51 +11:00
|
|
|
// Display total
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.classList.add('count');
|
|
|
|
elTd.style.borderTop = '1px solid black';
|
|
|
|
elTd.innerText = result.total;
|
|
|
|
elTotal.appendChild(elTd);
|
|
|
|
|
2020-10-17 23:38:42 +11:00
|
|
|
// Display quota
|
|
|
|
elTd = document.createElement('td');
|
2020-10-18 00:23:25 +11:00
|
|
|
elTd.classList.add('count');
|
2020-10-17 23:38:42 +11:00
|
|
|
elTd.style.borderTop = '1px solid black';
|
2020-10-18 00:23:25 +11:00
|
|
|
elTd.style.borderBottom = '1px solid black';
|
2020-10-17 23:38:42 +11:00
|
|
|
elTd.innerText = result.quota;
|
|
|
|
elQuota.appendChild(elTd);
|
2020-10-17 22:20:13 +11:00
|
|
|
}
|
2020-10-17 23:38:42 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
worker.onerror = function(evt) {
|
|
|
|
throw evt;
|
|
|
|
}
|
|
|
|
|
2020-10-18 02:55:15 +11:00
|
|
|
worker.postMessage({
|
|
|
|
'numbers': document.getElementById('numbers').value,
|
|
|
|
'data': text
|
|
|
|
});
|
2020-10-17 18:40:18 +11:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|