Skip to content

Commit c29622d

Browse files
Merge pull request #6 from virtual-labs/dev
Dev: Update backend
2 parents e7fa57e + f44f196 commit c29622d

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

experiment/simulation/exp.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
118118
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
119119
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
120-
<script src="../js/utils.js"></script>
120+
<script src="./js/utils.js"></script>
121121
<script src="exp.js"></script>
122122
</body>
123123
</html>

experiment/simulation/js/utils.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
window.SERVER =
2+
"svc-test.vlabs.ac.in/octave-ann";
3+
4+
function submitForm() {
5+
const $form = $('form[name="myform"]');
6+
7+
let args = $form.serializeArray().reduce((prev, cur) => {
8+
return { ...prev, [cur.name]: cur.value };
9+
}, {});
10+
11+
if (window.extraArgs) args = { ...args, ...window.extraArgs() };
12+
13+
$.ajax({
14+
url: SERVER + "/exp-" + window.EXP_NAME,
15+
type: "POST",
16+
headers: {
17+
token: localStorage.getItem("token"),
18+
},
19+
dataType: "json",
20+
data: JSON.stringify(args),
21+
contentType: "application/json",
22+
success: function (data, status, xhr) {
23+
if (status === "success") {
24+
if (data.error) console.error(data.error);
25+
else {
26+
const $container = document.querySelector(".image-container");
27+
while ($container.firstElementChild)
28+
$container.removeChild($container.firstElementChild);
29+
const $result = $("#result");
30+
$result.html("");
31+
32+
if (data.images) {
33+
for (let i = 1; i <= data.images.length; i++) {
34+
const img = document.createElement("img");
35+
img.setAttribute("width", "600");
36+
img.classList.add("my-2");
37+
img.src = "data:image/png;base64," + data.images[i - 1];
38+
$container.appendChild(img);
39+
}
40+
}
41+
42+
if (data.result) {
43+
$result.html(data.result);
44+
}
45+
}
46+
}
47+
},
48+
error: function (err) {
49+
console.log(err);
50+
},
51+
});
52+
}
53+
54+
function submit(e) {
55+
submitForm();
56+
e.preventDefault();
57+
}
58+
59+
/**
60+
* @param elm {Element}
61+
* @param list {Object<String,String>}
62+
*/
63+
function appendOptions(elm, list) {
64+
for (const [value, str] of Object.entries(list)) {
65+
const option = document.createElement("option");
66+
option.innerText = str;
67+
option.value = value;
68+
elm.appendChild(option);
69+
}
70+
elm.firstElementChild.setAttribute("selected", "selected");
71+
}
72+
73+
function onload() {
74+
if (document.readyState === "complete") {
75+
if (!localStorage.getItem("token")) {
76+
$.get(
77+
`${SERVER}/get_token`,
78+
(success = function (data) {
79+
localStorage.setItem("token", data);
80+
})
81+
);
82+
}
83+
if (window.appenditure)
84+
for (const [elmName, value] of Object.entries(window.appenditure)) {
85+
appendOptions(document.myform[elmName], value);
86+
}
87+
88+
if (window.otherSetters) {
89+
for (const [key, value] of Object.entries(window.otherSetters)) {
90+
document.myform[key].value = value;
91+
}
92+
}
93+
94+
$('form[name="myform"]').submit(submit);
95+
96+
submitForm();
97+
} else {
98+
setTimeout(onload, 100);
99+
}
100+
}
101+
102+
$(document).ready(onload);

0 commit comments

Comments
 (0)