Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {Injectable} from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class StorageService {
/**
* This is so we can detect if they have local or session storage available, which we report on
* From MDN - https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
* @param type the type of storage (localStorage, or sessionStorage)
*/
static storageAvailable(type): boolean {
let storage;
try {
storage = window[type];
var x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
} catch (e) {
return e instanceof DOMException && (
// everything except Firefox
e.code === 22 ||
// Firefox
e.code === 1014 ||
// test name field too, because code might not be present
// everything except Firefox
e.name === 'QuotaExceededError' ||
// Firefox
e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
// acknowledge QuotaExceededError only if there's something already stored
(storage && storage.length !== 0);
}
}
/**
* save to localStorage (forever storage)
* @param key the key to store the data under
* @param payload the data
*/
saveLocal(key: string, payload: string): boolean {
if (StorageService.storageAvailable('localStorage')) { // if we have localstorage, we will us that
localStorage.setItem(key, payload);
return true;
} else if (navigator.cookieEnabled) { // we have no localstorage, so we'll try cookies
this.saveCookie(key, payload, false);
return true;
}
return false;
}
/**
* retrieve data from localStorage (forever storage)
* @param key the key to the stored data get
*/
getLocal(key: string): any {
if (StorageService.storageAvailable('localStorage')) {
return localStorage.getItem(key);
} else {
return this.getCookie(key);
}
}
/**
* delete data from localStorage (forever storage) identified by key (not delete all)
* @param key the key to the stored data we want to delete
*/
deleteLocal(key: string): any {
if (StorageService.storageAvailable('localStorage')) {
localStorage.removeItem(key);
} else {
this.deleteCookie(key);
}
}
/**
* save data to sessionStorage (until browsers closes storage)
* @param key the key to save the data under
* @param payload the data
*/
saveSession(key: string, payload: string): boolean {
if (StorageService.storageAvailable('sessionStorage')) { // if we have localstorage, we will us that
sessionStorage.setItem(key, payload);
return true;
} else if (navigator.cookieEnabled) { // we have no localstorage, so we'll try cookies
this.saveCookie(key, payload, true);
return true;
}
return false;
}
/**
* retrieve data from session storage (until browser closes storage)
* @param key the key to the data we want to get
*/
getSession(key: string): any {
if (StorageService.storageAvailable('sessionStorage')) {
return sessionStorage.getItem(key);
} else {
return this.getCookie(key);
}
}
/**
* delete data from session storage (until browsers closes storage) by key (not delete all)
* @param key the key to the data we want to delete
*/
deleteSession(key: string): any {
if (StorageService.storageAvailable('sessionStorage')) {
sessionStorage.removeItem(key);
} else {
this.deleteCookie(key);
}
}
/**
* if local or session storage isn't available (browser doesn't support, or its full) we fall back to using cookies.
* This is the save fallback
* @param key the key to the data we want to save
* @param payload the data
* @param session whether this is session storage (temporary - short term cookie), otherwise its local (long term)
*/
private saveCookie(key: string, payload: string, session: boolean = false) {
let expires = session ? 'expires=Fri, 31 Dec 9999 23:59:59 GMT;' : '';
document.cookie = key + "=" + encodeURIComponent(payload) + "; " + expires + " domain=" + window.location.hostname + "; path=/";
}
/**
* the cookie fallback to getting a value
* @param key the key of the data we want to get
*/
private getCookie(key: string) {
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + key + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
}
/**
* the cookie fallback to delete a value by key (not delete all)
* @param key the key fo the data we want to delete
*/
private deleteCookie(key: string) {
document.cookie = key + "= ;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
}
}