changeset 77:1c99c08d2a54

Sort of simplified message display a bit
author Atul Varma <varmaa@toolness.com>
date Thu, 30 Apr 2009 17:47:32 -0700
parents 16a18e3a1098
children 0effc4481d18
files media/js/openwebchat.js
diffstat 1 files changed, 49 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/media/js/openwebchat.js	Thu Apr 30 16:29:36 2009 -0700
+++ b/media/js/openwebchat.js	Thu Apr 30 17:47:32 2009 -0700
@@ -117,10 +117,6 @@
     var ENTER_KEYCODE = 13;
     var RETURN_KEYCODE = 10;
 
-    var MAX_ANIMATING_MESSAGES = 3;
-
-    var animatingMessages = 0;
-
     var owStorage;
 
     try {
@@ -152,42 +148,45 @@
 
     $('#outgoing-message').focus();
 
-      function onKey(evt) {
-        var self = this;
-        var content = $(this).val();
-        var author = $('#name').val();
-        if (evt.keyCode == ENTER_KEYCODE ||
-            evt.keyCode == RETURN_KEYCODE) {
-          if (content) {
-            owStorage.set('lastMessage', content);
-            $(this).val('');
-            var msg = {content: content,
-                       time: new Date()};
-            if (author)
-              msg.author = author;
-            OpenWebChat.sendMessage(
-              {message: msg,
-               onError: function(exception) {
-                 $(self).val(content);
-               }
-              });
-          }
-          evt.preventDefault();
+    function onKey(evt) {
+      var self = this;
+      var content = $(this).val();
+      var author = $('#name').val();
+      if (evt.keyCode == ENTER_KEYCODE ||
+          evt.keyCode == RETURN_KEYCODE) {
+        if (content) {
+          owStorage.set('lastMessage', content);
+          $(this).val('');
+          var msg = {content: content,
+                     time: new Date()};
+          if (author)
+            msg.author = author;
+          OpenWebChat.sendMessage(
+            {message: msg,
+             onError: function(exception) {
+               $(self).val(content);
+             }
+            });
         }
+        evt.preventDefault();
       }
+    }
 
     $('#outgoing-message').keydown(onKey);
     $('#outgoing-message').keyup(onKey);
 
+    function scrollToBottom() {
+      window.scrollTo(0, $('#bottom').position().top);
+    }
+
     function onMessage(msg, doAnimation) {
       if (typeof(doAnimation) == "undefined")
         doAnimation = true;
 
-      var block = $('#templates .message').clone();
-
       if (owStorage.get('lastMessage') == msg.content)
         owStorage.set('lastMessage', '');
 
+      var block = $('#templates .message').clone();
       $('.content', block).html(msg.content);
 
       var author = msg.author ? msg.author : 'Anonymous';
@@ -198,50 +197,32 @@
 
       $('.timestamp', block).text(msg.time);
 
-      block.hide();
+      if (doAnimation)
+        block.hide();
+
       $('#incoming-messages').append(block);
 
-      if (doAnimation && animatingMessages < MAX_ANIMATING_MESSAGES) {
-        animatingMessages += 1;
-
-        block.slideDown(
-          function() {
-            animatingMessages -= 1;
-            window.scrollTo(0, $('#bottom').position().top);
-          });
-      } else {
-        block.show();
-        window.scrollTo(0, $('#bottom').position().top);
-      }
+      if (doAnimation)
+        block.slideDown(scrollToBottom);
     }
 
-    var currStoredMessage = 0;
-    var CHUNK_SIZE = 100;
-    var UI_BREATHE_TIME = 10;
+    $('#incoming-messages').hide();
+
+    for (var i = 0; i < convStorage.length; i++)
+      onMessage(convStorage.get(i), false);
+
+    $('#incoming-messages').slideDown();
 
-    function showStoredConversation() {
-      if (currStoredMessage < convStorage.length) {
-        var i = 0;
-        while (i < CHUNK_SIZE && currStoredMessage < convStorage.length) {
-          onMessage(convStorage.get(currStoredMessage), false);
-          currStoredMessage += 1;
-          i += 1;
-        }
-        window.setTimeout(showStoredConversation, UI_BREATHE_TIME);
-      } else
-        OpenWebChat.listenForMessages(
-          {storage: convStorage,
-           onMessage: onMessage,
-           onError: function onError(exception) {
-             if (window.console)
-               window.console.log('The error', exception, 'occurred.');
-             var error = $('#templates .error').clone();
-             error.hide();
-             $('#incoming-messages').append(error);
-             error.slideDown();
-           }
-          });
-    }
-
-    showStoredConversation();
+    OpenWebChat.listenForMessages(
+      {storage: convStorage,
+       onMessage: onMessage,
+       onError: function onError(exception) {
+         if (window.console)
+           window.console.log('The error', exception, 'occurred.');
+         var error = $('#templates .error').clone();
+         error.hide();
+         $('#incoming-messages').append(error);
+         error.slideDown();
+       }
+      });
   });