hw10.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id='container1'></div>
  11. <div id='formContainer'></div>
  12. </body>
  13. <script>
  14. //---------------------------------------Password
  15. function Password(parent, open) {
  16. let passField = document.createElement("input");
  17. passField.placeholder = "введите пароль";
  18. let checkBox = document.createElement("input");
  19. checkBox.setAttribute("type", "checkbox");
  20. parent.append(passField, checkBox);
  21. this.setValue = (newValue) => {
  22. passField.value = newValue;
  23. };
  24. this.getValue = () => passField.value;
  25. this.setOpen = (newOpen) => {
  26. open = newOpen;
  27. if (open === true) {
  28. checkBox.setAttribute("checked", "true");
  29. passField.setAttribute("type", "text");
  30. } else if (open === false) {
  31. checkBox.setAttribute("checked", "false");
  32. passField.setAttribute("type", "password");
  33. }
  34. };
  35. this.setOpen(open);
  36. this.getOpen = () => checkBox.checked;
  37. passField.oninput = () => {
  38. if (typeof this.onChange === "function") {
  39. this.onChange(this.getValue());
  40. }
  41. };
  42. checkBox.oninput = () => {
  43. if (this.getOpen() === true) {
  44. this.setOpen(true);
  45. } else {
  46. this.setOpen(false);
  47. }
  48. if (typeof this.onOpenChange === "function") {
  49. this.onOpenChange(this.getOpen());
  50. }
  51. };
  52. }
  53. // let p = new Password(document.body, true);
  54. // p.onchange = (data) => console.log(data);
  55. // p.onopenchange = (open) => console.log(open);
  56. // p.setValue("qwerty");
  57. // console.log(p.getValue());
  58. // p.setOpen(false);
  59. // console.log(p.getOpen());
  60. // //---------------------------------------LoginForm
  61. // let passwordInput = new Password(document.body, true)
  62. // let loginInput = document.createElement('input')
  63. // let loginButton = document.createElement('input')
  64. // loginInput.placeholder = 'введите логин'
  65. // loginButton.type = 'button'
  66. // loginButton.value = 'Войти'
  67. // loginButton.disabled = true
  68. // loginInput.setAttribute('oninput','f()')
  69. // formContainer.append(loginInput,loginButton)
  70. // passwordInput.onChange = data => console.log(data)
  71. // function f(){
  72. // if ((loginInput.value === '' ) || (passwordInput.getValue() === '')) {
  73. // loginButton.disabled=true
  74. // } else
  75. // loginButton.disabled=false
  76. // }
  77. // passwordInput.onChange = f
  78. // //---------------------------------------LoginForm constructor
  79. // function Login(parent) {
  80. // this.parent = parent;
  81. // let login = document.createElement('input')
  82. // login.placeholder = 'введите логин'
  83. // let button = document.createElement('button')
  84. // button.innerText = 'Войти'
  85. // button.disabled = true
  86. // let password = new Password(this.parent, true);
  87. // parent.append(login,button)
  88. // this.setActive = (value) => {
  89. // if (value === true) {
  90. // button.disabled = true;
  91. // } else {
  92. // button.disabled = false;
  93. // }
  94. // }
  95. // this.getPassword = () => {
  96. // return this.passwordInput
  97. // }
  98. // password.onChange = (data) => {
  99. // if(login.value === '' || data === '' ) {
  100. // this.setActive(true)
  101. // } else {
  102. // this.setActive(false)
  103. // }
  104. // }
  105. // login.oninput = () => {
  106. // login.innerHTML = login.value;
  107. // console.log(login.value)
  108. // if(login.value === '') {
  109. // this.setActive(true)
  110. // } else {
  111. // this.setActive(false)
  112. // }
  113. // }
  114. // }
  115. // let login = new Login(formContainer)
  116. //---------------------------------------Password verify
  117. let p = new Password(formContainer, true)
  118. let p2 = document.createElement('input')
  119. p2.type = 'password'
  120. p2.style.display = 'none'
  121. p2.placeholder = 'повторите пароль'
  122. p2.setAttribute('oninput','f()')
  123. let button = document.createElement('button')
  124. button.innerText = 'Войти'
  125. button.disabled = true
  126. p.onChange = data => console.log(data)
  127. formContainer.append(p2,button)
  128. function f () {
  129. if (p.getOpen() === true) {
  130. p2.style.display = 'none'
  131. if (p.getValue() != '') {
  132. button.disabled = false
  133. } else {button.disabled = true}
  134. } else {
  135. if (p.getValue() != '' && (p.getValue() === p2.value)) {
  136. button.disabled = false
  137. } else {button.disabled = true}
  138. p2.style.display = ''}
  139. }
  140. p.onOpenChange = f
  141. p.onChange = f
  142. console.log(p.getValue())
  143. </script>
  144. </body>
  145. </html>