-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.js
239 lines (223 loc) · 5.46 KB
/
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
@Name: sb.storage
@Author: Paul Visco
@Description: used to store data clientside. Uses localStorage if it exists
Safari, Chrome, Firefox, IE 8+ or UserData IE 6,7 or cookies if neither is available
When using IE6/7 data is restricted to the folder it was set in, otherwise it is only
restricted to the hostname.
@Example: var s = new sb.storage();
s.set('name', 'paul');
s.get('name');
*/
sb.storage = function(params){
sb.objects.infuse(params, this);
this.name = this.name || 'sb_storage';
if('localStorage' in window && window['localStorage'] !== null){
this.storage = new sb.storage.local(window.location.host);
} else if(typeof document.body.style.behavior == 'string'){
this.storage = new sb.storage.userData(this.name);
} else {
sb.include('cookies');
this.storage = sb.cookies;
}
};
/**
@Name: sb.storage.prototype
@Author: Paul Visco
*/
sb.storage.prototype = {
/**
@Name: sb.storage.prototype.onBeforeGet
@Description: fires before set is complete so that you can do things like check the engine used
@Param string key The key that is being set
@Param string val The value of the key being set
@Return If it returns false, the set will be canceled
@Example:
var s = new sb.storage({
onBeforeSet : function(key, val){
if(this.typeOf() == 'sb.cookies'){
//do something
}
}
});
*/
onBeforeGet : function(key){},
/**
@Name: sb.storage.prototype.onBeforeSet
@Description: fires before get is complete so that you can do things like check the engine used
@Param string key The key that is being gotten
@Return If it returns false, the set will be canceled
@Example:
var s = new sb.storage({
onBeforeGet : function(key){
if(this.typeOf() == 'sb.cookies'){
//do something
}
}
});
*/
onBeforeSet : function(key, val){},
/**
@Name: sb.storage.prototype.onBeforeClear
@Description: fires before the datastore is cleared
@Return If it returns false, the clearing will be canceled
@Example:
var s = new sb.storage({
onBeforeClear : function(key){
if(this.typeOf() == 'sb.cookies'){
//do something
}
}
});
*/
onBeforeClear : function(){},
/**
@Name: sb.storage.prototype.set
@Description: sets a key, value pair
@Return: Boolean Returns true if the key was set
@Example:
var s = new sb.storage();
s.set('name', 'paul');
*/
set : function(key, val){
if(this.onBeforeSet(key, val) !== false){
return this.storage.set(key, val);
}
return false;
},
/**
@Name: sb.storage.prototype.get
@Description: get a value by key
@Return: String Return value of the key
@Example:
var s = new sb.storage();
s.get('name');
*/
get : function(key){
if(this.onBeforeGet(key) !== false){
return this.storage.get(key);
}
return false;
},
/**
@Name: sb.storage.prototype.unset
@Description: get a value by key
@Return: String Return value of the key
@Example:
var s = new sb.storage();
s.unset('name');
*/
unset : function(key){
this.storage.unset(key);
},
/**
@Name: sb.storage.prototype.clearAll
@Description: clears all values in storage
@Example:
var s = new sb.storage();
s.clearAll();
*/
clearAll : function(){
if(this.onBeforeClear() !== false){
return this.storage.clearAll();
}
},
/**
@Name: sb.storage.prototype.typeOf
@Description: Gets the type of storage engine being used
@Return: String The type of storage engine being used e.g. sb.storage.local, sb.cookies, sb.userData
@Example:
var s = new sb.storage();
s.clearAll();
*/
typeOf : function(){
return this.storage.typeOf();
}
};
/**
@Name: sb.storage.prototype.userData
@Description: used internally Interface to store data in IE 6 and 7 where localStorage does not exist
*/
sb.storage.userData = function(name){
this.name = name || 'sb_storage';
if(typeof document.body.style.behavior != 'string'){
throw('sb.storage.userData only works in IE');
} else {
this.storage = new sb.element({
tag : 'div',
id : 'sb_storage',
value : '',
styles : {
'behavior' : 'url(#default#userdata)',
'display' : 'none'
}
});
this.storage.appendToTop('body')
}
};
sb.storage.userData.prototype = {
set : function(key, val){
this.storage.setAttribute(key, val);
try{
this.storage.save(this.name);
return true;
}catch(e){
return false;
}
},
get : function(key){
this.storage.load(this.name);
return this.storage.getAttribute(key);
},
unset : function(key){
this.set(key, null);
},
typeOf : function(){
return 'sb.storage.userData';
}
};
/**
@Name: sb.storage.prototype.local
@Description: used internally Interface to store data in browsers that support localStorage
*/
sb.storage.local = function(){
if(!window.localStorage){
throw('requires local strorage');
}
};
sb.storage.local.prototype = {
set : function(key, val){
try{
window.localStorage.setItem(key, val);
return true;
} catch(e){
return false;
}
},
get : function(key){
try{
return window.localStorage.getItem(key);
} catch(e){
return false;
}
},
unset : function(key){
try{
window.localStorage.removeItem(key);
return true;
} catch(e){
return false;
}
},
clearAll : function(key){
try{
window.localStorage.clear();
return true;
} catch(e){
return false;
}
},
typeOf : function(){
return 'sb.storage.local';
}
};