main.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // ДЗ: Functional OOP
  2. // Password
  3. function Password(parent, open) {
  4. let pass = document.createElement('input')
  5. pass.placeholder = 'enter password'
  6. let check = document.createElement('input')
  7. check.type = 'checkbox'
  8. parent.append(pass, check)
  9. check.onchange = () => {
  10. this.setOpen(check.checked)
  11. }
  12. pass.oninput = () => {
  13. if (typeof this.onChange === "function") {
  14. this.onChange(pass.value);
  15. }
  16. };
  17. this.setValue = (value) => (pass.value = value);
  18. this.getValue = () => pass.value;
  19. this.setOpen = function (open1) {
  20. open = open1
  21. check.checked = open
  22. pass.type = open ? 'text' : 'password'
  23. if (typeof this.onOpenChange === 'function') {
  24. this.onOpenChange(open)
  25. }
  26. }
  27. this.getOpen = function () {
  28. return open
  29. }
  30. }
  31. let p = new Password(document.body, false)
  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) {
  40. let login = document.createElement('input')
  41. login.placeholder = 'enter login'
  42. let button = document.createElement('button')
  43. button.innerText = 'push'
  44. button.disabled = true
  45. let pass = document.createElement('input')
  46. pass.placeholder = 'enter password'
  47. let check = document.createElement('input')
  48. check.type = 'checkbox'
  49. parent.append(login, pass, check, button)
  50. check.onchange = () => {
  51. this.setOpen(check.checked)
  52. }
  53. pass.oninput = () => {
  54. if (typeof this.onChange === "function") {
  55. this.onChange(pass.value);
  56. }
  57. }
  58. this.setValue = (value) => (pass.value = value);
  59. this.getValue = () => pass.value;
  60. this.setOpen = function (newOpen) {
  61. open = newOpen
  62. check.checked = open
  63. pass.type = open ? 'text' : 'password'
  64. if (typeof this.onOpenChange === 'function') {
  65. this.onOpenChange(open)
  66. }
  67. }
  68. this.getOpen = function () {
  69. return open
  70. }
  71. parent.oninput = function () {
  72. if ((!login.value || !pass.value)) {
  73. button.disabled = true
  74. }
  75. else {
  76. button.disabled = false
  77. }
  78. }
  79. }
  80. let Form = new LoginForm(login)
  81. Form.onChange = data => console.log(data)
  82. Form.onOpenChange = open => console.log(open)
  83. Form.setValue('qwerty')
  84. console.log(Form.getValue())
  85. Form.setOpen(false)
  86. console.log(Form.getOpen())