Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

144 строки
4.8 KiB

  1. import $ from "jquery";
  2. class IHKSocialBox {
  3. constructor(socialBox) {
  4. this.box = socialBox.addClass("initiated");
  5. this.checkHtmlFragmentCookiesAreSet(socialBox);
  6. this.checkIframe();
  7. }
  8. checkIframe() {
  9. setTimeout(() => {
  10. const iframe = this.box.find("iframe");
  11. if (iframe.length) {
  12. iframe.attr("tabindex", "-1");
  13. } else {
  14. this.checkIframe();
  15. }
  16. }, 1000);
  17. }
  18. checkHtmlFragmentCookiesAreSet(socialBox) {
  19. if (socialBox.data("useconsent")) {
  20. return;
  21. }
  22. let socialMediaProtectionText = socialBox.data("socialmediaprotection");
  23. let socialMediaType = socialBox.data("socialmediatype");
  24. let cookieBot = socialBox.data("usecookiebot");
  25. if (socialBox.find("iframe").length > 0) {
  26. let iframeElement = socialBox.find("iframe")[0];
  27. this.checkIframeCookieIsSet(socialMediaProtectionText, socialMediaType, cookieBot, iframeElement, socialBox);
  28. } else {
  29. this.checkEmbedCookieIsSet(socialMediaProtectionText, socialMediaType, cookieBot, socialBox);
  30. }
  31. }
  32. checkIframeCookieIsSet(socialMediaProtectionText, socialMediaType, cookieBot, iframeElement, socialBoxElement) {
  33. if (cookieBot) {
  34. $(iframeElement).data("cookieconsent", "marketing");
  35. }
  36. if (document.cookie.indexOf("social-media-consent-" + socialMediaType) === -1) {
  37. this.hideIframeElement(iframeElement);
  38. let textWithCheckbox =
  39. '<div class="social-media-protection-text-with-checkbox"> <input name="socialMediaType" type="checkbox" id=' +
  40. socialMediaType +
  41. '> <label for="' + socialMediaType + '">' +
  42. socialMediaProtectionText +
  43. "</label> </div>";
  44. $(socialBoxElement).append(textWithCheckbox);
  45. let elementsByClassName = this.box.find("[name='socialMediaType']")[0];
  46. elementsByClassName.addEventListener("change", () => this.setCookie(socialMediaType));
  47. }
  48. }
  49. checkEmbedCookieIsSet(socialMediaProtectionText, socialMediaType, cookieBot, socialBoxElement) {
  50. let scriptElements = socialBoxElement.find("script");
  51. let imageElements = socialBoxElement.find("img");
  52. if (cookieBot) {
  53. $(scriptElements).data("cookieconsent", "marketing");
  54. $(imageElements).data("cookieconsent", "marketing");
  55. }
  56. if (document.cookie.indexOf("social-media-consent-" + socialMediaType) === -1) {
  57. let textWithCheckbox =
  58. '<div class="social-media-protection-text-with-checkbox"> <input name="socialMediaType" id="' + socialMediaType + '" type="checkbox"> <label for="' + socialMediaType + '">' +
  59. socialMediaProtectionText +
  60. "</label> </div>";
  61. $(textWithCheckbox).appendTo(socialBoxElement);
  62. let elementsByClassName = this.box.find("[name='socialMediaType']")[0];
  63. elementsByClassName.addEventListener("change", () => this.setCookie(socialMediaType));
  64. }
  65. }
  66. hideIframeElement(iframeElement) {
  67. if (iframeElement) {
  68. iframeElement.src = "about:blank";
  69. $(iframeElement).hide();
  70. }
  71. }
  72. setCookie(socialMediaType) {
  73. // Anzahl der Tage für das Ablaufdatum des Cookies
  74. let expirationDays = 365;
  75. // Datum für das Ablaufdatum des Cookies berechnen
  76. let expirationDate = new Date();
  77. expirationDate.setDate(expirationDate.getDate() + expirationDays);
  78. // Checkbox wurde angeklickt, Cookie setzen
  79. document.cookie =
  80. "social-media-consent-" + socialMediaType + "=true; expires=" + expirationDate.toUTCString() + ";";
  81. // Finde alle Elemente mit dem selben socialMediaType
  82. let self = this;
  83. $(`[data-socialmediatype=${socialMediaType}]`).each(function () {
  84. let currentBox = $(this);
  85. currentBox.find(".social-media-protection-text-with-checkbox").hide();
  86. let scriptElements = currentBox.find("script");
  87. let imageElements = currentBox.find("img");
  88. let iframeElements = currentBox.find("iframe");
  89. self.showElements(scriptElements);
  90. self.showElements(imageElements);
  91. self.showElements(iframeElements);
  92. });
  93. }
  94. showElements(elements) {
  95. if (elements.length > 0) {
  96. for (let element of elements) {
  97. this.showElement(element);
  98. }
  99. }
  100. }
  101. showElement(htmlFragment) {
  102. let $element = $(htmlFragment);
  103. let elementType = $element.prop("tagName").toLowerCase();
  104. let dataSrc = $element.attr("data-src");
  105. if (typeof dataSrc === "undefined") {
  106. dataSrc = $element.attr("src");
  107. }
  108. if (elementType === "script") {
  109. $element.attr("src", dataSrc);
  110. $element.show();
  111. } else if (elementType === "img" || elementType === "iframe") {
  112. $element.attr("src", dataSrc);
  113. $element.show();
  114. }
  115. }
  116. }
  117. export default IHKSocialBox;
  118. $("body").on("ihk-init dynamic-component-loaded gfi-dynamic-init", function () {
  119. $(".social-box:not(.initiated)").each(function () {
  120. new IHKSocialBox($(this));
  121. });
  122. });