Debian Codegraph @

dojox/storage/FlashStorageProvider.js

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