script.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //Form
  2. function Form(el, data, okCallback, cancelCallback){
  3. let backUp = {};
  4. let formBody = document.createElement('div');
  5. let okButton = document.createElement('button');
  6. okButton.innerHTML = 'OK';
  7. let cancelButton = document.createElement('button');
  8. cancelButton.innerHTML = 'Cancel';
  9. formBody.innerHTML = '<h1>тут будет форма после перервы</h1>';
  10. const table = document.createElement('table');
  11. formBody.append(table);
  12. let flag = false;
  13. this.validators = {};
  14. //Отображение
  15. for (const key in data) {
  16. let tr = document.createElement('tr');
  17. let th = document.createElement('th');
  18. let td = document.createElement('td');
  19. let tdError = document.createElement('td');
  20. let strError = '';
  21. //Many Inputs
  22. let inp = inputCreators[data[key].constructor.name](key, data[key], (value) => data[key] = value);
  23. //Sync
  24. //let inp = document.createElement('input');
  25. //inp.value = data[key];
  26. //inp.oninput = () => data[key] = inp.value;
  27. //Mandatory
  28. if(data[key] === '') {
  29. inp.style.border = '2px solid #f00';
  30. td.append(inp);
  31. flag = false;
  32. } else {
  33. td.append(inp);
  34. flag = true;
  35. }
  36. if (key.slice(0,1) === "*") {
  37. th.innerHTML = `<strong>${key.slice(1)} <sup style="color: red">*</sup></strong>`;
  38. } else {
  39. th.textContent = key;
  40. }
  41. tr.append(th);
  42. tr.append(td);
  43. table.append(tr);
  44. //Validators / Validators messages
  45. inp.onchange = () => {
  46. if(data[key] === '') {
  47. inp.style.border = '2px solid #f00';
  48. flag = false;
  49. }
  50. else {
  51. inp.style.border = '2px solid #000';
  52. flag = true;
  53. }
  54. if (key in this.validators) {
  55. if (strError !== '') strError = '';
  56. let res = this.validators[key](data[key], key, data, inp);
  57. if (res === true){
  58. strError = res.toString();
  59. tdError.style.color = '#000';
  60. flag = true;
  61. }
  62. else {
  63. strError = res.toString();
  64. tdError.style.color = '#F00';
  65. flag = false;
  66. }
  67. tdError.textContent = strError;
  68. tr.append(tdError);
  69. }
  70. }
  71. }
  72. if (typeof okCallback === 'function'){
  73. formBody.appendChild(okButton);
  74. if (flag === true){
  75. okButton.disabled = false;
  76. okCallback(data);
  77. okButton.onclick = (e) => {
  78. console.log(this);
  79. this.okCallback(e);
  80. }
  81. }
  82. else {
  83. okButton.disabled = true;
  84. }
  85. }
  86. if (typeof cancelCallback === 'function'){
  87. formBody.appendChild(cancelButton);
  88. cancelButton.onclick = () => {
  89. backUp = {...data};
  90. table.remove();
  91. cancelCallback();
  92. }
  93. }
  94. el.appendChild(formBody);
  95. this.okCallback = okCallback;
  96. this.cancelCallback = cancelCallback;
  97. this.data = data;
  98. }
  99. //Many Inputs
  100. let inputCreators = {
  101. String(key, value, oninput){
  102. let input = document.createElement('input')
  103. //Password
  104. if (value.includes('*')){
  105. input.type = 'password'
  106. input.value = ''
  107. input.placeholder = 'password'
  108. } else {
  109. input.type = 'text'
  110. input.value = value
  111. input.placeholder = key
  112. }
  113. input.oninput = () => oninput(input.value)
  114. return input
  115. },
  116. Boolean(key, value, oninput){
  117. let input = document.createElement('input');
  118. input.type = 'checkbox';
  119. input.checked = value;
  120. input.oninput = () => oninput(input.checked);
  121. return input;
  122. },
  123. Date(key, value, oninput){
  124. let offset = value.getTimezoneOffset();
  125. let now = value.getTime();
  126. let nowPlusOffset = new Date(now - offset * 60 * 1000);
  127. let input = document.createElement('input');
  128. input.type = 'datetime-local';
  129. input.value = nowPlusOffset.toISOString().slice(0, -1);
  130. input.oninput = () => oninput(new Date(input.value));
  131. return input;
  132. }
  133. }
  134. let form = new Form(formContainer, {
  135. '*name': 'Anakin',
  136. surname: 'Skywalker',
  137. married: true,
  138. birthday: new Date((new Date).getTime() - 86400000 * 30 * 365)
  139. }, () => console.log('ok'),() => console.log('cancel') )
  140. form.okCallback = () => console.log('ok2');
  141. form.validators.surname = (value, key, data, input) =>
  142. value.length > 2 && value[0].toUpperCase() === value[0] && !value.includes(' ')
  143. ? true : 'Wrong name'
  144. console.log(form)