index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Password */
  2. function Password(parent, open) {
  3. let value = ''
  4. let pass = document.createElement('input')
  5. let check = document.createElement('input')
  6. check.type = 'checkbox'
  7. parent.append(pass, check)
  8. check.onchange = () => {
  9. this.setOpen(check.checked)
  10. }
  11. //добавить pass oninput, который из pass забирает value, запускает setValue
  12. this.getValue = function () {
  13. return value
  14. }
  15. this.setValue = function(value) {
  16. // передает value в onchange если он есть
  17. }
  18. this.getOpen = function () {
  19. return open
  20. }
  21. this.setOpen = function(newOpen) {
  22. open = newOpen
  23. pass.type = open ? 'text' : 'password'
  24. check.checked = open
  25. if(typeof this.onOpenChange === 'function') {
  26. this.onOpenChange(open)
  27. }
  28. }
  29. this.setOpen(open)
  30. }
  31. let p = new Password(document.body, true)
  32. p.onChange = data => console.log(data)
  33. p.onOpenChange = open => console.log(open)
  34. p.setValue('qwerty')
  35. console.log(p.getValue())
  36. p.setOpen(false)
  37. console.log(p.getOpen())
  38. /* LoginForm */
  39. function LoginForm(parent, open) {
  40. let value = ''
  41. let login = document.createElement('input')
  42. let submit = document.createElement('input')
  43. submit.type = 'submit'
  44. submit.disabled = true;
  45. login.placeholder = 'Введите логин'
  46. let pass = document.createElement('input')
  47. pass.placeholder = 'Введите пароль'
  48. let check = document.createElement('input')
  49. check.type = 'checkbox'
  50. parent.append(login, pass, check, submit)
  51. let passAndLogNotEmpty = function () {
  52. return login && login.value && pass && pass.value ? true : false;
  53. };
  54. let setSubmit = function () {
  55. if (passAndLogNotEmpty()) {
  56. submit.disabled = false;
  57. } else {
  58. submit.disabled = true;
  59. }
  60. }
  61. setSubmit();
  62. pass.onkeyup = () => {
  63. setSubmit();
  64. }
  65. login.onkeyup = () => {
  66. setSubmit();
  67. }
  68. check.onchange = () => {
  69. this.setOpen(check.checked)
  70. }
  71. //добавить pass oninput, который из pass забирает value, запускает setValue
  72. this.getValue = function () {
  73. return value
  74. }
  75. this.setValue = function (value) {
  76. // передает value в onchange если он есть
  77. }
  78. this.getOpen = function () {
  79. return open
  80. }
  81. this.setOpen = function (newOpen) {
  82. open = newOpen
  83. pass.type = open ? 'text' : 'password'
  84. check.checked = open
  85. if (typeof this.onOpenChange === 'function') {
  86. this.onOpenChange(open)
  87. }
  88. }
  89. this.setOpen(open)
  90. }
  91. let p = new LoginForm(document.body, true)
  92. p.onChange = data => console.log(data)
  93. p.onOpenChange = open => console.log(open)
  94. p.setValue('qwerty')
  95. console.log(p.getValue())
  96. p.setOpen(false)
  97. console.log(p.getOpen())
  98. /* LoginForm Constructor */
  99. function LoginFormConstructor(parent, open) {
  100. let login = document.createElement('input')
  101. let submit = document.createElement('input')
  102. submit.type = 'submit'
  103. submit.disabled = true;
  104. login.placeholder = 'Введите логин'
  105. let pass = document.createElement('input')
  106. pass.placeholder = 'Введите пароль'
  107. let checkbox = document.createElement('input')
  108. checkbox.type = 'checkbox'
  109. parent.append(login, pass, checkbox, submit)
  110. let form = {
  111. login: login.value,
  112. password: pass.value,
  113. checkbox: checkbox.checked,
  114. submitDisabled: submit.disabled
  115. }
  116. let passAndLoginNotEmpty = function () {
  117. return login && login.value && pass && pass.value ? true : false;
  118. };
  119. let setSubmit = function () {
  120. if (passAndLoginNotEmpty()) {
  121. submit.disabled = false;
  122. } else {
  123. submit.disabled = true;
  124. }
  125. form.submitDisabled = submit.disabled;
  126. if (typeof this.onChange === 'function') {
  127. this.onChange(form);
  128. }
  129. }
  130. setSubmit();
  131. pass.onkeyup = () => {
  132. setSubmit();
  133. form.password = pass.value;
  134. if (typeof this.onChange === 'function') {
  135. this.onChange(pass.value);
  136. }
  137. if (typeof this.onChange === 'function') {
  138. this.onChange(form);
  139. }
  140. }
  141. login.onkeyup = () => {
  142. setSubmit();
  143. form.login = login.value;
  144. if (typeof this.onChange === 'function') {
  145. this.onChange(login.value);
  146. }
  147. if (typeof this.onChange === 'function') {
  148. this.onChange(form);
  149. }
  150. }
  151. checkbox.onchange = () => {
  152. form.checkbox = checkbox.checked;
  153. if (checkbox.checked) {
  154. pass.type = 'text';
  155. } else {
  156. pass.type = 'password';
  157. };
  158. if (typeof this.onChange === 'function') {
  159. this.onChange(checkbox.checked);
  160. }
  161. if (typeof this.onChange === 'function') {
  162. this.onChange(form);
  163. }
  164. }
  165. this.getValue = function () {
  166. return form;
  167. }
  168. this.setValue = function (newLoginValue, newPasswordValue, newCheckboxValue) {
  169. if (typeof newLoginValue === 'string') {
  170. login.value = newLoginValue;
  171. login.onkeyup();
  172. };
  173. if (typeof newPasswordValue === 'string') {
  174. pass.value = newPasswordValue;
  175. pass.onkeyup();
  176. };
  177. if (typeof newCheckboxValue === 'boolean') {
  178. checkbox.disabled = newCheckboxValue;
  179. checkbox.onchange();
  180. };
  181. }
  182. }
  183. let loginForm = new LoginFormConstructor(document.body, true)
  184. loginForm.setValue('AntonLevshin', 'qwerty', false);