2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.storage.FlashStorageProvider"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.storage.FlashStorageProvider"] = true;
|
|
|
3 |
dojo.provide("dojox.storage.FlashStorageProvider");
|
|
|
4 |
dojo.require("dojox.flash");
|
|
|
5 |
dojo.require("dojox.storage.manager");
|
|
|
6 |
dojo.require("dojox.storage.Provider");
|
|
|
7 |
|
|
|
8 |
// summary:
|
|
|
9 |
// Storage provider that uses features in Flash to achieve permanent
|
|
|
10 |
// storage
|
|
|
11 |
// description:
|
|
|
12 |
// Authors of this storage provider-
|
|
|
13 |
// Brad Neuberg, bkn3@columbia.edu
|
|
|
14 |
dojo.declare("dojox.storage.FlashStorageProvider", [ dojox.storage.Provider ], {
|
|
|
15 |
initialized: false,
|
|
|
16 |
|
|
|
17 |
_available: null,
|
|
|
18 |
_statusHandler: null,
|
|
|
19 |
|
|
|
20 |
initialize: function(){
|
|
|
21 |
if(djConfig["disableFlashStorage"] == true){
|
|
|
22 |
return;
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
// initialize our Flash
|
|
|
26 |
var loadedListener = function(){
|
|
|
27 |
// indicate our Flash subsystem is now loaded
|
|
|
28 |
dojox.storage._flashLoaded();
|
|
|
29 |
}
|
|
|
30 |
dojox.flash.addLoadedListener(loadedListener);
|
|
|
31 |
dojox.flash.setSwf({
|
|
|
32 |
flash6: dojo.moduleUrl("dojox", "storage/Storage_version6.swf").toString(),
|
|
|
33 |
flash8: dojo.moduleUrl("dojox", "storage/Storage_version8.swf").toString(),
|
|
|
34 |
visible: false
|
|
|
35 |
});
|
|
|
36 |
},
|
|
|
37 |
|
|
|
38 |
// Set a new value for the flush delay timer.
|
|
|
39 |
// Possible values:
|
|
|
40 |
// 0 : Perform the flush synchronously after each "put" request
|
|
|
41 |
// > 0 : Wait until 'newDelay' ms have passed without any "put" request to flush
|
|
|
42 |
// -1 : Do not automatically flush
|
|
|
43 |
setFlushDelay: function(newDelay){
|
|
|
44 |
if(newDelay === null || typeof newDelay === "undefined" || isNaN(newDelay)){
|
|
|
45 |
throw new Error("Invalid argunment: " + newDelay);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
dojox.flash.comm.setFlushDelay(String(newDelay));
|
|
|
49 |
},
|
|
|
50 |
|
|
|
51 |
getFlushDelay: function(){
|
|
|
52 |
return Number(dojox.flash.comm.getFlushDelay());
|
|
|
53 |
},
|
|
|
54 |
|
|
|
55 |
flush: function(namespace){
|
|
|
56 |
//FIXME: is this test necessary? Just use !namespace
|
|
|
57 |
if(namespace == null || typeof namespace == "undefined"){
|
|
|
58 |
namespace = dojox.storage.DEFAULT_NAMESPACE;
|
|
|
59 |
}
|
|
|
60 |
dojox.flash.comm.flush(namespace);
|
|
|
61 |
},
|
|
|
62 |
|
|
|
63 |
isAvailable: function(){
|
|
|
64 |
return (this._available = !djConfig["disableFlashStorage"]);
|
|
|
65 |
},
|
|
|
66 |
|
|
|
67 |
put: function(key, value, resultsHandler, namespace){
|
|
|
68 |
if(this.isValidKey(key) == false){
|
|
|
69 |
throw new Error("Invalid key given: " + key);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
if(namespace == null || typeof namespace == "undefined"){
|
|
|
73 |
namespace = dojox.storage.DEFAULT_NAMESPACE;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
if(this.isValidKey(namespace) == false){
|
|
|
77 |
throw new Error("Invalid namespace given: " + namespace);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
this._statusHandler = resultsHandler;
|
|
|
81 |
|
|
|
82 |
// serialize the value;
|
|
|
83 |
// handle strings differently so they have better performance
|
|
|
84 |
if(dojo.isString(value)){
|
|
|
85 |
value = "string:" + value;
|
|
|
86 |
}else{
|
|
|
87 |
value = dojo.toJson(value);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
dojox.flash.comm.put(key, value, namespace);
|
|
|
91 |
},
|
|
|
92 |
|
|
|
93 |
putMultiple: function(keys, values, resultsHandler, namespace){
|
|
|
94 |
if(this.isValidKeyArray(keys) === false || ! values instanceof Array || keys.length != values.length){
|
|
|
95 |
throw new Error("Invalid arguments: keys = [" + keys + "], values = [" + values + "]");
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
if(namespace == null || typeof namespace == "undefined"){
|
|
|
99 |
namespace = dojox.storage.DEFAULT_NAMESPACE;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
if(this.isValidKey(namespace) == false){
|
|
|
103 |
throw new Error("Invalid namespace given: " + namespace);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
this._statusHandler = resultsHandler;
|
|
|
107 |
|
|
|
108 |
// Convert the arguments on strings we can pass along to Flash
|
|
|
109 |
var metaKey = keys.join(",");
|
|
|
110 |
var lengths = [];
|
|
|
111 |
for(var i=0;i<values.length;i++){
|
|
|
112 |
if(dojo.isString(values[i])){
|
|
|
113 |
values[i] = "string:" + values[i];
|
|
|
114 |
}else{
|
|
|
115 |
values[i] = dojo.toJson(values[i]);
|
|
|
116 |
}
|
|
|
117 |
lengths[i] = values[i].length;
|
|
|
118 |
}
|
|
|
119 |
var metaValue = values.join("");
|
|
|
120 |
var metaLengths = lengths.join(",");
|
|
|
121 |
|
|
|
122 |
dojox.flash.comm.putMultiple(metaKey, metaValue, metaLengths, this.namespace);
|
|
|
123 |
},
|
|
|
124 |
|
|
|
125 |
get: function(key, namespace){
|
|
|
126 |
if(this.isValidKey(key) == false){
|
|
|
127 |
throw new Error("Invalid key given: " + key);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
if(namespace == null || typeof namespace == "undefined"){
|
|
|
131 |
namespace = dojox.storage.DEFAULT_NAMESPACE;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
if(this.isValidKey(namespace) == false){
|
|
|
135 |
throw new Error("Invalid namespace given: " + namespace);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
var results = dojox.flash.comm.get(key, namespace);
|
|
|
139 |
|
|
|
140 |
if(results == ""){
|
|
|
141 |
return null;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
return this._destringify(results);
|
|
|
145 |
},
|
|
|
146 |
|
|
|
147 |
getMultiple: function(/*array*/ keys, /*string?*/ namespace){ /*Object*/
|
|
|
148 |
if(this.isValidKeyArray(keys) === false){
|
|
|
149 |
throw new ("Invalid key array given: " + keys);
|
|
|
150 |
}
|
|
|
151 |
if(namespace == null || typeof namespace == "undefined"){
|
|
|
152 |
namespace = dojox.storage.DEFAULT_NAMESPACE;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
if(this.isValidKey(namespace) == false){
|
|
|
156 |
throw new Error("Invalid namespace given: " + namespace);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
var metaKey = keys.join(",");
|
|
|
160 |
var metaResults = dojox.flash.comm.getMultiple(metaKey, this.namespace);
|
|
|
161 |
var results = eval("(" + metaResults + ")");
|
|
|
162 |
|
|
|
163 |
// destringify each entry back into a real JS object
|
|
|
164 |
//FIXME: use dojo.map
|
|
|
165 |
for(var i=0;i<results.length;i++){
|
|
|
166 |
results[i] = (results[i] == "") ? null : this._destringify(results[i]);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
return results;
|
|
|
170 |
},
|
|
|
171 |
|
|
|
172 |
_destringify: function(results){
|
|
|
173 |
// destringify the content back into a
|
|
|
174 |
// real JavaScript object;
|
|
|
175 |
// handle strings differently so they have better performance
|
|
|
176 |
if(dojo.isString(results) && (/^string:/.test(results))){
|
|
|
177 |
results = results.substring("string:".length);
|
|
|
178 |
}else{
|
|
|
179 |
results = dojo.fromJson(results);
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
return results;
|
|
|
183 |
},
|
|
|
184 |
|
|
|
185 |
getKeys: function(namespace){
|
|
|
186 |
if(namespace == null || typeof namespace == "undefined"){
|
|
|
187 |
namespace = dojox.storage.DEFAULT_NAMESPACE;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
if(this.isValidKey(namespace) == false){
|
|
|
191 |
throw new Error("Invalid namespace given: " + namespace);
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
var results = dojox.flash.comm.getKeys(namespace);
|
|
|
195 |
|
|
|
196 |
if(results == ""){
|
|
|
197 |
return [];
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
// the results are returned comma seperated; split them
|
|
|
201 |
results = results.split(",");
|
|
|
202 |
|
|
|
203 |
return results;
|
|
|
204 |
},
|
|
|
205 |
|
|
|
206 |
getNamespaces: function(){
|
|
|
207 |
var results = dojox.flash.comm.getNamespaces();
|
|
|
208 |
if(results == ""){
|
|
|
209 |
return [dojox.storage.DEFAULT_NAMESPACE];
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
// the results are returned comma seperated; split them
|
|
|
213 |
results = results.split(",");
|
|
|
214 |
|
|
|
215 |
return results;
|
|
|
216 |
},
|
|
|
217 |
|
|
|
218 |
clear: function(namespace){
|
|
|
219 |
if(namespace == null || typeof namespace == "undefined"){
|
|
|
220 |
namespace = dojox.storage.DEFAULT_NAMESPACE;
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
if(this.isValidKey(namespace) == false){
|
|
|
224 |
throw new Error("Invalid namespace given: " + namespace);
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
dojox.flash.comm.clear(namespace);
|
|
|
228 |
},
|
|
|
229 |
|
|
|
230 |
remove: function(key, namespace){
|
|
|
231 |
if(namespace == null || typeof namespace == "undefined"){
|
|
|
232 |
namespace = dojox.storage.DEFAULT_NAMESPACE;
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
if(this.isValidKey(namespace) == false){
|
|
|
236 |
throw new Error("Invalid namespace given: " + namespace);
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
dojox.flash.comm.remove(key, namespace);
|
|
|
240 |
},
|
|
|
241 |
|
|
|
242 |
removeMultiple: function(/*array*/ keys, /*string?*/ namespace){ /*Object*/
|
|
|
243 |
if(this.isValidKeyArray(keys) === false){
|
|
|
244 |
dojo.raise("Invalid key array given: " + keys);
|
|
|
245 |
}
|
|
|
246 |
if(namespace == null || typeof namespace == "undefined"){
|
|
|
247 |
namespace = dojox.storage.DEFAULT_NAMESPACE;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
if(this.isValidKey(namespace) == false){
|
|
|
251 |
throw new Error("Invalid namespace given: " + namespace);
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
var metaKey = keys.join(",");
|
|
|
255 |
dojox.flash.comm.removeMultiple(metaKey, this.namespace);
|
|
|
256 |
},
|
|
|
257 |
|
|
|
258 |
isPermanent: function(){
|
|
|
259 |
return true;
|
|
|
260 |
},
|
|
|
261 |
|
|
|
262 |
getMaximumSize: function(){
|
|
|
263 |
return dojox.storage.SIZE_NO_LIMIT;
|
|
|
264 |
},
|
|
|
265 |
|
|
|
266 |
hasSettingsUI: function(){
|
|
|
267 |
return true;
|
|
|
268 |
},
|
|
|
269 |
|
|
|
270 |
showSettingsUI: function(){
|
|
|
271 |
dojox.flash.comm.showSettings();
|
|
|
272 |
dojox.flash.obj.setVisible(true);
|
|
|
273 |
dojox.flash.obj.center();
|
|
|
274 |
},
|
|
|
275 |
|
|
|
276 |
hideSettingsUI: function(){
|
|
|
277 |
// hide the dialog
|
|
|
278 |
dojox.flash.obj.setVisible(false);
|
|
|
279 |
|
|
|
280 |
// call anyone who wants to know the dialog is
|
|
|
281 |
// now hidden
|
|
|
282 |
if(dojo.isFunction(dojox.storage.onHideSettingsUI)){
|
|
|
283 |
dojox.storage.onHideSettingsUI.call(null);
|
|
|
284 |
}
|
|
|
285 |
},
|
|
|
286 |
|
|
|
287 |
getResourceList: function(){ /* Array[] */
|
|
|
288 |
var swfloc6 = dojo.moduleUrl("dojox", "storage/Storage_version6.swf").toString();
|
|
|
289 |
var swfloc8 = dojo.moduleUrl("dojox", "storage/Storage_version8.swf").toString();
|
|
|
290 |
|
|
|
291 |
var results = dojox.flash.info.getResourceList(swfloc6, swfloc8);
|
|
|
292 |
results.push(dojo.moduleUrl("dojox", "storage/storage_dialog.swf").toString());
|
|
|
293 |
|
|
|
294 |
return results;
|
|
|
295 |
},
|
|
|
296 |
|
|
|
297 |
/** Called when the Flash is finished loading. */
|
|
|
298 |
_flashLoaded: function(){
|
|
|
299 |
// get available namespaces
|
|
|
300 |
this._allNamespaces = this.getNamespaces();
|
|
|
301 |
|
|
|
302 |
this._initialized = true;
|
|
|
303 |
|
|
|
304 |
// indicate that this storage provider is now loaded
|
|
|
305 |
dojox.storage.manager.loaded();
|
|
|
306 |
},
|
|
|
307 |
|
|
|
308 |
// Called if the storage system needs to tell us about the status
|
|
|
309 |
// of a put() request.
|
|
|
310 |
_onStatus: function(statusResult, key){
|
|
|
311 |
var ds = dojox.storage;
|
|
|
312 |
var dfo = dojox.flash.obj;
|
|
|
313 |
|
|
|
314 |
if(statusResult == ds.PENDING){
|
|
|
315 |
dfo.center();
|
|
|
316 |
dfo.setVisible(true);
|
|
|
317 |
}else{
|
|
|
318 |
dfo.setVisible(false);
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
if(ds._statusHandler){
|
|
|
322 |
ds._statusHandler.call(null, statusResult, key);
|
|
|
323 |
}
|
|
|
324 |
}
|
|
|
325 |
}
|
|
|
326 |
);
|
|
|
327 |
|
|
|
328 |
dojox.storage.manager.register("dojox.storage.FlashStorageProvider",
|
|
|
329 |
new dojox.storage.FlashStorageProvider());
|
|
|
330 |
|
|
|
331 |
dojox.storage.manager.initialize(); // is this redundant? GearsStorageProvider does this.
|
|
|
332 |
|
|
|
333 |
}
|