3 Jappix - An open social platform
4 These are the chat JS scripts for Jappix
6 -------------------------------------------------
9 Authors: Vanaryon, Eric
10 Last revision: 16/10/11
14 // Correctly opens a new chat
15 function checkChatCreate(xid, type, nickname, password, title) {
20 // We generate some stuffs
21 var hash = hex_md5(xid);
24 // Gets the name of the user/title of the room
29 // Private groupchat chat
31 name = thisResource(xid);
34 else if(xid.indexOf('@') != -1)
35 name = getBuddyName(xid);
42 // If the target div does not exist
43 if(!exists('#' + hash)) {
44 // We check the type of the chat to open
45 if((type == 'chat') || (type == 'private'))
46 chatCreate(hash, xid, name, type);
48 else if(type == 'groupchat') {
49 // Try to read the room stored configuration
50 if(!isAnonymous() && (!nickname || !password || !title)) {
51 // Catch the room data
52 var fData = $(XMLFromString(getDB('favorites', xid)));
53 var fNick = fData.find('nick').text();
54 var fPwd = fData.find('password').text();
55 var fName = fData.find('name').text();
57 // Apply the room data
58 if(!nickname && fNick)
66 groupchatCreate(hash, xid, name, nickname, password);
70 // Switch to the newly-created chat
76 // Generates the chat DOM elements
77 function generateChat(type, id, xid, nick) {
78 // Generate some stuffs
79 var path = '#' + id + ' .';
80 var escaped_xid = escape(xid);
83 var specialAttributes, specialAvatar, specialName, specialCode, specialLink, specialDisabled, specialStyle;
85 // Groupchat special code
86 if(type == 'groupchat') {
87 specialAttributes = ' data-type="groupchat"';
89 specialName = '<p class="bc-infos"><b>' + _e("Subject") + '</b> <span class="muc-topic">' + _e("no subject defined for this room.") + '</span></p>';
90 specialCode = '<div class="content groupchat-content" id="chat-content-' + id + '"></div><div class="list"><div class="moderator role"><p class="title">' + _e("Moderators") + '</p></div><div class="participant role"><p class="title">' + _e("Participants") + '</p></div><div class="visitor role"><p class="title">' + _e("Visitors") + '</p></div><div class="none role"><p class="title">' + _e("Others") + '</p></div></div>';
91 specialLink = '<a href="#" class="tools-mucadmin tools-tooltip talk-images chat-tools-content" title="' + _e("Administration panel for this room") + '"></a>';
98 specialDisabled = ' disabled=""';
101 // Chat (or other things?!) special code
103 specialAttributes = ' data-type="chat"';
104 specialAvatar = '<div class="avatar-container"><img class="avatar" src="' + './img/others/default-avatar.png' + '" alt="" /></div>';
105 specialName = '<div class="bc-pep"></div><p class="bc-infos"><span class="unavailable show talk-images"></span></p>';
106 specialCode = '<div class="content" id="chat-content-' + id + '"></div>';
107 specialLink = '<a href="#" class="tools-archives tools-tooltip talk-images chat-tools-content" title="' + _e("View chat history") + '"></a><a href="#" class="tools-infos tools-tooltip talk-images chat-tools-content" title="' + _e("Show user profile") + '"></a>';
108 specialStyle = ' style="display: none;"';
109 specialDisabled = '';
112 // Not a groupchat private chat, we can use the buddy add icon
113 if((type == 'chat') || (type == 'groupchat')) {
117 addTitle = _e("Add this contact to your friends");
119 addTitle = _e("Add this groupchat to your favorites");
121 specialLink += '<a href="#" class="tools-add tools-tooltip talk-images chat-tools-content" title="' + addTitle + '"></a>';
124 // IE DOM parsing bug fix
125 var specialStylePicker = '<div class="chat-tools-content chat-tools-style"' + specialStyle + '>' +
126 '<a href="#" class="tools-style tools-tooltip talk-images"></a>' +
129 if((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version < 9))
130 specialStylePicker = '';
132 // Append the chat HTML code
133 $('#page-engine').append(
134 '<div id="' + id + '" class="page-engine-chan chat one-counter"' + specialAttributes + ' data-xid="' + escaped_xid + '">' +
135 '<div class="top ' + id + '">' +
138 '<div class="name">' +
139 '<p class="bc-name bc-name-nick">' + nick.htmlEnc() + '</p>' +
146 '<div class="text">' +
147 '<div class="footer">' +
148 '<div class="chat-tools-content chat-tools-smileys">' +
149 '<a href="#" class="tools-smileys tools-tooltip talk-images"></a>' +
154 '<div class="chat-tools-content chat-tools-file">' +
155 '<a href="#" class="tools-file tools-tooltip talk-images"></a>' +
158 '<div class="chat-tools-content chat-tools-save">' +
159 '<a href="#" class="tools-save tools-tooltip talk-images"></a>' +
162 '<a href="#" class="tools-clear tools-tooltip talk-images chat-tools-content" title="' + _e("Clean current chat") + '"></a>' +
167 '<div class="compose">' +
168 '<textarea class="message-area focusable" ' + specialDisabled + ' data-to="' + escaped_xid + '" /></textarea>' +
174 // Click event: chat cleaner
175 $(path + 'tools-clear').click(function() {
179 // Click event: user-infos
180 $(path + 'tools-infos').click(function() {
185 // Generates the chat switch elements
186 function generateSwitch(type, id, xid, nick) {
187 // Path to the element
188 var chat_switch = '#page-switch .';
191 var specialClass = ' unavailable';
192 var show_close = true;
195 if(type == 'groupchat') {
196 specialClass = ' groupchat-default';
198 if(isAnonymous() && (xid == generateXID(ANONYMOUS_ROOM, 'groupchat')))
202 // Generate the HTML code
203 var html = '<div class="' + id + ' switcher chan" onclick="return switchChan(\'' + encodeOnclick(id) + '\')">' +
204 '<div class="icon talk-images' + specialClass + '"></div>' +
206 '<div class="name">' + nick.htmlEnc() + '</div>';
208 // Show the close button if not MUC and not anonymous
210 html += '<div class="exit" title="' + _e("Close this tab") + '" onclick="return quitThisChat(\'' + encodeOnclick(xid) + '\', \'' + encodeOnclick(id) + '\', \'' + encodeOnclick(type) + '\');">x</div>';
215 // Append the HTML code
216 $(chat_switch + 'chans, ' + chat_switch + 'more-content').append(html);
219 // Cleans given the chat lines
220 function cleanChat(chat) {
221 $('#page-engine #' + chat + ' .content .one-group').remove();
223 $(document).oneTime(10, function() {
224 $('#page-engine #' + chat + ' .text .message-area').focus();
228 // Creates a new chat
229 function chatCreate(hash, xid, nick, type) {
230 logThis('New chat: ' + xid, 3);
232 // Create the chat content
233 generateChat(type, hash, xid, nick);
235 // Create the chat switcher
236 generateSwitch(type, hash, xid, nick);
238 // If the user is not in our buddy-list
241 if(!exists('#buddy-list .buddy[data-xid=' + escape(xid) + ']'))
242 $('#' + hash + ' .tools-add').click(function() {
243 // Hide the icon (to tell the user all is okay)
246 // Send the subscribe request
247 addThisContact(xid, nick);
251 else if(enabledArchives() || enabledArchives('auto') || enabledArchives('manual') || enabledArchives('manage'))
252 $('#' + hash + ' .tools-archives').click(function() {
253 // Open the archives popup
256 // Get the archives for this user
257 $('#archives .filter .friend').val(xid);
262 // We catch the user's informations (like this avatar, vcard, and so on...)
263 getUserInfos(hash, xid, nick, type);
265 // The icons-hover functions
266 tooltipIcons(xid, hash);
268 // The event handlers
269 var inputDetect = $('#page-engine #' + hash + ' .message-area');
271 inputDetect.focus(function() {
272 chanCleanNotify(hash);
275 inputDetect.keypress(function(e) {
277 if(e.keyCode == 13) {
280 inputDetect.val(inputDetect.val() + '\n');
285 sendMessage(hash, 'chat');
287 // Reset the composing database entry
288 setDB('chatstate', xid, 'off');
296 eventsChatState(inputDetect, xid, hash);