Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
dojo.require("dojox.storage");
2
 
3
 
4
var TestStorage = {
5
	currentProvider: "default",
6
	currentNamespace: dojox.storage.DEFAULT_NAMESPACE,
7
 
8
	initialize: function(){
9
		//console.debug("test_storage.initialize()");
10
 
11
		// do we even have a storage provider?
12
		if(dojox.storage.manager.available == false){
13
			alert("No storage provider is available on this browser");
14
			return;
15
		}
16
 
17
		// clear out old values and enable input forms
18
		dojo.byId("storageNamespace").value = this.currentNamespace;
19
		dojo.byId("storageNamespace").disabled = false;
20
		dojo.byId("storageKey").value = "";
21
		dojo.byId("storageKey").disabled = false;
22
		dojo.byId("storageValue").value = "";
23
		dojo.byId("storageValue").disabled = false;
24
 
25
		// write out our available namespaces
26
		this._printAvailableNamespaces();
27
 
28
		// write out our available keys
29
		this._printAvailableKeys();
30
 
31
		// initialize our event handlers
32
		var namespaceDirectory = dojo.byId("namespaceDirectory");
33
		dojo.connect(namespaceDirectory, "onchange", this, this.namespaceChange);
34
		var directory = dojo.byId("directory");
35
		dojo.connect(directory, "onchange", this, this.directoryChange);
36
		var storageValueElem = dojo.byId("storageValue");
37
		dojo.connect(storageValueElem, "onkeyup", this, this.printValueSize);
38
 
39
		// make the directory be unselected if the key name field gets focus
40
		var keyNameField = dojo.byId("storageKey");
41
		dojo.connect(keyNameField, "onfocus", function(evt){
42
			directory.selectedIndex = -1;
43
		});
44
 
45
		// add onclick listeners to all of our buttons
46
		var buttonContainer = dojo.byId("buttonContainer");
47
		var currentChild = buttonContainer.firstChild;
48
		while(currentChild.nextSibling != null){
49
			if(currentChild.nodeType == 1){
50
				var buttonName = currentChild.id;
51
				var functionName = buttonName.match(/^(.*)Button$/)[1];
52
				dojo.connect(currentChild, "onclick", this, this[functionName]);
53
				currentChild.disabled = false;
54
			}
55
 
56
			currentChild = currentChild.nextSibling;
57
		}
58
 
59
		// print out metadata
60
		this._printProviderMetadata();
61
 
62
		// disable the configuration button if none is supported for this provider
63
		if(dojox.storage.hasSettingsUI() == false){
64
			dojo.byId("configureButton").disabled = true;
65
		}
66
	},
67
 
68
	namespaceChange: function(evt){
69
		var ns = evt.target.value;
70
		this.currentNamespace = ns;
71
 
72
		// update our available keys
73
		this._printAvailableKeys();
74
 
75
		// clear out our key and values
76
		dojo.byId("storageNamespace").value = this.currentNamespace;
77
		dojo.byId("storageKey").value = "";
78
		dojo.byId("storageValue").value = "";
79
	},
80
 
81
	directoryChange: function(evt){
82
		var key = evt.target.value;
83
 
84
		// add this value into the form
85
		var keyNameField = dojo.byId("storageKey");
86
		keyNameField.value = key;
87
 
88
		this._handleLoad(key);
89
	},
90
 
91
	load: function(evt){
92
		// cancel the button's default behavior
93
		evt.preventDefault();
94
		evt.stopPropagation();
95
 
96
		// get the key to load
97
		var key = dojo.byId("storageKey").value;
98
 
99
		if(key == null || typeof key == "undefined" || key == ""){
100
			alert("Please enter a key name");
101
			return;
102
		}
103
 
104
		this._handleLoad(key);
105
	},
106
 
107
	save: function(evt){
108
		// cancel the button's default behavior
109
		evt.preventDefault();
110
		evt.stopPropagation();
111
 
112
		// get the new values
113
		var key = dojo.byId("storageKey").value;
114
		var value = dojo.byId("storageValue").value;
115
		var namespace = dojo.byId("storageNamespace").value;
116
 
117
		if(key == null || typeof key == "undefined" || key == ""){
118
			alert("Please enter a key name");
119
			return;
120
		}
121
 
122
		if(value == null || typeof value == "undefined" || value == ""){
123
			alert("Please enter a key value");
124
			return;
125
		}
126
 
127
		// print out the size of the value
128
		this.printValueSize();
129
 
130
		// do the save
131
		this._save(key, value, namespace);
132
	},
133
 
134
	clearNamespace: function(evt){
135
		// cancel the button's default behavior
136
		evt.preventDefault();
137
		evt.stopPropagation();
138
 
139
		dojox.storage.clear(this.currentNamespace);
140
 
141
		this._printAvailableNamespaces();
142
		this._printAvailableKeys();
143
	},
144
 
145
	configure: function(evt){
146
		// cancel the button's default behavior
147
		evt.preventDefault();
148
		evt.stopPropagation();
149
 
150
		if(dojox.storage.hasSettingsUI()){
151
			// redraw our keys after the dialog is closed, in
152
			// case they have all been erased
153
			var self = this;
154
			dojox.storage.onHideSettingsUI = function(){
155
				self._printAvailableKeys();
156
			}
157
 
158
			// show the dialog
159
			dojox.storage.showSettingsUI();
160
		}
161
	},
162
 
163
	remove: function(evt){
164
		// cancel the button's default behavior
165
		evt.preventDefault();
166
		evt.stopPropagation();
167
 
168
		// determine what key to delete; if the directory has a selected value,
169
		// use that; otherwise, use the key name field
170
		var directory = dojo.byId("directory");
171
		var keyNameField = dojo.byId("storageKey");
172
		var keyValueField = dojo.byId("storageValue");
173
		var key;
174
		if(directory.selectedIndex != -1){
175
			key = directory.value;
176
			// delete this option
177
			var options = directory.childNodes;
178
			for(var i = 0; i < options.length; i++){
179
				if(options[i].nodeType == 1 &&
180
					 options[i].value == key){
181
					directory.removeChild(options[i]);
182
					break;
183
				}
184
			}
185
		}else{
186
			key = keyNameField.value;
187
		}
188
 
189
		keyNameField.value = "";
190
		keyValueField.value = "";
191
 
192
		// now delete the value
193
		this._printStatus("Removing '" + key + "'...");
194
		if(this.currentNamespace == dojox.storage.DEFAULT_NAMESPACE){
195
			dojox.storage.remove(key);
196
		}else{
197
			dojox.storage.remove(key, this.currentNamespace);
198
		}
199
 
200
		// update our UI
201
		this._printAvailableNamespaces();
202
		this._printStatus("Removed '" + key);
203
	},
204
 
205
	printValueSize: function(){
206
		var storageValue = dojo.byId("storageValue").value;
207
		var size = 0;
208
		if(storageValue != null && typeof storageValue != "undefined"){
209
			size = storageValue.length;
210
		}
211
 
212
		// determine the units we are dealing with
213
		var units;
214
		if(size < 1024)
215
			units = " bytes";
216
		else{
217
			units = " K";
218
			size = size / 1024;
219
			size = Math.round(size);
220
		}
221
 
222
		size = size + units;
223
 
224
		var valueSize = dojo.byId("valueSize");
225
		valueSize.innerHTML = size;
226
	},
227
 
228
	saveBook: function(evt){
229
		this._printStatus("Loading book...");
230
 
231
		var d = dojo.xhrGet({
232
			url: "resources/testBook.txt",
233
			handleAs: "text"
234
		});
235
 
236
		d.addCallback(dojo.hitch(this, function(results){
237
			this._printStatus("Book loaded");
238
			this._save("testBook", results);
239
		}));
240
 
241
		d.addErrback(dojo.hitch(this, function(error){
242
			alert("Unable to load testBook.txt: " + error);
243
		}));
244
 
245
		if(!typeof evt != "undefined" && evt != null){
246
			evt.preventDefault();
247
			evt.stopPropagation();
248
		}
249
 
250
		return false;
251
	},
252
 
253
	saveXML: function(evt){
254
		this._printStatus("Loading XML...");
255
 
256
		var d = dojo.xhrGet({
257
			url: "resources/testXML.xml",
258
			handleAs: "text"
259
		});
260
 
261
		d.addCallback(dojo.hitch(this, function(results){
262
			this._printStatus("XML loaded");
263
			this._save("testXML", results);
264
		}));
265
 
266
		d.addErrback(dojo.hitch(this, function(error){
267
			alert("Unable to load testXML.xml: " + error);
268
		}));
269
 
270
		if(!typeof evt != "undefined" && evt != null){
271
			evt.preventDefault();
272
			evt.stopPropagation();
273
		}
274
 
275
		return false;
276
	},
277
 
278
	_save: function(key, value, namespace){
279
		this._printStatus("Saving '" + key + "'...");
280
		var self = this;
281
		var saveHandler = function(status, keyName){
282
			if(status == dojox.storage.FAILED){
283
				alert("You do not have permission to store data for this web site. "
284
			        + "Press the Configure button to grant permission.");
285
			}else if(status == dojox.storage.SUCCESS){
286
				// clear out the old value
287
				dojo.byId("storageKey").value = "";
288
				dojo.byId("storageValue").value = "";
289
				self._printStatus("Saved '" + key + "'");
290
 
291
				if(typeof namespace != "undefined"
292
					&& namespace != null){
293
					self.currentNamespace = namespace;
294
				}
295
 
296
				// update the list of available keys and namespaces
297
				// put this on a slight timeout, because saveHandler is called back
298
				// from Flash, which can cause problems in Flash 8 communication
299
				// which affects Safari
300
				// FIXME: Find out what is going on in the Flash 8 layer and fix it
301
				// there
302
				window.setTimeout(function(){
303
					self._printAvailableKeys();
304
					self._printAvailableNamespaces();
305
				}, 1);
306
			}
307
		};
308
 
309
		try{
310
			if(namespace == dojox.storage.DEFAULT_NAMESPACE){
311
				dojox.storage.put(key, value, saveHandler);
312
			}else{
313
				dojox.storage.put(key, value, saveHandler, namespace);
314
			}
315
		}catch(exp){
316
			alert(exp);
317
		}
318
	},
319
 
320
	_printAvailableKeys: function(){
321
		var directory = dojo.byId("directory");
322
 
323
		// clear out any old keys
324
		directory.innerHTML = "";
325
 
326
		// add new ones
327
		var availableKeys;
328
		if(this.currentNamespace == dojox.storage.DEFAULT_NAMESPACE){
329
			availableKeys = dojox.storage.getKeys();
330
		}else{
331
			availableKeys = dojox.storage.getKeys(this.currentNamespace);
332
		}
333
 
334
		for (var i = 0; i < availableKeys.length; i++){
335
			var optionNode = document.createElement("option");
336
			optionNode.appendChild(document.createTextNode(availableKeys[i]));
337
			optionNode.value = availableKeys[i];
338
			directory.appendChild(optionNode);
339
		}
340
	},
341
 
342
	_printAvailableNamespaces: function(){
343
		var namespacesDir = dojo.byId("namespaceDirectory");
344
 
345
		// clear out any old namespaces
346
		namespacesDir.innerHTML = "";
347
 
348
		// add new ones
349
		var availableNamespaces = dojox.storage.getNamespaces();
350
 
351
		for (var i = 0; i < availableNamespaces.length; i++){
352
			var optionNode = document.createElement("option");
353
			optionNode.appendChild(document.createTextNode(availableNamespaces[i]));
354
			optionNode.value = availableNamespaces[i];
355
			namespacesDir.appendChild(optionNode);
356
		}
357
	},
358
 
359
	_handleLoad: function(key){
360
		this._printStatus("Loading '" + key + "'...");
361
 
362
		// get the value
363
		var results;
364
		if(this.currentNamespace == dojox.storage.DEFAULT_NAMESPACE){
365
			results = dojox.storage.get(key);
366
		}else{
367
			results = dojox.storage.get(key, this.currentNamespace);
368
		}
369
 
370
		// jsonify it if it is a JavaScript object
371
		if(typeof results != "string"){
372
			results = dojo.toJson(results);
373
		}
374
 
375
		// print out its value
376
		this._printStatus("Loaded '" + key + "'");
377
		dojo.byId("storageValue").value = results;
378
 
379
		// print out the size of the value
380
		this.printValueSize();
381
	},
382
 
383
	_printProviderMetadata: function(){
384
		var storageType = dojox.storage.manager.currentProvider.declaredClass;
385
		var isSupported = dojox.storage.isAvailable();
386
		var maximumSize = dojox.storage.getMaximumSize();
387
		var permanent = dojox.storage.isPermanent();
388
		var uiConfig = dojox.storage.hasSettingsUI();
389
		var moreInfo = "";
390
		if(dojox.storage.manager.currentProvider.declaredClass
391
				== "dojox.storage.FlashStorageProvider"){
392
			moreInfo = "Flash Comm Version " + dojo.flash.info.commVersion;
393
		}
394
 
395
		dojo.byId("currentStorageProvider").innerHTML = storageType;
396
		dojo.byId("isSupported").innerHTML = isSupported;
397
		dojo.byId("isPersistent").innerHTML = permanent;
398
		dojo.byId("hasUIConfig").innerHTML = uiConfig;
399
		dojo.byId("maximumSize").innerHTML = maximumSize;
400
		dojo.byId("moreInfo").innerHTML = moreInfo;
401
	},
402
 
403
	_printStatus: function(message){
404
		// remove the old status
405
		var top = dojo.byId("top");
406
		for (var i = 0; i < top.childNodes.length; i++){
407
			var currentNode = top.childNodes[i];
408
			if (currentNode.nodeType == 1 &&
409
					currentNode.className == "status"){
410
				top.removeChild(currentNode);
411
			}
412
		}
413
 
414
		var status = document.createElement("span");
415
		status.className = "status";
416
		status.innerHTML = message;
417
 
418
		top.appendChild(status);
419
		dojo.fadeOut({node: status, duration: 2000}).play();
420
	}
421
};
422
 
423
// wait until the storage system is finished loading
424
if(dojox.storage.manager.isInitialized() == false){ // storage might already be loaded when we get here
425
	dojo.connect(dojox.storage.manager, "loaded", TestStorage, TestStorage.initialize);
426
}else{
427
	dojo.connect(dojo, "loaded", TestStorage, TestStorage.initialize);
428
}