loginForm.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. function Password(parent, open) {
  2. const input = document.createElement('input');
  3. input.type = 'password';
  4. parent.appendChild(input);
  5. const button = document.createElement('button');
  6. button.type = 'button';
  7. button.textContent = 'показать';
  8. parent.appendChild(button);
  9. button.addEventListener('click', () => {
  10. this.setOpen(open !== true);
  11. });
  12. this.setValue = newValue => input.value = newValue;
  13. this.getValue = () => input.value;
  14. this.setOpen = openUpdate => {
  15. open = openUpdate;
  16. this.onOpenChange(openUpdate);
  17. button.textContent = (openUpdate) ? 'показать' : 'скрыть';
  18. input.type = (openUpdate) ? 'password' : 'text';
  19. }
  20. this.getOpen = () => open;
  21. input.addEventListener('input', event => {
  22. if (typeof this.onChange === 'function'){
  23. this.onChange(event.target.value);
  24. }
  25. });
  26. this.onOpenChange = isOpen => isOpen;
  27. }
  28. function LoginForm () {
  29. const createDivider = () => document.body.appendChild(document.createElement('br'));
  30. const input = document.createElement('input');
  31. input.type = 'text';
  32. input.classList.add('form-text');
  33. document.body.appendChild(input);
  34. createDivider();
  35. const password = new Password(document.body, true);
  36. createDivider();
  37. const button = document.createElement('button');
  38. button.type = 'button';
  39. button.classList.add('login-button');
  40. button.textContent = 'Логин';
  41. button.disabled = true;
  42. document.body.appendChild(button);
  43. const isDisabled = () => {
  44. button.disabled = (password.getValue() !== '' && input.value !== '') ? false : true;
  45. }
  46. password.onChange = () => {
  47. isDisabled();
  48. }
  49. input.addEventListener('input', () => {
  50. isDisabled();
  51. });
  52. }
  53. LoginForm();