File

projects/commons/src/lib/elements/cookie/services/cookie.service.ts

Index

Properties
Methods

Constructor

constructor(toastService: ToastService, document: any)
Parameters :
Name Type Optional
toastService ToastService No
document any No

Methods

Private eventClickAccept
eventClickAccept()
Decorators :
@autobind()
Returns : void
Public existCookie
existCookie(name)
Parameters :
Name Optional
name No
Returns : boolean
Public init
init(message: string)
Parameters :
Name Type Optional Default value
message string No COOKIE_MESSAGE
Returns : void
Public setElement
setElement(name, data)
Parameters :
Name Optional
name No
data No
Returns : void
Private setStyleNotification
setStyleNotification()
Returns : void

Properties

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';
    }
}

result-matching ""

    No results matching ""