index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. function Password(parent, open) {
  2. this.parent = parent;
  3. this.html = `
  4. <div>
  5. <input type="password" id="password" name="password" />
  6. <input type="checkbox" id="togglePassword" />
  7. <label id='button' for="toggle-password"></label>
  8. </div>`;
  9. this.parent.insertAdjacentHTML('afterbegin', this.html);
  10. const password = document.getElementById("password");
  11. this.getHTML = () => {
  12. return this.html;
  13. }
  14. this.value;
  15. this.onChange;
  16. this.open = open;
  17. this.onOpenChange;
  18. this.setParent = (parent) => {
  19. this.parent = parent;
  20. }
  21. this.getParent = () => {
  22. return this.parent;
  23. }
  24. this.setOpen = (open) => {
  25. this.open = open;
  26. if (this.open) {
  27. password.setAttribute('type', 'text');
  28. password.classList.add('invisible');
  29. password.classList.remove('visible');
  30. } else {
  31. password.setAttribute('type', 'password');
  32. password.classList.add('visible');
  33. password.classList.remove('invisible');
  34. }
  35. }
  36. this.getOpen = () => {
  37. return this.open;
  38. }
  39. this.setValue = (value) => {
  40. this.value = value;
  41. }
  42. this.getValue = () => {
  43. return this.value;
  44. }
  45. password.oninput = () => {
  46. this.onChange(password.value);
  47. }
  48. button.onclick = () => {
  49. this.onOpenChange(this.open);
  50. if (this.open === true) {
  51. this.setOpen(false)
  52. } else {
  53. this.setOpen(true)
  54. }
  55. }
  56. }
  57. // let createPassword = new Password(container, true);
  58. // createPassword.onChange = data => console.log(data);
  59. // createPassword.onOpenChange = open => console.log(open);
  60. // createPassword.setValue('qwerty');
  61. // console.log(createPassword.getValue());
  62. // createPassword.setOpen(false);
  63. // console.log(createPassword.getOpen());
  64. function Login(parent) {
  65. this.onChange;
  66. this.parent = parent;
  67. this.html = `<div>
  68. <input type="text" id="login" placeholder="Enter Username" name="uname" required>
  69. <button type='submit' id='btn' disabled>SUBMIT</button>
  70. </div>`;
  71. this.parent.insertAdjacentHTML('beforebegin', this.html);
  72. this.password = new Password(this.parent, true);
  73. const login = document.getElementById('login');
  74. this.activateBtn = () => {
  75. let loginValue = this.loginValue;
  76. let passwordValue = this.password.getValue();
  77. if (loginValue !== undefined && passwordValue !== undefined) {
  78. btn.disabled = false;
  79. }
  80. }
  81. this.loginValue;
  82. this.getValue = () => {
  83. return this.loginValue;
  84. }
  85. this.setLoginValue = (value) => {
  86. this.loginValue = value;
  87. }
  88. this.password.onChange = (data) => {
  89. console.log(data)
  90. this.password.setValue(data);
  91. this.activateBtn();
  92. }
  93. login.oninput = () => {
  94. this.setLoginValue(login.value);
  95. this.activateBtn();
  96. }
  97. }
  98. let createLogin = new Login(container)
  99. // createLogin.getPassword().onChange = data => console.log(data);
  100. // createLogin.getPassword().onOpenChange = open => console.log(open);
  101. //////////////////////////////////////////////////////////////
  102. function Form(el, data, okCallback, cancelCallback){
  103. let formBody = document.createElement('div')
  104. let okButton = document.createElement('button')
  105. okButton.innerHTML = 'OK'
  106. let cancelButton = document.createElement('button')
  107. cancelButton.innerHTML = 'Cancel'
  108. formBody.innerHTML = '<h1>Форма</h1>'
  109. const table = document.createElement('table')
  110. formBody.append(table)
  111. if (typeof okCallback === 'function'){
  112. formBody.appendChild(okButton);
  113. okButton.onclick = (e) => {
  114. console.log(this)
  115. this.okCallback(data)
  116. }
  117. }
  118. if (typeof cancelCallback === 'function'){
  119. formBody.appendChild(cancelButton);
  120. cancelButton.onclick = cancelCallback
  121. }
  122. let inputCreators = {
  123. String(key, value, oninput){
  124. let input = document.createElement('input')
  125. if (value.match(/\**/)[0] === value) {
  126. input.type = 'password'
  127. input.value = ''
  128. input.oninput = () => oninput(input.value)
  129. } else {
  130. input.type = 'text'
  131. input.placeholder = key
  132. input.value = value
  133. input.oninput = () => oninput(input.value)
  134. }
  135. return input
  136. },
  137. Boolean(key, value, oninput){
  138. let input = document.createElement('input')
  139. input.type = 'checkbox'
  140. input.checked = value
  141. input.oninput = () => oninput(input.checked)
  142. return input
  143. },
  144. Date(key, value, oninput){
  145. let input = document.createElement('input')
  146. input.type = 'datetime-local'
  147. // при преобразовании объекта к строке смещается часовой пояс на нулевой, выражение ниже восстанавливает текущий
  148. input.value = new Date(value - (new Date()).getTimezoneOffset() * 60 * 1000).toISOString().slice(0,-1)
  149. input.oninput = () => oninput(new Date(input.value))
  150. return input
  151. },
  152. Number(key, value, oninput){
  153. let input = document.createElement('input')
  154. input.type = 'text'
  155. input.placeholder = key
  156. input.value = value
  157. input.oninput = () => oninput(input.value)
  158. return input
  159. },
  160. }
  161. el.appendChild(formBody)
  162. for (const [key, value] of Object.entries(data)) {
  163. const tr = document.createElement('tr')
  164. table.append(tr)
  165. let rowMandatory = false
  166. const th = document.createElement('th')
  167. if (key[0] === '*') {
  168. const thPre = document.createElement('span')
  169. thPre.innerText = '* '
  170. thPre.style.color = 'red'
  171. th.append(thPre)
  172. const thCont = document.createElement('span')
  173. thCont.innerText = key.slice(1)
  174. th.append(thCont)
  175. rowMandatory = true
  176. } else {
  177. th.innerText = key
  178. }
  179. tr.append(th)
  180. const td = document.createElement('td')
  181. tr.append(td)
  182. const input = inputCreators[value.constructor.name](key, value, (valueOfInput) => {
  183. if (key in this.validators) {
  184. let valid = this.validators[key](valueOfInput,key,data,input)
  185. if (valid === true) {
  186. data[key] = valueOfInput
  187. tdErr.innerText = ''
  188. input.style.backgroundColor = 'white'
  189. } else if (typeof valid === 'string') {
  190. tdErr.innerText = valid
  191. input.style.backgroundColor = 'red'
  192. input.style.color = 'white'
  193. }
  194. } else {
  195. data[key] = valueOfInput
  196. }
  197. if (rowMandatory && !valueOfInput) {
  198. input.style.border = '4px solid red'
  199. } else {
  200. input.style.border = '1px solid rgb(118, 118, 118)'
  201. }
  202. })
  203. td.append(input)
  204. const tdErr = document.createElement('td')
  205. tr.append(tdErr)
  206. }
  207. this.okCallback = okCallback
  208. this.cancelCallback = cancelCallback
  209. this.data = data
  210. this.validators = {}
  211. }
  212. let form = new Form(formContainer, {
  213. name: 'Anakin',
  214. surname: 'Skywalker',
  215. married: true,
  216. birthday: new Date((new Date).getTime() - 86400000 * 30*365)
  217. }, () => console.log('ok'),() => console.log('cancel') )
  218. form.okCallback = () => console.log('ok2')
  219. form.validators.surname = (value, key, data, input) => value.length > 2 &&
  220. value[0].toUpperCase() == value[0] &&
  221. !value.includes(' ') ? true : 'Wrong name'
  222. console.log(form)