Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

173 linhas
6.0 KiB

  1. import "jquery"
  2. import "JSON";
  3. import "../vendor/jquery.lazy";
  4. import "../vendor/jquery-ui";
  5. // import "blueimp-file-upload";
  6. import "imports-loader?define=>false!blueimp-file-upload";
  7. import "./webforms9neu/jquery-autocomplete";
  8. import "./webforms9neu/jquery-format";
  9. import "./webforms9neu/jquery-webforms";
  10. import "../modules/webforms"
  11. // Loging for the webforms in case of error
  12. document.addEventListener("mwf-ajax-finished", (eventData) => {
  13. if (eventData.detail && eventData.detail.$dest) {
  14. const errorDiv = $(eventData.detail.$dest).find("p:contains(There has been an error while processing the form)");
  15. if (errorDiv.length) {
  16. const dataSettings = $('[data-mwf-settings]').data();
  17. let errorText = "";
  18. if(dataSettings && dataSettings.mwfSettings) {
  19. errorText = "Frontend Form Url:" + dataSettings.mwfSettings.url
  20. }
  21. errorText = errorText + errorDiv.parent().parent().html();
  22. $.ajax({
  23. url: "/blueprint/servlet/webformslogging/log",
  24. type: "POST",
  25. contentType: "text/html",
  26. traditional: true,
  27. cache: false,
  28. data: errorText
  29. }).done(function (stringData) {
  30. console.log("loging response", stringData);
  31. }).fail(function (jqXHR) {
  32. console.log("Error logging failed", jqXHR.responseText);
  33. });
  34. }
  35. const dataSuccess = $(eventData.detail.$dest).data("succes-template");
  36. if(dataSuccess){
  37. $("#webformsDetailText").hide();
  38. $(".anchorlinks").hide();
  39. $(".text.initial").hide();
  40. }
  41. }
  42. });
  43. /**
  44. * JS for rendering webforms as webcomponent.
  45. * Mandatory Attributes added to the <webforms-component>:
  46. * - formId: webform id
  47. * - navigationId: parent navigation chanel
  48. *
  49. * Optional Attributes added to the <webforms-component>:
  50. * - sendSubmittedEvent (true / false|default): emits custom event("gfi-webforms-submited") after special action "Aufgabe" is submited
  51. * - loadCSS (true|default / false): should the component load own css
  52. * - optionalDomainUrl (String.Empty|default): in some cases component is unable to calculate its domain url.
  53. * Either set the domain url or id on the including script
  54. *
  55. * Optional Attribute added to te including script.
  56. * - webformsComponentScript (id): in some cases component is unable to calculate its domain url.
  57. * Either set the domain url or id on the including script.
  58. */
  59. class WebformsComponent extends HTMLElement {
  60. constructor() {
  61. super();
  62. }
  63. connectedCallback() {
  64. const optionalDomainUrl = this.getAttribute("optionalDomainUrl");
  65. const optionalScriptId = document.getElementById("webformsComponentScript");
  66. this.mandantUrl = "";
  67. if (optionalDomainUrl) {
  68. this.mandantUrl = optionalDomainUrl;
  69. } else if (optionalScriptId) {
  70. const scriptUrl = optionalScriptId.src;
  71. this.mandantUrl = new URL(scriptUrl).origin;
  72. } else {
  73. const scriptUrl = document.currentScript.src;
  74. this.mandantUrl = new URL(scriptUrl).origin;
  75. }
  76. this.prefillServerUrl = this.getAttribute("prefillServerUrl");
  77. if (this.prefillServerUrl) {
  78. this.prefillWebform();
  79. } else {
  80. this.writeWebformsHtml();
  81. }
  82. this.shouldTriggerSubmittedEvent = this.getAttribute("sendSubmittedEvent");
  83. if (this.shouldTriggerSubmittedEvent) {
  84. document.addEventListener("mwf-ajax-finished", (eventData) => {
  85. const aufgabeData = $(eventData.detail.$dest).data("aufgabedata");
  86. if (aufgabeData) {
  87. document.dispatchEvent(new CustomEvent("gfi-webforms-submitted", {detail: aufgabeData}))
  88. }
  89. });
  90. }
  91. }
  92. prefillWebform() {
  93. const t = this;
  94. const principleId = $.getUrlVar(window.location.search, "principle");
  95. const processId = $.getUrlVar(window.location.search, "processId");
  96. const contentId = this.getAttribute("formId");
  97. const prefillUrl = this.mandantUrl + "/blueprint/servlet/serviceport/aufgabe/prefill/" + principleId + "/" + processId + "/" + contentId;
  98. $.ajax({
  99. url: prefillUrl,
  100. type: "GET",
  101. traditional: true,
  102. cache: false,
  103. xhrFields: {withCredentials: true}
  104. }).done(function (stringData) {
  105. const queryString = $.param(t.parseDataToJson(stringData));
  106. t.writeWebformsHtml(queryString);
  107. }).fail(function (jqXHR) {
  108. console.log("Prefill request failed", jqXHR.responseText);
  109. t.writeWebformsHtml();
  110. })
  111. }
  112. parseDataToJson(stringData) {
  113. try {
  114. // TODO: schöner schreiben, dient nur für POC
  115. const anmerkungenObj = {};
  116. const parsedData = JSON.parse(stringData).variablen.filter((elem) => elem.name === "anmerkungen")[0];
  117. anmerkungenObj[parsedData.name] = parsedData.wert;
  118. return anmerkungenObj;
  119. } catch (e) {
  120. console.log("Aufgabedata parsen ist fehlgeschlagen", e);
  121. return {}
  122. }
  123. }
  124. writeWebformsHtml(prefillData) {
  125. const formId = this.getAttribute("formId");
  126. const navigationId = this.getAttribute("navigationId");
  127. const shouldLoadCss = this.getAttribute("loadCSS");
  128. if (shouldLoadCss !== "false") {
  129. $("head").append("<link rel='stylesheet' href='" + this.mandantUrl + "/blueprint/default/responsive/css/webforms-component.css' type='text/css' />");
  130. }
  131. if (prefillData) {
  132. this.innerHTML = `<div data-mwf-form='${formId}' data-mwf-settings='{
  133. "url":"${this.mandantUrl}/blueprint/servlet/mwf-form/${formId}?view=ajax&${prefillData}",
  134. "query":"navigationId=${navigationId}",
  135. "appendUrlVars":"true"
  136. }'></div>`;
  137. } else {
  138. this.innerHTML = `<div data-mwf-form='${formId}' data-mwf-settings='{
  139. "url":"${this.mandantUrl}/blueprint/servlet/mwf-form/${formId}?view=ajax",
  140. "query":"navigationId=${navigationId}",
  141. "appendUrlVars":"true"}'></div>`;
  142. }
  143. // webforms need to be initialised in case they have not noticed new elements.
  144. // Sometimes we get 2 same requests but it is ok
  145. $('[data-mwf-form]').webforms({});
  146. }
  147. }
  148. window.customElements.define('webforms-component', WebformsComponent);