-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.freeTheCookies.js
162 lines (137 loc) · 3.36 KB
/
jquery.freeTheCookies.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
//
// jQuery.freeTheCookies
// https://github.com/berarma/jquery-freeTheCookies
//
// Copyright (c) 2013 Bernat Arlandis
// Licensed under the GPLv2 license.
//
(function($, window, undefined) {
"use strict";
var
$window = $( window ),
initialized = false,
$block = $( "<div/>" ),
$close = $( "<div/>" ),
defaults = {
text: "This site needs <em>cookies</em> to offer its services. By using our services you're accepting the use we make of <em>cookies</em>. <a href='/cookie_policy'>More information</a>.",
style: {},
disabled: false,
cookieName: "cookie-consent",
cookieOptions: {
expires: 365,
path: "/"
},
speed: 300,
blockAttrs: {},
blockCss: {
position: "fixed",
left: "0",
right: "0",
top: "0",
padding: "1em",
border: "1px solid #ccc",
borderStyle: "solid none",
textAlign: "center",
font: "normal 0.9em/1.3 sans-serif",
backgroundColor: "rgba(240, 232, 160, 0.95)",
color: "#333",
zIndex: "9999"
},
closeHtml: "X",
closeAttrs: {
title: 'Close'
},
closeCss: {
"float": "right",
marginLeft: "1em",
fontWeight: "bold",
cursor: "pointer"
},
parentBlock: "body",
position: "append",
onUserAction: function( e ) {
return true;
}
},
settings,
$events = $( defaults ),
self = $.freeTheCookies = function( options ) {
var callback = null;
if ( options !== undefined ) {
if ( $.isFunction( options ) ) {
callback = options;
options = {};
}
if ( options.onConsent ) {
callback = options.onConsent;
delete options.onConsent;
}
}
if ( !initialized ) {
settings = $.extend( {}, defaults, options );
}
if ( settings.disabled ) {
initialized = true;
return false;
}
if ( callback ) {
$events.bind( "onConsent", callback);
}
if ( $.cookie( settings.cookieName ) ) {
$events.triggerHandler( "onConsent" );
initialized = true;
return true;
}
if ( initialized ) {
return false;
}
$block.attr( settings.blockAttrs ).html( settings.text ).css( $.extend( {}, settings.blockCss, settings.style ) );
$close.attr( settings.closeAttrs ).html( settings.closeHtml ).css( settings.closeCss );
$close.prependTo( $block );
$(function() {
var $parent = $( settings.parentBlock );
if ( settings.position == 'append' ) {
$block.appendTo( $parent );
} else {
$block.prependTo( $parent );
}
$block.hide();
$block.slideDown( settings.speed, function() {
$block.bind( "click", function( e ) {
e.stopPropagation();
});
$close.bind( "click", function() {
setCookiesFree();
});
$( "body" ).bind( "click.cookie-consent", function( e ) {
userAction( e );
});
var blockHeight = $block.height();
$window.bind( 'scroll.cookie-consent', function( e ) {
if ( window.pageYOffset >= blockHeight ) {
userAction( e );
$window.unbind( ".cookie-consent" );
}
});
});
});
initialized = true;
return false;
};
function setCookiesFree() {
$window.unbind( ".cookie-consent" );
$( "body" ).unbind( ".cookie-consent" );
$.cookie( settings.cookieName, "1", settings.cookieOptions);
$block.slideUp( settings.speed );
$events.triggerHandler( "onConsent" );
}
function userAction( e ) {
if ( settings.onUserAction( e ) ) {
setCookiesFree();
return true;
}
return false;
}
self.now = setCookiesFree;
self.settings = defaults;
})(jQuery, window);