You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

173 rivejä
5.2 KiB

  1. const $ = require('jquery');
  2. class Registration {
  3. constructor(form) {
  4. this.form = form;
  5. this.participantTemplate = this.form.find('.row.Participant').clone();
  6. this.initAddress();
  7. this.initParticipants();
  8. this.restoreParticipantsDataFromHistory();
  9. this.onFormSubmit();
  10. this.initPollBookingState(0);
  11. };
  12. initPollBookingState(pollCount) {
  13. var that = this;
  14. var bookingStateUrl = $("#confirmation").data("bookingstate");
  15. if (bookingStateUrl != undefined) {
  16. var loops = $("#confirmation").data("loops");
  17. var timeout = $("#confirmation").data("timeout");
  18. $.getJSON(bookingStateUrl, function (data) {
  19. if (data.jsonResponse.bookingstate === "true") {
  20. $("#confirmation-loading").hide();
  21. $("#confirmation-success").show();
  22. } else {
  23. if (pollCount < loops) {
  24. setTimeout(function () {
  25. that.initPollBookingState(++pollCount)
  26. }, timeout);
  27. } else {
  28. $("#confirmation-loading").hide();
  29. $("#confirmation-error").show();
  30. }
  31. }
  32. });
  33. }
  34. }
  35. initParticipants() {
  36. const addBtn = this.form.find('.add-participant');
  37. this.form.find('.row.Participant').remove();
  38. this.form.on('click', '.remove-participant', (e) => {
  39. e.preventDefault();
  40. $(e.currentTarget).closest('.row.Participant').remove();
  41. });
  42. addBtn.on('click', () => {
  43. this.participantTemplate.clone().insertBefore(addBtn.closest('.row'));
  44. })
  45. };
  46. saveParticipantDataInHistory() {
  47. try {
  48. const participantsHistoryState = {participants: []};
  49. const participants = $(".row.Participant");
  50. participants.each(function () {
  51. const element = $(this);
  52. element.wrapInner("<form/>");
  53. const wrappedForm = element.find("form");
  54. const formSerializedData = wrappedForm.serializeArray();
  55. participantsHistoryState.participants.push(formSerializedData);
  56. $(wrappedForm).children().insertBefore(wrappedForm);
  57. wrappedForm.remove();
  58. });
  59. history.replaceState(participantsHistoryState, document.title);
  60. } catch (e) {
  61. console.warn("Could not save Participant data in Navigation History. Error:", e);
  62. }
  63. }
  64. restoreParticipantsDataFromHistory() {
  65. try {
  66. if (history.state && history.state.participants) {
  67. history.state.participants.forEach((historyParticipant,index) => {
  68. const participantTemplateClone = this.participantTemplate.clone();
  69. const addBtn = this.form.find('.add-participant');
  70. historyParticipant.forEach((formField) => {
  71. const formFieldValue = formField["value"];
  72. const formFieldName = formField["name"];
  73. const field = participantTemplateClone
  74. .find("[name='" + formFieldName + "']");
  75. field
  76. .val(formFieldValue)
  77. .attr("value",formFieldValue)
  78. .change()
  79. .attr("id", null);
  80. participantTemplateClone.find("[type='hidden']").remove();
  81. });
  82. participantTemplateClone.insertBefore(addBtn.closest('.row'));
  83. })
  84. }
  85. } catch (e) {
  86. console.warn("Could not restore Participant data from Navigation History. Error:", e);
  87. }
  88. }
  89. onFormSubmit() {
  90. this.form.find(".submit").on("click", (e) => {
  91. const invalidFields = this.form.find(":invalid");
  92. if(invalidFields.length > 0){
  93. this.scrollToInvalidFieldOnSubmit();
  94. } else {
  95. e.preventDefault();
  96. this.saveParticipantDataInHistory();
  97. this.form.submit();
  98. }
  99. })
  100. }
  101. scrollToInvalidFieldOnSubmit() {
  102. const invalidFields = this.form.find(":invalid");
  103. $('html, body').animate({
  104. scrollTop: invalidFields.first().offset().top - 150
  105. }, 10);
  106. };
  107. initAddress() {
  108. const diffAddress = this.form.find(".rechnungAnPrivatAdresse");
  109. const addressToggler = this.form.find('#vst_abweichend_check');
  110. if (addressToggler.prop('checked')) {
  111. diffAddress.show();
  112. }
  113. addressToggler.on('change', (e) => {
  114. if ($(e.currentTarget).prop('checked')) {
  115. diffAddress.stop().slideDown(400, 'easeOutQuad');
  116. this.setMandatory(diffAddress, true);
  117. $('#vst_abweichend_val').val("false");
  118. } else {
  119. diffAddress.stop().slideUp(400, 'easeOutQuad');
  120. this.setMandatory(diffAddress, false);
  121. $('#vst_abweichend_val').val("true");
  122. }
  123. });
  124. this.setMandatory(diffAddress, diffAddress.is(':visible'));
  125. };
  126. setMandatory(wrapper, visible) {
  127. const inputs = wrapper.find('.mandatory');
  128. if (visible) {
  129. inputs.prop('required', 'true');
  130. } else {
  131. inputs.removeAttr('required');
  132. }
  133. };
  134. }
  135. $('body').on('ihk-init dynamic-component-loaded gfi-dynamic-init', function () {
  136. $('.vst-registration:not(.initialized)').each(function () {
  137. $('.vst-registration').addClass("initialized");
  138. new Registration($(this));
  139. });
  140. $('#confirmation:not(.initialized)').each(function () {
  141. $('#confirmation').addClass("initialized");
  142. new Registration($(this));
  143. });
  144. // TODO
  145. if ($('#captcha-wrapper:not(.initialized)').length > 0 ) {
  146. $('#captcha-wrapper').addClass("initialized");
  147. window.gfiWebforms.initCaptcha();
  148. }
  149. });