Skip to content

Commit 7dd2e59

Browse files
authored
Refactor add/remove nameserver JS (hestiacp#3468)
* Simplify confirmation dialog JS * Refactor add/remove nameserver JS To reduce duplication. * Further refactor password input JS * Tidy JS
1 parent 20514d9 commit 7dd2e59

File tree

16 files changed

+84
-231
lines changed

16 files changed

+84
-231
lines changed

web/js/main.js

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,60 @@ document.addEventListener('alpine:init', () => {
228228
}));
229229
});
230230

231+
// Add new name server input
232+
const addNsButton = document.querySelector('.js-add-ns');
233+
if (addNsButton) {
234+
addNsButton.addEventListener('click', () => {
235+
const currentNsInputs = document.querySelectorAll('input[name^=v_ns]');
236+
const inputCount = currentNsInputs.length;
237+
238+
if (inputCount < 8) {
239+
const template = currentNsInputs[0].parentElement.cloneNode(true);
240+
const templateNsInput = template.querySelector('input');
241+
242+
templateNsInput.removeAttribute('value');
243+
templateNsInput.name = `v_ns${inputCount + 1}`;
244+
addNsButton.insertAdjacentElement('beforebegin', template);
245+
}
246+
247+
if (inputCount === 7) {
248+
addNsButton.classList.add('u-hidden');
249+
}
250+
});
251+
}
252+
253+
// Remove name server input
254+
document.querySelectorAll('.js-remove-ns').forEach((removeNsButton) => {
255+
removeNsButton.addEventListener('click', () => {
256+
removeNsButton.parentElement.remove();
257+
const currentNsInputs = document.querySelectorAll('input[name^=v_ns]');
258+
currentNsInputs.forEach((input, index) => (input.name = `v_ns${index + 1}`));
259+
document.querySelector('.js-add-ns').classList.remove('u-hidden');
260+
});
261+
});
262+
231263
// Intercept clicks on .js-confirm-action links and display dialog
232-
document.addEventListener('click', (evt) => {
233-
const triggerLink = evt.target.closest('.js-confirm-action');
234-
if (!triggerLink) {
235-
return;
236-
}
237-
evt.preventDefault();
264+
document.querySelectorAll('.js-confirm-action').forEach((triggerLink) => {
265+
triggerLink.addEventListener('click', (evt) => {
266+
evt.preventDefault();
267+
268+
const title = triggerLink.dataset.confirmTitle;
269+
const message = triggerLink.dataset.confirmMessage;
270+
const targetUrl = triggerLink.getAttribute('href');
238271

239-
const title = triggerLink.getAttribute('data-confirm-title');
240-
const message = triggerLink.getAttribute('data-confirm-message');
241-
const targetUrl = triggerLink.getAttribute('href');
272+
VE.helpers.createConfirmationDialog({ title, message, targetUrl });
273+
});
274+
});
275+
276+
// Listen for changes to password inputs and update the password strength
277+
document.querySelectorAll('.js-password-input').forEach((passwordInput) => {
278+
const updateTimeout = (evt) => {
279+
clearTimeout(window.frp_usr_tmt);
280+
window.frp_usr_tmt = setTimeout(() => {
281+
VE.helpers.recalculatePasswordStrength(evt.target);
282+
}, 100);
283+
};
242284

243-
VE.helpers.createConfirmationDialog({ title, message, targetUrl });
285+
passwordInput.addEventListener('keypress', updateTimeout);
286+
passwordInput.addEventListener('input', updateTimeout);
244287
});

web/js/pages/add_cron.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ if (tabs) {
2424
});
2525
}
2626

