loginFormConstructor.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. if(typeof this.onOpenChange === 'function') {
  17. this.onOpenChange(openUpdate);
  18. }
  19. button.textContent = (openUpdate) ? 'показать' : 'скрыть';
  20. input.type = (openUpdate) ? 'password' : 'text';
  21. }
  22. this.getOpen = () => open;
  23. input.addEventListener('input', event => {
  24. if (typeof this.onChange === 'function'){
  25. this.onChange(event.target.value);
  26. }
  27. });
  28. }
  29. function LoginForm (parent) {
  30. this.createDivider = () => document.body.appendChild(document.createElement('br'));
  31. const input = document.createElement('input');
  32. input.type = 'text';
  33. parent.appendChild(input);
  34. const button = document.createElement('button');
  35. button.type = 'button';
  36. button.textContent = 'Логин';
  37. button.disabled = true;
  38. input.addEventListener('input', event => {
  39. if (typeof this.onChange === 'function'){
  40. this.onChange(event.target.value);
  41. }
  42. });
  43. button.addEventListener('click', event => {
  44. if (typeof this.onLogin === 'function') {
  45. this.onLogin(event);
  46. }
  47. });
  48. this.getLogin = () => input.value;
  49. this.createDivider();
  50. const password = new Password(document.body, true);
  51. const getPassword = () => password.getValue();
  52. this.createDivider();
  53. parent.appendChild(button);
  54. const isDisabled = () => button.disabled = (!(getPassword() !== '' && this.getLogin() !== ''));
  55. password.onChange = () => isDisabled();
  56. this.onChange = () => isDisabled();
  57. }
  58. const loginForm = new LoginForm(document.body);
  59. loginForm.onLogin = data => console.log(data);