Part2_Form_.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Form</title>
  6. </head>
  7. <body>
  8. <div id="formContainer">
  9. </div>
  10. <script>
  11. function Form(el, data, okCallback, cancelCallback){
  12. let check=[];
  13. let formBody = document.createElement('div')
  14. let okButton = document.createElement('button')
  15. okButton.innerHTML = 'OK'
  16. okButton.disabled=true;
  17. let cancelButton = document.createElement('button')
  18. cancelButton.innerHTML = 'Cancel';
  19. formBody.innerHTML = '<h1>тут будет форма после перервы</h1>';
  20. const table=document.createElement('table');
  21. this.okCallback = okCallback
  22. this.cancelCallback = cancelCallback
  23. let backupData = {...data};
  24. this.data = data
  25. this.validators = {}
  26. this.createData = function (object) {
  27. formBody.append(table)
  28. formBody.appendChild(cancelButton);
  29. formBody.appendChild(okButton);
  30. let inputCreators = {
  31. String(key, value, oninput){
  32. let input = document.createElement('input')
  33. input.type = 'text'
  34. input.placeholder = key
  35. if (/^[*]+$/.test(value)) {
  36. input.type = 'password';
  37. input.value = '';
  38. } else {
  39. input.type = 'text'
  40. input.value = value
  41. }
  42. input.oninput = () => oninput(input.value)
  43. return input;
  44. },
  45. Boolean(key, value, oninput){
  46. //label for с input type='checkbox' внутри
  47. let input = document.createElement('input')
  48. input.type = 'checkbox'
  49. input.checked=value
  50. input.placeholder = key
  51. input.oninput = () => oninput(input.checked)
  52. return input;
  53. },
  54. Date(key, value, oninput){
  55. //используйте datetime-local
  56. let input = document.createElement('input')
  57. input.type = 'datetime-local'
  58. input.placeholder = key
  59. let offset = value.getTimezoneOffset()
  60. const now = value.getTime()
  61. const nowPlusOffset = new Date(now - offset * 60 * 1000)
  62. input.value = nowPlusOffset.toISOString().slice(0, -1)
  63. input.oninput = () => oninput(input.value)
  64. return input
  65. },
  66. }
  67. for(let item in data){
  68. let tr=document.createElement('tr');
  69. let th=document.createElement('th');
  70. let p=document.createElement('p');
  71. let errorField = document.createElement('td')
  72. let errorFieldMessage = document.createElement('p')
  73. p.innerText=item+":";
  74. th.append(p);
  75. tr.append(th);
  76. let td=document.createElement('td');
  77. let input=inputCreators[data[item].constructor.name](item,data[item],
  78. value=>{
  79. data[item]=value;
  80. }
  81. )
  82. td.append(input);
  83. tr.append(td);
  84. table.append(tr);
  85. errorField.append(errorFieldMessage)
  86. formBody.append(table);
  87. input.onchange = () => {
  88. if(data[item] === '') {
  89. input.style.background = '#FFDEAD';
  90. check.push[false]
  91. } else {
  92. input.style.background = '#fff';
  93. check.push[true]
  94. }
  95. if (this.validators[item]) {
  96. let validationResult = this.validators[item](data[item], item, data, input)
  97. if (validationResult === true) {
  98. errorFieldMessage.innerText = ''
  99. input.style.background = '#fff';
  100. check.push[true]
  101. }
  102. else if (typeof validationResult === 'string') {
  103. errorFieldMessage.innerText = validationResult
  104. input.style.background = '#f00';
  105. check.push[false]
  106. }
  107. }
  108. if (item[0] === "*") {
  109. input.required = true;
  110. th.innerHTML = `<strong>${key.slice(1)} <sup style="color: red">*</sup></strong>`;
  111. } else {
  112. th.innerText = item;
  113. }
  114. if (data[item] === '') {
  115. input.style.background = '#FFDEAD';
  116. check.push[false]
  117. } else {
  118. input.style.background = '#fff';
  119. check.push[true]
  120. }
  121. }
  122. }
  123. }
  124. this.createData(this.data);
  125. if(typeof okCallback === 'function'){
  126. /*formBody.appendChild(okButton);
  127. okButton.onclick = (e) => {
  128. console.log(this);
  129. this.okCallback(e);
  130. }*/
  131. if (check.includes(false)){
  132. okButton.disabled = true;
  133. }else{
  134. okButton.disabled = false;
  135. okButton.onclick = (e) => {
  136. okCallback(this.data);
  137. console.log(this);
  138. this.okCallback(e);
  139. }
  140. }
  141. }
  142. if (typeof cancelCallback === 'function'){
  143. /*formBody.appendChild(cancelButton);
  144. cancelButton.onclick = cancelCallback;*/
  145. cancelButton.onclick = () => {
  146. while (table.hasChildNodes()) {
  147. table.removeChild(table.firstChild);
  148. }
  149. this.createData(backupData)
  150. cancelCallback();
  151. }
  152. }
  153. el.appendChild(formBody);
  154. }
  155. let form = new Form(formContainer, {
  156. name: 'Anakin',
  157. surname: 'Skywalker',
  158. married: true,
  159. birthday: new Date((new Date).getTime() - 86400000 * 30*365)
  160. }, () => console.log('ok'),() => console.log('cancel') );
  161. form.okCallback = () => console.log('ok2');
  162. form.validators.surname = (value, key, data, input) => value.length > 2 && value[0].toUpperCase() == value[0] && !value.includes(' ') ? true : 'Wrong name';
  163. form.validators.name = (value, key, data, input) => value.length > 2 && value[0].toUpperCase() == value[0] && !value.includes(' ') ? true : 'Wrong name'
  164. /*if(item=='married'){
  165. let check=document.createElement('input');
  166. check.type='checkbox';
  167. if(data[item]==true){
  168. check.checked=true;
  169. }else{
  170. check.checked=false;
  171. }
  172. td.append(check);
  173. }else{*/
  174. </script>
  175. </body>
  176. </html>