forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnameServerInput.js
More file actions
40 lines (34 loc) · 1.29 KB
/
nameServerInput.js
File metadata and controls
40 lines (34 loc) · 1.29 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
// Attaches listeners to nameserver add and remove links to clone or remove the input
export default function handleNameServerInput() {
// Add new name server input
const addNsButton = document.querySelector('.js-add-ns');
if (addNsButton) {
addNsButton.addEventListener('click', () => addNsInput(addNsButton));
}
// Remove name server input
document.querySelectorAll('.js-remove-ns').forEach((removeNsElem) => {
removeNsElem.addEventListener('click', () => removeNsInput(removeNsElem));
});
}
function addNsInput(addNsButton) {
const currentNsInputs = document.querySelectorAll('input[name^=v_ns]');
const inputCount = currentNsInputs.length;
if (inputCount < 8) {
const template = currentNsInputs[0].parentElement.cloneNode(true);
const templateNsInput = template.querySelector('input');
templateNsInput.removeAttribute('value');
templateNsInput.name = `v_ns${inputCount + 1}`;
addNsButton.before(template);
}
if (inputCount === 7) {
addNsButton.classList.add('u-hidden');
}
}
function removeNsInput(removeNsElement) {
removeNsElement.parentElement.remove();
const currentNsInputs = document.querySelectorAll('input[name^=v_ns]');
currentNsInputs.forEach((input, index) => {
input.name = `v_ns${index + 1}`;
});
document.querySelector('.js-add-ns').classList.remove('u-hidden');
}