hw14_06_login_form_constructor.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <header>login_form_constructor</header>
  2. <body>
  3. <script>
  4. function Login(parent) {
  5. this.input = document.createElement("input");
  6. parent.append(this.input);
  7. this.setValue = function (value) {
  8. this.input.value = value;
  9. }
  10. this.getValue = function () {
  11. return this.input.value;
  12. }
  13. this.input.onchange = function () {
  14. if (this.onChange)
  15. this.onChange(this.getValue());
  16. }.bind(this);
  17. }
  18. function Password(parent, open) {
  19. this.input = document.createElement("input");
  20. parent.append(this.input);
  21. this.setValue = function (value) {
  22. this.input.value = value;
  23. }
  24. this.getValue = function () {
  25. return this.input.value;
  26. }
  27. this.input.onchange = function () {
  28. if (this.onChange)
  29. this.onChange(this.getValue());
  30. }.bind(this);
  31. this.setOpen = function (open) {
  32. this.input.type = open ? "text" : "password";
  33. }
  34. this.getOpen = function () {
  35. return this.input.type == "text";
  36. }
  37. let check = document.createElement("input");
  38. parent.append(check);
  39. check.onchange = function () {
  40. this.setOpen(check.checked);
  41. if (this.onOpenChange)
  42. this.onOpenChange(this.getOpen());
  43. }.bind(this);
  44. check.checked = open;
  45. check.value = "open";
  46. check.type = "checkbox";
  47. }
  48. function LoginForm(parent, open) {
  49. let form = document.createElement("form");
  50. parent.append(form);
  51. this.login = new Login(form);
  52. this.password = new Password(form, open);
  53. this.button = document.createElement("button");
  54. this.button.innerText = "OK";
  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. let loginForm = new LoginForm(document.body, true);
  68. loginForm.onLogin = function (login, password) {
  69. console.log(`${login}:${password}`)
  70. }
  71. </script>
  72. </body>