27-
const generateCronButtons = document.querySelectorAll('.js-generate-cron');
28-
generateCronButtons.forEach((button) => {
27+
document.querySelectorAll('.js-generate-cron').forEach((button) => {
2928
button.addEventListener('click', () => {
3029
const fieldset = button.closest('fieldset');
3130
const inputNames = ['min', 'hour', 'day', 'month', 'wday'];

web/js/pages/add_db.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,14 @@ App.Listeners.DB.keypress_db_databasename = function () {
6060
});
6161
};
6262

63-
App.Listeners.DB.keypress_v_password = function () {
64-
var ref = $('input[name="v_password"]');
65-
ref.bind('keypress input', function (evt) {
66-
clearTimeout(window.frp_usr_tmt);
67-
window.frp_usr_tmt = setTimeout(function () {
68-
VE.helpers.recalculatePasswordStrength(evt.target);
69-
}, 100);
70-
});
71-
};
72-
73-
App.Listeners.DB.keypress_v_password();
74-
7563
//
7664
// Page entry point
7765
// Trigger listeners
7866
App.Listeners.DB.keypress_db_username();
7967
App.Listeners.DB.keypress_db_databasename();
8068

8169
applyRandomPassword = function (min_length = 16) {
82-
const passwordInput = document.querySelector('input[name=v_password]');
70+
const passwordInput = document.querySelector('.js-password-input');
8371
if (passwordInput) {
8472
passwordInput.value = randomString(min_length);
8573
VE.helpers.recalculatePasswordStrength(passwordInput);

web/js/pages/add_dns.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

web/js/pages/add_mail_acc.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,9 @@ $('form[name="v_quota"]').on('submit', function () {
1717
});
1818
});
1919

20-
App.Listeners.MAIL_ACC.keypress_v_password = function () {
21-
var ref = $('input[name="v_password"]');
22-
ref.bind('keypress input', function (evt) {
23-
clearTimeout(window.frp_usr_tmt);
24-
window.frp_usr_tmt = setTimeout(function () {
25-
VE.helpers.recalculatePasswordStrength(evt.target);
26-
}, 100);
27-
});
28-
};
29-
30-
App.Listeners.MAIL_ACC.keypress_v_password();
31-
3220
applyRandomPassword = function (min_length = 16) {
3321
const randomPassword = randomString(min_length);
34-
const passwordInput = document.querySelector('input[name=v_password]');
22+
const passwordInput = document.querySelector('.js-password-input');
3523
if (passwordInput) {
3624
passwordInput.value = randomPassword;
3725
VE.helpers.recalculatePasswordStrength(passwordInput);
@@ -66,23 +54,23 @@ generate_mail_credentials = function () {
6654

6755
$(document).ready(function () {
6856
$('.js-account-output').text($('input[name=v_account]').val());
69-
$('.js-password-output').text($('input[name=v_password]').val());
57+
$('.js-password-output').text($('.js-password-input').val());
7058
generate_mail_credentials();
7159

7260
$('input[name=v_account]').change(function () {
7361
$('.js-account-output').text($(this).val());
7462
generate_mail_credentials();
7563
});
7664

77-
$('input[name=v_password]').change(function () {
78-
if ($('input[name=v_password]').attr('type') == 'text')
65+
$('.js-password-input').change(function () {
66+
if ($('.js-password-input').attr('type') == 'text')
7967
$('.js-password-output').text($(this).val());
8068
else $('.js-password-output').text(Array($(this).val().length + 1).join('*'));
8169
generate_mail_credentials();
8270
});
8371

8472
$('.toggle-psw-visibility-icon').click(function () {
85-
$('.js-password-output').text($('input[name=v_password]').val());
73+
$('.js-password-output').text($('.js-password-input').val());
8674
generate_mail_credentials();
8775
});
8876

web/js/pages/add_package.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,3 @@ $('form[name="v_add_package"]').on('submit', function () {
66
}
77
});
88
});
9-
10-
$(document).ready(function () {
11-
$('.js-add-ns').click(function () {
12-
var n = $('input[name^=v_ns]').length;
13-
if (n < 8) {
14-
var t = $($('input[name=v_ns1]').parents('div')[0]).clone(true, true);
15-
t.find('input').attr({ value: '', name: 'v_ns' + (n + 1) });
16-
t.find('span').show();
17-
$('.js-add-ns').before(t);
18-
}
19-
if (n == 7) {
20-
$('.js-add-ns').addClass('u-hidden');
21-
}
22-
});
23-
24-
$('.js-remove-ns').click(function () {
25-
$(this).parents('div')[0].remove();
26-
$('input[name^=v_ns]').each(function (i, ns) {
27-
$(ns).attr({ name: 'v_ns' + (i + 1) });
28-
i < 2 ? $(ns).parent().find('span').hide() : $(ns).parent().find('span').show();
29-
});
30-
$('.js-add-ns').removeClass('u-hidden');
31-
});
32-
33-
$('input[name^=v_ns]').each(function (i, ns) {
34-
i < 2 ? $(ns).parent().find('span').hide() : $(ns).parent().find('span').show();
35-
});
36-
});

web/js/pages/add_user.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,9 @@ $(function () {
1414
});
1515

1616
applyRandomPassword = function (min_length = 16) {
17-
const passwordInput = document.querySelector('input[name=v_password]');
17+
const passwordInput = document.querySelector('.js-password-input');
1818
if (passwordInput) {
1919
passwordInput.value = randomString(min_length);
2020
VE.helpers.recalculatePasswordStrength(passwordInput);
2121
}
2222
};
23-
24-
App.Listeners.WEB.keypress_v_password = function () {
25-
var ref = $('input[name="v_password"]');
26-
ref.bind('keypress input', function (evt) {
27-
clearTimeout(window.frp_usr_tmt);
28-
window.frp_usr_tmt = setTimeout(function () {
29-
VE.helpers.recalculatePasswordStrength(evt.target);
30-
}, 100);
31-
});
32-
};
33-
34-
App.Listeners.WEB.keypress_v_password();

web/js/pages/edit_cron.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ if (tabs) {
2424
});
2525
}
2626

27-
const generateCronButtons = document.querySelectorAll('.js-generate-cron');
28-
generateCronButtons.forEach((button) => {
27+
document.querySelectorAll('.js-generate-cron').forEach((button) => {
2928
button.addEventListener('click', () => {
3029
const fieldset = button.closest('fieldset');
3130
const inputNames = ['min', 'hour', 'day', 'month', 'wday'];

web/js/pages/edit_db.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,14 @@ App.Listeners.DB.keypress_db_databasename = function () {
6767
});
6868
};
6969

70-
App.Listeners.DB.keypress_v_password = function () {
71-
var ref = $('input[name="v_password"]');
72-
ref.bind('keypress input', function (evt) {
73-
clearTimeout(window.frp_usr_tmt);
74-
window.frp_usr_tmt = setTimeout(function () {
75-
VE.helpers.recalculatePasswordStrength(evt.target);
76-
}, 100);
77-
});
78-
};
79-
80-
App.Listeners.DB.keypress_v_password();
81-
8270
//
8371
// Page entry point
8472
// Trigger listeners
8573
App.Listeners.DB.keypress_db_username();
8674
App.Listeners.DB.keypress_db_databasename();
8775

8876
applyRandomPassword = function (min_length = 16) {
89-
const passwordInput = document.querySelector('input[name=v_password]');
77+
const passwordInput = document.querySelector('.js-password-input');
9078
if (passwordInput) {
9179
passwordInput.value = randomString(min_length);
9280
VE.helpers.recalculatePasswordStrength(passwordInput);

web/js/pages/edit_mail_acc.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
App.Listeners.MAIL_ACC.keypress_v_password = function () {
2-
var ref = $('input[name="v_password"]');
3-
ref.bind('keypress input', function (evt) {
4-
clearTimeout(window.frp_usr_tmt);
5-
window.frp_usr_tmt = setTimeout(function () {
6-
VE.helpers.recalculatePasswordStrength(evt.target);
7-
}, 100);
8-
});
9-
};
10-
111
$('#v_blackhole').on('click', function () {
122
if ($('#v_blackhole').is(':checked')) {
133
$('#v_fwd').prop('disabled', true);
@@ -19,11 +9,9 @@ $('#v_blackhole').on('click', function () {
199
}
2010
});
2111

22-
App.Listeners.MAIL_ACC.keypress_v_password();
23-
2412
applyRandomPassword = function (min_length = 16) {
2513
const randomPassword = randomString(min_length);
26-
const passwordInput = document.querySelector('input[name=v_password]');
14+
const passwordInput = document.querySelector('.js-password-input');
2715
if (passwordInput) {
2816
passwordInput.value = randomPassword;
2917
VE.helpers.recalculatePasswordStrength(passwordInput);
@@ -60,26 +48,25 @@ generate_mail_credentials = function () {
6048

6149
$(document).ready(function () {
6250
$('.js-account-output').text($('input[name=v_account]').val());
63-
$('.js-password-output').text($('input[name=v_password]').val());
51+
$('.js-password-output').text($('.js-password-input').val());
6452
generate_mail_credentials();
6553

6654
$('input[name=v_account]').change(function () {
6755
$('.js-account-output').text($(this).val());
6856
generate_mail_credentials();
6957
});
7058

71-
$('input[name=v_password]').change(function () {
72-
if ($('input[name=v_password]').attr('type') == 'text')
59+
$('.js-password-input').change(function () {
60+
if ($('.js-password-input').attr('type') == 'text')
7361
$('.js-password-output').text($(this).val());
7462
else $('.js-password-output').text(Array($(this).val().length + 1).join('*'));
7563
generate_mail_credentials();
7664
});
7765

7866
$('.toggle-psw-visibility-icon').click(function () {
79-
if ($('input[name=v_password]').attr('type') == 'text')
80-
$('.js-password-output').text($('input[name=v_password]').val());
81-
else
82-
$('.js-password-output').text(Array($('input[name=v_password]').val().length + 1).join('*'));
67+
if ($('.js-password-input').attr('type') == 'text')
68+
$('.js-password-output').text($('.js-password-input').val());
69+
else $('.js-password-output').text(Array($('.js-password-input').val().length + 1).join('*'));
8370
generate_mail_credentials();
8471
});
8572

0 commit comments

Comments
 (0)