hw_22_02_pass_ES6 .html 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <head>
  2. 02 to ES6 Password
  3. </head>
  4. <body>
  5. <script>
  6. class AuthInput {
  7. #input = document.createElement("input");
  8. onChange;
  9. get input() {
  10. return this.#input;
  11. }
  12. /*set value(val) {
  13. this.#input.value = val;
  14. }*/
  15. get value() {
  16. return this.#input.value;
  17. }
  18. constructor(parent) {
  19. parent.append(this.#input);
  20. this.#input.oninput = this.#input.onchange = function () {
  21. if (this.onChange)
  22. this.onChange(this.value);
  23. }.bind(this);
  24. }
  25. }
  26. class Login extends AuthInput { }
  27. class Password extends AuthInput {
  28. #check = document.createElement("input");
  29. set open(val) {
  30. this.input.type = val ? "text" : "password";
  31. }
  32. get open() {
  33. return this.input.type == "text";
  34. }
  35. constructor(parent, open) {
  36. super(parent);
  37. parent.append(this.#check);
  38. this.#check.onchange = function () {
  39. this.open = this.#check.checked;
  40. if (this.onOpenChange)
  41. this.onOpenChange(this.open);
  42. }.bind(this);
  43. this.#check.checked = open;
  44. this.#check.value = "open";
  45. this.#check.type = "checkbox";
  46. this.open = open;
  47. }
  48. }
  49. class LoginForm {
  50. #form = document.createElement("form");
  51. #login = new Login(this.#form);
  52. #password = new Password(this.#form, open);
  53. #button = document.createElement("button");
  54. onLogin;
  55. constructor(parent, open) {
  56. parent.append(this.#form);
  57. this.#button.innerText = "OK";
  58. this.#button.classList.add('btn');
  59. this.#button.classList.add('ref-button');
  60. this.#form.append(this.#button);
  61. let setButtonStateCallback = function setButtonState() {
  62. this.#button.disabled = !this.#login.value || !this.#password.value;
  63. }.bind(this);
  64. this.#login.onChange = setButtonStateCallback;
  65. this.#password.onChange = setButtonStateCallback;
  66. this.#button.disabled = true;
  67. this.#button.onclick = function () {
  68. if (this.onLogin)
  69. this.onLogin(this.#login.value, this.#password.value);
  70. }.bind(this);
  71. }
  72. }
  73. const form = new LoginForm(document.body, true);
  74. </script>
  75. </body>