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

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