script.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. let date = new Date();
  2. function dataToDataTimeLocal(date) {
  3. let timeStamp = date.getTime();
  4. let timeZoneoffset = -date.getTimezoneOffset() * 60 * 1000;
  5. let localTime = timeStamp + timeZoneoffset;
  6. return new Date(localTime).toISOString().slice(0, -1);
  7. }
  8. function Form(el, data, okCallback, cancelCallback) {
  9. let formBody = document.createElement('div')
  10. let okButton = document.createElement('button')
  11. okButton.innerHTML = 'OK'
  12. let cancelButton = document.createElement('button')
  13. cancelButton.innerHTML = 'Cancel';
  14. let inputCreators = {
  15. String(key, value, oninput) {
  16. let input = document.createElement('input')
  17. if (key[0] == '*'){
  18. let reqSpan = document.createElement('span');
  19. reqSpan.style.color = 'red';
  20. reqSpan.style.display ='none';
  21. reqSpan.innerText = ` ${key[0]} `;
  22. formBody.append(reqSpan);
  23. key = key.slice(1);
  24. input.setAttribute("required", "");
  25. }
  26. input.placeholder = key;
  27. console.log(key)
  28. input.value = value
  29. input.oninput = () => {
  30. /*
  31. if(this.validators[key] && this.validators[key] == [key]){
  32. value = this.validators[key](value, key, data, input) ? input.value: value;
  33. } else if (this.validators) {
  34. errorSpan.style.backgroundColor = 'red';
  35. errorSpan.innerText = `Error ${newValue}`;
  36. } */
  37. if(value == '*****'){
  38. input.type = 'password'
  39. } else input.type = 'text';
  40. if (!value.trim()){
  41. return value;
  42. }
  43. if (input.value.length == 0){
  44. input.style.border = '1px solid red';
  45. okButton.setAttribute('disabled','disabled')
  46. } else if (value.trim() !== '') {
  47. /* if (this.validators[key](value, key, data, input)){
  48. console.log('test')
  49. } */
  50. console.log('ok')
  51. input.style.border = '1px solid black'
  52. okButton.removeAttribute('disabled','')
  53. }
  54. oninput(input.value)
  55. }
  56. return input
  57. },
  58. Boolean(key, value, oninput) {
  59. let input = document.createElement('input')
  60. if (key[0] == '*')input.setAttribute("required", "");
  61. input.type = 'checkbox'
  62. input.placeholder = key
  63. input.checked = value
  64. input.oninput = () => oninput(input.checked)
  65. return input
  66. },
  67. Date(key, value, oninput) {
  68. console.log(this.validators);
  69. let input = document.createElement('input')
  70. console.log(key[0])
  71. if (key[0] == '*')input.setAttribute("required", "");
  72. input.type = 'datetime-local'
  73. input.placeholder = key
  74. input.value = dataToDataTimeLocal(value)
  75. input.onchange = () => {
  76. /* if (typeof this.validators === 'function') {
  77. if(this.validators == [key]){
  78. this.validators[key](value, key, data, input)? data[key] = newValue: data[key];
  79. } else {
  80. errorSpan.style.backgroundColor = 'red';
  81. errorSpan.innerText = `Error ${newValue}`;
  82. } */
  83. oninput(new Date(input.value))
  84. }
  85. // }
  86. return input
  87. },
  88. }
  89. function createForm(initialState) {
  90. let formBody = document.createElement('div')
  91. let okButton = document.createElement('button')
  92. okButton.innerHTML = 'OK'
  93. let cancelButton = document.createElement('button')
  94. cancelButton.innerHTML = 'Cancel';
  95. for (let [key, value] of Object.entries(initialState)) {
  96. let errorSpan = document.createElement('span');
  97. let input = inputCreators[value.constructor.name](key, value, newValue => {
  98. initialState[key] = newValue;
  99. });
  100. formBody.append(input, errorSpan, okButton, cancelButton );
  101. el.appendChild(formBody)
  102. this.okCallback = okCallback
  103. this.cancelCallback = cancelCallback;
  104. this.data = data
  105. this.validators = {
  106. }
  107. }
  108. }
  109. for (let [key, value] of Object.entries(data)) {
  110. let errorSpan = document.createElement('span');
  111. let input = inputCreators[value.constructor.name](key, value, newValue => {
  112. data[key] = newValue;
  113. });
  114. formBody.append(input);
  115. formBody.append(errorSpan);
  116. }
  117. if (typeof okCallback === 'function') {
  118. okButton.setAttribute('disabled','')
  119. formBody.appendChild(okButton);
  120. okButton.onclick = (e) => {
  121. this.okCallback(e)
  122. }
  123. }
  124. if (typeof cancelCallback === 'function') {
  125. formBody.appendChild(cancelButton);
  126. cancelButton.onclick = () => {
  127. formBody.remove();
  128. createForm(initialState);
  129. this.cancelCallback();
  130. }
  131. }
  132. el.appendChild(formBody)
  133. this.okCallback = okCallback
  134. this.cancelCallback = cancelCallback;
  135. let initialState = Object.assign({}, data);
  136. this.initialState = initialState;
  137. this.data = data
  138. this.validators = {
  139. }
  140. }
  141. let form = new Form(formContainer, {
  142. '*name': 'Anakin',
  143. '*surname': 'Skywalker',
  144. married: true,
  145. birthday: new Date((new Date).getTime() - 86400000 * 30 * 365)
  146. }, () => console.log('ok'), () =>{console.log('ok') } )
  147. form.okCallback = () => console.log('ok2')
  148. /* form.cancelCallback = () => {
  149. console.log('datainit')
  150. // form = new Form(data);
  151. } */
  152. form.validators.surname = (value, key, data, input) => value.length > 2 &&
  153. value[0].toUpperCase() == value[0] &&
  154. !value.includes(' ') ? true : 'Wrong name'
  155. console.log(form);