]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/chat.js
Merge branch '3.6-release'
[friendica-addons.git] / jappixmini / jappix / js / chat.js
1 /*
2
3 Jappix - An open social platform
4 These are the chat JS scripts for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Authors: Vanaryon, Eric
10 Last revision: 16/10/11
11
12 */
13
14 // Correctly opens a new chat
15 function checkChatCreate(xid, type, nickname, password, title) {
16         // No XID?
17         if(!xid)
18                 return false;
19         
20         // We generate some stuffs
21         var hash = hex_md5(xid);
22         var name;
23         
24         // Gets the name of the user/title of the room
25         if(title)
26                 name = title;
27         
28         else {
29                 // Private groupchat chat
30                 if(type == 'private')
31                         name = thisResource(xid);
32                 
33                 // XMPP-ID
34                 else if(xid.indexOf('@') != -1)
35                         name = getBuddyName(xid);
36                 
37                 // Gateway
38                 else
39                         name = xid;
40         }
41         
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);
47                 
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();
56                                 
57                                 // Apply the room data
58                                 if(!nickname && fNick)
59                                         nickname = fNick;
60                                 if(!password && fPwd)
61                                         password = fPwd;
62                                 if(!title && fName)
63                                         name = fName;
64                         }
65                         
66                         groupchatCreate(hash, xid, name, nickname, password);
67                 }
68         }
69         
70         // Switch to the newly-created chat
71         switchChan(hash);
72         
73         return false;
74 }
75
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);
81         
82         // Special code
83         var specialAttributes, specialAvatar, specialName, specialCode, specialLink, specialDisabled, specialStyle;
84         
85         // Groupchat special code
86         if(type == 'groupchat') {
87                 specialAttributes = ' data-type="groupchat"';
88                 specialAvatar = '';
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>';
92                 specialStyle = '';
93                 
94                 // Is this a gateway?
95                 if(xid.match(/%/))
96                         specialDisabled = '';
97                 else
98                         specialDisabled = ' disabled=""';
99         }
100         
101         // Chat (or other things?!) special code
102         else {
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 = '';
110         }
111         
112         // Not a groupchat private chat, we can use the buddy add icon
113         if((type == 'chat') || (type == 'groupchat')) {
114                 var addTitle;
115                 
116                 if(type == 'chat')
117                         addTitle = _e("Add this contact to your friends");
118                 else
119                         addTitle = _e("Add this groupchat to your favorites");
120                 
121                 specialLink += '<a href="#" class="tools-add tools-tooltip talk-images chat-tools-content" title="' + addTitle + '"></a>';
122         }
123         
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>' + 
127                                  '</div>';
128         
129         if((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version < 9))
130                 specialStylePicker = '';
131         
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 + '">' + 
136                                 specialAvatar + 
137                                 
138                                 '<div class="name">' + 
139                                         '<p class="bc-name bc-name-nick">' + nick.htmlEnc() + '</p>' + 
140                                         specialName + 
141                                 '</div>' + 
142                         '</div>' + 
143                         
144                         specialCode + 
145                         
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>' + 
150                                         '</div>' + 
151                                         
152                                         specialStylePicker + 
153                                         
154                                         '<div class="chat-tools-content chat-tools-file">' + 
155                                                 '<a href="#" class="tools-file tools-tooltip talk-images"></a>' + 
156                                         '</div>' + 
157                                         
158                                         '<div class="chat-tools-content chat-tools-save">' + 
159                                                 '<a href="#" class="tools-save tools-tooltip talk-images"></a>' + 
160                                         '</div>' + 
161                                         
162                                         '<a href="#" class="tools-clear tools-tooltip talk-images chat-tools-content" title="' + _e("Clean current chat") + '"></a>' + 
163                                         
164                                         specialLink + 
165                                 '</div>' + 
166                                 
167                                 '<div class="compose">' + 
168                                         '<textarea class="message-area focusable" ' + specialDisabled + ' data-to="' + escaped_xid + '" /></textarea>' + 
169                                 '</div>' + 
170                         '</div>' + 
171                 '</div>'
172         );
173         
174         // Click event: chat cleaner
175         $(path + 'tools-clear').click(function() {
176                 cleanChat(id);
177         });
178         
179         // Click event: user-infos
180         $(path + 'tools-infos').click(function() {
181                 openUserInfos(xid);
182         });
183 }
184
185 // Generates the chat switch elements
186 function generateSwitch(type, id, xid, nick) {
187         // Path to the element
188         var chat_switch = '#page-switch .';
189         
190         // Special code
191         var specialClass = ' unavailable';
192         var show_close = true;
193         
194         // Groupchat
195         if(type == 'groupchat') {
196                 specialClass = ' groupchat-default';
197                 
198                 if(isAnonymous() && (xid == generateXID(ANONYMOUS_ROOM, 'groupchat')))
199                         show_close = false;
200         }
201         
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>' + 
205                         
206                         '<div class="name">' + nick.htmlEnc() + '</div>';
207         
208         // Show the close button if not MUC and not anonymous
209         if(show_close)
210                 html += '<div class="exit" title="' + _e("Close this tab") + '" onclick="return quitThisChat(\'' + encodeOnclick(xid) + '\', \'' + encodeOnclick(id) + '\', \'' + encodeOnclick(type) + '\');">x</div>';
211         
212         // Close the HTML
213         html += '</div>';
214         
215         // Append the HTML code
216         $(chat_switch + 'chans, ' + chat_switch + 'more-content').append(html);
217 }
218
219 // Cleans given the chat lines
220 function cleanChat(chat) {
221         $('#page-engine #' + chat + ' .content .one-group').remove();
222         
223         $(document).oneTime(10, function() {
224                 $('#page-engine #' + chat + ' .text .message-area').focus();
225         });
226 }
227
228 // Creates a new chat
229 function chatCreate(hash, xid, nick, type) {
230         logThis('New chat: ' + xid, 3);
231         
232         // Create the chat content
233         generateChat(type, hash, xid, nick);
234         
235         // Create the chat switcher
236         generateSwitch(type, hash, xid, nick);
237         
238         // If the user is not in our buddy-list
239         if(type == 'chat') {
240                 // Add button
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)
244                                 $(this).hide();
245                                 
246                                 // Send the subscribe request
247                                 addThisContact(xid, nick);
248                         }).show();
249                 
250                 // Archives button
251                 else if(enabledArchives() || enabledArchives('auto') || enabledArchives('manual') || enabledArchives('manage'))
252                         $('#' + hash + ' .tools-archives').click(function() {
253                                 // Open the archives popup
254                                 openArchives();
255                                 
256                                 // Get the archives for this user
257                                 $('#archives .filter .friend').val(xid);
258                                 updateArchives();
259                         }).show();
260         }
261         
262         // We catch the user's informations (like this avatar, vcard, and so on...)
263         getUserInfos(hash, xid, nick, type);
264         
265         // The icons-hover functions
266         tooltipIcons(xid, hash);
267         
268         // The event handlers
269         var inputDetect = $('#page-engine #' + hash + ' .message-area');
270         
271         inputDetect.focus(function() {
272                 chanCleanNotify(hash);
273         })
274         
275         inputDetect.keypress(function(e) {
276                 // Enter key
277                 if(e.keyCode == 13) {
278                         // Add a new line
279                         if(e.shiftKey)
280                                 inputDetect.val(inputDetect.val() + '\n');
281                         
282                         // Send the message
283                         else {
284                                 // Send the message
285                                 sendMessage(hash, 'chat');
286                                 
287                                 // Reset the composing database entry
288                                 setDB('chatstate', xid, 'off');
289                         }
290                         
291                         return false;
292                 }
293         });
294         
295         // Chatstate events
296         eventsChatState(inputDetect, xid, hash);
297 }