form_view.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var builder;
  2. function openlink(menuitem){
  3. var menu = { name: menuitem };
  4. $.ajax({
  5. type: "PUT",
  6. url: window.name,
  7. contentType: "application/json",
  8. data: JSON.stringify(menu),
  9. success: function (data) {
  10. }
  11. });
  12. }
  13. //https://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-a-url
  14. function validURL(str) {
  15. var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
  16. '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
  17. '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
  18. '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
  19. '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
  20. '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
  21. return !!pattern.test(str);
  22. }
  23. function convertLinks(){
  24. $("#form a").each(function() {
  25. if(!validURL($(this).attr('href'))){
  26. $(this).attr('href', "javascript:openlink('" + $(this).attr('href') + "');")
  27. }
  28. })
  29. }
  30. $(document).ready(function(){
  31. var formElement = document.getElementById('form');
  32. var subJSON = document.getElementById('submission');
  33. var onForm = function(form) {
  34. subJSON.appendChild(document.createTextNode(JSON.stringify(form.submission, null, 4)));
  35. convertLinks();
  36. form.on('change', function() {
  37. subJSON.innerHTML = '';
  38. subJSON.appendChild(document.createTextNode(JSON.stringify(form.submission, null, 4)));
  39. });
  40. };
  41. $.ajax({
  42. type: "GET",
  43. url: (window.location.href + "/json").replace(/([^:]\/)\/+/g, "$1"),
  44. dataType: "json",
  45. success: function(json) {
  46. var builder = new Formio.FormBuilder(document.getElementById("builder"), json);
  47. Formio.createForm(formElement, builder.instance.form).then(onForm);
  48. },
  49. error: function (request, status, error) {
  50. alert(request.responseText + status + error);
  51. }
  52. });
  53. });