OpenTally/html/index.js

78 lines
3.0 KiB
JavaScript

/* 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';
}
}
console.log = function(v) {
document.getElementById('resultLogs1').append(v);
document.getElementById('resultLogs1').append("\n");
};
var wasm = wasm_bindgen;
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);
let state = wasm.CountStateRational.new(election);
// Init STV options
let stv_opts = wasm.STVOptions.new(
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
wasm.count_init_Rational(state, stv_opts);
wasm.make_and_print_result_Rational(1, state);
for (let stage_num = 2;; stage_num++) {
let is_done = wasm.count_one_stage_Rational(state, stv_opts);
if (is_done) {
break;
}
wasm.make_and_print_result_Rational(stage_num, state);
}
}