Skip to content

Commit efff604

Browse files
authored
Refactor JS (hestiacp#3508)
* Refactor email input sync JS * Bump npm packages * Refactor New IP List JS
1 parent 62adf2a commit efff604

23 files changed

+264
-234
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"packageManager": "yarn@3.5.0",
2121
"dependencies": {
2222
"@fortawesome/fontawesome-free": "^6.4.0",
23-
"chart.js": "^4.2.1",
23+
"chart.js": "^4.3.0",
2424
"check-password-strength": "^2.0.7",
2525
"nanoid": "^4.0.2",
2626
"normalize.css": "^8.0.1"
@@ -36,8 +36,8 @@
3636
"eslint-plugin-editorconfig": "^4.0.2",
3737
"eslint-plugin-import": "^2.27.5",
3838
"husky": "^8.0.3",
39-
"lint-staged": "^13.2.1",
40-
"markdownlint-cli2": "^0.7.0",
39+
"lint-staged": "^13.2.2",
40+
"markdownlint-cli2": "^0.7.1",
4141
"postcss": "^8.4.23",
4242
"postcss-import": "^15.1.0",
4343
"postcss-path-replace": "^1.0.4",

web/js/dist/chart.js-auto.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/js/dist/main.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/js/dist/main.min.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/js/pages/add_user.js

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

web/js/pages/add_web.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ App.Actions.WEB.update_ftp_username_hint = function (elm, hint) {
1010

1111
hint = hint.replace(/[^\w\d]/gi, '');
1212

13-
$(elm).parent().find('.v-ftp-user').val(hint);
13+
$(elm).parent().find('.js-ftp-user').val(hint);
1414
$(elm)
1515
.parent()
1616
.find('.hint')
1717
.text(Alpine.store('globals').FTP_USER_PREFIX + hint);
1818
};
1919

2020
App.Listeners.WEB.keypress_ftp_username = function () {
21-
var ftp_user_inputs = $('.v-ftp-user');
21+
var ftp_user_inputs = $('.js-ftp-user');
2222
$.each(ftp_user_inputs, function (i, ref) {
2323
var $ref = $(ref);
2424
var current_val = $ref.val();
@@ -40,9 +40,6 @@ App.Listeners.WEB.keypress_domain_name = function () {
4040
$('#v_domain').bind('keypress input', function () {
4141
clearTimeout(window.frp_usr_tmt);
4242
window.frp_usr_tmt = setTimeout(function () {
43-
// var domain = $('.js-ftp-path-prefix').text(
44-
// Alpine.store('globals').FTP_USER_PREPATH + '/' + $('#v_domain').val()
45-
// );
4643
$('#v-custom-doc-domain-main').text($('#v_domain').val());
4744
$('#v-custom-doc-domain-main').val($('#v_domain').val());
4845
App.Actions.WEB.update_custom_doc_root(13, 12);

web/js/pages/edit_web.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ App.Actions.WEB.update_ftp_username_hint = function (elm, hint) {
4444

4545
hint = hint.replace(/[^\w\d]/gi, '');
4646

47-
$(elm).parent().find('.v-ftp-user').val(hint);
47+
$(elm).parent().find('.js-ftp-user').val(hint);
4848
$(elm)
4949
.parent()
5050
.find('.hint')
5151
.text(Alpine.store('globals').FTP_USER_PREFIX + hint);
5252
};
5353

5454
App.Listeners.WEB.keypress_ftp_username = function () {
55-
var ftp_user_inputs = $('.v-ftp-user');
55+
var ftp_user_inputs = $('.js-ftp-user');
5656
$.each(ftp_user_inputs, function (i, ref) {
5757
var $ref = $(ref);
5858
var current_val = $ref.val();
@@ -132,28 +132,28 @@ App.Actions.WEB.add_ftp_user_form = function () {
132132
$elm.attr('for', forAttr.replace('%INDEX%', newIndex));
133133
});
134134

135-
template.find('.ftp-user-number').text(newIndex);
135+
template.find('.js-ftp-user-number').text(newIndex);
136136
$('#ftp_users').append(template);
137137

138138
let counter = 1;
139-
$('.form-container .ftp-user-number:visible').each((_, o) => {
139+
$('.form-container .js-ftp-user-number:visible').each((_, o) => {
140140
$(o).text(counter);
141141
counter += 1;
142142
});
143143
};
144144

145145
App.Actions.WEB.remove_ftp_user = function (elm) {
146146
var ref = $(elm).parents('.js-ftp-account');
147-
ref.find('.v-ftp-user-deleted').val('1');
148-
if (ref.find('.v-ftp-user-is-new').val() == 1) {
147+
ref.find('.js-ftp-user-deleted').val('1');
148+
if (ref.find('.js-ftp-user-is-new').val() == 1) {
149149
ref.remove();
150150
return true;
151151
}
152152
ref.removeClass('js-ftp-account-nrm');
153153
ref.hide();
154154

155155
var index = 1;
156-
$('.form-container .ftp-user-number:visible').each(function (i, o) {
156+
$('.form-container .js-ftp-user-number:visible').each(function (i, o) {
157157
$(o).text(index);
158158
index += 1;
159159
});
@@ -168,17 +168,17 @@ App.Actions.WEB.toggle_additional_ftp_accounts = function (elm) {
168168
if ($(elm).prop('checked')) {
169169
$('.js-ftp-account-nrm, .v-add-new-user, .js-add-new-ftp-user-button').show();
170170
$('.js-ftp-account-nrm').each(function (i, elm) {
171-
var login = $(elm).find('.v-ftp-user');
171+
var login = $(elm).find('.js-ftp-user');
172172
if (login.val().trim() != '') {
173-
$(elm).find('.v-ftp-user-deleted').val(0);
173+
$(elm).find('.js-ftp-user-deleted').val(0);
174174
}
175175
});
176176
} else {
177177
$('.js-ftp-account-nrm, .v-add-new-user, .js-add-new-ftp-user-button').hide();
178178
$('.js-ftp-account-nrm').each(function (i, elm) {
179-
var login = $(elm).find('.v-ftp-user');
179+
var login = $(elm).find('.js-ftp-user');
180180
if (login.val().trim() != '') {
181-
$(elm).find('.v-ftp-user-deleted').val(1);
181+
$(elm).find('.js-ftp-user-deleted').val(1);
182182
}
183183
});
184184
}
@@ -191,7 +191,7 @@ App.Actions.WEB.randomPasswordGenerated = function (elm) {
191191
App.Actions.WEB.passwordChanged = function (elm) {
192192
var ref = $(elm).parents('.js-ftp-account');
193193
if (ref.find('.js-email-alert-on-psw').length == 0) {
194-
var inp_name = ref.find('.v-ftp-user-is-new').prop('name');
194+
var inp_name = ref.find('.js-ftp-user-is-new').prop('name');
195195
inp_name = inp_name.replace('is_new', 'v_ftp_email');
196196
ref.find('div:last').after(
197197
`<div class="u-pl30 u-mb10">
@@ -211,7 +211,7 @@ App.Listeners.WEB.keypress_ftp_username();
211211
App.Listeners.WEB.keypress_ftp_path();
212212

213213
$(function () {
214-
$('.v-ftp-user-psw').on('keypress', function (evt) {
214+
$('.js-ftp-user-psw').on('keypress', function (evt) {
215215
var elm = $(evt.target);
216216
App.Actions.WEB.passwordChanged(elm);
217217
});
@@ -262,18 +262,13 @@ $(function () {
262262
});
263263
});
264264

265-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
266-
function WEBrandom() {
267-
document.v_edit_web.v_stats_password.value = Hestia.helpers.randomPassword();
268-
}
269-
270265
// eslint-disable-next-line @typescript-eslint/no-unused-vars
271266
function FTPrandom(elm) {
272-
$(elm).parents('.js-ftp-account').find('.v-ftp-user-psw').val(Hestia.helpers.randomPassword());
267+
$(elm).parents('.js-ftp-account').find('.js-ftp-user-psw').val(Hestia.helpers.randomPassword());
273268
App.Actions.WEB.randomPasswordGenerated && App.Actions.WEB.randomPasswordGenerated(elm);
274269
}
275270

276-
$('.v-redirect-custom-value').change(function () {
271+
$('.js-redirect-custom-value').change(function () {
277272
if (this.value == 'custom') {
278273
$('#custom_redirect').show();
279274
} else {

web/js/src/ipListDataSource.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Populates the "Data Source" select with various IP lists on the New IP List page
2+
export default function handleIpListDataSource() {
3+
const dataSourceSelect = document.querySelector('.js-datasource-select');
4+
5+
if (!dataSourceSelect) return;
6+
7+
// Parse IP lists from HTML and sort them alphabetically
8+
const countryIplists = parseAndSortIplists(dataSourceSelect.dataset.countryIplists);
9+
const blacklistIplists = parseAndSortIplists(dataSourceSelect.dataset.blacklistIplists);
10+
11+
// Add IP lists to the "Data Source" select
12+
addIPListsToSelect(dataSourceSelect, Alpine.store('globals').BLACKLIST, blacklistIplists);
13+
addIPListsToSelect(dataSourceSelect, Alpine.store('globals').IPVERSE, countryIplists);
14+
}
15+
16+
function parseAndSortIplists(iplistsData) {
17+
const iplists = JSON.parse(iplistsData || '[]');
18+
return iplists.sort((a, b) => a.name.localeCompare(b.name));
19+
}
20+
21+
function addIPListsToSelect(dataSourceSelect, label, iplists) {
22+
// Add a disabled option as a label
23+
addOption(dataSourceSelect, label, '', true);
24+
25+
// Add IP lists to the select element
26+
iplists.forEach((iplist) => {
27+
addOption(dataSourceSelect, iplist.name, iplist.source, false);
28+
});
29+
}
30+
31+
function addOption(element, text, value, disabled) {
32+
const option = document.createElement('option');
33+
option.text = text;
34+
option.value = value;
35+
if (disabled) option.disabled = true;
36+
element.appendChild(option);
37+
}

web/js/src/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import handleCronGenerator from './cronGenerator';
66
import handleDiscardAllMail from './discardAllMail';
77
import handleErrorMessage from './errorHandler';
88
import handleFormSubmit from './formSubmit';
9+
import handleIpListDataSource from './ipListDataSource';
910
import handleListSelectAll from './listSelectAll';
1011
import handleListSorting from './listSorting';
1112
import handleNameServerInput from './nameServerInput';
1213
import handlePasswordInput from './passwordInput';
1314
import handleShortcuts from './shortcuts';
1415
import handleStickyToolbar from './stickyToolbar';
16+
import handleSyncEmailValues from './syncEmailValues';
1517
import handleTabPanels from './tabPanels';
1618
import handleToggleAdvanced from './toggleAdvanced';
1719
import handleUnlimitedInput from './unlimitedInput';
@@ -33,13 +35,15 @@ function initListeners() {
3335
handleNameServerInput();
3436
handlePasswordInput();
3537
handleStickyToolbar();
38+
handleSyncEmailValues();
3639
handleTabPanels();
3740
handleToggleAdvanced();
3841
}
3942

4043
document.addEventListener('alpine:init', () => {
4144
alpineInit();
4245
handleErrorMessage();
46+
handleIpListDataSource();
4347
handleShortcuts();
4448
handleUnlimitedInput();
4549
});

web/js/src/syncEmailValues.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Synchronizes the "Email" input value with "Email login credentials to" input value
2+
// based on the "Send welcome email" checkbox state on Add User page
3+
export default function handleSyncEmailValues() {
4+
const emailInput = document.querySelector('.js-sync-email-input');
5+
const sendWelcomeEmailCheckbox = document.querySelector('.js-sync-email-checkbox');
6+
const emailCredentialsToInput = document.querySelector('.js-sync-email-output');
7+
8+
if (!emailInput || !sendWelcomeEmailCheckbox || !emailCredentialsToInput) {
9+
return;
10+
}
11+
12+
function syncEmailValues() {
13+
if (sendWelcomeEmailCheckbox.checked) {
14+
emailCredentialsToInput.value = emailInput.value;
15+
} else {
16+
emailCredentialsToInput.value = '';
17+
}
18+
}
19+
20+
emailInput.addEventListener('input', syncEmailValues);
21+
sendWelcomeEmailCheckbox.addEventListener('change', syncEmailValues);
22+
}

0 commit comments

Comments
 (0)