forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunlimitedInput.js
More file actions
60 lines (51 loc) · 1.59 KB
/
unlimitedInput.js
File metadata and controls
60 lines (51 loc) · 1.59 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
export default function handleUnlimitedInput() {
// Add listeners to "unlimited" input toggles
document.querySelectorAll('.js-unlimited-toggle').forEach((toggleButton) => {
const input = toggleButton.parentElement.querySelector('input');
if (isUnlimitedValue(input.value)) {
enableInput(input, toggleButton);
} else {
disableInput(input, toggleButton);
}
toggleButton.addEventListener('click', () => {
toggleInput(input, toggleButton);
});
});
}
// Called on form submit to enable any disabled unlimited inputs
export function enableUnlimitedInputs() {
document.querySelectorAll('input:disabled').forEach((input) => {
if (isUnlimitedValue(input.value)) {
input.disabled = false;
input.value = 'unlimited';
}
});
}
function isUnlimitedValue(value) {
const trimmedValue = value.trim();
return trimmedValue === 'unlimited' || trimmedValue === Alpine.store('globals').UNLIMITED;
}
function enableInput(input, toggleButton) {
toggleButton.classList.add('active');
input.dataset.prevValue = input.value;
input.value = Alpine.store('globals').UNLIMITED;
input.disabled = true;
}
function disableInput(input, toggleButton) {
toggleButton.classList.remove('active');
const previousValue = input.dataset.prevValue ? input.dataset.prevValue.trim() : null;
if (previousValue) {
input.value = previousValue;
}
if (isUnlimitedValue(input.value)) {
input.value = '0';
}
input.disabled = false;
}
function toggleInput(input, toggleButton) {
if (toggleButton.classList.contains('active')) {
disableInput(input, toggleButton);
} else {
enableInput(input, toggleButton);
}
}