|
/** |
|
* Created by Lasse on 01/09/15. |
|
*/ |
|
class CookieOptOut { |
|
disableStr:string; |
|
alreadyAccepted:string; |
|
message:string; |
|
optOutOfAnalytics:boolean; |
|
constructor(message:string = 'Websitet anvender cookies til at huske dine indstillinger og statistik.', optOutOfAnalytics:boolean = false) { |
|
this.message = message; |
|
this.optOutOfAnalytics = optOutOfAnalytics; |
|
if (optOutOfAnalytics) { |
|
// Must be printed in html.tpl.php like so: window.gaProperty = '<?php print variable_get('googleanalytics_account'); ?>'; |
|
var gaProperty = window.gaProperty; |
|
|
|
// Disable tracking if the opt-out cookie exists. |
|
this.disableStr = 'ga-disable-' + gaProperty; |
|
if (document.cookie.indexOf(this.disableStr + '=true') > -1) { |
|
window[this.disableStr] = true; |
|
} |
|
} |
|
|
|
this.alreadyAccepted = 'alreadyAcceptedCookies'; |
|
if (document.cookie.indexOf(this.alreadyAccepted + '=true') == -1 && document.cookie.indexOf(this.disableStr + '=true') == -1) { |
|
this.showCookieAcceptDialogue(); |
|
} |
|
} |
|
|
|
showCookieAcceptDialogue() { |
|
var analyticsOptoutButton:string = ""; |
|
if (this.optOutOfAnalytics) { |
|
analyticsOptoutButton = `<a class="cookie-dialogue-button cookie-dialogue-deny-analytics-button" href="#denyAnalytics">Nej tak</a>`; |
|
} |
|
jQuery(`<div class="cookie-dialogue"><span class="cookie-message">${this.message}</span><div class="cookie-dialogue-actions"><a class="cookie-dialogue-button cookie-dialogue-accept-button" href="#acceptCookie">Okay</a>${analyticsOptoutButton}</div></div>`).appendTo("body"); |
|
jQuery(".cookie-dialogue-accept-button").on("click", (event) => this.accept(event)); |
|
if (this.optOutOfAnalytics) { |
|
jQuery(".cookie-dialogue-deny-analytics-button").on("click", (event) => this.gaOptout(event)); |
|
} |
|
} |
|
|
|
accept(event:JQueryEventObject) { |
|
event.preventDefault(); |
|
document.cookie = this.alreadyAccepted + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/'; |
|
this.removeDialog(); |
|
return false; |
|
} |
|
|
|
gaOptout(event:JQueryEventObject) { |
|
event.preventDefault(); |
|
document.cookie = this.disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/'; |
|
window[this.disableStr] = true; |
|
this.removeDialog(); |
|
return false; |
|
} |
|
|
|
removeDialog() { |
|
jQuery(".cookie-dialogue").remove(); |
|
} |
|
} |