mcalc/main.js

153 lines
5.1 KiB
JavaScript
Raw Normal View History

2025-12-12 04:49:34 +00:00
/*
Copyright (C) 2025 filifa
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2025-12-12 04:49:34 +00:00
2025-12-12 04:49:34 +00:00
function evaluate() {
2025-12-12 04:49:34 +00:00
const expr = document.querySelector("#expr");
const modulus = document.querySelector("#modulus");
2025-12-12 04:49:34 +00:00
const result = document.querySelector("#result");
worker.postMessage({command: "compute", expr: expr.value, modulus: modulus.value});
timeoutId = setTimeout(() => {
result.value = "calculating...";
switchInputs(false);
switchButtons(false);
}, 500);
2025-12-12 04:49:34 +00:00
}
function setupExprInput() {
selectionStart = expr.selectionStart;
selectionEnd = expr.selectionEnd;
switchButtons(true);
2025-12-12 04:49:34 +00:00
}
function clear() {
expr.value = "";
expr.focus();
2025-12-12 04:49:34 +00:00
}
2025-12-12 04:49:34 +00:00
2025-12-12 04:49:34 +00:00
function keyPress(c) {
expr.focus();
const begin = expr.value.slice(0, selectionStart);
const end = expr.value.slice(selectionEnd);
expr.value = begin + c + end;
expr.setSelectionRange(selectionStart+c.length, selectionStart+c.length);
2025-12-12 04:49:34 +00:00
}
2025-12-12 04:49:34 +00:00
function backspace() {
expr.focus();
2025-12-12 04:49:34 +00:00
if (selectionEnd === 0) {
return;
}
let begin = null;
if (selectionStart === selectionEnd) {
begin = expr.value.slice(0, selectionStart-1);
2025-12-12 04:49:34 +00:00
} else {
begin = expr.value.slice(0, selectionStart);
2025-12-12 04:49:34 +00:00
}
const end = expr.value.slice(selectionEnd);
2025-12-12 04:49:34 +00:00
expr.value = begin + end;
2025-12-12 04:49:34 +00:00
if (selectionStart === 0) {
expr.setSelectionRange(0, 0);
2025-12-12 04:49:34 +00:00
} else if (selectionStart === selectionEnd) {
expr.setSelectionRange(selectionStart-1, selectionStart-1);
2025-12-12 04:49:34 +00:00
} else {
expr.setSelectionRange(selectionStart, selectionStart);
2025-12-12 04:49:34 +00:00
}
}
2025-12-12 04:49:34 +00:00
function evalOnEnter(e) {
if (e.key === "Enter") {
evaluate();
}
}
function switchButtons(enabled) {
for (const btn of document.querySelectorAll("#main-buttons button")) {
btn.disabled = !enabled;
}
for (const btn of document.querySelectorAll("#extra-buttons button")) {
btn.disabled = !enabled;
}
}
function switchInputs(enabled) {
document.querySelector("#expr").disabled = !enabled;
document.querySelector("#modulus").disabled = !enabled;
}
2025-12-12 04:49:35 +00:00
function endOfComputation() {
clearTimeout(timeoutId);
switchInputs(true);
switchButtons(true);
}
2025-12-12 04:49:35 +00:00
const expr = document.querySelector("#expr");
let selectionStart = 0;
let selectionEnd = 0;
let timeoutId = null;
2025-12-12 04:49:34 +00:00
const worker = new Worker("./workers/compute.js");
2025-12-12 04:49:34 +00:00
worker.addEventListener("message", (message) => {
document.querySelector("#result").value = message.data;
2025-12-12 04:49:35 +00:00
endOfComputation();
2025-12-12 04:49:34 +00:00
});
2025-12-12 04:49:34 +00:00
worker.addEventListener("error", (e) => {
console.log(e);
document.querySelector("#result").value = e.message;
2025-12-12 04:49:35 +00:00
endOfComputation();
2025-12-12 04:49:34 +00:00
});
2025-12-12 04:49:34 +00:00
document.querySelector("#expr").addEventListener("focus", setupExprInput);
2025-12-12 04:49:34 +00:00
document.querySelector("#expr").addEventListener("keyup", (e) => evalOnEnter(e));
document.querySelector("#modulus").addEventListener("focus", () => switchButtons(false));
2025-12-12 04:49:34 +00:00
document.querySelector("#modulus").addEventListener("keyup", (e) => evalOnEnter(e));
2025-12-12 04:49:34 +00:00
document.querySelector("#zero").addEventListener("click", () => keyPress("0"));
document.querySelector("#one").addEventListener("click", () => keyPress("1"));
document.querySelector("#two").addEventListener("click", () => keyPress("2"));
document.querySelector("#three").addEventListener("click", () => keyPress("3"));
document.querySelector("#four").addEventListener("click", () => keyPress("4"));
document.querySelector("#five").addEventListener("click", () => keyPress("5"));
document.querySelector("#six").addEventListener("click", () => keyPress("6"));
document.querySelector("#seven").addEventListener("click", () => keyPress("7"));
document.querySelector("#eight").addEventListener("click", () => keyPress("8"));
document.querySelector("#nine").addEventListener("click", () => keyPress("9"));
document.querySelector("#plus").addEventListener("click", () => keyPress("+"));
document.querySelector("#minus").addEventListener("click", () => keyPress("-"));
document.querySelector("#times").addEventListener("click", () => keyPress("*"));
document.querySelector("#divide").addEventListener("click", () => keyPress("/"));
document.querySelector("#lparen").addEventListener("click", () => keyPress("("));
document.querySelector("#rparen").addEventListener("click", () => keyPress(")"));
document.querySelector("#pow").addEventListener("click", () => keyPress("^"));
2025-12-12 04:49:34 +00:00
document.querySelector("#inv").addEventListener("click", () => keyPress("^-1"));
2025-12-12 04:49:34 +00:00
document.querySelector("#sqrt").addEventListener("click", () => keyPress("sqrt("));
2025-12-12 04:49:34 +00:00
document.querySelector("#ord").addEventListener("click", () => keyPress("ord("));
2025-12-12 04:49:34 +00:00
document.querySelector("#clear").addEventListener("click", clear);
2025-12-12 04:49:34 +00:00
document.querySelector("#enter").addEventListener("click", evaluate);
2025-12-12 04:49:34 +00:00
2025-12-12 04:49:34 +00:00
document.querySelector("#backspace").addEventListener("click", backspace);