comparison openwebchat.js @ 62:b7b9932823e4

We now store conversations in local/globalStorage.
author Atul Varma <varmaa@toolness.com>
date Wed, 29 Apr 2009 06:15:32 -0700
parents c1b46b60e838
children 559c48a58254
comparison
equal deleted inserted replaced
61:c1b46b60e838 62:b7b9932823e4
1 var OpenWebChat = { 1 var OpenWebChat = {
2 ClientStorage: function ClientStorage(localStorage, prefix) {
3 var self = this;
4 if (!localStorage[prefix + 'length'])
5 localStorage[prefix + 'length'] = '0';
6
7 self.length = Number(localStorage[prefix + 'length'].value);
8
9 self.get = function get(id) {
10 return JSON.parse(localStorage[prefix + id].value);
11 };
12
13 self.append = function append(msg) {
14 localStorage[prefix + self.length] = JSON.stringify(msg);
15 self.length += 1;
16 localStorage[prefix + 'length'] = self.length.toString();
17 };
18
19 self.wipe = function wipe() {
20 var names = [];
21 for (name in localStorage)
22 names.push(name);
23 names.forEach(
24 function(name) {
25 if (name.indexOf(prefix) == 0)
26 delete localStorage[name];
27 });
28 };
29 },
30
2 startMessageListener: function startMessageListener(options) { 31 startMessageListener: function startMessageListener(options) {
3 var req = new XMLHttpRequest(); 32 var req = new XMLHttpRequest();
4 req.multipart = true; 33 req.multipart = true;
5 req.open('GET', 'listen/multipart?start=0', true); 34 req.open('GET',
35 'listen/multipart?start=' + options.storage.length,
36 true);
6 req.overrideMimeType('application/json'); 37 req.overrideMimeType('application/json');
7 req.addEventListener( 38 req.addEventListener(
8 "error", 39 "error",
9 function() { 40 function() {
10 options.onError(null); 41 options.onError(null);
20 msg = JSON.parse(req.responseText); 51 msg = JSON.parse(req.responseText);
21 } catch (e) { 52 } catch (e) {
22 options.onError(e); 53 options.onError(e);
23 errorOccurred = true; 54 errorOccurred = true;
24 } 55 }
25 if (!errorOccurred) 56 if (!errorOccurred) {
57 options.storage.append(msg);
26 options.onMessage(msg); 58 options.onMessage(msg);
59 }
27 }, 60 },
28 false 61 false
29 ); 62 );
30 req.send(null); 63 req.send(null);
31 }, 64 },
51 var MAX_ANIMATING_MESSAGES = 3; 84 var MAX_ANIMATING_MESSAGES = 3;
52 85
53 var animatingMessages = 0; 86 var animatingMessages = 0;
54 87
55 var localStorage = globalStorage[document.location.hostname]; 88 var localStorage = globalStorage[document.location.hostname];
89
90 var convStorage = new OpenWebChat.ClientStorage(
91 localStorage,
92 '/conv' + document.location.pathname
93 );
94
56 if (!localStorage.name) 95 if (!localStorage.name)
57 localStorage.name = "A Mysterious Stranger"; 96 localStorage.name = "A Mysterious Stranger";
58 97
59 if (!localStorage.lastMessage) 98 if (!localStorage.lastMessage)
60 localStorage.lastMessage = ""; 99 localStorage.lastMessage = "";
96 } 135 }
97 evt.preventDefault(); 136 evt.preventDefault();
98 } 137 }
99 }); 138 });
100 139
101 OpenWebChat.startMessageListener( 140 function onMessage(msg) {
102 {onMessage: function onMessage(msg) { 141 var block = $('#templates .message').clone();
103 var block = $('#templates .message').clone(); 142
104 143 if (localStorage.lastMessage.value == msg.content)
105 if (localStorage.lastMessage.value == msg.content) 144 localStorage.lastMessage = "";
106 localStorage.lastMessage = ""; 145
107 146 $('.content', block).html(msg.content);
108 $('.content', block).html(msg.content); 147
109 148 var author = msg.author ? msg.author : 'Anonymous';
110 var author = msg.author ? msg.author : 'Anonymous'; 149 if (author != $('#content .author:last').text())
111 if (author != $('#content .author:last').text()) 150 $('.author', block).text(author);
112 $('.author', block).text(author); 151 else
113 else 152 $('.author', block).remove();
114 $('.author', block).remove(); 153
115 154 $('.timestamp', block).text(msg.time);
116 $('.timestamp', block).text(msg.time); 155
117 156 block.hide();
118 block.hide(); 157 $('#incoming-messages').append(block);
119 $('#incoming-messages').append(block); 158
120 159 if (animatingMessages < MAX_ANIMATING_MESSAGES) {
121 if (animatingMessages < MAX_ANIMATING_MESSAGES) { 160 animatingMessages += 1;
122 animatingMessages += 1; 161
123 162 block.slideDown(
124 block.slideDown( 163 function() {
125 function() { 164 animatingMessages -= 1;
126 animatingMessages -= 1; 165 window.scrollTo(0, $('#bottom').position().top);
127 window.scrollTo(0, $('#bottom').position().top); 166 });
128 }); 167 } else
129 } else 168 block.show();
130 block.show(); 169 }
131 }, 170
132 onError: function onError(exception) { 171 var currStoredMessage = 0;
133 if (window.console) 172 var CHUNK_SIZE = 20;
134 window.console.log('The error', exception, 'occurred.'); 173 var UI_BREATHE_TIME = 10;
135 var error = $('#templates .error').clone(); 174
136 error.hide(); 175 function showStoredConversation() {
137 $('#incoming-messages').append(error); 176 if (currStoredMessage < convStorage.length) {
138 error.slideDown(); 177 var i = 0;
139 } 178 while (i < CHUNK_SIZE && currStoredMessage < convStorage.length) {
140 }); 179 onMessage(convStorage.get(currStoredMessage));
180 currStoredMessage += 1;
181 i += 1;
182 }
183 window.setTimeout(showStoredConversation, UI_BREATHE_TIME);
184 } else
185 OpenWebChat.startMessageListener(
186 {storage: convStorage,
187 onMessage: onMessage,
188 onError: function onError(exception) {
189 if (window.console)
190 window.console.log('The error', exception, 'occurred.');
191 var error = $('#templates .error').clone();
192 error.hide();
193 $('#incoming-messages').append(error);
194 error.slideDown();
195 }
196 });
197 }
198
199 showStoredConversation();
141 }); 200 });