databaseHints.js 1.2 KB

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