|
| 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