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