script.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. function Password(parent, open){
  2. this.textBox = document.createElement("input")
  3. this.textBox.setAttribute("type", "password")
  4. parent.appendChild(this.textBox)
  5. this.checkBox = document.createElement("input")
  6. this.checkBox.setAttribute("type", "checkbox")
  7. parent.appendChild(this.checkBox)
  8. this.isChecked = function(checkBox, textBox) {
  9. if(open === false){
  10. this.textBox.setAttribute("type", "password")
  11. open = true;
  12. } else {
  13. this.textBox.setAttribute("type", "text")
  14. open = false;
  15. }
  16. }
  17. this.getValue = function(arg = this.textBox) {
  18. return arg.value
  19. }
  20. this.setValue = function(inner) {
  21. this.textBox.value = inner;
  22. }
  23. this.getOpen = function() {
  24. let str;
  25. open == true ? str = "closed" : str = "opened";
  26. return str;
  27. }
  28. this.onChange = function(arg) {
  29. let data = this.getValue(arg);
  30. return data;
  31. }
  32. this.checkBox.onchange = () => {
  33. this.isChecked(this.checkBox, this.textBox)
  34. this.onOpenChange(this.getOpen())
  35. };
  36. this.textBox.oninput = () => this.onChange(this.getValue(this.textBox));
  37. this.setOpen = function(arg) {
  38. if(!arg == true){
  39. this.checkBox.checked = "checked"
  40. } else {
  41. this.checkBox.checked = ""
  42. }
  43. this.isChecked()
  44. }
  45. }
  46. let p = new Password(document.body, true)
  47. p.onChange = data => console.log(data)
  48. p.onOpenChange = open => console.log(open)
  49. p.setValue('qwerty')
  50. console.log(p.getValue())
  51. p.setOpen(false)
  52. console.log(p.getOpen())
  53. // Напишите функцию конструктор Password, которая будет в родительском
  54. //элементе создавать поле ввода для пароля и кнопку/иконку/чекбокс,
  55. //который будет переключать режим просмотра пароля в поле ввода.
  56. // Параметры:
  57. // parent - родительский элемент
  58. // open - стартовое состояние
  59. // Методы:
  60. // setValue/getValue - задают/читают значения
  61. // setOpen/getOpen - задают/читают открытость текста в поле ввода
  62. // Колбэки (функции обратного вызова, данные изнутри объекта):
  63. // onChange - запускается по событию oninput в поле ввода, передает текст наружу
  64. // onOpenChange - запускается по изменению состояния открытости пароля