login.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. function LoginForm(parent, open) {
  2. function Login(parent) {
  3. this.input = document.createElement("input");
  4. parent.append(this.input);
  5. this.setValue = function (value) {
  6. this.input.value = value;
  7. }
  8. this.getValue = function () {
  9. return this.input.value;
  10. }
  11. this.input.oninput = this.input.onchange = function () {
  12. if (this.onChange)
  13. this.onChange(this.getValue());
  14. }.bind(this);
  15. }
  16. function Password(parent, open) {
  17. this.input = document.createElement("input");
  18. parent.append(this.input);
  19. this.setValue = function (value) {
  20. this.input.value = value;
  21. }
  22. this.getValue = function () {
  23. return this.input.value;
  24. }
  25. this.input.oninput = this.input.onchange = function () {
  26. if (this.onChange)
  27. this.onChange(this.getValue());
  28. }.bind(this);
  29. this.setOpen = function (open) {
  30. this.input.type = open ? "text" : "password";
  31. }
  32. this.getOpen = function () {
  33. return this.input.type == "text";
  34. }
  35. let check = document.createElement("input");
  36. parent.append(check);
  37. check.onchange = function () {
  38. this.setOpen(check.checked);
  39. if (this.onOpenChange)
  40. this.onOpenChange(this.getOpen());
  41. }.bind(this);
  42. check.checked = open;
  43. check.value = "open";
  44. check.type = "checkbox";
  45. this.setOpen(open);
  46. }
  47. let form = document.createElement("form");
  48. parent.append(form);
  49. this.login = new Login(form);
  50. this.password = new Password(form, open);
  51. this.button = document.createElement("button");
  52. this.button.innerText = "OK";
  53. this.button.classList.add('btn');
  54. this.button.classList.add('ref-button');
  55. form.append(this.button);
  56. let setButtonStateCallback = function setButtonState() {
  57. this.button.disabled = !this.login.getValue() || !this.password.getValue();
  58. }.bind(this);
  59. this.login.onChange = setButtonStateCallback;
  60. this.password.onChange = setButtonStateCallback;
  61. this.button.disabled = true;
  62. this.button.onclick = function () {
  63. if (this.onLogin)
  64. this.onLogin(this.login.getValue(), this.password.getValue());
  65. }.bind(this);
  66. }
  67. function loginPromise(parent) {
  68. function executor(fulfill, reject) {
  69. const form = new LoginForm(parent);
  70. form.onLogin = (login, password) => {
  71. fulfill({ 'login': login, 'password': password })
  72. }
  73. }
  74. return new Promise(executor);
  75. }
  76. /*loginPromise(document.body)
  77. .then(({ login, password }) => console.log(`Вы ввели ${login} и ${password}`));*/