MDL-65770 tool_policy: Update guest policy popup
[moodle.git] / admin / tool / policy / amd / src / jquery-eu-cookie-law-popup.js
CommitLineData
3f9b276b 1/**\r
fcdc8223 2 *\r
3f9b276b 3 * JQUERY EU COOKIE LAW POPUPS\r
fcdc8223
MM
4 * version 1.1.1\r
5 *\r
3f9b276b
SA
6 * Code on Github:\r
7 * https://github.com/wimagguc/jquery-eu-cookie-law-popup\r
fcdc8223 8 *\r
3f9b276b 9 * To see a live demo, go to:\r
fcdc8223
MM
10 * http://www.wimagguc.com/2018/05/gdpr-compliance-with-the-jquery-eu-cookie-law-plugin/\r
11 *\r
3f9b276b
SA
12 * by Richard Dancsi\r
13 * http://www.wimagguc.com/\r
fcdc8223 14 *\r
3f9b276b
SA
15 */\r
16\r
fcdc8223 17(function($) {\r
3f9b276b
SA
18\r
19// for ie9 doesn't support debug console >>>\r
20if (!window.console) window.console = {};\r
21if (!window.console.log) window.console.log = function () { };\r
22// ^^^\r
23\r
24$.fn.euCookieLawPopup = (function() {\r
25\r
26 var _self = this;\r
27\r
28 ///////////////////////////////////////////////////////////////////////////////////////////////\r
29 // PARAMETERS (MODIFY THIS PART) //////////////////////////////////////////////////////////////\r
30 _self.params = {\r
fcdc8223 31 cookiePolicyUrl : '/?cookie-policy',\r
3f9b276b
SA
32 popupPosition : 'top',\r
33 colorStyle : 'default',\r
34 compactStyle : false,\r
35 popupTitle : 'This website is using cookies',\r
36 popupText : 'We use cookies to ensure that we give you the best experience on our website. If you continue without changing your settings, we\'ll assume that you are happy to receive all cookies on this website.',\r
37 buttonContinueTitle : 'Continue',\r
38 buttonLearnmoreTitle : 'Learn more',\r
39 buttonLearnmoreOpenInNewWindow : true,\r
40 agreementExpiresInDays : 30,\r
41 autoAcceptCookiePolicy : false,\r
42 htmlMarkup : null\r
43 };\r
44\r
45 ///////////////////////////////////////////////////////////////////////////////////////////////\r
46 // VARIABLES USED BY THE FUNCTION (DON'T MODIFY THIS PART) ////////////////////////////////////\r
47 _self.vars = {\r
48 INITIALISED : false,\r
49 HTML_MARKUP : null,\r
50 COOKIE_NAME : 'EU_COOKIE_LAW_CONSENT'\r
51 };\r
52\r
53 ///////////////////////////////////////////////////////////////////////////////////////////////\r
54 // PRIVATE FUNCTIONS FOR MANIPULATING DATA ////////////////////////////////////////////////////\r
55\r
56 // Overwrite default parameters if any of those is present\r
57 var parseParameters = function(object, markup, settings) {\r
58\r
59 if (object) {\r
60 var className = $(object).attr('class') ? $(object).attr('class') : '';\r
61 if (className.indexOf('eupopup-top') > -1) {\r
62 _self.params.popupPosition = 'top';\r
63 }\r
64 else if (className.indexOf('eupopup-fixedtop') > -1) {\r
65 _self.params.popupPosition = 'fixedtop';\r
66 }\r
67 else if (className.indexOf('eupopup-bottomright') > -1) {\r
68 _self.params.popupPosition = 'bottomright';\r
69 }\r
70 else if (className.indexOf('eupopup-bottomleft') > -1) {\r
71 _self.params.popupPosition = 'bottomleft';\r
72 }\r
73 else if (className.indexOf('eupopup-bottom') > -1) {\r
74 _self.params.popupPosition = 'bottom';\r
75 }\r
76 else if (className.indexOf('eupopup-block') > -1) {\r
77 _self.params.popupPosition = 'block';\r
78 }\r
79 if (className.indexOf('eupopup-color-default') > -1) {\r
80 _self.params.colorStyle = 'default';\r
81 }\r
82 else if (className.indexOf('eupopup-color-inverse') > -1) {\r
83 _self.params.colorStyle = 'inverse';\r
84 }\r
85 if (className.indexOf('eupopup-style-compact') > -1) {\r
86 _self.params.compactStyle = true;\r
87 }\r
88 }\r
89\r
90 if (markup) {\r
91 _self.params.htmlMarkup = markup;\r
92 }\r
93\r
94 if (settings) {\r
95 if (typeof settings.cookiePolicyUrl !== 'undefined') {\r
96 _self.params.cookiePolicyUrl = settings.cookiePolicyUrl;\r
97 }\r
98 if (typeof settings.popupPosition !== 'undefined') {\r
99 _self.params.popupPosition = settings.popupPosition;\r
100 }\r
101 if (typeof settings.colorStyle !== 'undefined') {\r
102 _self.params.colorStyle = settings.colorStyle;\r
103 }\r
104 if (typeof settings.popupTitle !== 'undefined') {\r
105 _self.params.popupTitle = settings.popupTitle;\r
106 }\r
107 if (typeof settings.popupText !== 'undefined') {\r
108 _self.params.popupText = settings.popupText;\r
109 }\r
110 if (typeof settings.buttonContinueTitle !== 'undefined') {\r
111 _self.params.buttonContinueTitle = settings.buttonContinueTitle;\r
112 }\r
113 if (typeof settings.buttonLearnmoreTitle !== 'undefined') {\r
114 _self.params.buttonLearnmoreTitle = settings.buttonLearnmoreTitle;\r
115 }\r
116 if (typeof settings.buttonLearnmoreOpenInNewWindow !== 'undefined') {\r
117 _self.params.buttonLearnmoreOpenInNewWindow = settings.buttonLearnmoreOpenInNewWindow;\r
118 }\r
119 if (typeof settings.agreementExpiresInDays !== 'undefined') {\r
120 _self.params.agreementExpiresInDays = settings.agreementExpiresInDays;\r
121 }\r
122 if (typeof settings.autoAcceptCookiePolicy !== 'undefined') {\r
123 _self.params.autoAcceptCookiePolicy = settings.autoAcceptCookiePolicy;\r
124 }\r
125 if (typeof settings.htmlMarkup !== 'undefined') {\r
126 _self.params.htmlMarkup = settings.htmlMarkup;\r
127 }\r
128 }\r
129\r
130 };\r
131\r
132 var createHtmlMarkup = function() {\r
133\r
134 if (_self.params.htmlMarkup) {\r
135 return _self.params.htmlMarkup;\r
136 }\r
137\r
fcdc8223
MM
138 var html =\r
139 '<div class="eupopup-container' +\r
140 ' eupopup-container-' + _self.params.popupPosition +\r
141 (_self.params.compactStyle ? ' eupopup-style-compact' : '') +\r
3f9b276b
SA
142 ' eupopup-color-' + _self.params.colorStyle + '">' +\r
143 '<div class="eupopup-head">' + _self.params.popupTitle + '</div>' +\r
144 '<div class="eupopup-body">' + _self.params.popupText + '</div>' +\r
145 '<div class="eupopup-buttons">' +\r
146 '<a href="#" class="eupopup-button eupopup-button_1">' + _self.params.buttonContinueTitle + '</a>' +\r
147 '<a href="' + _self.params.cookiePolicyUrl + '"' +\r
148 (_self.params.buttonLearnmoreOpenInNewWindow ? ' target=_blank ' : '') +\r
149 ' class="eupopup-button eupopup-button_2">' + _self.params.buttonLearnmoreTitle + '</a>' +\r
150 '<div class="clearfix"></div>' +\r
151 '</div>' +\r
152 '<a href="#" class="eupopup-closebutton">x</a>' +\r
153 '</div>';\r
154\r
155 return html;\r
156 };\r
157\r
158 // Storing the consent in a cookie\r
159 var setUserAcceptsCookies = function(consent) {\r
160 var d = new Date();\r
161 var expiresInDays = _self.params.agreementExpiresInDays * 24 * 60 * 60 * 1000;\r
162 d.setTime( d.getTime() + expiresInDays );\r
163 var expires = "expires=" + d.toGMTString();\r
164 document.cookie = _self.vars.COOKIE_NAME + '=' + consent + "; " + expires + ";path=/";\r
165\r
166 $(document).trigger("user_cookie_consent_changed", {'consent' : consent});\r
167 };\r
168\r
169 // Let's see if we have a consent cookie already\r
170 var userAlreadyAcceptedCookies = function() {\r
171 var userAcceptedCookies = false;\r
172 var cookies = document.cookie.split(";");\r
173 for (var i = 0; i < cookies.length; i++) {\r
174 var c = cookies[i].trim();\r
175 if (c.indexOf(_self.vars.COOKIE_NAME) == 0) {\r
176 userAcceptedCookies = c.substring(_self.vars.COOKIE_NAME.length + 1, c.length);\r
177 }\r
178 }\r
179\r
180 return userAcceptedCookies;\r
181 };\r
fcdc8223 182\r
3f9b276b
SA
183 var hideContainer = function() {\r
184 // $('.eupopup-container').slideUp(200);\r
185 $('.eupopup-container').animate({\r
186 opacity: 0,\r
187 height: 0\r
188 }, 200, function() {\r
189 $('.eupopup-container').hide(0);\r
190 });\r
191 };\r
192\r
193 ///////////////////////////////////////////////////////////////////////////////////////////////\r
194 // PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////////\r
195 var publicfunc = {\r
196\r
197 // INITIALIZE EU COOKIE LAW POPUP /////////////////////////////////////////////////////////\r
198 init : function(settings) {\r
199\r
200 parseParameters(\r
201 $(".eupopup").first(),\r
202 $(".eupopup-markup").html(),\r
203 settings);\r
204\r
205 // No need to display this if user already accepted the policy\r
206 if (userAlreadyAcceptedCookies()) {\r
fcdc8223 207 $(document).trigger("user_cookie_already_accepted", {'consent': true});\r
3f9b276b
SA
208 return;\r
209 }\r
210\r
211 // We should initialise only once\r
212 if (_self.vars.INITIALISED) {\r
213 return;\r
214 }\r
215 _self.vars.INITIALISED = true;\r
216\r
217 // Markup and event listeners >>>\r
218 _self.vars.HTML_MARKUP = createHtmlMarkup();\r
219\r
220 if ($('.eupopup-block').length > 0) {\r
221 $('.eupopup-block').append(_self.vars.HTML_MARKUP);\r
222 } else {\r
223 $('BODY').append(_self.vars.HTML_MARKUP);\r
224 }\r
225\r
226 $('.eupopup-button_1').click(function() {\r
227 setUserAcceptsCookies(true);\r
228 hideContainer();\r
229 return false;\r
230 });\r
231 $('.eupopup-closebutton').click(function() {\r
232 setUserAcceptsCookies(true);\r
233 hideContainer();\r
234 return false;\r
235 });\r
236 // ^^^ Markup and event listeners\r
237\r
238 // Ready to start!\r
239 $('.eupopup-container').show();\r
240\r
fcdc8223 241 // In case it's alright to just display the message once\r
3f9b276b
SA
242 if (_self.params.autoAcceptCookiePolicy) {\r
243 setUserAcceptsCookies(true);\r
244 }\r
245\r
246 }\r
247\r
248 };\r
249\r
250 return publicfunc;\r
251});\r
252\r
fcdc8223
MM
253$(document).ready( function() {\r
254 if ($(".eupopup").length > 0) {\r
255 $(document).euCookieLawPopup().init({\r
256 'info' : 'YOU_CAN_ADD_MORE_SETTINGS_HERE',\r
257 'popupTitle' : 'This website is using cookies. ',\r
258 'popupText' : 'We use them to give you the best experience. If you continue using our website, we\'ll assume that you are happy to receive all cookies on this website.'\r
259 });\r
260 }\r
3f9b276b 261});\r
fcdc8223
MM
262\r
263$(document).bind("user_cookie_consent_changed", function(event, object) {\r
264 console.log("User cookie consent changed: " + $(object).attr('consent') );\r
265});\r
266\r
267}(jQuery));\r