loginForm.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. function Password(parent, open){
  2. this.textBox = document.createElement("input");
  3. this.textBox.setAttribute("type", "password");
  4. this.textBox.id = "password";
  5. parent.appendChild(this.textBox);
  6. this.checkBox = document.createElement("input")
  7. this.checkBox.setAttribute("type", "checkbox")
  8. parent.appendChild(this.checkBox)
  9. this.isChecked = function(checkBox, textBox) {
  10. if(open === false){
  11. this.textBox.setAttribute("type", "password")
  12. open = true;
  13. } else {
  14. this.textBox.setAttribute("type", "text")
  15. open = false;
  16. }
  17. }
  18. this.getValue = function(arg = this.textBox) {
  19. return arg.value
  20. }
  21. this.setValue = function(inner) {
  22. this.textBox.value = inner;
  23. }
  24. this.getOpen = function() {
  25. let str;
  26. open == true ? str = "closed" : str = "opened";
  27. return str;
  28. }
  29. this.onChange = function(arg) {
  30. let data = this.getValue(arg);
  31. return data;
  32. }
  33. this.onOpenChange = function(arg) {
  34. console.log(arg)
  35. return arg
  36. }
  37. this.checkBox.onchange = () => {
  38. this.isChecked(this.checkBox, this.textBox)
  39. this.onOpenChange(this.getOpen())
  40. };
  41. this.textBox.oninput = () => this.onChange(this.getValue(this.textBox));
  42. this.setOpen = function(arg) {
  43. if(!arg == true){
  44. this.checkBox.checked = "checked"
  45. } else {
  46. this.checkBox.checked = ""
  47. }
  48. this.isChecked()
  49. }
  50. }
  51. function login(parent) {
  52. this.loginBox = document.createElement("input");
  53. this.loginBox.setAttribute("type", "text");
  54. this.loginBox.setAttribute("value", "login")
  55. this.loginBox.id = "login";
  56. parent.appendChild(this.loginBox);
  57. }
  58. function send(parent) {
  59. this.sendBottom = document.createElement("button");
  60. this.sendBottom.setAttribute("disabled", true);
  61. this.sendBottom.innerText = "send";
  62. this.sendBottom.id = "send";
  63. parent.appendChild(this.sendBottom)
  64. }
  65. function loginForm(parent) {
  66. this.login = new login(parent);
  67. this.login.loginBox.oninput = () => this.sendOnChange();
  68. this.password = new Password(parent);
  69. this.password.textBox.oninput = () => { this.sendOnChange()
  70. }
  71. this.sendBottom = new send(parent)
  72. this.sendOnChange = function() {
  73. if(this.login.loginBox.value != "" && this.password.textBox.value != "") {
  74. return this.sendBottom.sendBottom.removeAttribute("disabled");
  75. } return this.sendBottom.sendBottom.setAttribute("disabled", true);
  76. }
  77. this.setValue = function(inner, box = this.password.textBox) {
  78. box.value = inner;
  79. }
  80. this.setValue("qwerty")
  81. this.sendOnChange()
  82. }
  83. // let j = new loginForm(document.body)
  84. // j.setValue("Вооот")
  85. function passwordVerify(parent) {
  86. this.login = new login(parent);
  87. this.login.loginBox.oninput = () => this.sendOnChange();
  88. this.checkPasswordBox = document.createElement("span");
  89. parent.appendChild(this.checkPasswordBox);
  90. this.password = new Password(this.checkPasswordBox);
  91. this.password.checkBox.onchange = () => {
  92. if(this.password.checkBox.checked) {
  93. this.checkPassword.remove()
  94. } else {
  95. this.checkPassword = document.createElement("input");
  96. this.checkPassword.setAttribute("type", "password");
  97. this.checkPasswordBox.appendChild(this.checkPassword);
  98. this.checkPassword.oninput = () => this.sendOnChange();
  99. }
  100. this.sendOnChange()
  101. }
  102. this.password.textBox.oninput = () => this.sendOnChange();
  103. this.checkPassword = document.createElement("input");
  104. this.checkPassword.setAttribute("type", "password");
  105. this.checkPassword.id = "checkPassword";
  106. this.checkPasswordBox.appendChild(this.checkPassword);
  107. this.checkPassword.oninput = () => this.sendOnChange();
  108. this.sendBottom = new send(parent);
  109. this.setValue = function(inner, box = this.password.textBox) {
  110. box.value = inner;
  111. }
  112. this.setValue("qwerty")
  113. this.sendOnChange = function() {
  114. if(this.password.checkBox.checked &&
  115. this.login.loginBox.value != "" &&
  116. this.password.textBox.value != "") {
  117. return this.sendBottom.sendBottom.removeAttribute("disabled");
  118. }
  119. else {
  120. if(this.login.loginBox.value != "" &&
  121. this.password.textBox.value != "" &&
  122. this.password.textBox.value === this.checkPassword.value) {
  123. return this.sendBottom.sendBottom.removeAttribute("disabled");
  124. }
  125. }
  126. return this.sendBottom.sendBottom.setAttribute("disabled", true);
  127. }
  128. }
  129. let loginFormDiv = document.getElementById("loginForm");
  130. let thisWriteLog = loginForm(loginFormDiv);
  131. sendClick = document.getElementById("send");
  132. let log = document.getElementById("login");
  133. let password = document.getElementById("password");
  134. sendClick.onclick = () => {
  135. console.log(`login: ${log.value} \n password: ${password.value}`)
  136. getGQL(gqlUrl)(`
  137. query login($login:String, $password:String) {
  138. login(login:$login, password:$password)
  139. }`,{login: log.value, password: password.value}).then(res => {
  140. localStorage.authtoken = res.data.login;
  141. if(localStorage.authtoken) {
  142. loginFormDiv.innerHTML = ""
  143. let sayHi = document.createElement("h2");
  144. sayHi.setAttribute("style", "margin-bottom: 0px")
  145. sayHi.innerText = `Hi, user!`;
  146. loginFormDiv.appendChild(sayHi);
  147. }
  148. });
  149. }