forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabaseHints.js
More file actions
44 lines (35 loc) · 1.23 KB
/
databaseHints.js
File metadata and controls
44 lines (35 loc) · 1.23 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
import { debounce } from './helpers';
// Attach listener to database "Name" and "Username" fields to update their hints
export default function handleDatabaseHints() {
const usernameInput = document.querySelector('.js-db-hint-username');
const databaseNameInput = document.querySelector('.js-db-hint-database-name');
if (!usernameInput || !databaseNameInput) {
return;
}
removeUserPrefix(databaseNameInput);
attachUpdateHintListener(usernameInput);
attachUpdateHintListener(databaseNameInput);
}
// Remove prefix from "Database" input if it exists during initial load (for editing)
function removeUserPrefix(input) {
const prefixIndex = input.value.indexOf(Alpine.store('globals').USER_PREFIX);
if (prefixIndex === 0) {
input.value = input.value.slice(Alpine.store('globals').USER_PREFIX.length);
}
}
function attachUpdateHintListener(input) {
if (input.value.trim() !== '') {
updateHint(input);
}
input.addEventListener(
'input',
debounce((evt) => updateHint(evt.target)),
);
}
function updateHint(input) {
const hintElement = input.parentElement.querySelector('.hint');
if (input.value.trim() === '') {
hintElement.textContent = '';
}
hintElement.textContent = Alpine.store('globals').USER_PREFIX + input.value;
}