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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
if(!dojo._hasResource["dojox.storage.WhatWGStorageProvider"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.storage.WhatWGStorageProvider"] = true;
dojo.provide("dojox.storage.WhatWGStorageProvider");
dojo.require("dojox.storage.Provider");
dojo.require("dojox.storage.manager");
dojo.declare("dojox.storage.WhatWGStorageProvider", [ dojox.storage.Provider ], {
// summary:
// Storage provider that uses WHAT Working Group features in Firefox 2
// to achieve permanent storage.
// description:
// The WHAT WG storage API is documented at
// http://www.whatwg.org/specs/web-apps/current-work/#scs-client-side
//
// You can disable this storage provider with the following djConfig
// variable:
// var djConfig = { disableWhatWGStorage: true };
//
// Authors of this storage provider-
// JB Boisseau, jb.boisseau@eutech-ssii.com
// Brad Neuberg, bkn3@columbia.edu
initialized: false,
_domain: null,
_available: null,
_statusHandler: null,
_allNamespaces: null,
_storageEventListener: null,
initialize: function(){
if(dojo.config["disableWhatWGStorage"] == true){
return;
}
// get current domain
// see: https://bugzilla.mozilla.org/show_bug.cgi?id=357323
this._domain = (location.hostname == "localhost") ? "localhost.localdomain" : location.hostname;
// console.debug(this._domain);
// indicate that this storage provider is now loaded
this.initialized = true;
dojox.storage.manager.loaded();
},
isAvailable: function(){
try{
// see: https://bugzilla.mozilla.org/show_bug.cgi?id=357323
var myStorage = globalStorage[((location.hostname == "localhost") ? "localhost.localdomain" : location.hostname)];
}catch(e){
this._available = false;
return this._available;
}
this._available = true;
return this._available;
},
put: function(key, value, resultsHandler, namespace){
if(this.isValidKey(key) == false){
throw new Error("Invalid key given: " + key);
}
namespace = namespace||this.DEFAULT_NAMESPACE;
// get our full key name, which is namespace + key
key = this.getFullKey(key, namespace);
this._statusHandler = resultsHandler;
// serialize the value;
// handle strings differently so they have better performance
if(dojo.isString(value)){
value = "string:" + value;
}else{
value = dojo.toJson(value);
}
// register for successful storage events.
var storageListener = dojo.hitch(this, function(evt){
// remove any old storage event listener we might have added
// to the window on old put() requests; Firefox has a bug
// where it can occassionaly go into infinite loops calling
// our storage event listener over and over -- this is a
// workaround
// FIXME: Simplify this into a test case and submit it
// to Firefox
window.removeEventListener("storage", storageListener, false);
// indicate we succeeded
if(resultsHandler){
resultsHandler.call(null, this.SUCCESS, key);
}
});
window.addEventListener("storage", storageListener, false);
// try to store the value
try{
var myStorage = globalStorage[this._domain];
myStorage.setItem(key, value);
}catch(e){
// indicate we failed
this._statusHandler.call(null, this.FAILED, key, e.toString());
}
},
get: function(key, namespace){
if(this.isValidKey(key) == false){
throw new Error("Invalid key given: " + key);
}
namespace = namespace||this.DEFAULT_NAMESPACE;
// get our full key name, which is namespace + key
key = this.getFullKey(key, namespace);
// sometimes, even if a key doesn't exist, Firefox
// will return a blank string instead of a null --
// this _might_ be due to having underscores in the
// keyname, but I am not sure.
// FIXME: Simplify this bug into a testcase and
// submit it to Firefox
var myStorage = globalStorage[this._domain];
var results = myStorage.getItem(key);
if(results == null || results == ""){
return null;
}
results = results.value;
// destringify the content back into a
// real JavaScript object;
// handle strings differently so they have better performance
if(dojo.isString(results) && (/^string:/.test(results))){
results = results.substring("string:".length);
}else{
results = dojo.fromJson(results);
}
return results;
},
getNamespaces: function(){
var results = [ this.DEFAULT_NAMESPACE ];
// simply enumerate through our array and save any string
// that starts with __
var found = {};
var myStorage = globalStorage[this._domain];
var tester = /^__([^_]*)_/;
for(var i = 0; i < myStorage.length; i++){
var currentKey = myStorage.key(i);
if(tester.test(currentKey) == true){
var currentNS = currentKey.match(tester)[1];
// have we seen this namespace before?
if(typeof found[currentNS] == "undefined"){
found[currentNS] = true;
results.push(currentNS);
}
}
}
return results;
},
getKeys: function(namespace){
namespace = namespace||this.DEFAULT_NAMESPACE;
if(this.isValidKey(namespace) == false){
throw new Error("Invalid namespace given: " + namespace);
}
// create a regular expression to test the beginning
// of our key names to see if they match our namespace;
// if it is the default namespace then test for the presence
// of no namespace for compatibility with older versions
// of dojox.storage
var namespaceTester;
if(namespace == this.DEFAULT_NAMESPACE){
namespaceTester = new RegExp("^([^_]{2}.*)$");
}else{
namespaceTester = new RegExp("^__" + namespace + "_(.*)$");
}
var myStorage = globalStorage[this._domain];
var keysArray = [];
for(var i = 0; i < myStorage.length; i++){
var currentKey = myStorage.key(i);
if(namespaceTester.test(currentKey) == true){
// strip off the namespace portion
currentKey = currentKey.match(namespaceTester)[1];
keysArray.push(currentKey);
}
}
return keysArray;
},
clear: function(namespace){
namespace = namespace||this.DEFAULT_NAMESPACE;
if(this.isValidKey(namespace) == false){
throw new Error("Invalid namespace given: " + namespace);
}
// create a regular expression to test the beginning
// of our key names to see if they match our namespace;
// if it is the default namespace then test for the presence
// of no namespace for compatibility with older versions
// of dojox.storage
var namespaceTester;
if(namespace == this.DEFAULT_NAMESPACE){
namespaceTester = new RegExp("^[^_]{2}");
}else{
namespaceTester = new RegExp("^__" + namespace + "_");
}
var myStorage = globalStorage[this._domain];
var keys = [];
for(var i = 0; i < myStorage.length; i++){
if(namespaceTester.test(myStorage.key(i)) == true){
keys[keys.length] = myStorage.key(i);
}
}
dojo.forEach(keys, dojo.hitch(myStorage, "removeItem"));
},
remove: function(key, namespace){
// get our full key name, which is namespace + key
key = this.getFullKey(key, namespace);
var myStorage = globalStorage[this._domain];
myStorage.removeItem(key);
},
isPermanent: function(){
return true;
},
getMaximumSize: function(){
return this.SIZE_NO_LIMIT;
},
hasSettingsUI: function(){
return false;
},
showSettingsUI: function(){
throw new Error(this.declaredClass + " does not support a storage settings user-interface");
},
hideSettingsUI: function(){
throw new Error(this.declaredClass + " does not support a storage settings user-interface");
},
getFullKey: function(key, namespace){
namespace = namespace||this.DEFAULT_NAMESPACE;
if(this.isValidKey(namespace) == false){
throw new Error("Invalid namespace given: " + namespace);
}
// don't append a namespace string for the default namespace,
// for compatibility with older versions of dojox.storage
if(namespace == this.DEFAULT_NAMESPACE){
return key;
}else{
return "__" + namespace + "_" + key;
}
}
});
dojox.storage.manager.register("dojox.storage.WhatWGStorageProvider",
new dojox.storage.WhatWGStorageProvider());
}
|