1
|
/**
|
2
|
* angular2-cookie-law
|
3
|
*
|
4
|
* Copyright 2016-2017, @andreasonny83, All rights reserved.
|
5
|
*
|
6
|
* @author: @andreasonny83 <andreasonny83@gmail.com>
|
7
|
*/
|
8
|
|
9
|
import { Injectable } from '@angular/core';
|
10
|
|
11
|
@Injectable()
|
12
|
export class CookieLawService {
|
13
|
|
14
|
|
15
|
seen(): boolean {
|
16
|
return this.cookieExists('cookieLawSeen');
|
17
|
}
|
18
|
|
19
|
storeCookie(): void {
|
20
|
return this.setCookie('cookieLawSeen');
|
21
|
}
|
22
|
|
23
|
/**
|
24
|
* try to read a saved cookie
|
25
|
*
|
26
|
* @param {string} name [the cookie name]
|
27
|
*
|
28
|
* @return {string} [the cookie's value]
|
29
|
*/
|
30
|
private cookieExists(name: string): boolean {
|
31
|
if (typeof document !== 'undefined') {
|
32
|
let ca: Array<string> = document.cookie.split(';');
|
33
|
let caLen: number = ca.length;
|
34
|
let cookieName = name + '=';
|
35
|
let c: string;
|
36
|
|
37
|
for (let i: number = 0; i < caLen; i += 1) {
|
38
|
c = ca[i].replace(/^\s\+/g, '');
|
39
|
if (c.indexOf(cookieName) !== -1) {
|
40
|
return true;
|
41
|
}
|
42
|
}
|
43
|
}
|
44
|
return false;
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* store a new cookie in the browser
|
49
|
*
|
50
|
* @param {string} name [the name for the cookie]
|
51
|
*/
|
52
|
private setCookie(name: string): void {
|
53
|
if (typeof document !== 'undefined') {
|
54
|
document.cookie = encodeURIComponent(name) + '=true; path=/';
|
55
|
}
|
56
|
}
|
57
|
}
|