-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommon.js
More file actions
69 lines (65 loc) · 2.26 KB
/
common.js
File metadata and controls
69 lines (65 loc) · 2.26 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
// Clips all inputs to their min/max values.
// Returns true iff any input was changed.
function validateInputs() {
let changed = false;
$("input").each(function() {
switch ($(this).attr('type')) {
case 'checkbox':
let is_checked = $(this).attr('checked');
$(this).val(is_checked);
return;
case 'number':
let originalVal = parseInt($(this).val());
let maxVal = parseInt($(this).attr('max'));
let minVal = parseInt($(this).attr('min'));
if (isNaN(originalVal)) {
let defaultVal = parseInt($(this).prop('defaultValue'));
$(this).val(defaultVal);
changed = true;
} else if (originalVal > maxVal) {
$(this).val(maxVal);
changed = true;
} else if (originalVal < minVal) {
$(this).val(minVal);
changed = true;
}
}
});
return changed;
}
function inputsAreValid() {
let allValid = true;
$("input").each(function() {
if ($(this).attr('type') != 'number') {
return;
}
let val = parseInt($(this).val());
let maxVal = parseInt($(this).attr('max'));
let minVal = parseInt($(this).attr('min'));
if (isNaN(val) || val > maxVal || val < minVal) {
allValid = false;
}
});
return allValid;
}
// Sets inputs based on search query by id (assumed to be equal to name).
function setInputsFromSearchQuery() {
let searchParams = new URLSearchParams(window.location.search);
searchParams.forEach(function(value, key) {
let field = $("#" + key);
if (field.attr('type') == 'checkbox') {
field.attr('checked', true);
} else if (field.attr('type') == 'number') {
let n = parseInt(value);
if (!(n >= field.attr('min') && n <= field.attr('max'))) {
return;
}
}
field.val(value);
});
}
// Updates search query based on all forms.
function updateSearchQueryFromForms() {
let searchParams = $("form").serialize();
history.replaceState(null, "", "?" + searchParams);
}