main.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. function Form(el, data, okCallback, cancelCallback){
  2. let formBody = document.createElement('div')
  3. let inputBox = document.createElement('div')
  4. let okButton = document.createElement('button')
  5. okButton.innerHTML = 'OK'
  6. let cancelButton = document.createElement('button')
  7. cancelButton.innerHTML = 'Cancel'
  8. let firstInfo = {...data};
  9. okButton.disabled = true;
  10. formBody.innerHTML = '<h1>тут будет форма после перервы</h1>'
  11. formBody.appendChild(inputBox);
  12. let inputCreators = {
  13. String(key, value, oninput){
  14. /* let label = document.createElement('label'); */
  15. let input = document.createElement('input');
  16. if(value == '*'){
  17. input.type = 'password'
  18. } else {
  19. input.type = 'text'
  20. }
  21. /* if(key[0]=='*'){
  22. input.onblur = function(){
  23. if(input.value.includes('')){
  24. input.style.borderColor = 'red';
  25. }
  26. }
  27. input.onfocus = function(){
  28. input.style.borderColor = '';
  29. }
  30. } */
  31. /* label.innerText = `${key}` */
  32. input.placeholder = key
  33. input.value = value
  34. input.oninput = () => oninput(input.value)
  35. /* label.appendChild(input); */
  36. return input
  37. },
  38. Boolean(key, value, oninput){
  39. //label for с input type='checkbox' внутри
  40. /* */
  41. let label = document.createElement('label');
  42. let input = document.createElement('input');
  43. input.type = 'checkbox'
  44. input.checked= value
  45. input.oninput = () => oninput(input.checked);
  46. label.innerText = `${key}`
  47. label.appendChild(input);
  48. return label
  49. },
  50. Date(key, value, oninput){
  51. //используйте datetime-local
  52. let input = document.createElement('input');
  53. input.type = "datetime-local"
  54. let timeStamp = value.getTime();
  55. input.value = ((new Date(timeStamp - value.getTimezoneOffset()*60*1000)).toISOString()).slice(0,-1);
  56. input.oninput= () => oninput(new Date(input.value));
  57. return input
  58. }
  59. //и др. по вкусу, например textarea для длинных строк
  60. }
  61. let creatingInputs = (data)=>{
  62. inputBox.innerHTML = '';
  63. for (let [key,value] of Object.entries(data)){
  64. console.log(key+":"+data[key]);
  65. let errorArr = [];
  66. let label = document.createElement('label');
  67. label.innerText=`${key}`;
  68. let inputCreations = inputCreators[value.constructor.name];
  69. let createdInput = inputCreations(key,value,(value)=>{
  70. if(typeof this.validators[key] === "function"){
  71. let result = this.validators[key](value,key,data,createdInput)
  72. if(result !== true){
  73. createdInput.style.backgroundColor = "red";
  74. okButton.disabled = true;
  75. errorArr.push(key);
  76. console.log(`when erors have some erors ${errorArr}`);
  77. } else {
  78. errorArr.pop(key);
  79. createdInput.style.backgroundColor = "";
  80. okButton.disabled = false;
  81. console.log(`when erors empty ${errorArr}`);
  82. data[key] = value;
  83. }
  84. }
  85. data[key]=value;
  86. })
  87. if(key[0]== '*'){
  88. label.classList.add('required');
  89. createdInput.onblur = function(){
  90. if(createdInput.value.includes('')){
  91. createdInput.style.borderColor = 'red';
  92. }
  93. }
  94. createdInput.onfocus = function(){
  95. createdInput.style.borderColor = '';
  96. }
  97. }
  98. label.appendChild(createdInput);
  99. inputBox.appendChild(label);
  100. inputCreations.oninput = () => {data[input.placeholder] = input.value;
  101. }
  102. }
  103. }
  104. creatingInputs(data);
  105. if (typeof okCallback === 'function'){
  106. formBody.appendChild(okButton);
  107. okButton.onclick = (e) => {
  108. console.log(this)
  109. this.okCallback(e)
  110. }
  111. }
  112. if (typeof cancelCallback === 'function'){
  113. formBody.appendChild(cancelButton);
  114. cancelButton.onclick = () => {
  115. /* let inputs = document.querySelectorAll('input');
  116. debugger;
  117. for(let [key,value] of Object.entries(firstInfo)){
  118. for(let i = 0; i < inputs.length; i++){
  119. inputs[i].value = value;
  120. console.log(inputs.value);
  121. }
  122. } */
  123. this.data = {...firstInfo};
  124. creatingInputs(this.data,this.okCallback,this.cancelCallback);
  125. cancelCallback();
  126. }
  127. }
  128. el.appendChild(formBody);
  129. this.okCallback = okCallback
  130. this.cancelCallback = cancelCallback
  131. this.data = data
  132. this.validators = {}
  133. }
  134. let form = new Form(formContainer, {
  135. name: 'Anakin',
  136. surname: 'Skywalker',
  137. married: true,
  138. password: '*',
  139. '*something': 'SOME thing',
  140. birthday: new Date((new Date).getTime() - 86400000 * 30*365)
  141. }, () => console.log('ok'),() => console.log('cancel') )
  142. form.okCallback = () => console.log('ok2')
  143. form.cancelCallback = () => console.log('cancel')
  144. form.validators.surname = (value, key, data, input) => value.length > 2 &&
  145. value[0].toUpperCase() == value[0] &&
  146. !value.includes(' ') ? true : 'Wrong name'
  147. form.validators.password = (value,key,data,input) => value.length >= 3 && value.length <=10 ? true : console.log('password not full')
  148. console.log(form);