-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathjax.html
214 lines (193 loc) · 8.36 KB
/
mathjax.html
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Zadanie</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css">
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" src="https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-MML-AM_CHTML-full"></script>
<script src="Utils.js"></script>
<script>
const zadanie = {
"type": "EqEx",
"name": "Pociągi 2",
"content": {
"main": "Z miast A i B odległych o \\( d=300 \\mathrm{km} \\) wyruszają jednocześnie dwa pociągi z prędkościami \\( v_a=50 \\mathrm{\\frac{m}{s}} \\) oraz \\( v_b=67 \\mathrm{\\frac{m}{s}} \\). W jakiej odległości \\( x \\) od miasta A spotkają się te pociągi? Po jakim czasie \\( t \\) się to stanie?",
"imgs": ["img/back.png", "img/start.png", "https://bulma.io/images/css-book/css-in-44-minutes-book-cover.png"],
"unknowns": [
["x", "km"],
["t", "s"]
]
}
};
const unknowns = [];
function setData() {
document.getElementById("title").innerHTML = zadanie.name;
document.getElementById("description").innerHTML = zadanie.content.main;
const image_container = document.getElementById("image_container");
for (var i = 0; i < zadanie.content.imgs.length; i++)
image_container.appendChild(createImage(zadanie.content.imgs[i]));
createUnknowns(zadanie.content.unknowns);
//Converting LaTeX
MathJax.Hub.Queue(["Typeset", MathJax.Hub, 'math-panel'], [function() {
//Disabling focus
const elementList = document.querySelectorAll(".MathJax_CHTML");
for (var i = 0; i < elementList.length; i++) {
elementList[i].tabIndex = -1;
}
}]);
}
/**
* @param {string[][]} list
*/
function createUnknowns(list) {
for (var i = 0; i < list.length; i++) {
const input = createElement("input", ["input", "is-primary"]);
setValueChecking(input);
const warning = createElement("p", ["help", "is-warning"], [], "Wymagana liczba");
hide(warning);
const unknown = {
"name": list[i][0],
"unit": list[i][1],
"input": input,
"warning": warning
};
unknowns.push(unknown);
}
for (let i = 0; i < unknowns.length; i++) {
const unknown = unknowns[i];
const name = createElement("div", ["subtitle", "is-8", "full-height", "is-flex", "is-align-items-center"], [], "\\( " + unknown.name + " = \\)");
const unit = createElement("div", ["subtitle", "is-8", "full-height", "is-flex", "is-align-items-center"], [], "\\(\\mathrm{" + unknown.unit + "} \\)");
unit.style.minWidth = "2em";
const dummyLabel = createElement("p", ["help"]);
const nameColumn = createElement("div", ["column", "is-narrow", "pb-2rem"], [name]);
const inputColumn = createElement("div", ["column", "px-0"], [unknown.input, unknown.warning]);
const unitColumn = createElement("div", ["column", "is-narrow", "pb-2rem"], [unit]);
const columns = createElement("div", ["columns", "is-mobile"], [nameColumn, inputColumn, unitColumn]);
const tile = createElement("div", ["tile", "column", "is-flex", "is-justify-content-center"], [columns]);
document.getElementById("unknowns").appendChild(tile);
}
const button = createElement("button", ["button", "is-primary"], [], "Submit");
button.id = "submitButton";
button.onclick = function() {
onSubmitClick()
};
document.getElementById("unknowns").appendChild(createElement("div", ["column", "is-narrow", "pb-2rem", "is-flex", "is-justify-content-right", "is-align-items-center"], [button]));
}
/**
* @param {string} path
*/
function createImage(path) {
const img = createElement("img");
img.src = path;
const box = createElement("div", ["box", "is-flex", "is-justify-content-center", "is-align-items-center"], [img]);
const center = createElement("center", ["full-height"], [box]);
const figure = createElement("figure", ["column"], [center]);
return figure;
}
/**
* @param {HTMLElement} textbox
*/
function setValueChecking(textbox) {
["input", "keydown", "keyup", "mousedown", "mouseup", "select", "contextmenu", "drop"].forEach(function(event) {
textbox.addEventListener(event, function() {
const regex = /^[+-]?\d*[,\.]?\d*$/;
if (!regex.test(textbox.value)) {
if (!textbox.classList.contains("is-warning")) {
textbox.classList.remove("is-primary");
textbox.classList.add("is-warning");
const i = getUnknownIndex(textbox);
if (i != -1)
show(unknowns[i].warning);
}
} else {
if (!textbox.classList.contains("is-primary")) {
textbox.classList.remove("is-warning");
textbox.classList.add("is-primary");
const i = getUnknownIndex(textbox);
if (i != -1)
hide(unknowns[i].warning);
}
}
});
});
}
/**
* Returns index of element in unknowns or -1 when element is not found
* @param {HTMLElement} textbox
*/
function getUnknownIndex(textbox) {
var i = -1
unknowns.forEach(unknown => {
if (unknown.input == textbox) {
i = unknowns.indexOf(unknown);
return;
}
});
return i;
}
function checkInputValues() {
const regex = /^[+-]?\d*[,\.]?\d+$/;
for (let i = 0; i < unknowns.length; i++) {
const value = unknowns[i].input.value
if (!regex.test(value))
return false;
}
return true;
}
function onSubmitClick() {
const button = document.getElementById("submitButton");
button.classList.add("is-loading");
setTimeout(() => {
button.classList.remove("is-loading")
}, 1000);
if (checkInputValues()) {
const response = {}
for (let i = 0; i < unknowns.length; i++) {
const unknown = unknowns[i];
response[unknown.name] = parseFloat(unknown.input.value);
}
console.log(response);
}
}
</script>
<style>
img {
max-height: 8rem;
height: auto;
}
.box {
padding: 0rem;
height: 100%;
}
.justify {
text-align: justify;
}
.full-height {
height: 100%;
}
.pb-2rem {
padding-bottom: 2rem;
}
</style>
</head>
<body onload="setData();">
<section class="section">
<div class="is-flex-direction-column">
<p id="title" class="title is-1">Test</p>
<p id="description" class="subtitle justify m-4 mb-6">Desc</p>
<div class="container">
<div id="image_container" class="columns is-justify-content-center">
</div>
</div>
<div class="container mt-6">
<div id="unknowns" class="columns">
</div>
</div>
</div>
</div>
</div>
</section>
</body>
</html>