2021-06-02 13:37:47 +02:00
|
|
|
|
/* OpenTally: Open-source election vote counting
|
2022-03-18 15:47:40 +01:00
|
|
|
|
* Copyright © 2021–2022 Lee Yingtong Li (RunasSudo)
|
2021-06-02 13:37:47 +02:00
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
function clickAdvancedOptions() {
|
|
|
|
|
if (document.getElementById('divAdvancedOptions').style.display === 'none') {
|
|
|
|
|
document.getElementById('divAdvancedOptions').style.display = 'grid';
|
|
|
|
|
document.getElementById('btnAdvancedOptions').innerHTML = 'Hide advanced options';
|
|
|
|
|
} else {
|
|
|
|
|
document.getElementById('divAdvancedOptions').style.display = 'none';
|
|
|
|
|
document.getElementById('btnAdvancedOptions').innerHTML = 'Show advanced options';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-02 14:46:36 +02:00
|
|
|
|
var tblResult = document.getElementById('result');
|
2021-06-03 07:47:19 +02:00
|
|
|
|
var divLogs2 = document.getElementById('resultLogs2');
|
|
|
|
|
var olStageComments;
|
2021-06-02 14:46:36 +02:00
|
|
|
|
|
2021-09-11 13:08:36 +02:00
|
|
|
|
var detailedTransfers = {};
|
|
|
|
|
|
2021-09-03 18:26:30 +02:00
|
|
|
|
var worker = new Worker('worker.js?v=GITVERSION');
|
2021-06-02 14:46:36 +02:00
|
|
|
|
|
2021-06-04 07:01:53 +02:00
|
|
|
|
worker.onmessage = function(evt) {
|
|
|
|
|
if (evt.data.type === 'init') {
|
|
|
|
|
document.getElementById('spanRevNum').innerText = evt.data.version;
|
|
|
|
|
document.getElementById('divLoading').style.display = 'none';
|
|
|
|
|
document.getElementById('divUI').style.display = 'block';
|
|
|
|
|
|
2021-07-20 05:41:38 +02:00
|
|
|
|
// Init dropdowns
|
|
|
|
|
// Can't compute correct width until #divUI, etc. is display: block
|
|
|
|
|
//document.getElementById('divAdvancedOptions').style.display = 'grid';
|
|
|
|
|
//for (let elSel of document.querySelectorAll('select')) {
|
|
|
|
|
let elSel = document.getElementById('selPreset'); {
|
|
|
|
|
var sel = new CustomSelect({elem: elSel});
|
|
|
|
|
sel.open();
|
|
|
|
|
document.getElementById('custom-' + elSel.id).style.width = (document.getElementById('custom-' + elSel.id).querySelector('.js-Dropdown-list').clientWidth + 32) + 'px';
|
|
|
|
|
sel.close();
|
|
|
|
|
}
|
|
|
|
|
//document.getElementById('divAdvancedOptions').style.display = 'none';
|
2021-06-04 07:01:53 +02:00
|
|
|
|
} else if (evt.data.type === 'initResultsTable') {
|
|
|
|
|
tblResult.innerHTML = evt.data.content;
|
|
|
|
|
divLogs2.innerHTML = '<p>Stage comments:</p>';
|
|
|
|
|
olStageComments = document.createElement('ol');
|
2022-08-26 16:58:51 +02:00
|
|
|
|
olStageComments.id = 'olStageComments';
|
2021-06-04 07:01:53 +02:00
|
|
|
|
divLogs2.append(olStageComments);
|
|
|
|
|
|
|
|
|
|
} else if (evt.data.type === 'describeCount') {
|
|
|
|
|
document.getElementById('resultLogs1').innerHTML = evt.data.content;
|
|
|
|
|
|
|
|
|
|
} else if (evt.data.type === 'updateResultsTable') {
|
2021-08-16 10:48:49 +02:00
|
|
|
|
for (let row = 0; row < evt.data.result.length; row++) {
|
|
|
|
|
if (evt.data.result[row]) {
|
|
|
|
|
tblResult.rows[row].insertAdjacentHTML('beforeend', evt.data.result[row]);
|
2021-06-05 15:29:31 +02:00
|
|
|
|
|
|
|
|
|
// Update candidate status
|
2021-08-16 10:48:49 +02:00
|
|
|
|
if (
|
|
|
|
|
(document.getElementById('selReport').value == 'votes' && row >= 3 && row % 2 == 1) ||
|
|
|
|
|
(document.getElementById('selReport').value == 'ballots_votes' && row >= 4 && row % 2 == 0)
|
|
|
|
|
) {
|
|
|
|
|
if (tblResult.rows[row].lastElementChild.classList.contains('elected')) {
|
|
|
|
|
tblResult.rows[row].cells[0].classList.add('elected');
|
2021-06-05 15:29:31 +02:00
|
|
|
|
} else {
|
2021-08-16 10:48:49 +02:00
|
|
|
|
tblResult.rows[row].cells[0].classList.remove('elected');
|
2021-06-05 15:29:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-04 07:01:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (evt.data.type === 'updateStageComments') {
|
|
|
|
|
let elLi = document.createElement('li');
|
2021-09-11 13:08:36 +02:00
|
|
|
|
elLi.id = 'stage' + evt.data.stageNum;
|
2021-06-04 07:01:53 +02:00
|
|
|
|
elLi.innerHTML = evt.data.comment;
|
|
|
|
|
olStageComments.append(elLi);
|
|
|
|
|
|
2021-09-11 13:08:36 +02:00
|
|
|
|
} else if (evt.data.type === 'updateDetailedTransfers') {
|
|
|
|
|
detailedTransfers[evt.data.stageNum] = evt.data.table;
|
|
|
|
|
|
2021-06-04 07:01:53 +02:00
|
|
|
|
} else if (evt.data.type === 'finalResultSummary') {
|
|
|
|
|
divLogs2.insertAdjacentHTML('beforeend', evt.data.summary);
|
2021-06-06 09:31:20 +02:00
|
|
|
|
document.getElementById('printPane').style.display = 'block';
|
2021-06-11 18:09:26 +02:00
|
|
|
|
|
2021-07-22 16:04:43 +02:00
|
|
|
|
// Linkify stage numbers
|
|
|
|
|
document.querySelectorAll('tr.stage-no a').forEach(function(elA) {
|
|
|
|
|
elA.onclick = function() {
|
|
|
|
|
olStageComments.childNodes.forEach(function(elLi) { elLi.classList.remove('highlight'); });
|
|
|
|
|
document.getElementById(elA.href.substring(elA.href.indexOf('#') + 1)).classList.add('highlight');
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2021-06-11 18:09:26 +02:00
|
|
|
|
} else if (evt.data.type === 'requireInput') {
|
|
|
|
|
let response = window.prompt(evt.data.message);
|
|
|
|
|
while (response === null) {
|
|
|
|
|
response = window.prompt(evt.data.message);
|
|
|
|
|
}
|
|
|
|
|
worker.postMessage({'type': 'userInput', 'response': response});
|
2021-07-31 07:24:23 +02:00
|
|
|
|
|
|
|
|
|
} else if (evt.data.type === 'errorMessage') {
|
|
|
|
|
divLogs2.insertAdjacentHTML('beforeend', evt.data.message);
|
2021-06-04 07:01:53 +02:00
|
|
|
|
}
|
2021-06-03 07:47:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 10:59:50 +02:00
|
|
|
|
worker.onerror = function(evt) {
|
|
|
|
|
alert('An unknown error occurred while counting the votes. More details may be available in the browser\'s developer console.');
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-02 13:37:47 +02:00
|
|
|
|
async function clickCount() {
|
|
|
|
|
if (document.getElementById('bltFile').files.length === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read BLT file
|
2021-06-27 14:09:34 +02:00
|
|
|
|
let bltPath = document.getElementById('bltFile').value;
|
|
|
|
|
bltPath = bltPath.substring(Math.max(bltPath.lastIndexOf('\\'), bltPath.lastIndexOf('/')) + 1);
|
2021-06-04 07:01:53 +02:00
|
|
|
|
|
2021-06-02 13:37:47 +02:00
|
|
|
|
let bltFile = document.getElementById('bltFile').files[0];
|
2021-06-27 14:09:34 +02:00
|
|
|
|
let bltData = await bltFile.text();
|
|
|
|
|
|
|
|
|
|
// Read CON file (if applicable)
|
|
|
|
|
let conPath = null;
|
|
|
|
|
let conData = null;
|
|
|
|
|
if (document.getElementById('conFile').files.length > 0) {
|
|
|
|
|
conPath = document.getElementById('conFile').value;
|
|
|
|
|
conPath = conPath.substring(Math.max(conPath.lastIndexOf('\\'), conPath.lastIndexOf('/')) + 1);
|
|
|
|
|
|
|
|
|
|
let conFile = document.getElementById('conFile').files[0];
|
|
|
|
|
conData = await conFile.text();
|
|
|
|
|
}
|
2021-06-02 13:37:47 +02:00
|
|
|
|
|
|
|
|
|
// Init STV options
|
2021-06-04 07:01:53 +02:00
|
|
|
|
let optsStr = [
|
2021-08-03 08:46:21 +02:00
|
|
|
|
document.getElementById('chkRoundSFs').checked ? parseInt(document.getElementById('txtRoundSFs').value) : null,
|
|
|
|
|
document.getElementById('chkRoundValues').checked ? parseInt(document.getElementById('txtRoundValues').value) : null,
|
2021-06-02 13:37:47 +02:00
|
|
|
|
document.getElementById('chkRoundVotes').checked ? parseInt(document.getElementById('txtRoundVotes').value) : null,
|
|
|
|
|
document.getElementById('chkRoundQuota').checked ? parseInt(document.getElementById('txtRoundQuota').value) : null,
|
2021-06-11 13:22:28 +02:00
|
|
|
|
document.getElementById('selSumTransfers').value,
|
2021-06-18 10:48:12 +02:00
|
|
|
|
document.getElementById('txtMeekSurplusTolerance').value,
|
2021-06-02 13:37:47 +02:00
|
|
|
|
document.getElementById('selQuota').value,
|
|
|
|
|
document.getElementById('selQuotaCriterion').value,
|
2021-06-07 12:52:18 +02:00
|
|
|
|
document.getElementById('selQuotaMode').value,
|
2021-06-12 16:39:49 +02:00
|
|
|
|
document.getElementById('selTies').value.split(','),
|
2021-06-12 19:15:15 +02:00
|
|
|
|
document.getElementById('txtSeed').value,
|
2021-08-04 05:46:32 +02:00
|
|
|
|
document.getElementById('selMethod').value,
|
2021-06-02 13:37:47 +02:00
|
|
|
|
document.getElementById('selSurplus').value,
|
2022-03-24 16:46:30 +01:00
|
|
|
|
document.getElementById('selPapers').value,
|
2021-06-02 13:37:47 +02:00
|
|
|
|
document.getElementById('selExclusion').value,
|
2021-06-19 17:28:54 +02:00
|
|
|
|
document.getElementById('chkMeekNZExclusion').checked,
|
2021-08-05 10:41:39 +02:00
|
|
|
|
document.getElementById('selSample').value,
|
2021-08-04 05:46:32 +02:00
|
|
|
|
document.getElementById('chkSamplePerBallot').checked,
|
2021-06-22 16:52:25 +02:00
|
|
|
|
document.getElementById('chkBulkElection').checked,
|
2021-06-09 04:42:47 +02:00
|
|
|
|
document.getElementById('chkBulkExclusion').checked,
|
|
|
|
|
document.getElementById('chkDeferSurpluses').checked,
|
2021-08-07 10:51:48 +02:00
|
|
|
|
document.getElementById('chkImmediateElect').checked,
|
2021-08-03 15:22:52 +02:00
|
|
|
|
document.getElementById('txtMinThreshold').value,
|
2021-06-27 14:24:25 +02:00
|
|
|
|
conPath,
|
2022-04-15 18:27:59 +02:00
|
|
|
|
document.getElementById('selConstraintMethod').value,
|
2021-06-02 13:37:47 +02:00
|
|
|
|
parseInt(document.getElementById('txtPPDP').value),
|
2021-06-04 07:01:53 +02:00
|
|
|
|
];
|
2021-06-03 07:47:19 +02:00
|
|
|
|
|
2021-07-31 07:24:23 +02:00
|
|
|
|
// Reset UI
|
|
|
|
|
document.getElementById('printPane').style.display = 'none';
|
2021-08-04 05:46:32 +02:00
|
|
|
|
document.getElementById('resultLogs1').innerHTML = '';
|
|
|
|
|
tblResult.innerHTML = '';
|
|
|
|
|
divLogs2.innerHTML = '';
|
2021-06-18 10:48:12 +02:00
|
|
|
|
|
2021-09-11 13:08:36 +02:00
|
|
|
|
detailedTransfers = {};
|
|
|
|
|
|
2021-06-04 07:01:53 +02:00
|
|
|
|
// Dispatch to worker
|
|
|
|
|
worker.postMessage({
|
|
|
|
|
'type': 'countElection',
|
2021-08-16 10:48:49 +02:00
|
|
|
|
// Data
|
2021-06-27 14:09:34 +02:00
|
|
|
|
'bltData': bltData,
|
|
|
|
|
'conData': conData,
|
|
|
|
|
'bltPath': bltPath,
|
|
|
|
|
'conPath': conPath,
|
2021-08-16 10:48:49 +02:00
|
|
|
|
// Options
|
|
|
|
|
'optsStr': optsStr,
|
2021-06-04 10:59:50 +02:00
|
|
|
|
'numbers': document.getElementById('selNumbers').value,
|
2021-06-04 14:05:48 +02:00
|
|
|
|
'decimals': document.getElementById('txtDP').value,
|
2021-08-16 10:48:49 +02:00
|
|
|
|
'reportStyle': document.getElementById('selReport').value,
|
2021-06-04 07:01:53 +02:00
|
|
|
|
});
|
2021-06-02 13:37:47 +02:00
|
|
|
|
}
|
2021-06-05 16:38:25 +02:00
|
|
|
|
|
2021-09-11 13:08:36 +02:00
|
|
|
|
function viewDetailedTransfers(stageNum) {
|
|
|
|
|
let wtransfers = window.open('', '', 'location=0,width=800,height=600');
|
|
|
|
|
wtransfers.document.title = 'OpenTally Detailed Transfers: Stage ' + stageNum;
|
|
|
|
|
|
|
|
|
|
// Add stylesheets
|
|
|
|
|
for (let elCSSBase of document.querySelectorAll('head link')) {
|
|
|
|
|
let elCSS = wtransfers.document.createElement('link');
|
|
|
|
|
elCSS.rel = elCSSBase.rel;
|
|
|
|
|
elCSS.type = elCSSBase.type;
|
|
|
|
|
if (elCSSBase.href.endsWith('?v=GITVERSION')) {
|
|
|
|
|
elCSS.href = elCSSBase.href.replace('?v=GITVERSION', '?v=' + Math.random());
|
|
|
|
|
} else {
|
|
|
|
|
elCSS.href = elCSSBase.href;
|
|
|
|
|
}
|
|
|
|
|
wtransfers.document.head.appendChild(elCSS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wtransfers.document.body.innerHTML = detailedTransfers[stageNum];
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-12 19:15:15 +02:00
|
|
|
|
// Provide a default seed
|
|
|
|
|
if (document.getElementById('txtSeed').value === '') {
|
|
|
|
|
function pad(x) { if (x < 10) { return '0' + x; } return '' + x; }
|
|
|
|
|
let d = new Date();
|
|
|
|
|
document.getElementById('txtSeed').value = d.getFullYear() + pad(d.getMonth() + 1) + pad(d.getDate());
|
|
|
|
|
}
|