747aa3c1f4e554767b66c02da1189bc55dc0af7f.svn-base 12 KB

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