main.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //Password
  2. // function Password(parent, open){
  3. // let input = document.createElement('input');
  4. // input.placeholder = 'your password';
  5. // input.type = 'password';
  6. // let btn= document.createElement('button');
  7. // btn.type = 'checkbox';
  8. // parent.append(input);
  9. // parent.append(btn);
  10. // let value = '';
  11. // let innerTextForBtn = open;
  12. // btn.innerText = innerTextForBtn ? 'show password' : 'hide password';
  13. // this.setValue = newValue => {
  14. // if (newValue != ' ' && newValue && newValue !== value){
  15. // value = newValue;
  16. // }
  17. // };
  18. // this.setOpen = newOpen => {
  19. // if (newOpen != innerTextForBtn && newOpen != undefined){
  20. // innerTextForBtn = newOpen;
  21. // }
  22. // };
  23. // input.oninput = this.onChange = (input) => {
  24. // value = input.data;
  25. // };
  26. // btn.onclick = this.onOpenChange = (newOpen) => {
  27. // innerTextForBtn = !innerTextForBtn;
  28. // if(input.getAttribute('type') === 'password'){
  29. // input.type = 'text';
  30. // btn.innerHTML = 'show password';
  31. // } else if (input.getAttribute('type') === 'text') {
  32. // input.type = 'password';
  33. // btn.innerHTML = 'hide password';
  34. // }
  35. // };
  36. // this.getValue = () => value;
  37. // this.getOpen = () => innerTextForBtn;
  38. // }
  39. // let p = new Password(document.body, true);
  40. // p.onChange = data => console.log(data);
  41. // p.onOpenChange = open => console.log(open);
  42. // p.setValue('qwerty');
  43. // console.log(p.getValue());
  44. // p.setOpen(false);
  45. // console.log(p.getOpen());
  46. //LoginForm
  47. // function Login(parent){
  48. // let form = document.createElement('form');
  49. // let inputLogin = document.createElement('input');
  50. // inputLogin.placeholder = 'login';
  51. // inputLogin.type = 'text';
  52. // let inputPaswd = document.createElement('input');
  53. // inputPaswd.placeholder = 'password';
  54. // inputPaswd.type = 'password';
  55. // let btn= document.createElement('button');
  56. // btn.innerHTML = 'First, write login and password';
  57. // parent.append(form);
  58. // form.append(inputLogin);
  59. // form.append(inputPaswd);
  60. // form.append(btn);
  61. // let valueLogin = '';
  62. // let valuePaswd = '';
  63. // inputLogin.oninput = () =>{
  64. // valueLogin = inputLogin.value;
  65. // };
  66. // inputPaswd.oninput = () =>{
  67. // valuePaswd = inputPaswd.value;
  68. // };
  69. // btn.onclick = (event) =>{
  70. // event.preventDefault();
  71. // if(valueLogin === '' && valuePaswd === ''){
  72. // btn.innerHTML = 'Write login and password';
  73. // btn.style.backgroundColor = 'red';
  74. // } else {
  75. // btn.disabled = true;
  76. // btn.innerHTML = 'Sent';
  77. // btn.style.backgroundColor = 'green';
  78. // }
  79. // };
  80. // }
  81. // let p = new Login(document.body, true);
  82. //Password Verify
  83. // function Password (parent){
  84. // let pswd = document.createElement('input');
  85. // let pswdVerify = document.createElement('input');
  86. // let btn = document.createElement('button');
  87. // pswd.type = 'password';
  88. // pswdVerify.type = 'password';
  89. // btn.innerHTML = 'Check';
  90. // parent.append(pswd);
  91. // parent.append(pswdVerify);
  92. // parent.append(btn);
  93. // let valuePswd = '';
  94. // let valuePswdVerify = '';
  95. // pswdVerify.style.display = 'none';
  96. // pswd.oninput = () =>{
  97. // valuePswd = pswd.value;
  98. // };
  99. // pswdVerify.oninput = () =>{
  100. // valuePswdVerify = pswdVerify.value;
  101. // };
  102. // let p = document.createElement('p');
  103. // parent.append(p);
  104. // btn.onclick = () => {
  105. // if(valuePswd === ''){
  106. // p.innerHTML = 'Write password';
  107. // p.style.color = 'red';
  108. // } else if (pswdVerify.style.display === 'block') {
  109. // if(valuePswd === valuePswdVerify){
  110. // p.innerHTML = 'Right password';
  111. // p.style.color = 'green';
  112. // } else{
  113. // p.innerHTML = 'You made mistake';
  114. // p.style.color = 'red';
  115. // }
  116. // } else {
  117. // pswdVerify.style.display = 'block';
  118. // p.innerHTML = '';
  119. // }
  120. // };
  121. // }
  122. // let p = new Password(document.body);
  123. //Form
  124. const dateToDataTimeLocal = date => (new Date(date.getTime() - date.getTimezoneOffset() * 60000)).toISOString().slice(0, -1);
  125. dateToDataTimeLocal(new Date());
  126. function Form(el, data, okCallback, cancelCallback){
  127. let formBody = document.createElement('div');
  128. let okButton = document.createElement('button');
  129. okButton.innerHTML = 'OK';
  130. let cancelButton = document.createElement('button');
  131. cancelButton.innerHTML = 'Cancel';
  132. let inputCreators = {
  133. String(key, value, oninput){
  134. let div = document.createElement('div');
  135. let label = document.createElement('label');
  136. let input = document.createElement('input');
  137. label.innerHTML = key;
  138. input.required = true;
  139. input.placeholder = key;
  140. input.value = value;
  141. if(key === 'password'){
  142. input.type = 'password';
  143. }
  144. let p = document.createElement('p');
  145. p.innerHTML = '';
  146. input.oninput = () => {
  147. (function() {
  148. if (input.placeholder in this.validators) {
  149. let x = this.validators[input.placeholder](input.value, input.placeholder, this.data, input);
  150. if(x !== true){
  151. p.style.color = 'red';
  152. p.innerHTML = 'Error!';
  153. input.style.backgroundColor = 'red';
  154. label.appendChild(p);
  155. } else {
  156. p.innerHTML = '';
  157. input.style.backgroundColor = '';
  158. console.log(x);
  159. }
  160. } else {
  161. oninput(input.value);
  162. }
  163. }).call(this);
  164. }
  165. formBody.appendChild(div);
  166. div.appendChild(label,input);
  167. let abbr = document.createElement('abbr');
  168. abbr.title = 'Обязательно';
  169. abbr.className = 'importantInput';
  170. abbr.innerHTML = '*';
  171. label.appendChild(abbr);
  172. return input;
  173. },
  174. Boolean(key, value, oninput){
  175. let div = document.createElement('div');
  176. let label = document.createElement('label');
  177. let input = document.createElement('input');
  178. label.innerHTML = key;
  179. input.required = true;
  180. input.type = 'checkbox';
  181. input.checked = value;
  182. input.appendChild(label);
  183. input.oninput = () => oninput(input.checked);
  184. formBody.appendChild(div);
  185. div.appendChild(label,input);
  186. let abbr = document.createElement('abbr');
  187. abbr.title = 'Обязательно';
  188. abbr.className = 'importantInput';
  189. abbr.innerHTML = '*';
  190. label.appendChild(abbr);
  191. return input;
  192. },
  193. Date(key, value, oninput){
  194. let div = document.createElement('div');
  195. let label = document.createElement('label');
  196. let input = document.createElement('input');
  197. label.innerHTML = key;
  198. input.required = true;
  199. input.type = 'datetime-local';
  200. input.value = dateToDataTimeLocal(value);
  201. input.onchange = () =>{
  202. oninput(new Date(input.value));
  203. };
  204. formBody.appendChild(div);
  205. div.appendChild(label,input);
  206. let abbr = document.createElement('abbr');
  207. abbr.title = 'Обязательно';
  208. abbr.className = 'importantInput';
  209. abbr.innerHTML = '*';
  210. label.appendChild(abbr);
  211. return input;
  212. }
  213. }
  214. for(const [key, value] of Object.entries(data)){
  215. let input = inputCreators[value.constructor.name].call(this, key, value, newValue => {
  216. data[key] = newValue;
  217. });
  218. formBody.appendChild(input);
  219. }
  220. if (typeof okCallback === 'function'){
  221. formBody.appendChild(okButton);
  222. okButton.onclick = (e) => {
  223. this.okCallback(e)
  224. // console.log(this)
  225. }
  226. }
  227. if (typeof cancelCallback === 'function'){
  228. formBody.appendChild(cancelButton);
  229. cancelButton.onclick = () => window.location.reload();
  230. }
  231. el.appendChild(formBody);
  232. this.okCallback = okCallback;
  233. this.cancelCallback = cancelCallback;
  234. this.data = data;
  235. this.validators = {};
  236. };
  237. let form = new Form(formContainer, {
  238. name: 'Anakin',
  239. surname: 'Skywalker',
  240. password: 'qwerty',
  241. married: true,
  242. birthday: new Date((new Date).getTime() - 86400000 * 30*365)
  243. }, () => console.log('ok'),() => console.log('cancel') );
  244. form.okCallback = () => console.log('ok')
  245. form.validators.name = (value, key, data, input) => value.length > 2 &&
  246. value[0].toUpperCase() == value[0] &&
  247. !value.includes(' ') ? true : 'Wrong name';
  248. form.validators.surname = (value, key, data, input) => value.length > 2 &&
  249. value[0].toUpperCase() == value[0] &&
  250. !value.includes(' ') ? true : 'Wrong name';
  251. form.validators.password = (value, key, data, input) => value.length > 5 &&
  252. !value.includes(' ') ? true : 'Wrong name';
  253. console.log(form);