/[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.52 - (show annotations) (download) (as text)
Fri Nov 6 16:56:18 2009 UTC (2 weeks, 4 days ago) by justinrandell
Branch: MAIN
Changes since 1.51: +41 -3 lines
File MIME type: text/javascript
sync up HEAD with 6.x-2 BETA 1
1 // $Id: chatroom.js,v 1.50.4.14 2009/10/31 12:07:44 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 // If this chat was configured as a popout, but we're not in a popout, load a
48 // popout from here, and redirect to a configured url which will set a
49 // message so the user knows what happened.
50 if (opener == undefined && Drupal.settings.chatroom.viewAsPopout == 1) {
51 Drupal.chatroom.loadPopout();
52 window.location = Drupal.settings.basePath + Drupal.settings.chatroom.popoutRedirect + '/' + Drupal.settings.chatroom.chatId;
53 }
54 };
55
56 Drupal.chatroom.loadPopout = function() {
57 var popoutWindow;
58 if (popoutWindow != null) {
59 if (!popoutWindow.closed) {
60 popoutWindow.focus();
61 }
62 }
63 else {
64 popoutWindow = open(location.href, '', Drupal.settings.chatroom.popoutParams);
65 }
66 }
67
68 Drupal.chatroom.poll = function() {
69
70 var skipCacheCheck = 0;
71 if (Drupal.settings.chatroom.successiveCacheHits > Drupal.settings.chatroom.skipCacheCheckCount) {
72 skipCacheCheck = 1;
73 }
74
75 $.ajax({
76 type: 'POST',
77 url: Drupal.settings.basePath + 'chatroomread.php',
78 dataType: 'json',
79 success: Drupal.chatroom.pollHandler,
80 data: {
81 latest_msg_id: Drupal.settings.chatroom.latestMsgId,
82 chat_cache_directory: Drupal.settings.chatroom.cacheDirectory,
83 chat_id: Drupal.settings.chatroom.chatId,
84 skip_cache: skipCacheCheck,
85 successive_cache_hits: Drupal.settings.chatroom.successiveCacheHits,
86 }
87 });
88 };
89
90 Drupal.chatroom.pollHandler = function(response, responseStatus) {
91
92 // If the user was kicked or banned, get the out of here.
93 if (response.data.accessDenied) {
94 window.location = Drupal.settings.basePath + Drupal.settings.chatroom.accessDeniedPath + '/' + Drupal.settings.chatroom.chatId + '/' + response.data.accessDenied;
95 }
96
97 // If the chat was archived, reload the page.
98 if (response.data.archived) {
99 window.location = Drupal.settings.basePath + Drupal.settings.chatroom.chatPath;
100 }
101
102 // If we hit the cache, then keep track of that. If the number of
103 // successive cache hits gets high enough, we may want to signal to the
104 // server that we should skip the cache check so that our online time
105 // gets updated.
106 if (response.data.cacheHit) {
107 Drupal.settings.chatroom.successiveCacheHits++;
108 }
109 else {
110 Drupal.settings.chatroom.successiveCacheHits = 0;
111 }
112
113 // Add any messages we haven't already seen to the board. Poll requests can
114 // pass each other over the wire, so we can't rely on getting a given
115 // message once only.
116 var newMessage = false;
117 for (var i = 0; i < response.data.messages.length; i++) {
118 if (response.data.messages[i].cmid > Drupal.settings.chatroom.latestMsgId) {
119 $('#chatroom-board').append(response.data.messages[i].html);
120 Drupal.settings.chatroom.latestMsgId = response.data.messages[i].cmid;
121 newMessage = response.data.messages[i];
122 }
123 }
124 if (newMessage) {
125 var boardOffset = $('#chatroom-board').offset().top;
126 var targetOffset = $('div.new-message:last').offset().top;
127 var scrollAmount = targetOffset - boardOffset;
128 $('#chatroom-board').animate({scrollTop: '+='+ scrollAmount +'px'}, 500);
129 $('.new-message').removeClass('new-message');
130 if (Drupal.settings.chatroom.hasFocus == false) {
131 Drupal.settings.chatroom.newMsg = newMessage;
132 clearInterval(Drupal.settings.chatroom.warnInterval);
133 Drupal.settings.chatroom.warnInterval = setInterval("Drupal.chatroom.warnNewMsgLoop()", 1500);
134 }
135 }
136
137 if (response.data.usersHtml) {
138 $('#chatroom-user-list').html(response.data.usersHtml);
139 }
140
141 if (response.data.commandResponse) {
142 Drupal.chatroom.addCommandMessage(response.data.commandResponse);
143 }
144 };
145
146 Drupal.chatroom.addCommandMessage = function(response) {
147 $('#chatroom-board').append('<div class="new-message command-message">** ' + response.msg + '</div>');
148 var boardOffset = $('#chatroom-board').offset().top;
149 var targetOffset = $('div.new-message:last').offset().top;
150 var scrollAmount = targetOffset - boardOffset;
151 $('#chatroom-board').animate({scrollTop: '+='+ scrollAmount +'px'}, 500);
152 $('.new-message').removeClass('new-message');
153 }
154
155 Drupal.chatroom.postMessage = function(message) {
156 $.ajax({
157 type: 'POST',
158 url: Drupal.settings.basePath + Drupal.settings.chatroom.postMessagePath + '/' + Drupal.settings.chatroom.chatId + '/' + Drupal.settings.chatroom.latestMsgId,
159 dataType: 'json',
160 success: Drupal.chatroom.pollHandler,
161 data: { message: message }
162 })
163 }
164
165 /**
166 * Toggle message alert status.
167 */
168 Drupal.chatroom.setMsgAlerts = function(obj) {
169 if ($(obj).attr('checked')) {
170 Drupal.settings.chatroom.msgAlerts = true;
171 }
172 else {
173 Drupal.settings.chatroom.msgAlerts = false;
174 }
175 }
176
177 Drupal.chatroom.warnNewMsgLoop = function() {
178 if (document.title == Drupal.settings.chatroom.pageTitle) {
179 document.title = Drupal.settings.chatroom.newMsg.name + ' says: ' + Drupal.settings.chatroom.newMsg.text;
180 }
181 else {
182 document.title = Drupal.settings.chatroom.pageTitle;
183 }
184 }
185
186 // vi:ai:expandtab:sw=2 ts=2
187

  ViewVC Help
Powered by ViewVC 1.1.2