loginFormConstructor.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 (parent) {
  29. this.createDivider = () => document.body.appendChild(document.createElement('br'));
  30. const input = document.createElement('input');
  31. input.type = 'text';
  32. input.classList.add('form-text');
  33. parent.appendChild(input);
  34. const button = document.createElement('button');
  35. button.type = 'button';
  36. button.classList.add('login-button');
  37. button.textContent = 'Логин';
  38. button.disabled = true;
  39. input.addEventListener('input', event => {
  40. if (typeof this.onChange === 'function'){
  41. this.onChange(event.target.value);
  42. }
  43. });
  44. this.getValue = () => input.value;
  45. this.addButton = () => parent.appendChild(button);
  46. }
  47. const loginForm = new LoginForm(document.body);
  48. loginForm.createDivider();
  49. const password = new Password(document.body, true);
  50. loginForm.createDivider();
  51. loginForm.addButton();
  52. const isDisabled = () => {
  53. document.querySelector('.login-button').disabled = (password.getValue() !== '' && loginForm.getValue() !== '') ? false : true;
  54. }
  55. password.onChange = () => {
  56. isDisabled();
  57. }
  58. loginForm.onChange = () => {
  59. isDisabled();
  60. }