dnsRecordHint.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { debounce } from './helpers';
  2. // Attach listener to DNS "Record" field to update its hint
  3. export default function handleDnsRecordHint() {
  4. const recordInput = document.querySelector('.js-dns-record-input');
  5. if (!recordInput) {
  6. return;
  7. }
  8. if (recordInput.value.trim() != '') {
  9. updateHint(recordInput);
  10. }
  11. recordInput.addEventListener(
  12. 'input',
  13. debounce((evt) => updateHint(evt.target))
  14. );
  15. }
  16. // Update DNS "Record" field hint
  17. function updateHint(input) {
  18. const domainInput = document.querySelector('.js-dns-record-domain');
  19. const hintElement = input.parentElement.querySelector('.hint');
  20. let hint = input.value.trim();
  21. // Clear the hint if input is empty
  22. if (hint === '') {
  23. hintElement.textContent = '';
  24. return;
  25. }
  26. // Set domain name without rec in case of @ entries
  27. if (hint === '@') {
  28. hint = '';
  29. }
  30. // Don't show prefix if domain name equals rec value
  31. if (hint === domainInput.value) {
  32. hint = '';
  33. }
  34. // Add dot at the end if needed
  35. if (hint !== '' && hint.slice(-1) !== '.') {
  36. hint += '.';
  37. }
  38. hintElement.textContent = hint + domainInput.value;
  39. }