main.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Password
  2. function Password(parent, open) {
  3. let input = document.createElement('input')
  4. input.style.marginTop = '30px'
  5. parent.append(input)
  6. let checkbox = document.createElement('input')
  7. checkbox.setAttribute('type', 'checkbox')
  8. parent.append(checkbox)
  9. let content = document.createElement('span')
  10. content.append('See the password')
  11. content.style.color = 'blue'
  12. parent.append(content)
  13. this.setValue = (text) => input.value = text
  14. this.getValue = () => input.value
  15. this.getOpen = () => input.type
  16. checkbox.onclick = () => this.onOpenChange(input.type)
  17. checkbox.oninput = () => this.onChange(input.type)
  18. this.setOpen = function(open) {
  19. if(open === false) {
  20. input.type = 'password'
  21. checkbox.checked = false
  22. checkbox.onchange = function() {
  23. if (input.type === 'password') {
  24. input.type = 'text'
  25. console.log('Current input type is: ' + p.getOpen())
  26. }
  27. else {
  28. input.type = 'password'
  29. console.log('Current input type is: ' + p.getOpen())
  30. }
  31. }
  32. } else {
  33. if (open === true) {
  34. input.type = 'text'
  35. checkbox.checked = true
  36. checkbox.onchange = function() {
  37. if (input.type === 'text') {
  38. input.type = 'password'
  39. }
  40. else {
  41. input.type = 'text'
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }
  48. let p = new Password(document.body, true)
  49. p.onChange = () => {console.log('Input type is successfully changed, exact time of change is: ' + new Date())}
  50. p.onOpenChange = type => confirm('Current type : ' + type + '\n' + 'Cancel - leave type unchanged' + '\n' + 'OK - change type')
  51. p.setValue('qwerty')
  52. console.log('Current password is: ' + p.getValue())
  53. p.setOpen(false)
  54. console.log('Current type of input is: ' + p.getOpen())
  55. // p.setOpen(true)
  56. // console.log(p.getOpen()) // for true
  57. // // // or
  58. let showPassword = document.querySelectorAll('.show-password')
  59. showPassword.forEach(item => item.addEventListener('click', toogleType))
  60. function toogleType() {
  61. let input = this.closest('.thepassword').querySelector('.password')
  62. if(input.type === 'password') {
  63. input.type = 'text'
  64. } else {
  65. input.type = 'password'
  66. }
  67. } // decided to leave that way too - input hide/show password without 'qwerty' by default
  68. // // // LoginForm
  69. // // LoginForm Constructor
  70. function Loggin() {
  71. let loginForm = document.createElement('input')
  72. loginForm.setAttribute('placeholder', 'Login')
  73. loginForm.style.marginLeft = '10px'
  74. loginForm.style.marginRight = '4px'
  75. document.body.appendChild(loginForm)
  76. let passwordForm = document.createElement('input')
  77. passwordForm.setAttribute('placeholder', 'Password')
  78. document.body.appendChild(passwordForm)
  79. let button = document.createElement('button')
  80. button.style.marginLeft = '4px'
  81. button.textContent = 'Button'
  82. document.body.appendChild(button)
  83. this.setInfo = () => [loginForm.value, passwordForm.value]
  84. this.getLog = (log) => loginForm.value = log
  85. this.getPass = (pass) => passwordForm.value = pass
  86. this.setForm = function() {
  87. button.disabled = true
  88. loginForm.oninput = passwordForm.oninput = () => {
  89. if(loginForm.value.length > 0 & passwordForm.value.length > 0){
  90. button.disabled = false
  91. }
  92. else {
  93. button.disabled = true
  94. }
  95. }
  96. }
  97. }
  98. let l = new Loggin()
  99. l.setForm()
  100. l.getLog('') // any login
  101. l.getPass('') // any password
  102. console.log(l.setInfo()) // array of log and pass
  103. // for example
  104. // l.getLog('qwerty')
  105. // l.getPass('123')
  106. // console.log(l.setInfo()) // Array (2) ["qwerty", "123"]
  107. // // // // or
  108. function checkform() {
  109. let f = document.forms['theform'].elements
  110. let cansubmit = true;
  111. for (let i = 0; i < f.length; i++) {
  112. if (f[i].value.length == 0) {
  113. cansubmit = false
  114. }
  115. }
  116. if (cansubmit) {
  117. document.getElementById('submitbutton').disabled = false
  118. }
  119. }
  120. // Password Verify
  121. // function Password(parent, open) {
  122. // let input1 = document.createElement('input')
  123. // parent.append(input1)
  124. // let checkbox = document.createElement('input')
  125. // checkbox.setAttribute('type', 'checkbox')
  126. // parent.append(checkbox)
  127. // let content = document.createElement('span')
  128. // content.append('See the password')
  129. // content.style.marginRight = '5px'
  130. // content.style.color = 'blue'
  131. // parent.append(content)
  132. // let input2 = document.createElement('input')
  133. // parent.appendChild(input2)
  134. // let btn = document.createElement('button')
  135. // btn.textContent = 'Button'
  136. // parent.appendChild(btn)
  137. // this.setValue = (text) => input1.value = text
  138. // this.getValue = () => input1.value
  139. // this.getOpen = () => input1.type
  140. // checkbox.onclick = () => this.onOpenChange(input1.type)
  141. // checkbox.oninput = () => this.onChange(input1.type)
  142. // this.setOpen = function(open) {
  143. // if(open === false) {
  144. // input1.type = 'password'
  145. // checkbox.checked = false
  146. // checkbox.onchange = function() {
  147. // if (input1.type === 'password') {
  148. // input1.type = 'text'
  149. // console.log('Current input type is: ' + p.getOpen())
  150. // }
  151. // else {
  152. // input1.type = 'password'
  153. // console.log('Current input type is: ' + p.getOpen())
  154. // }
  155. // if(input1.type === 'text') {
  156. // input2.hidden = true
  157. // }
  158. // else if (input1.type === 'password') {
  159. // input2.hidden = false
  160. // }
  161. // }
  162. // }
  163. // else {
  164. // if (open === true) {
  165. // input1.type = 'text'
  166. // checkbox.checked = true
  167. // checkbox.onchange = function() {
  168. // if (input1.type === 'password') {
  169. // input1.type = 'text'
  170. // }
  171. // else {
  172. // input1.type = 'password'
  173. // }
  174. // }
  175. // }
  176. // }
  177. // }
  178. // this.passwordVerify = function() {
  179. // btn.disabled = true
  180. // input1.oninput = input2.oninput = () => {
  181. // if(input1.value === input2.value) {
  182. // btn.disabled = false
  183. // }
  184. // else {
  185. // btn.disabled = true
  186. // }
  187. // }
  188. // }
  189. // }
  190. // let p = new Password(document.body, true)
  191. // p.onChange = () => {console.log('Input type is successfully changed, exact time of change is: ' + new Date())}
  192. // p.onOpenChange = type => confirm('Current type : ' + type + '\n' + 'Cancel - leave type unchanged' + '\n' + 'OK - change type and hide the second input')
  193. // p.setValue('qwerty')
  194. // console.log('Current password is: ' + p.getValue())
  195. // p.setOpen(false)
  196. // console.log('Current type of input is: ' + p.getOpen())
  197. // p.passwordVerify()
  198. // // p.setOpen(true)
  199. // // console.log(p.getOpen()) // for true