source: trunk/client/modules/Elezioni/blocchi/privacy/cookiechoices.js@ 344

Last change on this file since 344 was 241, checked in by luc, 9 years ago

Aggiunta del widget Privacy e CookieLaw per l'informativa sui cookie - Aggiunto Tema Realistic - Aggiunto Tema Spectral

  • Property svn:executable set to *
File size: 6.0 KB
Line 
1/*
2 Copyright 2014 Google Inc. All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17(function(window) {
18
19 if (!!window.cookieChoices) {
20 return window.cookieChoices;
21 }
22
23 var document = window.document;
24 // IE8 does not support textContent, so we should fallback to innerText.
25 var supportsTextContent = 'textContent' in document.body;
26
27 var cookieChoices = (function() {
28
29 var cookieName = 'cook_law';
30 var cookieConsentId = 'cookieChoiceInfo';
31 var dismissLinkId = 'cookieChoiceDismiss';
32
33 function _createHeaderElement(cookieText, dismissText, linkText, linkHref) {
34 var butterBarStyles = 'position:fixed;width:100%;background-color:#eee;' +
35 'margin:0; left:0; top:0;padding:4px;z-index:1000;text-align:center;';
36
37 var cookieConsentElement = document.createElement('div');
38 cookieConsentElement.id = cookieConsentId;
39 cookieConsentElement.style.cssText = butterBarStyles;
40 cookieConsentElement.appendChild(_createConsentText(cookieText));
41
42 if (!!linkText && !!linkHref) {
43 cookieConsentElement.appendChild(_createInformationLink(linkText, linkHref));
44 }
45 cookieConsentElement.appendChild(_createDismissLink(dismissText));
46 return cookieConsentElement;
47 }
48
49 function _createDialogElement(cookieText, dismissText, linkText, linkHref) {
50 var glassStyle = 'position:fixed;width:100%;height:100%;z-index:999;' +
51 'top:0;left:0;opacity:0.5;filter:alpha(opacity=50);' +
52 'background-color:#ccc;';
53 var dialogStyle = 'z-index:1000;position:fixed;left:50%;top:50%';
54 var contentStyle = 'position:relative;left:-50%;margin-top:-25%;' +
55 'background-color:#fff;padding:20px;box-shadow:4px 4px 25px #888;';
56
57 var cookieConsentElement = document.createElement('div');
58 cookieConsentElement.id = cookieConsentId;
59
60 var glassPanel = document.createElement('div');
61 glassPanel.style.cssText = glassStyle;
62
63 var content = document.createElement('div');
64 content.style.cssText = contentStyle;
65
66 var dialog = document.createElement('div');
67 dialog.style.cssText = dialogStyle;
68
69 var dismissLink = _createDismissLink(dismissText);
70 dismissLink.style.display = 'block';
71 dismissLink.style.textAlign = 'right';
72 dismissLink.style.marginTop = '8px';
73
74 content.appendChild(_createConsentText(cookieText));
75 if (!!linkText && !!linkHref) {
76 content.appendChild(_createInformationLink(linkText, linkHref));
77 }
78 content.appendChild(dismissLink);
79 dialog.appendChild(content);
80 cookieConsentElement.appendChild(glassPanel);
81 cookieConsentElement.appendChild(dialog);
82 return cookieConsentElement;
83 }
84
85 function _setElementText(element, text) {
86 if (supportsTextContent) {
87 element.textContent = text;
88 } else {
89 element.innerText = text;
90 }
91 }
92
93 function _createConsentText(cookieText) {
94 var consentText = document.createElement('span');
95 _setElementText(consentText, cookieText);
96 return consentText;
97 }
98
99 function _createDismissLink(dismissText) {
100 var dismissLink = document.createElement('a');
101 _setElementText(dismissLink, dismissText);
102 dismissLink.id = dismissLinkId;
103 dismissLink.href = '#';
104 dismissLink.style.marginLeft = '24px';
105 return dismissLink;
106 }
107
108 function _createInformationLink(linkText, linkHref) {
109 var infoLink = document.createElement('a');
110 _setElementText(infoLink, linkText);
111 infoLink.href = linkHref;
112 infoLink.target = '_blank';
113 infoLink.style.marginLeft = '8px';
114 return infoLink;
115 }
116
117 function _dismissLinkClick() {
118 _saveUserPreference();
119 _removeCookieConsent();
120 return false;
121 }
122
123 function _showCookieConsent(cookieText, dismissText, linkText, linkHref, isDialog) {
124 if (_shouldDisplayConsent()) {
125 _removeCookieConsent();
126 var consentElement = (isDialog) ?
127 _createDialogElement(cookieText, dismissText, linkText, linkHref) :
128 _createHeaderElement(cookieText, dismissText, linkText, linkHref);
129 var fragment = document.createDocumentFragment();
130 fragment.appendChild(consentElement);
131 document.body.appendChild(fragment.cloneNode(true));
132 document.getElementById(dismissLinkId).onclick = _dismissLinkClick;
133 }
134 }
135
136 function showCookieConsentBar(cookieText, dismissText, linkText, linkHref) {
137 _showCookieConsent(cookieText, dismissText, linkText, linkHref, false);
138 }
139
140 function showCookieConsentDialog(cookieText, dismissText, linkText, linkHref) {
141 _showCookieConsent(cookieText, dismissText, linkText, linkHref, true);
142 }
143
144 function _removeCookieConsent() {
145 var cookieChoiceElement = document.getElementById(cookieConsentId);
146 if (cookieChoiceElement != null) {
147 cookieChoiceElement.parentNode.removeChild(cookieChoiceElement);
148 }
149 }
150
151 function _saveUserPreference() {
152 // Set the cookie expiry to one year after today.
153 var expiryDate = new Date();
154 expiryDate.setFullYear(expiryDate.getFullYear() + 1);
155 document.cookie = cookieName + '=y; expires=' + expiryDate.toGMTString();
156 }
157
158 function _shouldDisplayConsent() {
159 // Display the header only if the cookie has not been set.
160 return !document.cookie.match(new RegExp(cookieName + '=([^;]+)'));
161 }
162
163 var exports = {};
164 exports.showCookieConsentBar = showCookieConsentBar;
165 exports.showCookieConsentDialog = showCookieConsentDialog;
166 return exports;
167 })();
168
169 window.cookieChoices = cookieChoices;
170 return cookieChoices;
171})(this);
Note: See TracBrowser for help on using the repository browser.