-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-storage.js
131 lines (96 loc) · 2.66 KB
/
web-storage.js
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
define(['personalisation/util'],function(util) {
var personalisationWebStorage = {}
var namespace;
var useCookies=true
personalisationWebStorage.init = function(namespace,options) {
try{
//accesing localstorage when user disabled will throw errors in some browsers
if(typeof(window.localStorage) == 'undefined'){
useCookies=true;
}else{
useCookies=false;
}
}catch(e){
useCookies=true;
}
if(options && options.forceUseCookies){
useCookies=true;
}
if(options && options.forceUseLocalStorage){
useCookies=false;
}
//TODO Check cookies enabled via a util method util.areCookiesEnabled
personalisationWebStorage.setNamespace(namespace);
return true;
//some more stuff
};
personalisationWebStorage.setNamespace = function(ns) {
namespace=ns + ':';
};
personalisationWebStorage.set = function(key,value) {
try{
(useCookies) ? document.cookie = namespace + key + '=' + value + ';path=/;domain=.bbc.co.uk'
: window.localStorage.setItem(namespace + key,value);
}catch(e){
if(e.name.toString()=='QUOTA_EXCEEDED_ERR'){
throw new Error(
"Local Storage Limit Reached",
"Error detected. Local Storage Limit Reached"
);
}else{
throw new Error(e.name.toString(),e.message.toString());
}
}
};
personalisationWebStorage.get = function(key,json) {
var ret;
try{
if(useCookies){
ret=util.getCookie(namespace + key);
}else{
ret=window.localStorage.getItem(namespace + key);
}
if(!ret && json){
ret='{"entry":[]}';
}
return ret;
}catch(e){
throw new Error(e.name.toString(),e.message.toString());
}
};
personalisationWebStorage.remove = function(key) {
window.localStorage.removeItem(namespace + key);
};
personalisationWebStorage.clear = function() {
window.localStorage.clear();
};
personalisationWebStorage.toString = function() {
var string='';
for (var i = 0; i < localStorage.length; i++){
string+=localStorage.getItem(localStorage.key(i));
}
return string;
};
personalisationWebStorage.toArray = function() {
var array=new Array();
for (var i = 0; i < localStorage.length; i++){
array[localStorage.key(i)]=localStorage.getItem(localStorage.key(i));
}
return array;
};
personalisationWebStorage.testStorageLimit=function(){
localStorage.removeItem("DATA");
for(i=0 ; i<40 ; i++) {
var data = this.get("DATA");
try{
this.set("DATA", data + data);
}catch(e){
localStorage.removeItem("DATA");
console.log('testStorageLimit error: ' + e)
throw new Error(e);
}
}
localStorage.removeItem("DATA");
}
return personalisationWebStorage;
});