/[drupal]/contributions/modules/chatroom/chatroom.js
ViewVC logotype

Contents of /contributions/modules/chatroom/chatroom.js

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.51 - (show annotations) (download) (as text)
Thu Oct 29 21:41:18 2009 UTC (4 weeks, 2 days ago) by justinrandell
Branch: MAIN
Changes since 1.50: +92 -615 lines
File MIME type: text/javascript
pulling in changes from dev branch to head
1 // $Id: chatroom.js,v 1.50.4.9 2009/10/29 21:13:48 justinrandell Exp $
2
3 Drupal.chatroom = Drupal.chatroom || {};
4
5 /**
6 * Add behaviours to chatroom elements.
7 */
8 Drupal.behaviors.chatroom = function(context) {
9
10 // This is the 'are there any new messages' polling.
11 setInterval("Drupal.chatroom.poll()", Drupal.settings.chatroom.pollInterval * 1000);
12
13 // Add behaviour to the 'send a message' field.
14 $('#chatroom-chat-message-submit').keyup(function (e) {
15 var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
16 var messageText = $('#chatroom-chat-message-submit').val();
17 if (key == 13) {
18 if (messageText) {
19 Drupal.chatroom.postMessage(messageText);
20 $('#chatroom-chat-message-submit').val('');
21 }
22 }
23 });
24 if (Drupal.settings.chatroom.latestMsgId > 0) {
25 var boardOffset = $('#chatroom-board').offset().top;
26 var targetOffset = $('div.new-message:last').offset().top;
27 var scrollAmount = targetOffset - boardOffset;
28 $('#chatroom-board').animate({scrollTop: '+='+ scrollAmount +'px'}, 500);
29 $('.new-message').removeClass('new-message');
30 }
31
32 Drupal.settings.chatroom.pageTitle = document.title;
33 Drupal.settings.chatroom.hasFocus = true;
34 $(self).focus(
35 function() {
36 clearInterval(Drupal.settings.chatroom.warnInterval);
37 Drupal.settings.chatroom.hasFocus = true;
38 document.title = Drupal.settings.chatroom.pageTitle;
39 }
40 );
41 $(self).blur(
42 function() {
43 Drupal.settings.chatroom.hasFocus = false;
44 }
45 );
46 };
47
48 Drupal.chatroom.poll = function() {
49
50 var skipCacheCheck = 0;
51 if (Drupal.settings.chatroom.successiveCacheHits > Drupal.settings.chatroom.skipCacheCheckCount) {
52 skipCacheCheck = 1;
53 }
54
55 $.ajax({
56 type: 'POST',
57 url: Drupal.settings.basePath + 'chatroomread.php',
58 dataType: 'json',
59 success: Drupal.chatroom.pollHandler,
60 data: {
61 latest_msg_id: Drupal.settings.chatroom.latestMsgId,
62 chat_cache_directory: Drupal.settings.chatroom.cacheDirectory,
63 chat_id: Drupal.settings.chatroom.chatId,
64 skip_cache: skipCacheCheck,
65 successive_cache_hits: Drupal.settings.chatroom.successiveCacheHits,
66 }
67 });
68 };
69
70 Drupal.chatroom.pollHandler = function(response, responseStatus) {
71
72 // If the chat was archived, reload the page.
73 if (response.data.archived) {
74 window.location = Drupal.settings.basePath + Drupal.settings.chatroom.chatPath;
75 }
76
77 // If we hit the cache, then keep track of that. If the number of
78 // successive cache hits gets high enough, we may want to signal to the
79 // server that we should skip the cache check so that our online time
80 // gets updated.
81 if (response.data.cacheHit) {
82 Drupal.settings.chatroom.successiveCacheHits++;
83 }
84 else {
85 Drupal.settings.chatroom.successiveCacheHits = 0;
86 }
87
88 // Add any messages we haven't already seen to the board. Poll requests can
89 // pass each other over the wire, so we can't rely on getting a given
90 // message once only.
91 var newMessage = false;
92 for (var i = 0; i < response.data.messages.length; i++) {
93 if (response.data.messages[i].cmid > Drupal.settings.chatroom.latestMsgId) {
94 $('#chatroom-board').append(response.data.messages[i].html);
95 Drupal.settings.chatroom.latestMsgId = response.data.messages[i].cmid;
96 newMessage = response.data.messages[i];
97 }
98 }
99 if (newMessage) {
100 var boardOffset = $('#chatroom-board').offset().top;
101 var targetOffset = $('div.new-message:last').offset().top;
102 var scrollAmount = targetOffset - boardOffset;
103 $('#chatroom-board').animate({scrollTop: '+='+ scrollAmount +'px'}, 500);
104 $('.new-message').removeClass('new-message');
105 if (Drupal.settings.chatroom.hasFocus == false) {
106 Drupal.settings.chatroom.newMsg = newMessage;
107 clearInterval(Drupal.settings.chatroom.warnInterval);
108 Drupal.settings.chatroom.warnInterval = setInterval("Drupal.chatroom.warnNewMsgLoop()", 1500);
109 }
110 }
111
112 if (response.data.usersHtml) {
113 $('#chatroom-user-list').html(response.data.usersHtml);
114 }
115 };
116
117 Drupal.chatroom.postMessage = function(message) {
118 $.ajax({
119 type: 'POST',
120 url: Drupal.settings.basePath + Drupal.settings.chatroom.postMessagePath + '/' + Drupal.settings.chatroom.chatId + '/' + Drupal.settings.chatroom.latestMsgId,
121 dataType: 'json',
122 success: Drupal.chatroom.pollHandler,
123 data: { message: message }
124 })
125 }
126
127 /**
128 * Toggle message alert status.
129 */
130 Drupal.chatroom.setMsgAlerts = function(obj) {
131 if ($(obj).attr('checked')) {
132 Drupal.settings.chatroom.msgAlerts = true;
133 }
134 else {
135 Drupal.settings.chatroom.msgAlerts = false;
136 }
137 }
138
139 Drupal.chatroom.warnNewMsgLoop = function() {
140 if (document.title == Drupal.settings.chatroom.pageTitle) {
141 document.title = Drupal.settings.chatroom.newMsg.name + ' says: ' + Drupal.settings.chatroom.newMsg.text;
142 }
143 else {
144 document.title = Drupal.settings.chatroom.pageTitle;
145 }
146 }
147
148 // vi:ai:expandtab:sw=2 ts=2
149

  ViewVC Help
Powered by ViewVC 1.1.2