cookie-service-banner
Version:
[](https://badge.fury.io/js/cookie-service-banner) [](https://travis-ci.org/liyokuna/cookie-consent) [![depen
305 lines (296 loc) • 13.2 kB
JavaScript
import { InjectionToken, ɵɵdefineInjectable, Injectable, Inject, EventEmitter, Output, Component, NgModule } from '@angular/core';
import { __decorate, __metadata, __param } from 'tslib';
import { of } from 'rxjs';
import { CommonModule } from '@angular/common';
const CookieConfigService = new InjectionToken('CookieConfig');
let CookieServiceService = class CookieServiceService {
constructor() {
this.GA_COOKIE_NAMES = ['__utma', '__utmb', '__utmc', '__utmz', '_ga', '_gid', '_gat', '_gat_gtag'];
}
setCookie(name, val, domain) {
const date = new Date();
const value = val;
// Set it expire in 395 days, compliance with Cookie Laws in EU
date.setTime(date.getTime() + (395 * 24 * 60 * 60 * 1000));
document.cookie = name + ' = ' + value + '; expires=' + date.toUTCString() + '; path=/';
}
setCookieWithString(name, val, domain) {
const date = new Date();
const value = val;
// Set it expire in 395 days, compliance with Cookie Laws in EU
date.setTime(date.getTime() + (395 * 24 * 60 * 60 * 1000));
document.cookie = name + ' = ' + value + '; expires=' + date.toUTCString() + '; path=/';
}
getCookie(name) {
const value = '; ' + document.cookie;
const parts = value.split('; ' + name + '=');
if (parts.length === 2) {
return parts.pop().split(';').shift();
}
}
deleteCookie(name) {
const date = new Date();
date.setTime(date.getTime() + (-1 * 24 * 60 * 60 * 1000));
document.cookie = name + '=; expires=' + date.toUTCString() + '; path=/';
}
rejectCookie(gaId) {
// disable GA
this.deleteCookie(`ga-disable-${gaId}`);
this.deleteCookie('consent');
window[`ga-disable-${gaId}`] = true;
window[`_gat_gtag_${gaId}`] = false;
this.GA_COOKIE_NAMES.forEach((cookieName) => {
this.deleteCookie(cookieName);
});
}
isAnalytics(str) {
return (/^ua-\d{4,9}-\d{1,4}$/i).test(str.toString());
}
};
CookieServiceService.ɵprov = ɵɵdefineInjectable({ factory: function CookieServiceService_Factory() { return new CookieServiceService(); }, token: CookieServiceService, providedIn: "root" });
CookieServiceService = __decorate([
Injectable({
providedIn: 'root'
}),
__metadata("design:paramtypes", [])
], CookieServiceService);
let ConfigService = class ConfigService {
constructor(config) {
this.config = config;
}
getConfig() {
return of(this.config);
}
getHeader() {
return of(this.config.header.title);
}
getHeaderColor() {
return of(this.config.header.color);
}
getHeaderBackgroundColor() {
return of(this.config.header.bcolor);
}
getDomain() {
return of(this.config.header.domain);
}
getGA_id() {
return of(this.config.header.ga_id);
}
getMessage() {
return of(this.config.header.message);
}
getAcceptEnable() {
return of(this.config.acceptButton.enable);
}
getAcceptMessage() {
return of(this.config.acceptButton.accept);
}
getAcceptColor() {
return of(this.config.acceptButton.color);
}
getAcceptBackgroundColor() {
return of(this.config.acceptButton.bcolor);
}
getDenyEnable() {
return of(this.config.declineButton.enable);
}
getDenyMessage() {
return of(this.config.declineButton.deny);
}
getDenyColor() {
return of(this.config.declineButton.color);
}
getDenyBackgroundColor() {
return of(this.config.declineButton.bcolor);
}
getAllowEnable() {
return of(this.config.allowButton.enable);
}
getAllowMessage() {
return of(this.config.allowButton.allow);
}
getAllowColor() {
return of(this.config.allowButton.color);
}
getAllowBackgroundColor() {
return of(this.config.allowButton.bcolor);
}
getLinkButton() {
return of(this.config.learnMoreLink);
}
getLinkMessage() {
return of(this.config.learnMoreLink.learnMore);
}
getLinkLink() {
return of(this.config.learnMoreLink.link);
}
getLinkEnable() {
return of(this.config.learnMoreLink.enable);
}
getLinkColor() {
return of(this.config.learnMoreLink.color);
}
getReviewEnable() {
return of(this.config.review.enable);
}
getReviewMessage() {
return of(this.config.review.message);
}
getReviewColor() {
return of(this.config.review.color);
}
getReviewBackgroundColor() {
return of(this.config.review.bcolor);
}
};
ConfigService.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Inject, args: [CookieConfigService,] }] }
];
ConfigService = __decorate([
Injectable(),
__param(0, Inject(CookieConfigService)),
__metadata("design:paramtypes", [Object])
], ConfigService);
let CookieServiceComponent = class CookieServiceComponent {
constructor(cookiemanager, cookieconfig) {
this.cookiemanager = cookiemanager;
this.cookieconfig = cookieconfig;
this.isOpened = new EventEmitter();
}
ngOnInit() {
this.getValues();
this.getColors();
if (!this.cookiemanager.isAnalytics(this.GA_ID)) {
this.GA_ID = '0000';
console.log('Your Google Analytics ID seems to have a problem');
return;
}
if (!this.cookiemanager.getCookie('consent')) {
this.showAlertCookie = true;
this.isOpened.emit(this.showAlertCookie);
}
if (this.cookiemanager.getCookie('consent') && !this.showAlertCookie) {
const TrackNavigator = navigator.doNotTrack;
if ((TrackNavigator === '1' || TrackNavigator === 'yes')) {
this.cookiemanager.rejectCookie(this.GA_ID);
this.showAlertCookie = false;
this.isOpened.emit(false);
return;
}
if (this.cookiemanager.getCookie('consent') === 'false' && ((TrackNavigator === '1' || TrackNavigator === 'yes'))) {
this.cookiemanager.rejectCookie(this.GA_ID);
this.showAlertCookie = false;
this.isOpened.emit(this.showAlertCookie);
return;
}
if (this.cookiemanager.getCookie('consent') === 'false') {
this.cookiemanager.rejectCookie(this.GA_ID);
this.showAlertCookie = false;
this.isOpened.emit(this.showAlertCookie);
return;
}
}
}
getValues() {
this.cookieconfig.getHeader().subscribe(val => this.header = val);
this.cookieconfig.getMessage().subscribe(val => this.message = val);
this.cookieconfig.getAcceptMessage().subscribe(val => this.AcceptMessage = val);
this.cookieconfig.getAcceptEnable().subscribe(val => this.AcceptEnable = val);
this.cookieconfig.getLinkMessage().subscribe(val => this.LearnMoreMessage = val);
this.cookieconfig.getLinkEnable().subscribe(val => this.LearnMoreEnable = val);
this.cookieconfig.getLinkLink().subscribe(val => this.LearnMoreLink = val);
this.cookieconfig.getDenyMessage().subscribe(val => this.DenyMessage = val);
this.cookieconfig.getDenyEnable().subscribe(val => this.DenyEnable = val);
this.cookieconfig.getAllowMessage().subscribe(val => this.AllowMessage = val);
this.cookieconfig.getAllowEnable().subscribe(val => this.AllowEnable = val);
this.cookieconfig.getDomain().subscribe(val => this.domain = val);
this.cookieconfig.getGA_id().subscribe(val => this.GA_ID = val);
this.cookieconfig.getReviewEnable().subscribe(val => this.ReviewEnable = val);
this.cookieconfig.getReviewMessage().subscribe(val => this.Review = val);
}
getColors() {
this.cookieconfig.getHeaderColor().subscribe(val => this.HeaderColor = val);
this.cookieconfig.getHeaderBackgroundColor().subscribe(val => this.HeaderBackgroundColor = val);
this.cookieconfig.getAcceptColor().subscribe(val => this.AcceptColor = val);
this.cookieconfig.getAcceptBackgroundColor().subscribe(val => this.AcceptBackgroundColor = val);
this.cookieconfig.getDenyColor().subscribe(val => this.DenyColor = val);
this.cookieconfig.getDenyBackgroundColor().subscribe(val => this.DenyBackgroundColor = val);
this.cookieconfig.getAllowColor().subscribe(val => this.AllowColor = val);
this.cookieconfig.getAllowBackgroundColor().subscribe(val => this.AllowBackgroundColor = val);
this.cookieconfig.getLinkColor().subscribe(val => this.LearnMoreColor = val);
this.cookieconfig.getReviewColor().subscribe(val => this.ReviewColor = val);
this.cookieconfig.getReviewBackgroundColor().subscribe(val => this.ReviewBcolor = val);
}
cssClass(color, bcolor) {
return {
'color': color,
'background-color': bcolor,
'border-color': bcolor,
};
}
deny() {
this.cookiemanager.rejectCookie(this.GA_ID);
this.cookiemanager.setCookie('consent', true, this.domain);
this.showAlertCookie = false;
this.isOpened.emit(this.showAlertCookie);
return;
}
allow() {
this.cookiemanager.setCookie('consent', true, this.domain);
this.showAlertCookie = false;
this.isOpened.emit(this.showAlertCookie);
return;
}
review() {
this.cookiemanager.rejectCookie(this.GA_ID);
location.reload();
}
};
CookieServiceComponent.ctorParameters = () => [
{ type: CookieServiceService },
{ type: ConfigService }
];
__decorate([
Output(),
__metadata("design:type", EventEmitter)
], CookieServiceComponent.prototype, "isOpened", void 0);
CookieServiceComponent = __decorate([
Component({
selector: 'lib-cookie-service',
template: "<section id=\"cookie-alert\">\n <div class=\"alert-cookie bottom\" *ngIf=\"showAlertCookie\" attr.aria-label=\"{{ header }}\" attr.title= \"{{ header }}\" [ngStyle]=\"cssClass(HeaderColor, HeaderBackgroundColor )\">\n <div class=\"row\">\n <div class=\"col-sm-10\">\n <p>\n <b>{{ message }}</b>\n <a *ngIf=\"LearnMoreEnable \" href=\"{{ LearnMoreLink }}\" attr.aria-label=\"{{ LearnMoreMessage }}\" attr.title= \"{{ LearnMoreMessage }}\" >{{ LearnMoreMessage }}</a>\n </p>\n </div>\n <div class=\"col-sm-2\">\n <button *ngIf=\"DenyEnable \" type=\"button\" class=\"cbtn\" (click)=\"this.deny()\" attr.aria-label=\"{{ DenyMessage }}\" attr.title= \"{{ DenyMessage }}\" [ngStyle]=\"cssClass(AcceptColor, 'transparent' )\">{{ DenyMessage }}</button>\n <button *ngIf=\"AllowEnable \" type=\"button\" class=\"cbtn\" (click)=\"this.allow()\" attr.aria-label=\"{{ AllowMessage }}\" attr.title= \"{{ AllowMessage }}\" [ngStyle]=\"cssClass(AllowColor, AllowBackgroundColor)\">{{ AllowMessage }}</button>\n <button *ngIf=\"AcceptEnable \" type=\"button\" class=\"cbtn\" (click)=\"this.allow()\" attr.aria-label=\"{{ AcceptMessage }}\" attr.title= \"{{ AcceptMessage }}\" [ngStyle]=\"cssClass(AcceptColor, AcceptBackgroundColor)\">{{ AcceptMessage }}</button>\n </div>\n </div>\n </div>\n\n <div class=\"bottom\" *ngIf=\"!showAlertCookie && ReviewEnable\">\n <button class=\"btn btn-dark btn-sm\" (click)=\"this.review()\" href=\"#\" [ngStyle]=\"cssClass(ReviewColor, ReviewBcolor )\" attr.aria-label=\"{{ Review }}\" attr.title= \"{{ Review }}\" >{{Review}}</button>\n </div>\n</section>",
providers: [CookieServiceService, ConfigService],
styles: [".alert-cookie{position:fixed;width:100%;padding:.75rem 1.25rem}.bottom{bottom:0;left:0;position:fixed;right:0}.cbtn{border:1px solid transparent;padding:.375rem .75rem;font-size:1rem;line-height:1.5;border-radius:.25rem}"]
}),
__metadata("design:paramtypes", [CookieServiceService, ConfigService])
], CookieServiceComponent);
var CookieServiceModule_1;
let CookieServiceModule = CookieServiceModule_1 = class CookieServiceModule {
static forRoot(config) {
return {
ngModule: CookieServiceModule_1,
providers: [
CookieServiceService,
{
provide: CookieConfigService,
useValue: config
}
]
};
}
};
CookieServiceModule = CookieServiceModule_1 = __decorate([
NgModule({
imports: [CommonModule
],
declarations: [CookieServiceComponent],
exports: [CookieServiceComponent]
})
], CookieServiceModule);
/*
* Public API Surface of cookie-service
*/
/**
* Generated bundle index. Do not edit.
*/
export { CookieConfigService, CookieServiceComponent, CookieServiceModule, CookieServiceService as ɵa, ConfigService as ɵb };
//# sourceMappingURL=cookie-service-banner.js.map