main.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 (open) {
  20. check.checked = open
  21. pass.type = open ? 'text' : 'password'
  22. if (typeof this.onOpenChange === 'function') {
  23. this.onOpenChange(open)
  24. }
  25. }
  26. this.getOpen = function () {
  27. return open
  28. }
  29. }
  30. let p = new Password(document.body, false)
  31. p.onChange = data => console.log(data)
  32. p.onOpenChange = open => console.log(open)
  33. p.setValue('qwerty')
  34. console.log(p.getValue())
  35. p.setOpen(false)
  36. console.log(p.getOpen())
  37. // LoginForm
  38. function LoginForm(parent) {
  39. let login = document.createElement('input')
  40. login.placeholder = 'enter login'
  41. let button = document.createElement('button')
  42. button.innerText = 'push'
  43. button.disabled = true
  44. let pass = document.createElement('input')
  45. pass.placeholder = 'enter password'
  46. let check = document.createElement('input')
  47. check.type = 'checkbox'
  48. parent.append(login, pass, check, button)
  49. check.onchange = () => {
  50. this.setOpen(check.checked)
  51. }
  52. pass.oninput = () => {
  53. if (typeof this.onChange === "function") {
  54. this.onChange(pass.value);
  55. }
  56. }
  57. this.setValue = (value) => (pass.value = value);
  58. this.getValue = () => pass.value;
  59. this.setOpen = function (open) {
  60. check.checked = open
  61. pass.type = open ? 'text' : 'password'
  62. if (typeof this.onOpenChange === 'function') {
  63. this.onOpenChange(open)
  64. }
  65. }
  66. this.getOpen = function () {
  67. return open
  68. }
  69. parent.oninput = function () {
  70. if ((!login.value || !pass.value)) {
  71. button.disabled = true
  72. }
  73. else {
  74. button.disabled = false
  75. }
  76. }
  77. }
  78. let form = new LoginForm(document.body)
  79. form.onChange = data => console.log(data)
  80. form.onOpenChange = open => console.log(open)
  81. form.setValue('qwerty')
  82. console.log(form.getValue())
  83. form.setOpen(false)
  84. console.log(form.getOpen())