main.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. function dateToDateTimeLocal(date) {
  2. let dateTime = date.getTime()
  3. let dateTimeOffset = 60000 * date.getTimezoneOffset()
  4. let nowDate = dateTime - dateTimeOffset
  5. let newD = new Date(nowDate)
  6. let dateString = (newD.toISOString().slice(0, -1))
  7. return dateString
  8. }
  9. console.log(dateToDateTimeLocal(new Date()))
  10. //форма
  11. function Form(el, data, okCallback, cancelCallback) {
  12. let formBody = document.createElement('div')
  13. let okButton = document.createElement('button')
  14. okButton.innerHTML = 'OK'
  15. let cancelButton = document.createElement('button')
  16. cancelButton.innerHTML = 'Cancel'
  17. let inputCreators = {
  18. String(key, value, oninput) {
  19. let input = document.createElement('input')
  20. input.placeholder = key
  21. input.value = value
  22. input.oninput = () => {
  23. oninput(input.value)
  24. if (input.value.length == 0)
  25. okButton.disabled = true
  26. else okButton.disabled = false
  27. }
  28. if (value[0] == '*') {
  29. input.type = "password"
  30. input.placeholder = "password"
  31. } else
  32. input.type = 'text'
  33. return input
  34. },
  35. Boolean(key, value, oninput) {
  36. let input = document.createElement('input')
  37. input.type = 'checkbox'
  38. input.placeholder = key
  39. input.checked = value
  40. input.onchange = () => {
  41. oninput(input.checked)
  42. }
  43. //label for с input type='checkbox' внутри
  44. return input
  45. },
  46. Date(key, value, oninput) {
  47. let input = document.createElement('input')
  48. input.type = 'datetime-local'
  49. input.placeholder = key
  50. input.value = dateToDateTimeLocal(value)
  51. input.oninput = () => {
  52. oninput(new Date(input.value))
  53. if (input.value.length == 0)
  54. okButton.disabled = true
  55. else okButton.disabled = false
  56. }
  57. return input
  58. }
  59. }
  60. el.appendChild(formBody)
  61. let table = document.createElement("table");
  62. formBody.prepend(table);
  63. this.validators = {}
  64. //create form
  65. function createForm(data) {
  66. for (let key in data) {
  67. let tr = document.createElement("tr");
  68. table.appendChild(tr);
  69. let th = document.createElement("th");
  70. let keyStar = document.createElement("label");
  71. tr.appendChild(keyStar);
  72. tr.appendChild(th);
  73. let label = document.createElement("label");
  74. if (key[0] === "*") {
  75. let keyStart = key[0];
  76. keyStar.style.color = "red";
  77. keyStar.innerText = `${keyStart}`;
  78. th.style.color = "black";
  79. let newKey = key.slice(1)
  80. th.innerHTML = `${newKey}`;
  81. } else {
  82. th.innerHTML = key;
  83. }
  84. let td = document.createElement("td");
  85. tr.appendChild(td);
  86. tr.appendChild(label)
  87. let result;
  88. let resultInput;
  89. let valueConstructor = data[key].constructor.name
  90. if ([valueConstructor] in inputCreators) {
  91. result = inputCreators[valueConstructor]
  92. (key, data[key], (value) => {
  93. data[key] = value
  94. })
  95. }
  96. result.onchange = () => {
  97. if (this.validators.hasOwnProperty(key)) {
  98. resultInput = this.validators[key](
  99. result.value, key, data, result);
  100. if (resultInput == "true") {
  101. result.style.border = '';
  102. label.innerHTML = ""
  103. result.style.border = '1px solid black';
  104. result.style.backgroundColor = "white"
  105. okButton.disabled = false
  106. } else {
  107. result.style.border = '1px solid red';
  108. label.innerHTML = ""
  109. label.append(resultInput)
  110. label.style.color = '#FA8072'
  111. result.style.backgroundColor = "#FA8072"
  112. okButton.disabled = true
  113. }
  114. if (result.value.length == 0) {
  115. result.style.border = '1px solid red';
  116. result.style.backgroundColor = '#FA8072';
  117. okButton.disabled = true
  118. label.innerHTML = `ERROR: you entered empty field '${key}'`
  119. label.style.color = '#FA8072'
  120. } else {
  121. label.innerHTML = ''
  122. result.style.border = '1px solid black';
  123. result.style.backgroundColor = "white";
  124. okButton.disabled = false
  125. }
  126. }
  127. }
  128. if (result) td.appendChild(result)
  129. }
  130. }
  131. createForm.call(this, data)
  132. const my_obj = {...data }
  133. this.myData = Object.freeze(my_obj)
  134. if (typeof okCallback === 'function') {
  135. formBody.appendChild(okButton);
  136. okButton.onclick = (e) => {
  137. console.log(this)
  138. this.okCallback(e)
  139. }
  140. }
  141. if (typeof cancelCallback === 'function') {
  142. formBody.appendChild(cancelButton);
  143. cancelButton.onclick = (e) => {
  144. table.innerText = ""
  145. createForm.call(this, this.myData)
  146. }
  147. }
  148. this.okCallback = okCallback
  149. this.cancelCallback = cancelCallback
  150. }
  151. let form = new Form(formContainer, {
  152. "*name": 'Anakin',
  153. surname: 'Skywalker',
  154. married: true,
  155. password: '*****',
  156. birthday: new Date((new Date).getTime() - 86400000 * 30 * 365)
  157. }, () => console.log('ok'), () => console.log('cancel'))
  158. form.okCallback = () => console.log('ok2')
  159. form.validators.surname = (value, key, data, input) =>
  160. value.length > 2 && value[0].toUpperCase() == value[0] && !value.includes(" ") ?
  161. true :
  162. "Wrong name";