2021-06-02 13:37:47 +02:00
|
|
|
/* OpenTally: Open-source election vote counting
|
|
|
|
* Copyright © 2021 Lee Yingtong Li (RunasSudo)
|
|
|
|
*
|
|
|
|
* 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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var wasm = wasm_bindgen;
|
|
|
|
|
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
|
|
|
|
|
|
|
function updateResultTable(result) {
|
|
|
|
for (let i = 0; i < result.length; i++) {
|
|
|
|
tblResult.rows[i].insertAdjacentHTML('beforeend', result[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 07:47:19 +02:00
|
|
|
function updateStageComments(comment) {
|
|
|
|
let elLi = document.createElement('li');
|
|
|
|
elLi.innerHTML = comment;
|
|
|
|
olStageComments.append(elLi);
|
|
|
|
}
|
|
|
|
|
2021-06-02 13:37:47 +02:00
|
|
|
async function clickCount() {
|
|
|
|
if (document.getElementById('bltFile').files.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read BLT file
|
|
|
|
let bltFile = document.getElementById('bltFile').files[0];
|
|
|
|
let electionData = await bltFile.text();
|
|
|
|
|
|
|
|
// Load WASM
|
|
|
|
await wasm_bindgen('opentally_bg.wasm');
|
|
|
|
|
|
|
|
// Init election
|
|
|
|
let election = wasm.election_from_blt_Rational(electionData);
|
2021-06-02 14:46:36 +02:00
|
|
|
|
|
|
|
// Init results table
|
|
|
|
tblResult.innerHTML = wasm.init_results_table_Rational(election);
|
2021-06-02 13:37:47 +02:00
|
|
|
|
2021-06-03 07:47:19 +02:00
|
|
|
divLogs2.innerHTML = '<p>Stage comments:</p>';
|
|
|
|
olStageComments = document.createElement('ol');
|
|
|
|
divLogs2.append(olStageComments);
|
|
|
|
|
2021-06-02 13:37:47 +02:00
|
|
|
// Init STV options
|
2021-06-02 14:46:36 +02:00
|
|
|
let opts = wasm.STVOptions.new(
|
2021-06-02 13:37:47 +02:00
|
|
|
document.getElementById('chkRoundTVs').checked ? parseInt(document.getElementById('txtRoundTVs').value) : null,
|
|
|
|
document.getElementById('chkRoundWeights').checked ? parseInt(document.getElementById('txtRoundWeights').value) : null,
|
|
|
|
document.getElementById('chkRoundVotes').checked ? parseInt(document.getElementById('txtRoundVotes').value) : null,
|
|
|
|
document.getElementById('chkRoundQuota').checked ? parseInt(document.getElementById('txtRoundQuota').value) : null,
|
|
|
|
document.getElementById('selQuota').value,
|
|
|
|
document.getElementById('selQuotaCriterion').value,
|
|
|
|
document.getElementById('selTransfers').value,
|
|
|
|
document.getElementById('selSurplus').value,
|
|
|
|
document.getElementById('selPapers').value == 'transferable',
|
|
|
|
document.getElementById('selExclusion').value,
|
|
|
|
parseInt(document.getElementById('txtPPDP').value),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Step election
|
2021-06-02 14:46:36 +02:00
|
|
|
let state = wasm.CountStateRational.new(election);
|
|
|
|
wasm.count_init_Rational(state, opts);
|
|
|
|
updateResultTable(wasm.update_results_table_Rational(1, state, opts));
|
2021-06-03 07:47:19 +02:00
|
|
|
updateStageComments(wasm.update_stage_comments_Rational(state));
|
2021-06-02 13:37:47 +02:00
|
|
|
|
2021-06-02 14:46:36 +02:00
|
|
|
for (let stageNum = 2;; stageNum++) {
|
|
|
|
let isDone = wasm.count_one_stage_Rational(state, opts);
|
|
|
|
if (isDone) {
|
2021-06-02 13:37:47 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-06-02 14:46:36 +02:00
|
|
|
updateResultTable(wasm.update_results_table_Rational(stageNum, state, opts));
|
2021-06-03 07:47:19 +02:00
|
|
|
updateStageComments(wasm.update_stage_comments_Rational(state));
|
2021-06-02 13:37:47 +02:00
|
|
|
}
|
2021-06-03 07:47:19 +02:00
|
|
|
|
|
|
|
updateResultTable(wasm.finalise_results_table_Rational(state));
|
|
|
|
|
|
|
|
divLogs2.insertAdjacentHTML('beforeend', wasm.final_result_summary_Rational(state));
|
2021-06-02 13:37:47 +02:00
|
|
|
}
|