|
|
@ -1,28 +1,29 @@ |
|
|
|
<tiny-consent> |
|
|
|
<div class="tiny-consent"> |
|
|
|
<div class="tiny-consent__banner"> |
|
|
|
<div class="tiny-consent__content" show={ state.hasCookie === false }> |
|
|
|
<div class={ state.isOpen === true ? 'tiny-consent__content show' : 'tiny-consent__content' }> |
|
|
|
<p> |
|
|
|
{ state.options.messages.content } |
|
|
|
</p> |
|
|
|
<ul class="tiny-consent__cookies"> |
|
|
|
<li each={ cookie in props.cookies }> |
|
|
|
<li class={ state.hasChanged && cookie.hasConsent === true ? 'tiny-consent__cookie' : 'tiny-consent__cookie show' } each={ (cookie, index) in props.cookies }> |
|
|
|
<div class="tiny-consent__cookie-content"> |
|
|
|
<p> |
|
|
|
<span if={ cookie.title } class="tiny-consent__cookie.title">{ cookie.title } - </span> |
|
|
|
{ cookie.content } |
|
|
|
</p> |
|
|
|
<p if={ cookie.names } class="tiny-consent__cookie-small"> |
|
|
|
|
|
|
|
<p if={ cookie.name } class="tiny-consent__cookie-name"> |
|
|
|
{ cookie.name } |
|
|
|
</p> |
|
|
|
</div> |
|
|
|
<div class="tiny-consent__cookie-decision"> |
|
|
|
<button if={ !cookie.essential } class={ getAgreeClasses(cookie) } onclick={ (event) => { handleAgree(event, cookie) }}> |
|
|
|
<button if={ !cookie.essential } class={ getAgreeClasses(cookie) } onclick={ (event) => { handleAgree(event, cookie, index) }}> |
|
|
|
{ state.options.messages.yes } |
|
|
|
</button> |
|
|
|
<button if={ !cookie.essential } class={ getRejectClasses(cookie) } onclick={ (event) => { handleReject(event, cookie) }}> |
|
|
|
<button if={ !cookie.essential } class={ getRejectClasses(cookie) } onclick={ (event) => { handleReject(event, cookie, index) }}> |
|
|
|
{ state.options.messages.no } |
|
|
|
</button> |
|
|
|
<button if={ cookie.essential } disabled={ isOk(cookie) === true } class="button button--ok tiny-consent__button" onclick={ (event) => { handleOk(event, cookie) }}> |
|
|
|
<button if={ cookie.essentialLast === true } disabled={ isOk() } class="button button--ok tiny-consent__button" onclick={ (event) => { handleOk(event, cookies) }}> |
|
|
|
{ state.options.messages.ok } |
|
|
|
</button> |
|
|
|
</div> |
|
|
@ -61,6 +62,7 @@ |
|
|
|
options: { |
|
|
|
cookie_name: 'tiny_consent', |
|
|
|
messages: { |
|
|
|
'session': 'LALAL', |
|
|
|
'content': 'TTATA', |
|
|
|
'yes': 'Yes', |
|
|
|
'no': 'No', |
|
|
@ -68,7 +70,8 @@ |
|
|
|
'close': 'Close' |
|
|
|
} |
|
|
|
}, |
|
|
|
hasCookie: false |
|
|
|
isOpen: false, |
|
|
|
hasChanged: false |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
@ -86,26 +89,19 @@ |
|
|
|
return consent; |
|
|
|
}, |
|
|
|
|
|
|
|
isOk(cookie) |
|
|
|
{ |
|
|
|
let data = this.getData(); |
|
|
|
let result = false; |
|
|
|
|
|
|
|
if (data[cookie.id] === true) { |
|
|
|
result = true; |
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* @param {[type]} cookie [description] |
|
|
|
* @return {[type]} [description] |
|
|
|
*/ |
|
|
|
getAgreeClasses(cookie) |
|
|
|
{ |
|
|
|
let data = this.getData(); |
|
|
|
let classes = [ |
|
|
|
'button', |
|
|
|
'button--agree', |
|
|
|
'tiny-consent__button' |
|
|
|
]; |
|
|
|
let data = this.getData(); |
|
|
|
|
|
|
|
if (data[cookie.id] === true) { |
|
|
|
classes.push('button--active'); |
|
|
@ -114,14 +110,19 @@ |
|
|
|
return classes.join(' '); |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* @param {[type]} cookie [description] |
|
|
|
* @return {[type]} [description] |
|
|
|
*/ |
|
|
|
getRejectClasses(cookie) |
|
|
|
{ |
|
|
|
let data = this.getData(); |
|
|
|
let classes = [ |
|
|
|
'button', |
|
|
|
'button--reject', |
|
|
|
'tiny-consent__button' |
|
|
|
]; |
|
|
|
let data = this.getData(); |
|
|
|
|
|
|
|
if (data[cookie.id] === false) { |
|
|
|
classes.push('button--active'); |
|
|
@ -130,6 +131,37 @@ |
|
|
|
return classes.join(' '); |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* @return {Boolean} [description] |
|
|
|
*/ |
|
|
|
isOk() |
|
|
|
{ |
|
|
|
let data = this.getData(); |
|
|
|
let result = true; |
|
|
|
|
|
|
|
this.props.cookies.forEach((cookie) => { |
|
|
|
if (data[cookie.id] !== true && cookie.essential === true) { |
|
|
|
result = false; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
return result; |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
onMounted() |
|
|
|
{ |
|
|
|
window.addEventListener('tiny-consent-open', () => { |
|
|
|
this.state.isOpen = true; |
|
|
|
this.state.hasChanged = false; |
|
|
|
this.update(); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* |
|
|
@ -144,19 +176,71 @@ |
|
|
|
this.state.options = Object.assign(this.state.options, this.props.options); |
|
|
|
} |
|
|
|
|
|
|
|
// getting essential first |
|
|
|
this.props.cookies.sort(function(a, b) { |
|
|
|
return a['essential'] !== true; |
|
|
|
}); |
|
|
|
|
|
|
|
// check if consent already has set |
|
|
|
if (data) { |
|
|
|
this.state.hasCookie = true; |
|
|
|
if (Object.entries(data).length === 0) { |
|
|
|
this.state.hasChanged = true; |
|
|
|
this.state.isOpen = true; |
|
|
|
} |
|
|
|
|
|
|
|
this.props.cookies.forEach((cookie, index) => { |
|
|
|
if (data[cookie.id] === true || data[cookie.id] === false) { |
|
|
|
if (data[cookie.id] === true && cookie.handleAgree) { |
|
|
|
cookie.handleAgree(); |
|
|
|
} else if (data[cookie.id] === false && cookie.handleReject) { |
|
|
|
cookie.handleReject(); |
|
|
|
} |
|
|
|
|
|
|
|
this.props.cookies[index].hasConsent = true; |
|
|
|
} else { |
|
|
|
this.state.hasChanged = true; |
|
|
|
this.state.isOpen = true; |
|
|
|
|
|
|
|
this.props.cookies[index].hasConsent = false; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
this.updateEssential(); |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* @return {[type]} |
|
|
|
*/ |
|
|
|
onBeforeUpdate() { |
|
|
|
|
|
|
|
// state |
|
|
|
if (this.state.hasChanged === true) { |
|
|
|
let hasToClose = true; |
|
|
|
|
|
|
|
this.props.cookies.forEach((cookie) => { |
|
|
|
if (data[cookie.id] === true || data[cookie.id] === false) { |
|
|
|
if (data[cookie.id] === true && cookie.handleAgree) { |
|
|
|
cookie.handleAgree(); |
|
|
|
} else if (data[cookie.id] === false && cookie.handleReject) { |
|
|
|
cookie.handleReject(); |
|
|
|
} |
|
|
|
if (cookie.hasConsent !== true) { |
|
|
|
hasToClose = false; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
if (hasToClose === true) { |
|
|
|
this.state.isOpen = false; |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* @return {[type]} [description] |
|
|
|
*/ |
|
|
|
updateEssential() |
|
|
|
{ |
|
|
|
// set true for last essential cookie where consent is not already set |
|
|
|
for (let i = (this.props.cookies.length - 1); i > 0; i--) { |
|
|
|
if ((this.props.cookies[i].hasConsent === false && this.props.cookies[i].essential === true) || (this.state.hasChanged === false && this.props.cookies[i].essential === true)) { |
|
|
|
this.props.cookies[i].essentialLast = true; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
@ -165,7 +249,7 @@ |
|
|
|
* @param {[type]} event [description] |
|
|
|
* @return {[type]} [description] |
|
|
|
*/ |
|
|
|
handleAgree(event, cookie) { |
|
|
|
handleAgree(event, cookie, index) { |
|
|
|
event.preventDefault(); |
|
|
|
|
|
|
|
let data = this.getData(); |
|
|
@ -176,6 +260,7 @@ |
|
|
|
|
|
|
|
data[cookie.id] = true; |
|
|
|
Cookie.set(this.state.options.cookie_name, data); |
|
|
|
this.props.cookies[index].hasConsent = true; |
|
|
|
|
|
|
|
this.update(); |
|
|
|
}, |
|
|
@ -185,7 +270,7 @@ |
|
|
|
* @param {[type]} event [description] |
|
|
|
* @return {[type]} [description] |
|
|
|
*/ |
|
|
|
handleReject(event, cookie) { |
|
|
|
handleReject(event, cookie, index) { |
|
|
|
event.preventDefault(); |
|
|
|
|
|
|
|
let data = this.getData(); |
|
|
@ -196,6 +281,7 @@ |
|
|
|
|
|
|
|
data[cookie.id] = false; |
|
|
|
Cookie.set(this.state.options.cookie_name, data); |
|
|
|
this.props.cookies[index].hasConsent = true; |
|
|
|
|
|
|
|
this.update(); |
|
|
|
}, |
|
|
@ -205,24 +291,34 @@ |
|
|
|
* @param {[type]} event [description] |
|
|
|
* @return {[type]} [description] |
|
|
|
*/ |
|
|
|
handleOk(event, cookie) { |
|
|
|
handleOk(event) { |
|
|
|
event.preventDefault(); |
|
|
|
|
|
|
|
let data = this.getData(); |
|
|
|
|
|
|
|
data[cookie.id] = true; |
|
|
|
this.props.cookies.forEach((cookie, index) => { |
|
|
|
if (cookie.essential === true) { |
|
|
|
data[cookie.id] = true; |
|
|
|
this.props.cookies[index].hasConsent = true; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
Cookie.set(this.state.options.cookie_name, data); |
|
|
|
this.updateEssential(); |
|
|
|
|
|
|
|
this.update(); |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* @param {[type]} event [description] |
|
|
|
* @return {[type]} [description] |
|
|
|
* @param {[type]} event |
|
|
|
* @return {[type]} |
|
|
|
*/ |
|
|
|
handleClose(event) { |
|
|
|
event.preventDefault(); |
|
|
|
|
|
|
|
this.state.isOpen = false; |
|
|
|
this.update(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|