-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_handler.js
More file actions
95 lines (79 loc) · 3.14 KB
/
Copy pathinput_handler.js
File metadata and controls
95 lines (79 loc) · 3.14 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
function InputHandler() {
return this;
}
InputHandler.prototype.constructor = InputHandler;
InputHandler.prototype.setupInput = async function (el, d, prefix) {
// set task-id on parent input-form as common way to get, well, task-id
// (some elements can have id, others duplicate-id)
el.closest('.input-form').attr('task-id', d.id);
// (other forms can be appended to this el later on;
// so need to get submit buttons now)
const submit = el.find('input[value=submit]');
submit.on("click", (e) => this.submitInputData(e.target));
// register listener for enter-key for input-fields
el.find('input').not('input[type=submit]')
.keypress((e) => {
if (e.which == 13) // enter
submit.trigger("click");
});
// el.find("input[value=reset]").on("click", (e) => {
// let id = $(e.target).parents('.infobox').attr('id');
// this.resetObservations(id);
// });
// register listener for ensuring mutex property for checkboxes (if needed)
el.find('input[type=checkbox]').each((idx, checkbox) => {
checkbox = $(checkbox);
checkbox.change(() => {
if (checkbox.is(":checked")) {
if (checkbox.attr('mutex-with')) {
const mutexes = checkbox.attr('mutex-with').split(",");
mutexes.forEach(mutex => {
$("#" + mutex.replace('\.', '\\\.')).prop('checked', false);
});
}
}
});
});
if (prefix) {
el.find('input[type=radio]').each((idx, radio) => {
radio = $(radio);
// for cig_composite: make sure name attr is unique per type of cig
// (else, auto-populating input of other cig can mess up prior selection)
radio.attr('name', prefix + ":" + radio.attr('name'));
});
}
}
InputHandler.prototype.inputShown = async function (id, el) {
let inputs = el.find('input');
// console.log("shown:", el, inputs);
if (inputs.length > 0) {
// put focus on first input element
inputs.get(0).focus();
// try populating the input elements
this._tryPopulateInput(id, el);
}
}
InputHandler.prototype._tryPopulateInput = function (id, el) { }
InputHandler.prototype.submitInputData = async function (element) { }
// NOTE: currently assumes only 1 type of input element per info-box
InputHandler.prototype._checkInputData = function (el) {
var inputs = el.find('input[type=number]');
if (inputs.length > 0) {
const array = inputs.toArray();
if (!array.every(input => input.value !== ""))
return "Some input values are missing.";
}
inputs = el.find('input[type=checkbox]');
if (inputs.length > 0) {
const array = inputs.toArray();
if (!array.some((input) => input.checked))
return "Please check at least one option.";
}
inputs = el.find('input[type=radio]');
if (inputs.length > 0) {
const array = inputs.toArray();
if (!array.some((input) => input.checked))
return "Please select an option.";
}
return true;
}