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