forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftpAccountHints.js
More file actions
54 lines (42 loc) · 1.52 KB
/
ftpAccountHints.js
File metadata and controls
54 lines (42 loc) · 1.52 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
import { debounce } from './helpers';
// Attach event listeners to FTP account "Username" and "Path" fields to update their hints
export default function handleFtpAccountHints() {
addHintListeners('.js-ftp-user', updateFtpUsernameHint);
addHintListeners('.js-ftp-path', updateFtpPathHint);
}
function addHintListeners(selector, updateHintFunction) {
document.querySelectorAll(selector).forEach((inputElement) => {
const currentValue = inputElement.value.trim();
if (currentValue !== '') {
updateHintFunction(inputElement, currentValue);
}
inputElement.addEventListener(
'input',
debounce((event) => updateHintFunction(event.target, event.target.value)),
);
});
}
function updateFtpUsernameHint(usernameInput, username) {
const inputWrapper = usernameInput.parentElement;
const hintElement = inputWrapper.querySelector('.js-ftp-user-hint');
// Remove special characters
const sanitizedUsername = username.replace(/[^\w\d]/gi, '');
if (sanitizedUsername !== username) {
usernameInput.value = sanitizedUsername;
}
hintElement.textContent = Alpine.store('globals').USER_PREFIX + sanitizedUsername;
}
function updateFtpPathHint(pathInput, path) {
const inputWrapper = pathInput.parentElement;
const hintElement = inputWrapper.querySelector('.js-ftp-path-hint');
const normalizedPath = normalizePath(path);
hintElement.textContent = normalizedPath;
}
function normalizePath(path) {
// Add leading slash
if (path[0] !== '/') {
path = `/${path}`;
}
// Remove double slashes
return path.replace(/\/(\/+)/g, '/');
}