forms.js
2.66 KB
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
"use strict";
function collectToSearch(option) {
const f = document.getElementById("form");
const inputs = f.getElementsByTagName("input");
const defecto = document.getElementsByName("defecto")[0].value;
let data = {};
function BadInputException(input) {
this.error = "badinput";
this.input = input;
}
Array.from(inputs).forEach( input => {
if (input.checkValidity() === false){
input.reportValidity("Por favor llene este campo");
throw BadInputException(input);
}
const sub = input.getAttribute("sub");
if ( (sub in data) === false ) {
data[sub] = {}
}
data[sub][input.name] = (input.value === "" ? defecto : input.value);
});
message("Espere...");
let send = {
"contentType": "application/json",
"async": true,
"data": data,
"error": (response) => message(response),
"timeout": 120000,
"ontimeout": (response) => message("La conexión tardó demasiado.")
}
if (option == "data") {
send['url'] = "/report";
send['success'] = (response) => {
let r;
try {
r = JSON.parse(response);
document.getElementById("message").click();
} catch (error) {
message(response);
}
document.getElementById("report").style.display = "block";
document.getElementById("form").style.display = "none";
fillResults(r);
};
}
else if (option == "anomalies") {
send['url'] = "/anomalies";
send['success'] = (response) => {
document.getElementById("message").click();
document.getElementById("anomalies").style.display = "block";
document.getElementById("anomalies").innerHTML = response;
document.getElementById("form").style.display = "none";
};
}
let ajax = new Ajax(send);
ajax.post();
}
function collectToConvert() {
const r = document.getElementById("report")
const inputs = r.getElementsByTagName("input");
let data = {};
const defecto = document.getElementsByName("defecto")[0].value;
Array.from(inputs).forEach(input => {
if (input.name == "defecto") return;
const sub = input.getAttribute("sub");
if ((sub in data) === false) {
data[sub] = {}
}
data[sub][input.name] = (input.value == "?" || input.value == "" ? defecto : input.value);
});
return data;
}
function message(msg) {
let m = document.getElementById("message");
m.style.display = "block";
m = document.getElementById("msg");
m.innerHTML = msg;
}
function fillResults(results) {
const r = document.getElementById("report")
const inputs = r.getElementsByTagName("input");
Array.from(inputs).forEach(input => {
if (input.name == "defecto") return;
const sub = input.getAttribute("sub");
const value = results[sub] && results[sub][input.name] !== undefined ? results[sub][input.name] : "-!-";
input.value = value;
});
}