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