diff openwebchat.js @ 69:9004f7daf4c4

Abstracted away localStorage into a generic storage interface to make this easier to port to other browsers.
author Atul Varma <varmaa@toolness.com>
date Thu, 30 Apr 2009 12:38:55 -0700
parents 84bf6a3ac4ec
children 91767ca52ca9
line wrap: on
line diff
--- a/openwebchat.js	Thu Apr 30 11:57:28 2009 -0700
+++ b/openwebchat.js	Thu Apr 30 12:38:55 2009 -0700
@@ -1,30 +1,52 @@
-var OpenWebChat = {
-  ClientStorage: function ClientStorage(localStorage, prefix) {
+var OpenWebStorage = {
+  UnsupportedError: function UnsupportedError() {},
+
+  DOMLocalStorage: function DOMLocalStorage() {
     var self = this;
-    if (!localStorage[prefix + 'length'])
-      localStorage[prefix + 'length'] = '0';
+    var localStorage = window.localStorage;
+
+    if (window.globalStorage)
+      localStorage = window.globalStorage[document.location.hostname];
+
+    if (!localStorage)
+      throw new OpenWebStorage.UnsupportedError();
+
+    self.set = function set(key, value) {
+      localStorage[key] = JSON.stringify(value);
+    };
+
+    self.has = function has(key) {
+      if (localStorage[key])
+        return true;
+      return false;
+    };
 
-    self.length = Number(localStorage[prefix + 'length'].value);
+    self.get = function get(key, defaultValue) {
+      var value = defaultValue;
+
+      if (localStorage[key])
+        try {
+          value = JSON.parse(localStorage[key].value);
+        } catch (e) {}
+
+      return value;
+    };
+  }
+};
+
+var OpenWebChat = {
+  ClientStorage: function ClientStorage(owStorage, prefix) {
+    var self = this;
+    self.length = owStorage.get(prefix + 'length', 0);
 
     self.get = function get(id) {
-      return JSON.parse(localStorage[prefix + id].value);
+      return owStorage.get(prefix + id);
     };
 
     self.append = function append(msg) {
-      localStorage[prefix + self.length] = JSON.stringify(msg);
+      owStorage.set(prefix + self.length, msg);
       self.length += 1;
-      localStorage[prefix + 'length'] = self.length.toString();
-    };
-
-    self.wipe = function wipe() {
-      var names = [];
-      for (name in localStorage)
-        names.push(name);
-      names.forEach(
-        function(name) {
-          if (name.indexOf(prefix) == 0)
-            delete localStorage[name];
-        });
+      owStorage.set(prefix + 'length', self.length);
     };
   },
 
@@ -85,30 +107,24 @@
 
     var animatingMessages = 0;
 
-    var localStorage = globalStorage[document.location.hostname];
+    var owStorage = new OpenWebStorage.DOMLocalStorage();
 
     var convStorage = new OpenWebChat.ClientStorage(
-      localStorage,
+      owStorage,
       '/conv' + document.location.pathname
     );
 
-    if (!localStorage.name)
-      localStorage.name = "A Mysterious Stranger";
-
-    if (!localStorage.lastMessage)
-      localStorage.lastMessage = "";
-
-    $('#name').val(localStorage.name.value);
+    $('#name').val(owStorage.get('name', "A Mysterious Stranger"));
     $('#name').blur(
       function() {
-        localStorage.name = $(this).val();
+        owStorage.set('name', $(this).val());
       });
 
-    $('#outgoing-message').val(localStorage.lastMessage.value);
+    $('#outgoing-message').val(owStorage.get('lastMessage', ''));
     $('#outgoing-message').blur(
       function() {
         if ($(this).val())
-          localStorage.lastMessage = $(this).val();
+          owStorage.set('lastMessage', $(this).val());
       });
 
     $('#outgoing-message').focus();
@@ -120,7 +136,7 @@
         var author = $('#name').val();
         if (evt.keyCode == ENTER_KEYCODE) {
           if (content) {
-            localStorage.lastMessage = content;
+            owStorage.set('lastMessage', content);
             $(this).val('');
             var msg = {content: content,
                        time: new Date()};
@@ -140,8 +156,8 @@
     function onMessage(msg) {
       var block = $('#templates .message').clone();
 
-      if (localStorage.lastMessage.value == msg.content)
-        localStorage.lastMessage = "";
+      if (owStorage.get('lastMessage') == msg.content)
+        owStorage.set('lastMessage', '');
 
       $('.content', block).html(msg.content);