main.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //debugger;
  2. function Form(el, data, okCallback, cancelCallback){
  3. let inputCreators = {
  4. String(key, value, oninput){
  5. let input = document.createElement('input');
  6. input.type = 'text'
  7. input.placeholder = key.slice(1, key.length);
  8. input.id = `${key}`;
  9. if(value.charAt(0) === '*') {
  10. data[key] = '';
  11. input.type = 'password';
  12. input.placeholder = 'password';
  13. input.id = `${key}`;
  14. input.oninput = () => oninput(input.value);
  15. return input;
  16. }
  17. input.value = value
  18. input.oninput = () => oninput(input.value)
  19. return input
  20. },
  21. Boolean(key, value, oninput){
  22. //label for с input type='checkbox' внутри
  23. let input = document.createElement('input');
  24. input.type = 'checkbox'
  25. input.placeholder = key
  26. input.checked = value
  27. input.id = `${key}`;
  28. input.oninput = () => oninput(input.checked)
  29. return input
  30. },
  31. Date: function (key, value, oninput){
  32. //используйте datetime-local let input = document.createElement('input');
  33. let input = document.createElement('input')
  34. input.type = 'datetime-local'
  35. input.placeholder = key
  36. input.id = `${key}`;
  37. input.value = value.toISOString().slice(0,-1);
  38. input.oninput = () => oninput(Date(input.value))
  39. return input
  40. },
  41. //и др. по вкусу, например textarea для длинных строк
  42. }
  43. this.validators = {};
  44. let table = document.createElement('table');
  45. document.body.appendChild(table);
  46. for (let [key, value] of Object.entries(data)) {
  47. let tr = document.createElement('tr');
  48. let th = document.createElement('th');
  49. let td = document.createElement('td');
  50. let tdForWrong = document.createElement('td');
  51. const span = document.createElement('span');
  52. const spanKey = document.createElement('span');
  53. let input = inputCreators[value.constructor.name](key, value, function enterPlase (value){
  54. data[key] = value;
  55. })
  56. input.addEventListener('input', () => {
  57. let resultInputValue = input.value;
  58. let resultValadators = true;
  59. if (value.constructor.name === Boolean) {
  60. resultInputValue = input.checked;
  61. };
  62. if (typeof this.validators[key] === 'function') {
  63. resultValadators = this.validators[key](resultInputValue, key, data, input);
  64. if (typeof resultValadators === 'string') {
  65. resultValadators = false;
  66. input.style.backgroundColor = 'red';
  67. tdForWrong.innerHTML = ` ${this.validators[key](resultInputValue, key, data, input)}`
  68. }
  69. if (resultValadators) {
  70. input.style.backgroundColor = 'white';
  71. tdForWrong.innerHTML = ` `;
  72. }
  73. }
  74. resultInputValue === '' ? input.style.border = '3px solid red': input.style.border = null;
  75. })
  76. if (key.charAt(0) === '*') {
  77. const start = key.charAt(0);
  78. spanKey.innerHTML = `${key.slice(1, key.length)}`;
  79. span.style.color = 'red';
  80. span.innerText = `${start} `;
  81. } else {
  82. spanKey.innerHTML = key;
  83. }
  84. table.append(tr);
  85. tr.append(th);
  86. tr.append(td);
  87. th.appendChild(span);
  88. th.append(spanKey);
  89. td.appendChild(input);
  90. tr.append(tdForWrong);
  91. }
  92. this.copyData = {...data};
  93. this.checkInput = function () {
  94. let input = document.querySelector(input);
  95. input.value
  96. }
  97. let formBody = document.createElement('div')
  98. let okButton = document.createElement('button')
  99. okButton.innerHTML = 'OK';
  100. okButton.addEventListener('click', () => {
  101. let countKey = 0;
  102. let countvalidators = 0;
  103. for (let [key, value] of Object.entries(data)) {
  104. let resultInputValue = value;
  105. let resultValadators = true;
  106. let input = document.getElementById(value);
  107. if (typeof this.validators[key] === 'function') {
  108. resultValadators = this.validators[key](resultInputValue, key, data, input);
  109. if (typeof resultValadators === 'string') {
  110. resultValadators = false;
  111. }
  112. }
  113. if (resultValadators && resultInputValue ) {
  114. countvalidators++;
  115. }
  116. countKey++;
  117. }
  118. if (countKey === countvalidators) {
  119. this.okCallback(data);
  120. }
  121. })
  122. let cancelButton = document.createElement('button')
  123. cancelButton.innerHTML = 'Cancel'
  124. formBody.innerHTML = '<h1>тут будет форма после перервы</h1>'
  125. cancelButton.addEventListener('click', () => { console.log(this.copyData)
  126. for (let [key, value] of Object.entries(this.copyData)) {
  127. let input = document.getElementById(key);
  128. if( value.constructor.name === 'String') {
  129. input.value = `${value}`;
  130. }
  131. if (value.constructor.name === 'Boolean') {
  132. input.checked = `${value}`;
  133. }
  134. if (value.constructor.name === 'Date') {
  135. input.value = value.toISOString().slice(0,-1);
  136. }
  137. }
  138. })
  139. if (typeof okCallback === 'function'){
  140. formBody.appendChild(okButton);
  141. okButton.onclick = (e) => {
  142. //console.log(this)
  143. this.okCallback(e)
  144. }
  145. }
  146. if (typeof cancelCallback === 'function'){
  147. formBody.appendChild(cancelButton);
  148. cancelButton.onclick = cancelCallback
  149. }
  150. el.appendChild(formBody);
  151. this.okCallback;
  152. this.cancelCallback = cancelCallback
  153. this.data = data
  154. }
  155. let form = new Form(document.body, {
  156. '*name': 'Anaaa',
  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 = (what) => console.log(what)
  162. form.validators.surname = (value, key, data, input) => value.length > 2 &&
  163. value[0].toUpperCase() == value[0] &&
  164. !value.includes(' ') ? true : 'Wrong name'
  165. //console.log(form)