Skip to content

Commit

Permalink
Merge pull request #3 from CodiTramuntana/feat/new_functionalities
Browse files Browse the repository at this point in the history
New functionalities for EU Cookie Law
  • Loading branch information
laurajaime committed Feb 7, 2022
2 parents 4c685f4 + d1603c2 commit e026d00
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 180 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG
@@ -0,0 +1,9 @@
# CHANGELOG

## Version 0.2.0
- Adapt gem to GDPR.
- `link` params renamed to `policy_cookies`.
- Add locales in order to easy use.
- Now can deny cookies with deny button.
- New param `gtm_code` for Google Tag Manager code when user accept cookies.
- Bump [js-cookie](https://github.com/js-cookie/js-cookie) to v3.0.1.
11 changes: 6 additions & 5 deletions README.md
Expand Up @@ -34,8 +34,9 @@ $( document ).ready(function() {
Javascript optional parameters to select "#text_div" and add listener in "#button_confirm" to accept cookies:
```js
cookies.check({
div : '#div',
btn : '#button',
div : '#cookies',
btn : '#cookies .accept',
denyBtn: "#cookies .deny",
expires : 7 //Days
});
```
Expand All @@ -49,12 +50,12 @@ Add optional styles in "application.css":

Import optional view layout:
```erb
<%= render "cookies/alert", advice: "Cookies Text", link: "Link Advice", button: "Button" %>
<%= render "cookies/alert", advice: "Cookies Text", policy_cookies: "Link Advice", button: "Button", gtm_code: "GTM-XXXX" %>
```

Render optional parameters:
```erb
<%= render "cookies/alert", advice: "Cookies Text", link: link_to('Link Advice', root_path, target: '_blank'), button: image_tag('cross_cookies.svg') %>
<%= render "cookies/alert", advice: "Cookies Text", policy_cookies: link_to('Link Advice', root_path, target: '_blank'), button: image_tag('cross_cookies.svg'), gtm_code: "GTM-XXXX" %>
```

## Used Javascript Linter
Expand Down Expand Up @@ -89,7 +90,7 @@ module.exports = {
```

## Dependences included:
- [JavaScript Cookie v2.2.0](https://github.com/js-cookie/js-cookie)
- [JavaScript Cookie v3.0.1](https://github.com/js-cookie/js-cookie)

# Contributing

Expand Down
304 changes: 147 additions & 157 deletions app/assets/javascripts/cookiesalert/cookie.js
@@ -1,163 +1,153 @@
/*!
* JavaScript Cookie v2.2.0
* JavaScript Cookie v3.0.1
* https://github.com/js-cookie/js-cookie
*
* Copyright 2006, 2015 Klaus Hartl & Fagner Brack
* Released under the MIT license
*/
;(function (factory) {
var registeredInModuleLoader;
if (typeof define === 'function' && define.amd) {
define(factory);
registeredInModuleLoader = true;
}
if (typeof exports === 'object') {
module.exports = factory();
registeredInModuleLoader = true;
}
if (!registeredInModuleLoader) {
var OldCookies = window.Cookies;
var api = window.Cookies = factory();
api.noConflict = function () {
window.Cookies = OldCookies;
return api;
};
}
}(function () {
function extend () {
var i = 0;
var result = {};
for (; i < arguments.length; i++) {
var attributes = arguments[ i ];
for (var key in attributes) {
result[key] = attributes[key];
}
}
return result;
}

function decode (s) {
return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
}

function init (converter) {
function api() {}

function set (key, value, attributes) {
if (typeof document === 'undefined') {
return;
}

attributes = extend({
path: '/'
}, api.defaults, attributes);

if (typeof attributes.expires === 'number') {
attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5);
}

// We're using "expires" because "max-age" is not supported by IE
attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';

try {
var result = JSON.stringify(value);
if (/^[\{\[]/.test(result)) {
value = result;
}
} catch (e) {}

value = converter.write ?
converter.write(value, key) :
encodeURIComponent(String(value))
.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);

key = encodeURIComponent(String(key))
.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
.replace(/[\(\)]/g, escape);

var stringifiedAttributes = '';
for (var attributeName in attributes) {
if (!attributes[attributeName]) {
continue;
}
stringifiedAttributes += '; ' + attributeName;
if (attributes[attributeName] === true) {
continue;
}

// Considers RFC 6265 section 5.2:
// ...
// 3. If the remaining unparsed-attributes contains a %x3B (";")
// character:
// Consume the characters of the unparsed-attributes up to,
// not including, the first %x3B (";") character.
// ...
stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
}

return (document.cookie = key + '=' + value + stringifiedAttributes);
}

function get (key, json) {
if (typeof document === 'undefined') {
return;
}

var jar = {};
// To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all.
var cookies = document.cookie ? document.cookie.split('; ') : [];
var i = 0;

for (; i < cookies.length; i++) {
var parts = cookies[i].split('=');
var cookie = parts.slice(1).join('=');

if (!json && cookie.charAt(0) === '"') {
cookie = cookie.slice(1, -1);
}

try {
var name = decode(parts[0]);
cookie = (converter.read || converter)(cookie, name) ||
decode(cookie);

if (json) {
try {
cookie = JSON.parse(cookie);
} catch (e) {}
}

jar[name] = cookie;

if (key === name) {
break;
}
} catch (e) {}
}

return key ? jar[key] : jar;
}

api.set = set;
api.get = function (key) {
return get(key, false /* read as raw */);
};
api.getJSON = function (key) {
return get(key, true /* read as json */);
};
api.remove = function (key, attributes) {
set(key, '', extend(attributes, {
expires: -1
}));
};

api.defaults = {};

api.withConverter = init;

return api;
}

return init(function () {});
}));
;
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, (function () {
var current = global.Cookies;
var exports = global.Cookies = factory();
exports.noConflict = function () { global.Cookies = current; return exports; };
}()));
}(this, (function () { 'use strict';

/* eslint-disable no-var */
function assign (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
target[key] = source[key];
}
}
return target
}
/* eslint-enable no-var */

/* eslint-disable no-var */
var defaultConverter = {
read: function (value) {
if (value[0] === '"') {
value = value.slice(1, -1);
}
return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent)
},
write: function (value) {
return encodeURIComponent(value).replace(
/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,
decodeURIComponent
)
}
};
/* eslint-enable no-var */

/* eslint-disable no-var */

function init (converter, defaultAttributes) {
function set (key, value, attributes) {
if (typeof document === 'undefined') {
return
}

attributes = assign({}, defaultAttributes, attributes);

if (typeof attributes.expires === 'number') {
attributes.expires = new Date(Date.now() + attributes.expires * 864e5);
}
if (attributes.expires) {
attributes.expires = attributes.expires.toUTCString();
}

key = encodeURIComponent(key)
.replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent)
.replace(/[()]/g, escape);

var stringifiedAttributes = '';
for (var attributeName in attributes) {
if (!attributes[attributeName]) {
continue
}

stringifiedAttributes += '; ' + attributeName;

if (attributes[attributeName] === true) {
continue
}

// Considers RFC 6265 section 5.2:
// ...
// 3. If the remaining unparsed-attributes contains a %x3B (";")
// character:
// Consume the characters of the unparsed-attributes up to,
// not including, the first %x3B (";") character.
// ...
stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
}

return (document.cookie =
key + '=' + converter.write(value, key) + stringifiedAttributes)
}

function get (key) {
if (typeof document === 'undefined' || (arguments.length && !key)) {
return
}

// To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all.
var cookies = document.cookie ? document.cookie.split('; ') : [];
var jar = {};
for (var i = 0; i < cookies.length; i++) {
var parts = cookies[i].split('=');
var value = parts.slice(1).join('=');

try {
var foundKey = decodeURIComponent(parts[0]);
jar[foundKey] = converter.read(value, foundKey);

if (key === foundKey) {
break
}
} catch (e) {}
}

return key ? jar[key] : jar
}

return Object.create(
{
set: set,
get: get,
remove: function (key, attributes) {
set(
key,
'',
assign({}, attributes, {
expires: -1
})
);
},
withAttributes: function (attributes) {
return init(this.converter, assign({}, this.attributes, attributes))
},
withConverter: function (converter) {
return init(assign({}, this.converter, converter), this.attributes)
}
},
{
attributes: { value: Object.freeze(defaultAttributes) },
converter: { value: Object.freeze(converter) }
}
)
}

var api = init(defaultConverter, { path: '/' });
/* eslint-enable no-var */

return api;

})));

0 comments on commit e026d00

Please sign in to comment.