script.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //password
  2. function Password(parent, open=false) {
  3. let _isVisible = open
  4. let _value = ''
  5. let input = document.createElement('input')
  6. input.placeholder = 'password'
  7. let wrapper = document.createElement('div')
  8. let btn = document.createElement('button')
  9. btn.innerText = 'Hide/Show'
  10. this.getHTMLElement = function() {
  11. return input
  12. }
  13. this.getValue = function() {
  14. return _value
  15. }
  16. this.setValue = function(newValue) {
  17. _value = newValue
  18. input.value = _value
  19. }
  20. this.setOpen = function(bool) {
  21. _isVisible = bool
  22. let _type = _isVisible? 'text' : 'password'
  23. input.setAttribute('type', `${_type}`)
  24. }
  25. this.getOpen = function() {
  26. return _isVisible
  27. }
  28. this.onChange = function(event) {
  29. _value = event.target.value
  30. }
  31. this.onOpenChange = function(openSetter) {
  32. openSetter(!_isVisible)
  33. }
  34. input.addEventListener('input', this.onChange)
  35. btn.addEventListener('click', () => {
  36. this.onOpenChange(this.setOpen)
  37. })
  38. wrapper.setAttribute('class', 'Password')
  39. this.setOpen(open)
  40. wrapper.append(input, btn)
  41. parent.append(wrapper)
  42. }
  43. let pass = new Password(document.body, true)
  44. //check if Password constructor works
  45. pass.setValue('hello world')
  46. pass.setOpen(false)
  47. console.log(pass.getOpen())
  48. console.log(pass.getValue())
  49. window.setTimeout(()=> {
  50. console.log('old value:', pass.getValue())
  51. pass.setValue('YOU CAN SEE ME!')
  52. pass.setOpen(true)
  53. }, 5000)
  54. //_login form
  55. function Login (parent) {
  56. let _value = ''
  57. let input = document.createElement('input')
  58. input.type = 'text'
  59. input.placeholder = 'login'
  60. input.style.display = 'block'
  61. parent.append(input)
  62. this.getHTMLElement = function() {
  63. return input
  64. }
  65. this.getValue = function() {
  66. return _value
  67. }
  68. this.setValue = function(newValue) {
  69. _value = newValue
  70. input.value = _value
  71. }
  72. this.onChange = function(event) {
  73. _value = event.target.value
  74. }
  75. input.addEventListener('input', this.onChange)
  76. }
  77. //loginForm Constructor
  78. function loginForm(parent, heading='loginForm Constructor') {
  79. let form = document.createElement('div')
  80. form.id = 'loginForm'
  81. form.style.border = '5px double black'
  82. form.style.width = 'fit-content'
  83. form.style.textAlign = 'center'
  84. form.style.padding = '15px'
  85. form.style.margin = '15px'
  86. let h2 = document.createElement('h2')
  87. h2.innerText = heading
  88. form.append(heading)
  89. let _login = new Login(form)
  90. let _password = new Password(form)
  91. let login_btn = document.createElement('button')
  92. login_btn.innerText = 'Log in'
  93. login_btn.disabled = true
  94. form.append(login_btn)
  95. for(let item of form.children) {
  96. item.style.margin = '10px'
  97. }
  98. this.getLogin = function() {
  99. return _login.getValue()
  100. }
  101. this.getPassword = function() {
  102. return _password.getValue()
  103. }
  104. this.isPasswordOpened = function() {
  105. return _password.getOpen()
  106. }
  107. this.setPasswordOpened = function(bool) {
  108. _password.setOpen(bool)
  109. }
  110. this.setLogin = function(newLogin) {
  111. _login.setValue(newLogin)
  112. checkFields()
  113. }
  114. this.setPass = function(newPass) {
  115. _password.setValue(newPass)
  116. checkFields()
  117. }
  118. function checkFields() {
  119. if (_login.getValue() && _password.getValue()) {
  120. login_btn.disabled = false
  121. } else {
  122. login_btn.disabled = true
  123. }
  124. }
  125. _login.getHTMLElement().addEventListener('input', checkFields)
  126. _password.getHTMLElement().addEventListener('input', checkFields)
  127. parent.append(form)
  128. }
  129. let form = new loginForm(document.body)
  130. form.setLogin('default_user')
  131. form.setPass('pass example', true)
  132. window.setTimeout(()=> {
  133. console.log(form.getPassword())
  134. console.log('before open', form.isPasswordOpened())
  135. form.setPasswordOpened(true)
  136. console.log('after open', form.isPasswordOpened())
  137. console.log(form.getLogin())
  138. }, 5000)