Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. (function(window) {
  2.  
  3. if (!!window.cookieChoices) {
  4. return window.cookieChoices;
  5. }
  6.  
  7. var document = window.document;
  8. // IE8 does not support textContent, so we should fallback to innerText.
  9. var supportsTextContent = 'textContent' in document.body;
  10.  
  11. var cookieChoices = (function() {
  12.  
  13. var cookieName = 'displayCookieConsent';
  14. var cookieConsentId = 'cookieChoiceInfo';
  15. var dismissLinkId = 'cookieChoiceDismiss';
  16.  
  17. function _createHeaderElement(cookieText, dismissText, linkText, linkHref) {
  18. var butterBarStyles = 'position:fixed;width:100%;background-color:#000000;color:#ffffff;' +
  19. 'margin:0; left:0; top:0;padding:4px;z-index:1000;text-align:center;';
  20.  
  21. var cookieConsentElement = document.createElement('div');
  22. cookieConsentElement.id = cookieConsentId;
  23. cookieConsentElement.style.cssText = butterBarStyles;
  24. cookieConsentElement.appendChild(_createConsentText(cookieText));
  25.  
  26. if (!!linkText && !!linkHref) {
  27. cookieConsentElement.appendChild(_createInformationLink(linkText, linkHref));
  28. }
  29. cookieConsentElement.appendChild(_createDismissLink(dismissText));
  30. return cookieConsentElement;
  31. }
  32.  
  33. function _createDialogElement(cookieText, dismissText, linkText, linkHref) {
  34. var glassStyle = 'position:fixed;width:100%;height:100%;z-index:999;' +
  35. 'top:0;left:0;opacity:0.5;filter:alpha(opacity=50);' +
  36. 'background-color:#ccc;';
  37. var dialogStyle = 'z-index:1000;position:fixed;left:50%;top:50%';
  38. var contentStyle = 'position:relative;left:-50%;margin-top:-25%;' +
  39. 'background-color:#fff;padding:20px;box-shadow:4px 4px 25px #888;';
  40.  
  41. var cookieConsentElement = document.createElement('div');
  42. cookieConsentElement.id = cookieConsentId;
  43.  
  44. var glassPanel = document.createElement('div');
  45. glassPanel.style.cssText = glassStyle;
  46.  
  47. var content = document.createElement('div');
  48. content.style.cssText = contentStyle;
  49.  
  50. var dialog = document.createElement('div');
  51. dialog.style.cssText = dialogStyle;
  52.  
  53. var dismissLink = _createDismissLink(dismissText);
  54. dismissLink.style.display = 'block';
  55. dismissLink.style.textAlign = 'right';
  56. dismissLink.style.marginTop = '8px';
  57.  
  58. content.appendChild(_createConsentText(cookieText));
  59. if (!!linkText && !!linkHref) {
  60. content.appendChild(_createInformationLink(linkText, linkHref));
  61. }
  62. content.appendChild(dismissLink);
  63. dialog.appendChild(content);
  64. cookieConsentElement.appendChild(glassPanel);
  65. cookieConsentElement.appendChild(dialog);
  66. return cookieConsentElement;
  67. }
  68.  
  69. function _setElementText(element, text) {
  70. if (supportsTextContent) {
  71. element.textContent = text;
  72. } else {
  73. element.innerText = text;
  74. }
  75. }
  76.  
  77. function _createConsentText(cookieText) {
  78. var consentText = document.createElement('span');
  79. _setElementText(consentText, cookieText);
  80. return consentText;
  81. }
  82.  
  83. function _createDismissLink(dismissText) {
  84. var dismissLink = document.createElement('a');
  85. _setElementText(dismissLink, dismissText);
  86. dismissLink.id = dismissLinkId;
  87. dismissLink.href = '#';
  88. dismissLink.style.marginLeft = '24px';
  89. return dismissLink;
  90. }
  91.  
  92. function _createInformationLink(linkText, linkHref) {
  93. var infoLink = document.createElement('a');
  94. _setElementText(infoLink, linkText);
  95. infoLink.href = linkHref;
  96. infoLink.target = '_blank';
  97. infoLink.style.marginLeft = '8px';
  98. return infoLink;
  99. }
  100.  
  101. function _dismissLinkClick() {
  102. _saveUserPreference();
  103. _removeCookieConsent();
  104. return false;
  105. }
  106.  
  107. function _showCookieConsent(cookieText, dismissText, linkText, linkHref, isDialog) {
  108. if (_shouldDisplayConsent()) {
  109. _removeCookieConsent();
  110. var consentElement = (isDialog) ?
  111. _createDialogElement(cookieText, dismissText, linkText, linkHref) :
  112. _createHeaderElement(cookieText, dismissText, linkText, linkHref);
  113. var fragment = document.createDocumentFragment();
  114. fragment.appendChild(consentElement);
  115. document.body.appendChild(fragment.cloneNode(true));
  116. document.getElementById(dismissLinkId).onclick = _dismissLinkClick;
  117. }
  118. }
  119.  
  120. function showCookieConsentBar(cookieText, dismissText, linkText, linkHref) {
  121. _showCookieConsent(cookieText, dismissText, linkText, linkHref, false);
  122. }
  123.  
  124. function showCookieConsentDialog(cookieText, dismissText, linkText, linkHref) {
  125. _showCookieConsent(cookieText, dismissText, linkText, linkHref, true);
  126. }
  127.  
  128. function _removeCookieConsent() {
  129. var cookieChoiceElement = document.getElementById(cookieConsentId);
  130. if (cookieChoiceElement != null) {
  131. cookieChoiceElement.parentNode.removeChild(cookieChoiceElement);
  132. }
  133. }
  134.  
  135. function _saveUserPreference() {
  136. // Set the cookie expiry to one year after today.
  137. var expiryDate = new Date();
  138. expiryDate.setFullYear(expiryDate.getFullYear() + 1);
  139. document.cookie = cookieName + '=y; path=/; expires=' + expiryDate.toGMTString();
  140. }
  141.  
  142. function _shouldDisplayConsent() {
  143. // Display the header only if the cookie has not been set.
  144. return !document.cookie.match(new RegExp(cookieName + '=([^;]+)'));
  145. }
  146.  
  147. var exports = {};
  148. exports.showCookieConsentBar = showCookieConsentBar;
  149. exports.showCookieConsentDialog = showCookieConsentDialog;
  150. return exports;
  151. })();
  152.  
  153. window.cookieChoices = cookieChoices;
  154. return cookieChoices;
  155. })(this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement