Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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