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