Debian Codegraph @

dojox/storage/tests/test_storage.js

raw symbols SCIP
1 dojo.require("dojox.storage");
2
3 var TestStorage = {
4 currentProvider: "default",
5 currentNamespace: null,
6
7 initialize: function(){
8 //console.debug("test_storage.initialize()");
9
10 // do we even have a storage provider?
11 if(dojox.storage.manager.available == false){
12 var o = document.createElement("option");
13 o.appendChild(document.createTextNode("None"));
14 o.value = "None";
15 o.selected = true;
16 var selector = dojo.byId("currentStorageProvider");
17 selector.disabled = true;
18 selector.appendChild(o);
19
20 alert("No storage provider is available on this browser");
21 return;
22 }
23
24 // what should our starting namespace be?
25 this._determineCurrentNamespace();
26
27 // clear out old values and enable input forms
28 dojo.byId("storageNamespace").value = this.currentNamespace;
29 dojo.byId("storageNamespace").disabled = false;
30 dojo.byId("storageKey").value = "";
31 dojo.byId("storageKey").disabled = false;
32 dojo.byId("storageValue").value = "";
33 dojo.byId("storageValue").disabled = false;
34
35 // write out available providers
36 this._printAvailableProviders();
37
38 // write out our available namespaces
39 this._printAvailableNamespaces();
40
41 // write out our available keys
42 this._printAvailableKeys();
43
44 // initialize our event handlers
45 var providerDirectory = dojo.byId("currentStorageProvider");
46 dojo.connect(providerDirectory, "onchange", this, this.providerChange);
47 var namespaceDirectory = dojo.byId("namespaceDirectory");
48 dojo.connect(namespaceDirectory, "onchange", this, this.namespaceChange);
49 var directory = dojo.byId("directory");
50 dojo.connect(directory, "onchange", this, this.directoryChange);
51 var storageValueElem = dojo.byId("storageValue");
52 dojo.connect(storageValueElem, "onkeyup", this, this.printValueSize);
53
54 // make the directory be unselected if the key name field gets focus
55 var keyNameField = dojo.byId("storageKey");
56 dojo.connect(keyNameField, "onfocus", function(evt){
57 directory.selectedIndex = -1;
58 });
59
60 // add onclick listeners to all of our buttons
61 var buttonContainer = dojo.byId("buttonContainer");
62 var currentChild = buttonContainer.firstChild;
63 while(currentChild.nextSibling != null){
64 if(currentChild.nodeType == 1){
65 var buttonName = currentChild.id;
66 var functionName = buttonName.match(/^(.*)Button$/)[1];
67 dojo.connect(currentChild, "onclick", this, this[functionName]);
68 currentChild.disabled = false;
69 }
70
71 currentChild = currentChild.nextSibling;
72 }
73
74 // print out metadata
75 this._printProviderMetadata();
76
77 // disable the configuration button if none is supported for this provider
78 if(dojox.storage.hasSettingsUI() == false){
79 dojo.byId("configureButton").disabled = true;
80 }
81 },
82
83 providerChange: function(evt){
84 var provider = evt.target.value;
85
86 // reload the page with this provider
87 var query = "";
88 if(window.location.href.indexOf("forceStorageProvider") == -1){
89 query = "?";
90 }else{
91 query = "&";
92 }
93
94 window.location.href += query + "forceStorageProvider=" + provider;
95 },
96
97 namespaceChange: function(evt){
98 var ns = evt.target.value;
99 this.currentNamespace = ns;
100
101 // update our available keys
102 this._printAvailableKeys();
103
104 // clear out our key and values
105 dojo.byId("storageNamespace").value = this.currentNamespace;
106 dojo.byId("storageKey").value = "";
107 dojo.byId("storageValue").value = "";
108 },
109
110 directoryChange: function(evt){
111 var key = evt.target.value;
112
113 // add this value into the form
114 var keyNameField = dojo.byId("storageKey");
115 keyNameField.value = key;
116
117 this._handleLoad(key);
118 },
119
120 load: function(evt){
121 // cancel the button's default behavior
122 evt.preventDefault();
123 evt.stopPropagation();
124
125 // get the key to load
126 var key = dojo.byId("storageKey").value;
127
128 if(key == null || typeof key == "undefined" || key == ""){
129 alert("Please enter a key name");
130 return;
131 }
132
133 this._handleLoad(key);
134 },
135
136 loadMultiple: function(evt){
137 // cancel the button's default behavior
138 evt.preventDefault();
139 evt.stopPropagation();
140
141 // get the keys to load
142 var dir = dojo.byId("directory");
143 if(dir.selectedIndex < 0){
144 alert("Please select a key from the 'All Keys' list");
145 return;
146 }
147
148 var keyArray = [dir.value], i;
149
150 for(i = dir.options.selectedIndex + 1; i < dir.options.length
151 && keyArray.length < 10; i++){
152 keyArray.push(dir.options[i].value);
153 }
154
155 for(i = dir.options.selectedIndex - 1; i >= 0 && keyArray.length < 10; i--){
156 keyArray.push(dir.options[i].value);
157 }
158
159 this._handleLoadMultiple(keyArray);
160 },
161
162 save: function(evt){
163 // cancel the button's default behavior
164 evt.preventDefault();
165 evt.stopPropagation();
166
167 // get the new values
168 var key = dojo.byId("storageKey").value;
169 var value = dojo.byId("storageValue").value;
170 var namespace = dojo.byId("storageNamespace").value;
171
172 if(key == null || typeof key == "undefined" || key == ""){
173 alert("Please enter a key name");
174 return;
175 }
176
177 if(value == null || typeof value == "undefined" || value == ""){
178 alert("Please enter a key value");
179 return;
180 }
181
182 // print out the size of the value
183 this.printValueSize();
184
185 // do the save
186 this._save(key, value, namespace);
187 },
188
189 saveMultiple: function(evt) {
190 // cancel the button's default behavior
191 evt.preventDefault();
192 evt.stopPropagation();
193
194 // get the new values
195 var key = dojo.byId("storageKey").value;
196 var value = dojo.byId("storageValue").value;
197 var namespace = dojo.byId("storageNamespace").value;
198
199 if(key == null || typeof key == "undefined" || key == ""){
200 alert("Please enter a key name");
201 return;
202 }
203
204 if(value == null || typeof value == "undefined" || value == ""){
205 alert("Please enter a key value");
206 return;
207 }
208
209 // print out the size of the value
210 this.printValueSize();
211
212 // do the save
213 var keyArray = [], valueArray = [];
214 for(var i = 0; i < 10; i++) {
215 keyArray.push(key+i);
216 valueArray.push(i + ") " + value);
217 }
218 this._saveMultiple(keyArray, valueArray, namespace);
219 },
220
221 clearNamespace: function(evt){
222 // cancel the button's default behavior
223 evt.preventDefault();
224 evt.stopPropagation();
225
226 dojox.storage.clear(this.currentNamespace);
227
228 this._printAvailableNamespaces();
229 this._printAvailableKeys();
230 },
231
232 _saveMultiple: function(keyArray, valueArray, namespace) {
233 this._printStatus("Saving 10 keys, starting with '" + keyArray[0] + "'...");
234 var self = this;
235 var saveHandler = function(status, keyName){
236 //console.debug("saveHandler, status="+status+", keyName="+keyName);
237 if(status == dojox.storage.FAILED){
238 alert("You do not have permission to store data for this web site. "
239 + "Press the Configure button to grant permission.");
240 }else if(status == dojox.storage.SUCCESS){
241 // clear out the old value
242 dojo.byId("storageKey").value = "";
243 dojo.byId("storageValue").value = "";
244 self._printStatus("Saved '" + keyArray[0] + "' and "
245 + (keyArray.length - 1) + " others");
246
247 if(typeof namespace != "undefined" && namespace != null){
248 self.currentNamespace = namespace;
249 }
250
251 self._printAvailableKeys();
252 self._printAvailableNamespaces();
253 }
254 };
255
256 try{
257 if(namespace == dojox.storage.DEFAULT_NAMESPACE){
258 dojox.storage.putMultiple(keyArray, valueArray, saveHandler);
259 }else{
260 dojox.storage.putMultiple(keyArray, valueArray, saveHandler, namespace);
261 }
262 }catch(exp){
263 alert(exp);
264 }
265 },
266
267 configure: function(evt){
268 // cancel the button's default behavior
269 evt.preventDefault();
270 evt.stopPropagation();
271
272 if(dojox.storage.hasSettingsUI()){
273 // redraw our keys after the dialog is closed, in
274 // case they have all been erased
275 var self = this;
276 dojox.storage.onHideSettingsUI = function(){
277 self._printAvailableKeys();
278 }
279
280 // show the dialog
281 dojox.storage.showSettingsUI();
282 }
283 },
284
285 remove: function(evt){
286 // cancel the button's default behavior
287 evt.preventDefault();
288 evt.stopPropagation();
289
290 // determine what key to delete; if the directory has a selected value,
291 // use that; otherwise, use the key name field
292 var directory = dojo.byId("directory");
293 var keyNameField = dojo.byId("storageKey");
294 var keyValueField = dojo.byId("storageValue");
295 var key;
296 if(directory.selectedIndex != -1){
297 key = directory.value;
298 // delete this option
299 var options = directory.childNodes;
300 for(var i = 0; i < options.length; i++){
301 if(options[i].nodeType == 1 &&
302 options[i].value == key){
303 directory.removeChild(options[i]);
304 break;
305 }
306 }
307 }else{
308 key = keyNameField.value;
309 }
310
311 keyNameField.value = "";
312 keyValueField.value = "";
313
314 // now delete the value
315 this._printStatus("Removing '" + key + "'...");
316 if(this.currentNamespace == dojox.storage.DEFAULT_NAMESPACE){
317 dojox.storage.remove(key);
318 }else{
319 dojox.storage.remove(key, this.currentNamespace);
320 }
321
322 // update our UI
323 this._printAvailableNamespaces();
324 this._printStatus("Removed '" + key);
325 },
326
327 removeMultiple: function(evt){
328 // cancel the button's default behavior
329 evt.preventDefault();
330 evt.stopPropagation();
331
332 // Try to help the user understands what she's doing
333 if(confirm("Are you sure you want to delete the entry for the selected key and up to 10 more entries?")){
334
335 // get the keys for entries to remove
336 var dir = dojo.byId("directory");
337 if(dir.selectedIndex < 0){
338 alert("Please select a key from the 'All Keys' list");
339 return;
340 }
341
342 var keyArray = [];
343 for(var i = dir.options.selectedIndex; i < dir.options.length && keyArray.length < 10; i++){
344 keyArray.push( dir.options[i].value);
345 }
346
347 this._printStatus("Removing '" + (dir.options.selectedIndex - i) + "' keys starting with '" + dir.options[dir.options.selectedIndex].value + "' ...");
348 if(this.currentNamespace == dojox.storage.DEFAULT_NAMESPACE){
349 dojox.storage.removeMultiple(keyArray);
350 }else{
351 dojox.storage.removeMultiple(keyArray, this.currentNamespace);
352 }
353
354 // Remove the entries from the option list
355 var options = directory.childNodes;
356 for(i = dir.options.selectedIndex; i < dir.options.length && (i - dir.options.selectedIndex) < 10; i++){
357 directory.removeChild(options[i]);
358 }
359 }
360 },
361
362 printValueSize: function(){
363 var storageValue = dojo.byId("storageValue").value;
364 var size = 0;
365 if(storageValue != null && typeof storageValue != "undefined"){
366 size = storageValue.length;
367 }
368
369 // determine the units we are dealing with
370 var units;
371 if(size < 1024)
372 units = " bytes";
373 else{
374 units = " K";
375 size = size / 1024;
376 size = Math.round(size);
377 }
378
379 size = size + units;
380
381 var valueSize = dojo.byId("valueSize");
382 valueSize.innerHTML = size;
383 },
384
385 saveBook: function(evt){
386 this._printStatus("Loading book...");
387
388 var d = dojo.xhrGet({
389 url: "resources/testBook.txt",
390 handleAs: "text"
391 });
392
393 d.addCallback(dojo.hitch(this, function(results){
394 this._printStatus("Book loaded");
395 this._save("testBook", results);
396 }));
397
398 d.addErrback(dojo.hitch(this, function(error){
399 alert("Unable to load testBook.txt: " + error);
400 }));
401
402 if(!typeof evt != "undefined" && evt != null){
403 evt.preventDefault();
404 evt.stopPropagation();
405 }
406
407 return false;
408 },
409
410 saveXML: function(evt){
411 this._printStatus("Loading XML...");
412
413 var d = dojo.xhrGet({
414 url: "resources/testXML.xml",
415 handleAs: "text"
416 });
417
418 d.addCallback(dojo.hitch(this, function(results){
419 this._printStatus("XML loaded");
420 this._save("testXML", results);
421 }));
422
423 d.addErrback(dojo.hitch(this, function(error){
424 alert("Unable to load testXML.xml: " + error);
425 }));
426
427 if(!typeof evt != "undefined" && evt != null){
428 evt.preventDefault();
429 evt.stopPropagation();
430 }
431
432 return false;
433 },
434
435 _save: function(key, value, namespace){
436 this._printStatus("Saving '" + key + "'...");
437 var self = this;
438 var saveHandler = function(status, keyName){
439 //console.debug("saveHandler, status="+status+", keyName="+keyName);
440 if(status == dojox.storage.FAILED){
441 alert("You do not have permission to store data for this web site. "
442 + "Press the Configure button to grant permission.");
443 }else if(status == dojox.storage.SUCCESS){
444 // clear out the old value
445 dojo.byId("storageKey").value = "";
446 dojo.byId("storageValue").value = "";
447 self._printStatus("Saved '" + key + "'");
448
449 if(typeof namespace != "undefined"
450 && namespace != null){
451 self.currentNamespace = namespace;
452 }
453
454 self._printAvailableKeys();
455 self._printAvailableNamespaces();
456 }
457 };
458
459 try{
460 if(namespace == dojox.storage.DEFAULT_NAMESPACE){
461 dojox.storage.put(key, value, saveHandler);
462 }else{
463 dojox.storage.put(key, value, saveHandler, namespace);
464 }
465 }catch(exp){
466 alert(exp);
467 }
468 },
469
470 _printAvailableKeys: function(){
471 var directory = dojo.byId("directory");
472
473 // clear out any old keys
474 directory.innerHTML = "";
475
476 // add new ones
477 var availableKeys;
478 if(this.currentNamespace == dojox.storage.DEFAULT_NAMESPACE){
479 availableKeys = dojox.storage.getKeys();
480 }else{
481 availableKeys = dojox.storage.getKeys(this.currentNamespace);
482 }
483
484 for (var i = 0; i < availableKeys.length; i++){
485 var optionNode = document.createElement("option");
486 optionNode.appendChild(document.createTextNode(availableKeys[i]));
487 optionNode.value = availableKeys[i];
488 directory.appendChild(optionNode);
489 }
490 },
491
492 _handleLoadMultiple: function(keyArray){
493 this._printStatus("Loading '" + keyArray + "'...");
494
495 // get the value
496 var resultArray;
497 if(this.currentNamespace == dojox.storage.DEFAULT_NAMESPACE){
498 resultArray = dojox.storage.getMultiple(keyArray);
499 }else{
500 resultArray = dojox.storage.getMultiple(keyArray, this.currentNamespace);
501 }
502
503 // jsonify it if it is a JavaScript object
504 for(var i = 0; i < resultArray.length; i++) {
505 if(typeof resultArray[i] != "string"){
506 resultArray[i] = dojo.toJson(resultArray[i]);
507 }
508 }
509
510 // print out its value
511 this._printStatus("Loaded '" + keyArray[0] + "' and "
512 + (keyArray.length - 1) + " others");
513 dojo.byId("storageValue").value = "";
514 for(i = 0; i < resultArray.length; i++){
515 dojo.byId("storageValue").value += resultArray[i] + "\n----\n";
516 }
517
518 // print out the size of the value
519 this.printValueSize();
520 },
521
522 _printAvailableNamespaces: function(){
523 var namespacesDir = dojo.byId("namespaceDirectory");
524
525 // clear out any old namespaces
526 namespacesDir.innerHTML = "";
527
528 // add new ones
529 var availableNamespaces = dojox.storage.getNamespaces();
530 for (var i = 0; i < availableNamespaces.length; i++){
531 var optionNode = document.createElement("option");
532 optionNode.appendChild(document.createTextNode(availableNamespaces[i]));
533 optionNode.value = availableNamespaces[i];
534 if(this.currentNamespace == availableNamespaces[i]){
535 optionNode.selected = true;
536 }
537 namespacesDir.appendChild(optionNode);
538 }
539 },
540
541 _handleLoad: function(key){
542 this._printStatus("Loading '" + key + "'...");
543
544 // get the value
545 var results;
546 if(this.currentNamespace == dojox.storage.DEFAULT_NAMESPACE){
547 results = dojox.storage.get(key);
548 }else{
549 results = dojox.storage.get(key, this.currentNamespace);
550 }
551
552 // jsonify it if it is a JavaScript object
553 if(typeof results != "string"){
554 results = dojo.toJson(results);
555 }
556
557 // print out its value
558 this._printStatus("Loaded '" + key + "'");
559 dojo.byId("storageValue").value = results;
560
561 // print out the size of the value
562 this.printValueSize();
563 },
564
565 _printProviderMetadata: function(){
566 var storageType = dojox.storage.manager.currentProvider.declaredClass;
567 var isSupported = dojox.storage.isAvailable();
568 var maximumSize = dojox.storage.getMaximumSize();
569 var permanent = dojox.storage.isPermanent();
570 var uiConfig = dojox.storage.hasSettingsUI();
571 var moreInfo = "";
572 if(dojox.storage.manager.currentProvider.declaredClass
573 == "dojox.storage.FlashStorageProvider"){
574 moreInfo = "Flash Version " + dojox.flash.info.version;
575 }
576 dojo.byId("currentStorageProvider").innerHTML = storageType;
577 dojo.byId("isSupported").innerHTML = isSupported;
578 dojo.byId("isPersistent").innerHTML = permanent;
579 dojo.byId("hasUIConfig").innerHTML = uiConfig;
580 dojo.byId("maximumSize").innerHTML = maximumSize;
581 dojo.byId("moreInfo").innerHTML = moreInfo;
582 },
583
584 _printStatus: function(message){
585 // remove the old status
586 var top = dojo.byId("top");
587 for (var i = 0; i < top.childNodes.length; i++){
588 var currentNode = top.childNodes[i];
589 if (currentNode.nodeType == 1 &&
590 currentNode.className == "status"){
591 top.removeChild(currentNode);
592 }
593 }
594
595 var status = document.createElement("span");
596 status.className = "status";
597 status.innerHTML = message;
598
599 top.appendChild(status);
600 dojo.fadeOut({node: status, duration: 2000}).play();
601 },
602
603 _determineCurrentNamespace: function(){
604 this.currentNamespace = dojox.storage.DEFAULT_NAMESPACE;
605
606 // what is current namespace?
607 var availableNamespaces = dojox.storage.getNamespaces();
608 if(this.currentNamespace == dojox.storage.DEFAULT_NAMESPACE){
609 // do we even have the default namespace in our available namespaces?
610 var defaultPresent = false;
611 for(var i = 0; i < availableNamespaces.length; i++){
612 if(availableNamespaces[i] == dojox.storage.DEFAULT_NAMESPACE){
613 defaultPresent = true;
614 }
615 }
616
617 if(!defaultPresent && availableNamespaces.length){
618 this.currentNamespace = availableNamespaces[0];
619 }
620 }
621 },
622
623 _printAvailableProviders: function(){
624 // it is scary that this timeout is needed; if it is not present,
625 // the options don't appear on Firefox and Safari, even though the
626 // page is finished loading! it might have to do with some strange
627 // interaction where our initialize method is called from ExternalInterface,
628 // which originated inside of Flash. -- Brad Neuberg
629 window.setTimeout(function(){
630 var selector = dojo.byId("currentStorageProvider");
631 var p = dojox.storage.manager.providers;
632 for(var i = 0; i < p.length; i++){
633 var name = p[i].declaredClass;
634 var o = document.createElement("option");
635 o.appendChild(document.createTextNode(name));
636 o.value = name;
637 if(dojox.storage.manager.currentProvider == p[i]){
638 o.selected = true;
639 }
640
641 selector.appendChild(o);
642 }
643 }, 1);
644 }
645 };
646
647 // wait until the storage system is finished loading
648 dojo.addOnLoad(function(){
649 // is the storage already loaded?
650 if(dojox.storage.manager.isInitialized() == false){
651 dojo.connect(dojox.storage.manager, "loaded", TestStorage, TestStorage.initialize);
652 }else{
653 dojo.connect(dojo, "loaded", TestStorage, TestStorage.initialize);
654 }
655 });