]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/userinfos.js
35a58fe563942b472afafed82be17b3ed277bbad
[friendica-addons.git] / jappixmini / jappix / js / userinfos.js
1 /*
2
3 Jappix - An open social platform
4 These are the user-infos JS scripts for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 27/03/11
11
12 */
13
14 // Opens the user-infos popup
15 function openUserInfos(xid) {
16         // Can show shortcuts?
17         var shortcuts = '';
18         
19         if(xid != getXID()) {
20                 shortcuts = '<div class="shortcuts">' + 
21                                         '<a href="#" class="message talk-images" title="' + _e("Send him/her a message") + '" onclick="closeUserInfos(); return composeInboxMessage(\'' + encodeOnclick(xid) + '\');"></a>' + 
22                                         '<a href="#" class="chat talk-images" title="' + _e("Start a chat with him/her") + '" onclick="closeUserInfos(); return checkChatCreate(\'' + encodeOnclick(xid) + '\', \'chat\');"></a>' + 
23                                         '<a href="#" class="command talk-images" title="' + _e("Command") + '" onclick="closeUserInfos(); return retrieveAdHoc(\'' + encodeOnclick(xid) + '\');"></a>' + 
24                              '</div>';
25         }
26         
27         // Popup HTML content
28         var html = 
29         '<div class="top">' + _e("User profile") + '</div>' + 
30         
31         '<div class="tab">' + 
32                 '<a href="#" class="tab-active" data-key="1">' + _e("General") + '</a>' + 
33                 '<a href="#" data-key="2">' + _e("Advanced") + '</a>' + 
34                 '<a href="#" data-key="3">' + _e("Comments") + '</a>' + 
35         '</div>' + 
36         
37         '<div class="content">' + 
38                 '<div class="lap-active one-lap info1">' + 
39                         '<div class="main-infos">' + 
40                                 '<div class="avatar-container">' + 
41                                         '<img class="avatar" src="' + './img/others/default-avatar.png' + '" alt="" />' + 
42                                 '</div>' + 
43                                 
44                                 '<h1 id="BUDDY-FN" class="reset-info">' + _e("unknown") + '</h1>' + 
45                                 '<h2 class="buddy-xid" class="reset-info">' + _e("unknown") + '</h2>' + 
46                                 '<h3 class="buddy-last" class="reset-info">' + _e("unknown") + '</h3>' + 
47                                 
48                                 shortcuts + 
49                         '</div>' + 
50                         
51                         '<div class="block-infos">' + 
52                                 '<div class="one-line"><b>' + _e("Date of birth") + '</b><span id="BUDDY-BDAY" class="reset-info">' + _e("unknown") + '</span></div>' + 
53                                 
54                                 '<div class="one-line"><b>' + _e("E-mail") + '</b><span id="BUDDY-EMAIL-USERID" class="reset-info">' + _e("unknown") + '</span></div>' + 
55                                 
56                                 '<div class="one-line"><b>' + _e("Phone") + '</b><span id="BUDDY-TEL-NUMBER" class="reset-info">' + _e("unknown") + '</span></div>' + 
57                                 
58                                 '<div class="one-line"><b>' + _e("Website") + '</b><span id="BUDDY-URL" class="reset-info">' + _e("unknown") + '</span></div>' + 
59                         '</div>' + 
60                         
61                         '<div class="block-infos">' + 
62                                 '<div class="one-line"><b>' + _e("Client") + '</b><span id="BUDDY-CLIENT" class="reset-info">' + _e("unknown") + '</span></div>' + 
63                                 
64                                 '<div class="one-line"><b>' + _e("System") + '</b><span id="BUDDY-SYSTEM" class="reset-info">' + _e("unknown") + '</span></div>' + 
65                                 
66                                 '<div class="one-line"><b>' + _e("Local time") + '</b><span id="BUDDY-TIME" class="reset-info">' + _e("unknown") + '</span></div>' + 
67                         '</div>' + 
68                 '</div>' + 
69                 
70                 '<div class="one-lap info2">' + 
71                         '<div class="block-infos">' + 
72                                 '<div class="one-line"><b>' + _e("Street") + '</b><span id="BUDDY-ADR-STREET" class="reset-info">' + _e("unknown") + '</span></div>' + 
73                                 
74                                 '<div class="one-line"><b>' + _e("City") + '</b><span id="BUDDY-ADR-LOCALITY" class="reset-info">' + _e("unknown") + '</span></div>' + 
75                                 
76                                 '<div class="one-line"><b>' + _e("Postal code") + '</b><span id="BUDDY-ADR-PCODE" class="reset-info">' + _e("unknown") + '</span></div>' + 
77                                 
78                                 '<div class="one-line"><b>' + _e("Country") + '</b><span id="BUDDY-ADR-CTRY" class="reset-info">' + _e("unknown") + '</span></div>' + 
79                         '</div>' + 
80                         
81                         '<div class="block-infos">' + 
82                                 '<div class="one-line"><b>' + _e("Biography") + '</b><span id="BUDDY-DESC" class="reset-info">' + _e("unknown") + '</span></div>' + 
83                         '</div>' + 
84                 '</div>' + 
85                 
86                 '<div class="one-lap info3">' + 
87                         '<textarea class="rosternotes" rows="8" cols="60"></textarea>' + 
88                 '</div>' + 
89         '</div>' + 
90         
91         '<div class="bottom">' + 
92                 '<div class="wait wait-medium"></div>' + 
93                 
94                 '<a href="#" class="finish">' + _e("Close") + '</a>' + 
95         '</div>';
96         
97         // Create the popup
98         createPopup('userinfos', html);
99         
100         // Associate the events
101         launchUserInfos();
102         
103         // We retrieve the user's vcard
104         retrieveUserInfos(xid);
105         
106         return false;
107 }
108
109 // Closes the user-infos popup
110 function closeUserInfos() {
111         // Send the buddy comments
112         sendBuddyComments();
113         
114         // Destroy the popup
115         destroyPopup('userinfos');
116         
117         return false;
118 }
119
120 // Gets the user-infos
121 function retrieveUserInfos(xid) {
122         // We setup the waiting indicator
123         markers = 'vcard last';
124         
125         // We put the user's XID
126         $('#userinfos .buddy-xid').text(xid);
127         
128         // We get the vCard
129         getVCard(xid, 'buddy');
130         
131         // Get the highest resource for this XID
132         var cXID = getHighestResource(xid);
133         var pXID = xid;
134         
135         // If the user is logged in
136         if(cXID) {
137                 // Change the XID
138                 pXID = cXID;
139                 
140                 // We request the user's system infos
141                 queryUserInfos(cXID, 'version')
142                 
143                 // We request the user's local time
144                 queryUserInfos(cXID, 'time')
145                 
146                 // Add these to the markers
147                 markers += ' version time';
148         }
149         
150         // We request the user's last activity
151         queryUserInfos(pXID, 'last');
152         
153         // Add the markers
154         $('#userinfos .content').addClass(markers);
155         
156         // We request all the user's comments
157         displayBuddyComments(xid);
158 }
159
160 // Builds the asked user-infos query
161 function queryUserInfos(xid, mode) {
162         // Generate a session ID
163         var id = genID();
164         $('#userinfos').attr('data-' + mode, id);
165         
166         // New IQ
167         var iq = new JSJaCIQ();
168         
169         iq.setID(id);
170         iq.setType('get');
171         iq.setTo(xid);
172         
173         // Last activity query
174         if(mode == 'last') {
175                 iq.setQuery(NS_LAST);
176                 con.send(iq, lastActivityUserInfos);
177         }
178         
179         // Time query
180         else if(mode == 'time') {
181                 iq.appendNode('time', {'xmlns': NS_URN_TIME});
182                 con.send(iq, localTimeUserInfos);
183         }
184         
185         // Version query
186         else if(mode == 'version') {
187                 iq.setQuery(NS_VERSION);
188                 con.send(iq, versionUserInfos);
189         }
190 }
191
192 // Checks if the waiting item can be hidden
193 function vCardBuddyInfos() {
194         $('#userinfos .content').removeClass('vcard');
195         wUserInfos();
196 }
197
198 // Displays the buddy comments
199 function displayBuddyComments(xid) {
200         // We get the value in the database
201         var value = getDB('rosternotes', xid);
202         
203         // Display the value
204         if(value)
205                 $('#userinfos .rosternotes').val(value);
206 }
207
208 // Displays the user's last activity result
209 function lastActivityUserInfos(iq) {
210         // Extract the request ID
211         var id = iq.getID();
212         var path = '#userinfos[data-last=' + id + ']';
213         
214         // End if session does not exist
215         if(!exists(path))
216                 return;
217         
218         if(iq && (iq.getType() == 'result')) {
219                 // Get the values
220                 var from = fullXID(getStanzaFrom(iq));
221                 var seconds = $(iq.getNode()).find('query').attr('seconds');
222                 
223                 // Any seconds?
224                 if(seconds != undefined) {
225                         // Initialize the parsing
226                         var last;
227                         seconds = parseInt(seconds);
228                         
229                         // Active user
230                         if(seconds <= 60)
231                                 last = _e("User currently active");
232                         
233                         // Inactive user
234                         else {
235                                 // Parse the date
236                                 var date_now = new Date();
237                                 var time_now = date_now.getTime();
238                                 var date_last = new Date(date_now - (seconds * 1000));
239                                 var date = date_last.toLocaleString();
240                                 
241                                 // Offline user
242                                 if(from.indexOf('/') == -1)
243                                         last = printf(_e("Last seen: %s"), date);
244                                 
245                                 // Online user
246                                 else
247                                         last = printf(_e("Inactive since: %s"), date);
248                         }
249                         
250                         // Append this text
251                         $('#userinfos .buddy-last').text(last);
252                 }
253                 
254                 logThis('Last activity received: ' + from);
255         }
256         
257         $('#userinfos .content').removeClass('last');
258         wUserInfos();
259 }
260
261 // Displays the user's software version result
262 function versionUserInfos(iq) {
263         // Extract the request ID
264         var id = iq.getID();
265         var path = '#userinfos[data-version=' + id + ']';
266         
267         // End if session does not exist
268         if(!exists(path))
269                 return;
270         
271         // Extract the reply data
272         if(iq && (iq.getType() == 'result')) {
273                 // Get the values
274                 var xml = iq.getQuery();
275                 var name = $(xml).find('name').text();
276                 var version = $(xml).find('version').text();
277                 var os = $(xml).find('os').text();
278                 
279                 // Put the values together
280                 if(name && version)
281                         name = name + ' ' + version;
282                 
283                 // Display the values
284                 if(name)
285                         $(path + ' #BUDDY-CLIENT').text(name);
286                 if(os)
287                         $(path + ' #BUDDY-SYSTEM').text(os);
288                 
289                 logThis('Software version received: ' + fullXID(getStanzaFrom(iq)));
290         }
291         
292         $('#userinfos .content').removeClass('version');
293         wUserInfos();
294 }
295
296 // Displays the user's local time result
297 function localTimeUserInfos(iq) {
298         // Extract the request ID
299         var id = iq.getID();
300         var path = '#userinfos[data-time=' + id + ']';
301         
302         // End if session does not exist
303         if(!exists(path))
304                 return;
305         
306         if(iq && (iq.getType() == 'result')) {
307                 // Get the values
308                 var xml = iq.getNode();
309                 var tzo = $(xml).find('tzo').text();
310                 var utc = $(xml).find('utc').text();
311                 
312                 // Any UTC?
313                 if(utc) {
314                         // Add the TZO if there's no one
315                         if(tzo && utc.match(/^(.+)Z$/))
316                                 utc = RegExp.$1 + tzo;
317                         
318                         // Get the local date string
319                         var local_string = Date.hrTime(utc);
320                         
321                         // Then display it
322                         $(path + ' #BUDDY-TIME').text(local_string);
323                 }
324                 
325                 logThis('Local time received: ' + fullXID(getStanzaFrom(iq)));
326         }
327         
328         $('#userinfos .content').removeClass('time');
329         wUserInfos();
330 }
331
332 // Hides the waiting image if needed
333 function wUserInfos() {
334         var selector = $('#userinfos .content');
335         
336         if(!selector.hasClass('vcard') && !selector.hasClass('last') && !selector.hasClass('version') && !selector.hasClass('time'))
337                 $('#userinfos .wait').hide();
338 }
339
340 // Sends the buddy comments
341 function sendBuddyComments() {
342         // Update the current value
343         var value = $('#userinfos .rosternotes').val();
344         var xid = $('#userinfos .buddy-xid').text();
345         
346         // Necessary to update?
347         var old_value = getDB('rosternotes', xid);
348         
349         if((old_value == value) || (!old_value && !value))
350                 return false;
351         
352         // Update the database
353         setDB('rosternotes', xid, value);
354         
355         // Send the new buddy storage values
356         var iq = new JSJaCIQ();
357         iq.setType('set');
358         var query = iq.setQuery(NS_PRIVATE);
359         var storage = query.appendChild(iq.buildNode('storage', {'xmlns': NS_ROSTERNOTES}));
360         
361         // We regenerate the XML
362         for(var i = 0; i < sessionStorage.length; i++) {
363                 // Get the pointer values
364                 var current = sessionStorage.key(i);
365                 
366                 // If the pointer is on a stored rosternote
367                 if(explodeThis('_', current, 0) == 'rosternotes') {
368                         var xid = explodeThis('_', current, 1);
369                         var value = sessionStorage.getItem(current);
370                         
371                         if(xid && value)
372                                 storage.appendChild(iq.buildNode('note', {'jid': xid, 'xmlns': NS_ROSTERNOTES}, value));
373                 }
374         }
375         
376         con.send(iq);
377         
378         return false;
379 }
380
381 // Switches the user-infos tabs
382 function switchUInfos(id) {
383         $('#userinfos .content .one-lap').hide();
384         $('#userinfos .content .info' + id).show();
385         $('#userinfos .tab a').removeClass('tab-active');
386         $('#userinfos .tab a[data-key=' + id + ']').addClass('tab-active');
387         
388         return false;
389 }
390
391 // Gets the user's informations when creating a new chat
392 function getUserInfos(hash, xid, nick, type) {
393         // This is a normal chat
394         if(type != 'private') {
395                 // Display the buddy name
396                 if(nick) {
397                         $('#' + hash + ' .top .name .bc-name').text(nick);
398                         $('#page-switch .' + hash + ' .name').text(nick);
399                 }
400                 
401                 // Get the buddy PEP informations
402                 displayAllPEP(xid);
403         }
404         
405         // Display the buddy presence
406         presenceFunnel(xid, hash);
407 }
408
409 // Plugin launcher
410 function launchUserInfos() {
411         // Click events
412         $('#userinfos .tab a').click(function() {
413                 // Yet active?
414                 if($(this).hasClass('tab-active'))
415                         return false;
416                 
417                 // Switch to the good tab
418                 var key = parseInt($(this).attr('data-key'));
419                 
420                 return switchUInfos(key);
421         });
422         
423         $('#userinfos .bottom .finish').click(function() {
424                 return closeUserInfos();
425         });
426 }