form.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <!DOCTYPE HTML>
  2. <html lang="pt-BR">
  3. <head>
  4. <title>{title}</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. <meta name="author" content="LInE" />
  7. <link rel="stylesheet" href="../assets/foundation.min.css">
  8. <style>
  9. body {
  10. margin-left: 2rem;
  11. margin-right: 2rem;
  12. margin-top:1.5rem;
  13. }
  14. .rotulo {
  15. font-weight: bold;
  16. background-color: #e4f1fa;
  17. margin: 2px;
  18. }
  19. .required::before {
  20. content: '* ';
  21. color: red;
  22. }
  23. .condicional {
  24. visibility: hidden;
  25. }
  26. .page {
  27. display: none;
  28. }
  29. #btn_submit {
  30. display: none;
  31. }
  32. legend, #current_page, #max_page {
  33. font-weight: bold;
  34. }
  35. #current_page {
  36. font-family: monospace;
  37. font-size: 1rem;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <div class="callout" >
  43. <p class="paragrafo lead text-justify">
  44. <span style="font-weight: bold;">{title}</span></br>{description}</br>
  45. <span class="required" style="color:red">Obrigatório</span>
  46. </p>
  47. </div>
  48. <div class="formulario">
  49. <form class="form" method="POST" action="../app/post.php">
  50. <input type="hidden" name="form" value="{form_id}" />
  51. <input type="hidden" name="uuid" value="" />
  52. <input type="hidden" name="page" value="1" />
  53. <input type="hidden" name="status" value="" />
  54. <div class="grid-container">
  55. <div class="grid-x grid-padding-x">
  56. <div class="large-5">
  57. Página <span id="current_page"></span> de <span id="max_page"></span>
  58. </div>
  59. {pages}
  60. <fieldset class="fieldset float-center">
  61. <button id="btn_back" class="button primary">Anterior</button>
  62. <button id="btn_next" class="button primary">Próximo</button>
  63. <button id="btn_submit" class="button success" type="submit">Finalizar</button>
  64. </fieldset>
  65. </div>
  66. </div>
  67. </form>
  68. </div>
  69. <script src="../assets/populatejs.min.js"></script>
  70. <script>
  71. (function () {
  72. const form = document.forms[0];
  73. const MAX_PAGE = document.querySelectorAll("fieldset.page").length;
  74. const KEY_NAME = form['form'].value;
  75. const STATUS = {
  76. PARTIAL: "PARTIAL",
  77. COMPLETED: "COMPLETED"
  78. }
  79. const btnBack = document.querySelector("#btn_back");
  80. const header = document.querySelector('.callout > p.paragrafo');
  81. const btnNext = document.querySelector("#btn_next");
  82. const btnSubmit = document.querySelector("#btn_submit");
  83. const currentPageSpan = document.querySelector("#current_page");
  84. const maxPageSpan = document.querySelector("#max_page");
  85. let page = 1;
  86. let isValid = false;
  87. const getLocalStorage = function () {
  88. const test = 'test';
  89. try {
  90. localStorage.setItem(test, test);
  91. localStorage.removeItem(test);
  92. return localStorage;
  93. } catch(e) {
  94. return {
  95. setItem: () => null,
  96. removeItem: () => null,
  97. getItem: () => null
  98. };
  99. }
  100. }
  101. const storePartialForm = function (formData) {
  102. const obj = {};
  103. formData.forEach((value, key) => {
  104. obj[key] = value;
  105. });
  106. getLocalStorage().setItem(KEY_NAME, JSON.stringify(obj));
  107. }
  108. const loadFromStorage = function () {
  109. const storage = getLocalStorage();
  110. const oldForm = storage.getItem(KEY_NAME);
  111. if (oldForm != null) {
  112. const formDict = JSON.parse(oldForm);
  113. populatejs(formDict);
  114. page = +formDict['page'];
  115. }
  116. }
  117. const sendPartialForm = function (formData) {
  118. const xhr = new XMLHttpRequest();
  119. xhr.open("POST", form.action);
  120. formData.set("status", STATUS.PARTIAL);
  121. xhr.send(formData);
  122. }
  123. const updateCurrentPageSpan = function () {
  124. currentPageSpan.innerHTML = page;
  125. form['page'].value = page;
  126. }
  127. const getPage = function (pageID) {
  128. const page = document.querySelector(`.page[data-page='${pageID}']`);
  129. return page;
  130. }
  131. const showPage = function (pageID) {
  132. const page = getPage(pageID);
  133. page.style.display = 'block';
  134. header.scrollIntoView({behavior: 'smooth', inline:'start'})
  135. }
  136. const hidePage = function (pageID) {
  137. const page = getPage(pageID);
  138. page.style.display = 'none';
  139. }
  140. const nextPage = function () {
  141. let next = page + 1;
  142. next = next > MAX_PAGE ? MAX_PAGE : next;
  143. hidePage(page);
  144. showPage(next);
  145. page = next;
  146. updateCurrentPageSpan()
  147. }
  148. const prevPage = function () {
  149. let last = page - 1;
  150. last = last < 1 ? 1 : last;
  151. hidePage(page);
  152. showPage(last);
  153. page = last;
  154. updateCurrentPageSpan()
  155. }
  156. // Manage back button
  157. btnBack.disabled = true;
  158. btnBack.addEventListener('click', function (event) {
  159. event.preventDefault();
  160. if (page === 1) {
  161. return;
  162. }
  163. prevPage();
  164. validateConditionals();
  165. validateFormPage();
  166. btnSubmit.style.display = 'none';
  167. btnNext.style.display = 'inline-block';
  168. if (page === 1)
  169. btnBack.disabled = true;
  170. })
  171. // Manage next button
  172. btnNext.disabled = true;
  173. btnNext.addEventListener('click', function (event) {
  174. event.preventDefault();
  175. if (!isValid) {
  176. return;
  177. } else if(page === MAX_PAGE) {
  178. btnNext.style.display = 'none';
  179. return;
  180. }
  181. nextPage();
  182. validateConditionals();
  183. validateFormPage();
  184. const formData = new FormData(form);
  185. sendPartialForm(formData);
  186. storePartialForm(formData);
  187. btnBack.disabled = false;
  188. if(page === MAX_PAGE) {
  189. btnSubmit.style.display = 'inline-block';
  190. btnNext.style.display = 'none';
  191. }
  192. });
  193. // validate form
  194. const validateFormPage = function () {
  195. const pageElement = getPage(page);
  196. const requiredList = Array.prototype.slice.call(pageElement.querySelectorAll('label.required'))
  197. .filter(e => {
  198. const style = window.getComputedStyle(e);
  199. return e.offsetParent != null && style.visibility != 'hidden';
  200. })
  201. .map(e => e.attributes.for.value);
  202. //requiredList.forEach(x => console.log(form[x]));
  203. isValid = !requiredList.some( field => {
  204. const value = form[field].value;
  205. return value == "" || value == null;
  206. });
  207. btnNext.disabled = !isValid;
  208. btnSubmit.disabled = !isValid;
  209. btnBack.disabled = page === 1;
  210. }
  211. // validate conditional fields
  212. const validateConditionals = function () {
  213. const pageElement = getPage(page);
  214. const conditionals = Array.prototype.slice.call(pageElement.querySelectorAll('.condicional'))
  215. .filter(e => {
  216. const style = window.getComputedStyle(e);
  217. return e.offsetParent != null;
  218. });
  219. //console.log(conditionals);
  220. conditionals.forEach(cond => {
  221. const field = cond.dataset.cond;
  222. const valuesList = cond.dataset.values.split("|");
  223. const value = form[field].value;
  224. const shouldEnable = valuesList.some( v => v == value);
  225. cond.style.visibility = shouldEnable ? 'visible' : 'hidden';
  226. });
  227. }
  228. // Hook document change event to validate the form and conditions
  229. document.addEventListener('change', () => {
  230. validateConditionals();
  231. validateFormPage();
  232. });
  233. // Intercept form submit
  234. form.addEventListener('submit' ,() => {
  235. getLocalStorage().removeItem(KEY_NAME);
  236. form["status"].value = STATUS.COMPLETED;
  237. });
  238. // set uuid
  239. const createUUID = function () {
  240. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
  241. const r = Math.random() * 16 | 0;
  242. const v = c === 'x' ? r : (r & 0x3 | 0x8);
  243. return v.toString(16);
  244. });
  245. }
  246. form['uuid'].value = createUUID();
  247. // set max page span
  248. maxPageSpan.innerHTML = MAX_PAGE;
  249. //load from stogage if available
  250. loadFromStorage();
  251. if(page === MAX_PAGE) {
  252. btnSubmit.style.display = 'inline-block';
  253. btnNext.style.display = 'none';
  254. }
  255. // Always show page 1
  256. showPage(page);
  257. updateCurrentPageSpan();
  258. validateConditionals();
  259. validateFormPage();
  260. })()
  261. </script>
  262. </body>
  263. </html>