| 1 |
// $Id: chatroom.block.js,v 1.7.2.17 2007/10/02 16:16:06 darrenoh Exp $
|
| 2 |
|
| 3 |
Drupal.chatroom = Drupal.chatroom || {};
|
| 4 |
Drupal.chatroom.block = Drupal.chatroom.block || {};
|
| 5 |
|
| 6 |
/**
|
| 7 |
* Add chatroom events handlers to the on-screen widgets.
|
| 8 |
*/
|
| 9 |
Drupal.chatroom.block.addEvents = function() {
|
| 10 |
Drupal.chatroom.block.updates = setInterval("Drupal.chatroom.block.getUpdates()", Drupal.settings.chatroom.blockUpdateInterval);
|
| 11 |
return;
|
| 12 |
}
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Process response to HTTP request.
|
| 16 |
*/
|
| 17 |
Drupal.chatroom.block.blockCallback = function(responseText) {
|
| 18 |
if (responseText) {
|
| 19 |
var response = eval('('+ responseText +')');
|
| 20 |
if (typeof response == 'object') {
|
| 21 |
if (response.chatList != undefined) {
|
| 22 |
Drupal.chatroom.block.updateChats(response.chatList);
|
| 23 |
}
|
| 24 |
if (response.chatroomList != undefined) {
|
| 25 |
Drupal.chatroom.block.updateChatrooms(response.chatroomList);
|
| 26 |
}
|
| 27 |
if (response.userList != undefined) {
|
| 28 |
Drupal.chatroom.block.updateOnlineUsers(response.userList);
|
| 29 |
}
|
| 30 |
}
|
| 31 |
}
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Update the on-line users block.
|
| 36 |
*/
|
| 37 |
Drupal.chatroom.block.updateOnlineUsers = function(users) {
|
| 38 |
var userList = $('#chatroom-sitewide-online');
|
| 39 |
if (userList.length > 0) {
|
| 40 |
var userListUsers = $('li', userList); // Get the current user list.
|
| 41 |
if (users.length == 0) {
|
| 42 |
if (userListUsers.attr('id') != 'no_users') {
|
| 43 |
var li = $('<li>');
|
| 44 |
li.attr('id', 'no_users');
|
| 45 |
li.css('fontStyle', 'italic');
|
| 46 |
li.append(Drupal.settings.chatroom.usersMessage);
|
| 47 |
userList.empty();
|
| 48 |
userList.append(li);
|
| 49 |
}
|
| 50 |
}
|
| 51 |
else {
|
| 52 |
for (var i = 0; i < userListUsers.length; i++) { // Delete users not in the new user list.
|
| 53 |
var listUser = $(userListUsers[i]);
|
| 54 |
var deleteFlag = true;
|
| 55 |
for (var j = 0; j < users.length; j++) {
|
| 56 |
if (listUser.attr('id') == 'user-li-'+ users[j].uid) {
|
| 57 |
deleteFlag = false;
|
| 58 |
users.splice(j, 1);
|
| 59 |
break;
|
| 60 |
}
|
| 61 |
}
|
| 62 |
if (deleteFlag) {
|
| 63 |
listUser.remove();
|
| 64 |
}
|
| 65 |
}
|
| 66 |
for (var i = 0; i < users.length; i++) {
|
| 67 |
var userInfo = $('<strong>');
|
| 68 |
userInfo.append(users[i].user);
|
| 69 |
var li = $('<li>');
|
| 70 |
li.attr('id', 'user-li-'+ users[i].uid);
|
| 71 |
li.append(userInfo);
|
| 72 |
userList.append(li);
|
| 73 |
}
|
| 74 |
}
|
| 75 |
}
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* Update the active chats block.
|
| 80 |
*/
|
| 81 |
Drupal.chatroom.block.updateChats = function(chats) {
|
| 82 |
var chatList = $('#chatroom-sitewide-chats');
|
| 83 |
if (chatList.length > 0) {
|
| 84 |
Drupal.settings.chatroom.chatTimestamp = chats.pop(); // Set timestamp for use in next update request.
|
| 85 |
var chatListItems = $('li', chatList); // Get the current chat list.
|
| 86 |
if (chats.length == 0) {
|
| 87 |
if (chatListItems.attr('id') != 'chat_empty') {
|
| 88 |
var li = $('<li>');
|
| 89 |
li.attr('id', 'chat_empty');
|
| 90 |
li.css('fontStyle', 'italic');
|
| 91 |
li.append(Drupal.settings.chatroom.chatsMessage);
|
| 92 |
chatList.empty();
|
| 93 |
chatList.append(li);
|
| 94 |
}
|
| 95 |
}
|
| 96 |
else {
|
| 97 |
for (var i = 0; i < chatListItems.length; i++) { // Delete items not in the new chat list.
|
| 98 |
var chatItem = $(chatListItems[i]);
|
| 99 |
var deleteFlag = true;
|
| 100 |
for (var j = 0; j < chats.length; j++) {
|
| 101 |
if (chatItem.attr('id') == chats[j].chatListId) {
|
| 102 |
deleteFlag = false;
|
| 103 |
chats.splice(j, 1);
|
| 104 |
break;
|
| 105 |
}
|
| 106 |
}
|
| 107 |
if (deleteFlag) {
|
| 108 |
chatItem.remove();
|
| 109 |
}
|
| 110 |
}
|
| 111 |
for (var i = 0; i < chats.length; i++) {
|
| 112 |
var chatLink = $('<a>');
|
| 113 |
chatLink.append(chats[i].chatName);
|
| 114 |
chatLink.attr('href', Drupal.settings.chatroom.chatBase + chats[i].ccid);
|
| 115 |
var roomLink = $('<a>');
|
| 116 |
roomLink.append(chats[i].roomName);
|
| 117 |
roomLink.attr('href', Drupal.settings.chatroom.roomBase + chats[i].nid);
|
| 118 |
if (roomLink[0].href == window.location) {
|
| 119 |
roomLink.addClass('active');
|
| 120 |
}
|
| 121 |
var span = $('<span>');
|
| 122 |
span.append(roomLink);
|
| 123 |
span.html('in '+ span.html());
|
| 124 |
span.addClass('chatroomLink');
|
| 125 |
var li = $('<li>');
|
| 126 |
li.attr('id', chats[i].chatListId);
|
| 127 |
li.append(chatLink);
|
| 128 |
li.append('<br />');
|
| 129 |
li.append(span);
|
| 130 |
chatList.prepend(li);
|
| 131 |
}
|
| 132 |
}
|
| 133 |
}
|
| 134 |
}
|
| 135 |
|
| 136 |
/**
|
| 137 |
* Update the active chat rooms block.
|
| 138 |
*/
|
| 139 |
Drupal.chatroom.block.updateChatrooms = function(chatrooms) {
|
| 140 |
var chatroomList = $('#chatroom-sitewide-chatrooms');
|
| 141 |
if (chatroomList.length > 0) {
|
| 142 |
Drupal.settings.chatroom.roomTimestamp = chatrooms.pop(); // Set timestamp for use in next update request.
|
| 143 |
var chatroomListItems = $('li', chatroomList); // Get the current list of chat rooms.
|
| 144 |
if (chatrooms.length == 0) {
|
| 145 |
if (chatroomListItems.attr('id') != 'chatroom_empty') {
|
| 146 |
var li = $('<li>');
|
| 147 |
li.attr('id', 'chatroom_empty');
|
| 148 |
li.css('fontStyle', 'italic');
|
| 149 |
li.append(Drupal.settings.chatroom.roomsMessage);
|
| 150 |
chatroomList.empty();
|
| 151 |
chatroomList.append(li);
|
| 152 |
}
|
| 153 |
}
|
| 154 |
else {
|
| 155 |
for (var i = 0; i < chatroomListItems.length; i++) { // Delete items not in the new chat rooms list.
|
| 156 |
var chatroomItem = $(chatroomListItems[i]);
|
| 157 |
var deleteFlag = true;
|
| 158 |
for (var j = 0; j < chatrooms.length; j++) {
|
| 159 |
if (chatroomItem.attr('id') == chatrooms[j].chatroomListId) {
|
| 160 |
deleteFlag = false;
|
| 161 |
chatrooms.splice(j, 1);
|
| 162 |
break;
|
| 163 |
}
|
| 164 |
}
|
| 165 |
if (deleteFlag) {
|
| 166 |
chatroomItem.remove();
|
| 167 |
}
|
| 168 |
}
|
| 169 |
for (var i = 0; i < chatrooms.length; i++) {
|
| 170 |
var chatroomLink = $('<a>');
|
| 171 |
chatroomLink.append(chatrooms[i].chatroomName);
|
| 172 |
chatroomLink.attr('href', Drupal.settings.chatroom.roomBase + chatrooms[i].nid);
|
| 173 |
var li = $('<li>');
|
| 174 |
li.attr('id', chatrooms[i].chatroomListId);
|
| 175 |
li.append(chatroomLink);
|
| 176 |
chatroomList.prepend(li);
|
| 177 |
}
|
| 178 |
}
|
| 179 |
}
|
| 180 |
}
|
| 181 |
|
| 182 |
/**
|
| 183 |
* gets updates from the server for chatroom blocks
|
| 184 |
*/
|
| 185 |
Drupal.chatroom.block.getUpdates = function() {
|
| 186 |
var postData = {
|
| 187 |
block_update:1,
|
| 188 |
drupal_base:Drupal.settings.chatroom.drupalBase,
|
| 189 |
base_url:Drupal.settings.chatroom.baseUrl,
|
| 190 |
chatroom_base:Drupal.settings.chatroom.chatroomBase,
|
| 191 |
user_base:Drupal.settings.chatroom.userBase
|
| 192 |
};
|
| 193 |
var checkBlocks = false;
|
| 194 |
if ($('#chatroom-sitewide-chatrooms').length > 0) {
|
| 195 |
postData.room_cache_file = Drupal.settings.chatroom.roomsCacheFile;
|
| 196 |
postData.room_timestamp = Drupal.settings.chatroom.roomTimestamp;
|
| 197 |
checkBlocks = true;
|
| 198 |
}
|
| 199 |
if ($('#chatroom-sitewide-chats').length > 0) {
|
| 200 |
postData.chat_cache_file = Drupal.settings.chatroom.chatsCacheFile;
|
| 201 |
postData.chat_timestamp = Drupal.settings.chatroom.chatTimestamp;
|
| 202 |
checkBlocks = true;
|
| 203 |
}
|
| 204 |
if ($('#chatroom-sitewide-online').length > 0) {
|
| 205 |
postData.uid = Drupal.settings.chatroom.uid;
|
| 206 |
checkBlocks = true;
|
| 207 |
}
|
| 208 |
if (checkBlocks) {
|
| 209 |
return $.post(Drupal.settings.chatroom.updateUrl, postData, Drupal.chatroom.block.blockCallback);
|
| 210 |
}
|
| 211 |
return;
|
| 212 |
}
|
| 213 |
|
| 214 |
// Global Killswitch
|
| 215 |
if (Drupal.jsEnabled) {
|
| 216 |
$(document).ready(Drupal.chatroom.block.addEvents);
|
| 217 |
}
|
| 218 |
|