main.js 4.7 KB

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