Skip to content

Commit 552c103

Browse files
authored
Refactor Add/Edit Database JS (hestiacp#3511)
* Remove dead Add Web Domain JS * Simplify globals * Refactor Add/Edit Database JS
1 parent d3875f8 commit 552c103

21 files changed

+74
-189
lines changed

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_db.js

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

web/js/pages/add_web.js

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

web/js/pages/edit_db.js

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

web/js/pages/edit_web.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ App.Actions.WEB.update_custom_doc_root = function () {
33
var domain = $('select[name="v-custom-doc-domain"]').val();
44
var folder = $('input[name="v-custom-doc-folder"]').val();
55

6-
$('.custom_docroot_hint').html(prepath + domain + '/public_html/' + folder);
6+
$('.js-custom-docroot-hint').html(prepath + domain + '/public_html/' + folder);
77
};
88
App.Listeners.DB.keypress_custom_folder = function () {
99
var ref = $('input[name="v-custom-doc-folder"]');
@@ -48,7 +48,7 @@ App.Actions.WEB.update_ftp_username_hint = function (elm, hint) {
4848
$(elm)
4949
.parent()
5050
.find('.hint')
51-
.text(Alpine.store('globals').FTP_USER_PREFIX + hint);
51+
.text(Alpine.store('globals').USER_PREFIX + hint);
5252
};
5353

5454
App.Listeners.WEB.keypress_ftp_username = function () {

web/js/src/copyCreds.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ function monitorAndUpdate(inputSelector, outputSelector) {
2222

2323
inputElement.addEventListener(
2424
'input',
25-
debounce((evt) => {
26-
updateOutput(evt.target.value);
27-
}, 100)
25+
debounce((evt) => updateOutput(evt.target.value))
2826
);
2927
updateOutput(inputElement.value);
3028
}

web/js/src/databaseHints.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { debounce } from './helpers';
2+
3+
// Attach listener to database "Name" and "Username" fields to update their hints
4+
export default function handleDatabaseHints() {
5+
const usernameInput = document.querySelector('.js-db-hint-username');
6+
const databaseNameInput = document.querySelector('.js-db-hint-database-name');
7+
8+
if (!usernameInput || !databaseNameInput) {
9+
return;
10+
}
11+
12+
removeUserPrefix(databaseNameInput);
13+
attachUpdateHintListener(usernameInput);
14+
attachUpdateHintListener(databaseNameInput);
15+
}
16+
17+
// Remove prefix from "Database" input if it exists during initial load (for editing)
18+
function removeUserPrefix(input) {
19+
const prefixIndex = input.value.indexOf(Alpine.store('globals').USER_PREFIX);
20+
if (prefixIndex === 0) {
21+
input.value = input.value.slice(Alpine.store('globals').USER_PREFIX.length);
22+
}
23+
}
24+
25+
function attachUpdateHintListener(input) {
26+
if (input.value.trim() !== '') {
27+
updateHint(input);
28+
}
29+
30+
input.addEventListener(
31+
'input',
32+
debounce((evt) => updateHint(evt.target))
33+
);
34+
}
35+
36+
function updateHint(input) {
37+
const hintElement = input.parentElement.querySelector('.hint');
38+
39+
if (input.value.trim() === '') {
40+
hintElement.textContent = '';
41+
}
42+
43+
hintElement.textContent = Alpine.store('globals').USER_PREFIX + input.value;
44+
}

web/js/src/dnsRecordHint.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ export default function handleDnsRecordHint() {
1414

1515
recordInput.addEventListener(
1616
'input',
17-
debounce((evt) => {
18-
updateHint(evt.target);
19-
}, 100)
17+
debounce((evt) => updateHint(evt.target))
2018
);
2119
}
2220

2321
// Update DNS "Record" field hint
2422
function updateHint(input) {
25-
const domainInput = document.querySelector('input[name="v_domain"]');
23+
const domainInput = document.querySelector('.js-dns-record-domain');
2624
const hintElement = input.parentElement.querySelector('.hint');
2725
let hint = input.value.trim();
2826

web/js/src/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function randomPassword(length = 16) {
1919
}
2020

2121
// Debounces a function to avoid excessive calls
22-
export function debounce(func, wait) {
22+
export function debounce(func, wait = 100) {
2323
let timeout;
2424
return function (...args) {
2525
clearTimeout(timeout);

0 commit comments

Comments
 (0)