-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.eu-cookie-consent.js
171 lines (152 loc) · 5.13 KB
/
jquery.eu-cookie-consent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* jQuery EU Cookie Consent v0.9
* https://github.com/dcorb/eu-cookie-consent
* (c) 2016 David Corbacho
* @license MIT
*/
;(function ($, undefined){
'use strict';
var _createLink = function(href, val, clas) {
return $("<a />", {
'href' : href,
'text' : val,
'class' : clas
})[0].outerHTML;
};
// Replace {{variable}}s with their values
// Inspired on http://stackoverflow.com/questions/14879866/javascript-templating-function-replace-string-and-dont-take-care-of-whitespace
var _tpl = function(str, tokens) {
return str.replace(/\{\{([^}]+)\}\}/g, function(wholeMatch, key) {
var subst = tokens[$.trim(key)];
return (subst === undefined ? wholeMatch : subst);
});
};
$.EUCookie = function(options) {
var $el,
now,
storage,
$doc = $(document),
once = true,
opts = {
message: 'This website uses cookies. By using this website we assume you are ok with this.',
acceptBtn: 'OK',
links: [],
theme: 'dark',
expiryDays: 365,
hideOnScroll: true,
hideOnAnyClick: true,
scrollDelay: 3000,
debug:false, // don't persist while debugging
beforeShowFn: function() {
// return true, to give permission to show.
return true;
},
afterShowFn: $.noop // You can use $(this) here to reference the container
};
$.extend(opts, options);
var self = {
init: function () {
if (now){
return; // already initialized
}
now = new Date();
if (typeof jQuery.fn.on === "undefined") { // Support for < jQuery 1.7
$.fn.on = jQuery.fn.bind;
$.fn.off = jQuery.fn.unbind;
}
// Feature detect + local reference
// Based on https://mathiasbynens.be/notes/localstorage-pattern
var exception;
var fail;
var uid = now;
try {
(storage = window.localStorage).setItem(uid, uid);
fail = storage.getItem(uid) != uid;
storage.removeItem(uid);
fail && (storage = false);
} catch (exception) {
exception = e;
}
if (self.valid() && opts.beforeShowFn(opts, storage, exception)) {
self.show();
$doc.on('click.EUc', '.EUc__btn', self.accept);
if (opts.hideOnScroll) {
// Give a bit of time to attach the handler. Sometimes scroll is triggered by browser at loading,
// because of scrolling down to an #anchor, for example
setTimeout(function () {
$doc.on('scroll.EUc', self.accept);
}, opts.scrollDelay);
}
if (opts.hideOnAnyClick) {
$doc.on('click.EUc', self.accept);
}
// React to localStorage event, when the cookieEU value has been stored in other tab/window
$(window).on('storage', function(e) {
if (e.originalEvent.key === 'EUcookie') {
self.accept();
}
});
}
},
show: function () {
var links = {
'link_1' : undefined,
'link_2' : undefined
};
$.each(links, function (index, value) {
if (typeof (opts.links[index]) !== 'undefined'){
links[index] = _createLink(opts.links.index, opts.links[index + '_text'], 'EUc__' + index);
}
});
var button = _createLink('#', opts.acceptBtn, 'EUc__btn');
var inner = button + '<div class="EUc__message">' + _tpl(opts.message, links) + '</div>';
$el = $('<div class="EUc initialEffect ' + opts.theme + '">' + inner + '</div>');
$el.appendTo('body');
// use proxy to provide useful context (this)
$.proxy(opts.afterShowFn, $el)();
},
valid: function () {
var ret = false;
if (storage) {
var stored = JSON.parse(storage.getItem('EUcookie'));
// check it has not expired
// Inspired on https://gist.github.com/porkeypop/1096149
ret = !stored || (stored && !!stored.stamp && (now.getTime() < stored.stamp));
}
return ret;
},
accept: function (e) {
// protect against multiple events.
if (once) {
once = false;
$doc.off('click.EUc scroll.EUc');
// use proxy to provide useful context (this)
self.hide();
self.persist();
}
},
hide: function() {
if ($el.length){
$el.removeClass('initialEffect');
// needed to trigger a reverse CSS keyframe animation
setTimeout(function(){
$el.addClass('hideBar');
},0);
setTimeout(function(){
$el.remove();
},1000); // animation ends at 500ms, give margin in case of custom CSS
}
},
persist: function () {
if (storage && !opts.debug) {
storage.setItem('EUcookie', JSON.stringify({stamp: now.toUTCString()}));
}
},
destroy: function () { // for external use. Destroys localStorage too!
$el.remove();
storage.removeItem('EUcookie');
}
};
return self;
}
})(jQuery, undefined);