changeset 70:91767ca52ca9

Added support for safari via a new FakeStorage class (in the future we can make one that draws from sqlite).
author Atul Varma <varmaa@toolness.com>
date Thu, 30 Apr 2009 12:50:40 -0700
parents 9004f7daf4c4
children 801c45a9063a
files openwebchat.js
diffstat 1 files changed, 29 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/openwebchat.js	Thu Apr 30 12:38:55 2009 -0700
+++ b/openwebchat.js	Thu Apr 30 12:50:40 2009 -0700
@@ -1,6 +1,24 @@
 var OpenWebStorage = {
   UnsupportedError: function UnsupportedError() {},
 
+  FakeStorage: function FakeStorage() {
+    var contents = {};
+
+    function deepCopy(obj) {
+      return JSON.parse(JSON.stringify(obj));
+    }
+
+    this.set = function set(key, value) {
+      contents[key] = deepCopy(value);
+    };
+
+    this.get = function get(key, defaultValue) {
+      if (key in contents)
+        return deepCopy(contents[key]);
+      return defaultValue;
+    };
+  },
+
   DOMLocalStorage: function DOMLocalStorage() {
     var self = this;
     var localStorage = window.localStorage;
@@ -9,18 +27,12 @@
       localStorage = window.globalStorage[document.location.hostname];
 
     if (!localStorage)
-      throw new OpenWebStorage.UnsupportedError();
+      throw new OpenWebStorage.UnsupportedError('no local DOM storage');
 
     self.set = function set(key, value) {
       localStorage[key] = JSON.stringify(value);
     };
 
-    self.has = function has(key) {
-      if (localStorage[key])
-        return true;
-      return false;
-    };
-
     self.get = function get(key, defaultValue) {
       var value = defaultValue;
 
@@ -107,7 +119,16 @@
 
     var animatingMessages = 0;
 
-    var owStorage = new OpenWebStorage.DOMLocalStorage();
+    var owStorage;
+
+    try {
+      owStorage = new OpenWebStorage.DOMLocalStorage();
+    } catch (e) {
+      // TODO: Can we make this explicitly catch
+      // only if (e instanceof OpenWebStorage.UnsupportedError)
+      // and have it work on Safari?
+      owStorage = new OpenWebStorage.FakeStorage();
+    }
 
     var convStorage = new OpenWebChat.ClientStorage(
       owStorage,