form_view.js 2.0 KB

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