-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.js
92 lines (91 loc) · 2.77 KB
/
gui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
function m(id) {
return document.getElementById(id);
}
var unaryOps = m("unary_ops").getElementsByTagName("input"),
binaryOps = m("binary_ops").getElementsByTagName("input");
var digitsNum = m("digits_num"),
digitsContainer = m("digits"),
solForm = m("sol_form"),
solOutput = m("solutions");
var stopOpts = [m("fst"), m("arb"), m("all")],
desiredNum = m("desired_num"),
solLimit = m("sol_limit"),
numSols = m("num_sols"),
printLatex = m("print_latex");
function setTextContent(el, text) {
var propertyName = ("textContent" in el) ? "textContent" : "innerText";
el[propertyName] = text;
}
function addDigitInputs() {
var val = +digitsNum.value;
if(!isNaN(val)) {
val = Math.min(Math.max(2, val), 20);
var digits = [];
for(var i = 0; i < val; i++) {
digits.push('<input type="text" class="digit-input" value="' + (Math.floor((Math.random() * 9) + 1)) + '">');
}
digitsContainer.innerHTML = digits.join("");
}
}
function getDigits() {
var digits = [],
digits_coll = digitsContainer.childNodes;
for(var i = 0, num_digits = digits_coll.length; i < num_digits; i++) {
var digit_val = +digits_coll[i].value;
if(isNaN(digit_val)) {
digit_val = 0;
}
digits_coll[i].value = digit_val;
digits.push(digits_coll[i].value);
}
return digits;
}
digitsNum.oninput = addDigitInputs;
function printSolutions() {
for(var j = 0, num_uops = unaryOps.length; j < num_uops; j++) {
Deset.set_unary_enabled_state(unaryOps[j].id, unaryOps[j].checked);
}
for(j = 0, num_bops = binaryOps.length; j < num_bops; j++) {
Deset.set_binary_enabled_state(binaryOps[j].id, binaryOps[j].checked);
}
var digits = getDigits(),
desired_num = +desiredNum.value,
sol_limit = 0,
sol_limits = {
"fst": 1,
"arb": +solLimit.value,
"all": Infinity
};
if(isNaN(desired_num)) {
desiredNum.value = desired_num = 10;
}
for(var i = 0, num_opts = stopOpts.length; i < num_opts; i++) {
if(stopOpts[i].checked) {
sol_limit = sol_limits[stopOpts[i].id];
break;
}
}
var sols = Deset.get_solutions(digits, desired_num, sol_limit),
content = document.createDocumentFragment(),
num_sols = sols.length,
use_latex = printLatex.checked;
setTextContent(numSols, num_sols);
for(var j = 0; j < num_sols; j++) {
var p = document.createElement('p');
if (use_latex) {
var expr = Deset.LatexPrinter.strip_parens(Deset.LatexPrinter.get_readable_solution(sols[j]));
katex.render(expr, p);
} else {
setTextContent(p, Deset.HumanPrinter.strip_parens(Deset.HumanPrinter.get_readable_solution(sols[j])));
}
content.appendChild(p);
}
solOutput.innerHTML = "";
if(num_sols < 1) {
content.appendChild(document.createTextNode("No solution found."));//Nebylo nalezeno žádné řešení.
}
solOutput.appendChild(content);
return false;
}
solForm.onsubmit = printSolutions;
addDigitInputs();