projects/commons/src/lib/elements/cookie/services/cookie.service.ts
Properties |
|
Methods |
|
constructor(toastService: ToastService, document: any)
|
|||||||||
|
Parameters :
|
| Private eventClickAccept |
eventClickAccept()
|
Decorators :
@autobind()
|
|
Returns :
void
|
| Public existCookie | ||||
existCookie(name)
|
||||
|
Parameters :
Returns :
boolean
|
| Public init | ||||||||
init(message: string)
|
||||||||
|
Parameters :
Returns :
void
|
| Public setElement | ||||||
setElement(name, data)
|
||||||
|
Parameters :
Returns :
void
|
| Private setStyleNotification |
setStyleNotification()
|
|
Returns :
void
|
| Public element |
import autobind from 'autobind-decorator';
import { Inject, Injectable } from '@angular/core';
import { COOKIE_MESSAGE } from '../constants';
import { colorsEnum } from '../../../shared/enums';
import { ToastService } from '../../toast/services';
import { toastPositionEnum } from '../../toast/enums';
import { IToastConfig } from '../../toast/interfaces';
@Injectable()
export class CookieService {
public element;
constructor(
private readonly toastService: ToastService,
@Inject('DOCUMENT') private readonly document: any,
) {}
public init(message: string = COOKIE_MESSAGE) {
if (!this.existCookie('terms')) {
this.toastService.open({
message,
dismissible: true,
duration: 20000000,
position: toastPositionEnum.bottomCenter,
type: `${ colorsEnum.dark } cookie-notification`,
} as IToastConfig);
this.setStyleNotification();
}
}
public setElement(name, data) {
this.document.cookie = `${ name }=${ data }`;
}
public existCookie(name) {
return this.document.cookie.split('=').findIndex((element) => element === name) >= 0;
}
private setStyleNotification() {
const button = this.document.getElementsByClassName('cookie-accept')[0] as any;
this.element = this.document.getElementsByClassName('cookie-notification')[0] as any;
this.element.style.bottom = '0';
this.element.style.width = '100%';
this.element.style.position = 'absolute';
this.element.style['border-radius'] = '0';
button.addEventListener('click', this.eventClickAccept);
}
@autobind
private eventClickAccept() {
this.setElement('terms', 'accepted');
this.element.style.display = 'none';
}
}