main.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //Password
  2. function Password(parent, open) {
  3. let input = document.createElement("input");
  4. let checkbox = document.createElement("input");
  5. checkbox.type = "checkbox";
  6. let checker = (check) => {
  7. if (check) {
  8. input.type = "text";
  9. checkbox.checked = true;
  10. } else {
  11. input.type = "password";
  12. checkbox.checked = false;
  13. }
  14. }
  15. checker(open);
  16. input.oninput = () => {
  17. if (typeof this.onChange === "function") {
  18. this.onChange(input.value);
  19. }
  20. }
  21. checkbox.onchange = () => {
  22. checker(checkbox.checked);
  23. if (typeof this.onOpenChange === "function") {
  24. this.onOpenChange(checkbox.checked);
  25. }
  26. }
  27. this.getValue = () => input.value;
  28. this.setValue = value => input.value = value;
  29. this.getOpen = () => checkbox.checked;
  30. this.setOpen = (open) => checker(open);
  31. parent.append(input)
  32. parent.append(checkbox)
  33. }
  34. let p = new Password(task1, true)
  35. p.onChange = data => console.log(data)
  36. p.onOpenChange = open => console.log(open)
  37. p.setValue('qwerty')
  38. console.log(p.getValue())
  39. p.setOpen(false)
  40. console.log(p.getOpen())
  41. //LoginForm
  42. let lForm = document.createElement("input");
  43. task2.append(lForm);
  44. let pForm = new Password(task2, true);
  45. let bForm = document.createElement("input");
  46. bForm.type = "button"
  47. bForm.value = "Ввойти";
  48. bForm.disabled = true;
  49. task2.append(bForm);
  50. let btnOpen = () => (lForm.value != "" && pForm.getValue() != "") ? bForm.disabled = false : bForm.disabled = true;
  51. lForm.oninput = () => btnOpen();
  52. pForm.onChange = () => btnOpen();
  53. //LoginForm Constructor
  54. function LoginForm(parent, open) {
  55. let h3 = document.createElement("h3");
  56. let loginInput = document.createElement("input");
  57. let passwordInput = document.createElement("input");
  58. let checkbox = document.createElement("input");
  59. let btn = document.createElement("input");
  60. let form = document.createElement("div");
  61. h3.innerText = "Login Form"
  62. checkbox.type = "checkbox";
  63. btn.type = "button";
  64. btn.value = "Войти";
  65. btn.disabled = true;
  66. let btnOpen = () => (loginInput.value != "" && passwordInput.value != "") ? btn.disabled = false : btn.disabled = true;
  67. let checker = (check) => {
  68. if (check) {
  69. passwordInput.type = "text";
  70. checkbox.checked = true;
  71. } else {
  72. passwordInput.type = "password";
  73. checkbox.checked = false;
  74. }
  75. }
  76. checker(open);
  77. loginInput.oninput = () => {
  78. btnOpen();
  79. if (typeof this.onChange === "function") {
  80. this.onChange(loginInput.value, passwordInput.value);
  81. }
  82. }
  83. passwordInput.oninput = () => {
  84. btnOpen();
  85. if (typeof this.onChange === "function") {
  86. this.onChange(loginInput.value, passwordInput.value);
  87. }
  88. }
  89. checkbox.onchange = () => {
  90. checker(checkbox.checked)
  91. if (typeof this.onOpenChange === "function") {
  92. this.onOpenChange(checkbox.checked);
  93. }
  94. }
  95. this.getValue = () => [loginInput.value, passwordInput.value];
  96. this.setValue = (valueLogin, valuePassword) => {
  97. loginInput.value = valueLogin;
  98. passwordInput.value = valuePassword;
  99. btnOpen();
  100. }
  101. this.getOpen = () => checkbox.checked;
  102. this.setOpen = (open) => {
  103. checker(open);
  104. }
  105. form.append(h3)
  106. form.append(loginInput)
  107. form.append(passwordInput)
  108. form.append(checkbox)
  109. form.append(btn)
  110. form.style.display = "flex";
  111. form.style.flexDirection = "column";
  112. form.style.alignItems = "flex-start";
  113. form.style.justifyContent = "space-around";
  114. form.style.width = "20%";
  115. form.style.margin = "10px";
  116. form.style.padding = "5px";
  117. form.style.height = "200px";
  118. form.style.border = "1px solid black"
  119. parent.append(form)
  120. }
  121. let f = new LoginForm(task3, true)
  122. f.onChange = (dataLogin, dataPassword) => console.log(dataLogin + ": " + dataPassword)
  123. f.onOpenChange = open => console.log(open)
  124. f.setValue('qwerty', 12345)
  125. console.log(f.getValue())
  126. f.setOpen(false)
  127. console.log(f.getOpen())
  128. //Password Verify
  129. let passwordVerify = new Password(task4, true);
  130. passwordVerify.onOpenChange = open => {
  131. if (!open) {
  132. let passwordVerify2 = new Password(task4, false);
  133. task4.getElementsByTagName("input")[4].remove();
  134. passwordVerify.onChange = data => {
  135. if (data !== "" && data === passwordVerify2.getValue()) {
  136. btnVerify.disabled = false;
  137. } else {
  138. btnVerify.disabled = true;
  139. }
  140. }
  141. passwordVerify2.onChange = data => {
  142. if (data !== "" && passwordVerify.getValue() === data) {
  143. btnVerify.disabled = false;
  144. } else {
  145. btnVerify.disabled = true;
  146. }
  147. }
  148. } else {
  149. task4.getElementsByTagName("input")[3].remove();
  150. passwordVerify.onChange = data => {
  151. if (data !== "") {
  152. btnVerify.disabled = false;
  153. } else {
  154. btnVerify.disabled = true;
  155. }
  156. }
  157. }
  158. }
  159. let btnVerify = document.createElement("input");
  160. btnVerify.type = "button";
  161. btnVerify.value = "OK";
  162. btnVerify.disabled = true;
  163. task4.append(btnVerify);
  164. task4.style.display = "flex";
  165. btnVerify.style.order = "1";