]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/favorites.js
Revert to stable version 3.5.4
[friendica-addons.git] / jappixmini / jappix / js / favorites.js
1 /*
2
3 Jappix - An open social platform
4 These are the favorites JS scripts for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 23/06/11
11
12 */
13
14 // Opens the favorites popup
15 function openFavorites() {
16         // Popup HTML content
17         var html = 
18         '<div class="top">' + _e("Manage favorite rooms") + '</div>' + 
19         
20         '<div class="content">' + 
21                 '<div class="switch-fav">' + 
22                         '<div class="room-switcher room-list">' + 
23                                 '<div class="icon list-icon talk-images"></div>' + 
24                                 
25                                 _e("Change favorites") + 
26                         '</div>' + 
27                         
28                         '<div class="room-switcher room-search">' + 
29                                 '<div class="icon search-icon talk-images"></div>' + 
30                                 
31                                 _e("Search a room") + 
32                         '</div>' + 
33                 '</div>' + 
34                 
35                 '<div class="static-fav">' + 
36                         '<div class="favorites-edit favorites-content">' + 
37                                 '<div class="head fedit-head static-fav-head">' + 
38                                         '<div class="head-text fedit-head-text">' + _e("Select a favorite") + '</div>' + 
39                                         
40                                         '<select name="fedit-head-select" class="head-select fedit-head-select"></select>' + 
41                                 '</div>' + 
42                                 
43                                 '<div class="results fedit-results static-fav-results">' + 
44                                         '<div class="fedit-line">' + 
45                                                 '<label>' + _e("Name") + '</label>' + 
46                                                 
47                                                 '<input class="fedit-title" type="text" required="" />' + 
48                                         '</div>' + 
49                                         
50                                         '<div class="fedit-line">' + 
51                                                 '<label>' + _e("Nickname") + '</label>' + 
52                                                 
53                                                 '<input class="fedit-nick" type="text" value="' + getNick() + '" required="" />' + 
54                                         '</div>' + 
55                                         
56                                         '<div class="fedit-line">' + 
57                                                 '<label>' + _e("Room") + '</label>' + 
58                                                 
59                                                 '<input class="fedit-chan" type="text" required="" />' + 
60                                         '</div>' + 
61                                         
62                                         '<div class="fedit-line">' + 
63                                                 '<label>' + _e("Server") + '</label>' + 
64                                                 
65                                                 '<input class="fedit-server" type="text" value="' + HOST_MUC + '" required="" />' + 
66                                         '</div>' + 
67                                         
68                                         '<div class="fedit-line">' + 
69                                                 '<label>' + _e("Password") + '</label>' + 
70                                                 
71                                                 '<input class="fedit-password" type="password" />' + 
72                                         '</div>' + 
73                                         
74                                         '<div class="fedit-line">' + 
75                                                 '<label>' + _e("Automatic") + '</label>' + 
76                                                 
77                                                 '<input type="checkbox" class="fedit-autojoin" />' + 
78                                         '</div>' + 
79                                         
80                                         '<div class="fedit-actions">' + 
81                                                 '<a href="#" class="fedit-terminate fedit-add add one-button talk-images">' + _e("Add") + '</a>' + 
82                                                 '<a href="#" class="fedit-terminate fedit-edit one-button talk-images">' + _e("Edit") + '</a>' + 
83                                                 '<a href="#" class="fedit-terminate fedit-remove remove one-button talk-images">' + _e("Remove") + '</a>' + 
84                                         '</div>' + 
85                                 '</div>' + 
86                         '</div>' + 
87                         
88                         '<div class="favorites-search favorites-content">' + 
89                                 '<div class="head fsearch-head static-fav-head">' + 
90                                         '<div class="head-text fsearch-head-text">' + _e("Search a room on") + '</div>' + 
91                                         
92                                         '<input type="text" class="head-input fsearch-head-server" value="' + HOST_MUC + '" />' + 
93                                 '</div>' + 
94                                 
95                                 '<div class="results fsearch-results static-fav-results">' + 
96                                         '<p class="fsearch-noresults">' + _e("No room found on this server.") + '</p>' + 
97                                 '</div>' + 
98                         '</div>' + 
99                 '</div>' + 
100         '</div>' + 
101         
102         '<div class="bottom">' + 
103                 '<div class="wait wait-medium"></div>' + 
104                 
105                 '<a href="#" class="finish">' + _e("Close") + '</a>' + 
106         '</div>';
107         
108         // Create the popup
109         createPopup('favorites', html);
110         
111         // Load the favorites
112         loadFavorites();
113         
114         // Associate the events
115         launchFavorites();
116 }
117
118 // Resets the favorites elements
119 function resetFavorites() {
120         var path = '#favorites ';
121         
122         $(path + '.wait, ' + path + '.fedit-terminate').hide();
123         $(path + '.fedit-add').show();
124         $(path + '.fsearch-oneresult').remove();
125         $(path + 'input').val('');
126         $(path + '.please-complete').removeClass('please-complete');
127         $(path + '.fedit-nick').val(getNick());
128         $(path + '.fsearch-head-server, ' + path + '.fedit-server').val(HOST_MUC);
129         $(path + '.fedit-autojoin').attr('checked', false);
130 }
131
132 // Quits the favorites popup
133 function quitFavorites() {
134         // Destroy the popup
135         destroyPopup('favorites');
136         
137         return false;
138 }
139
140 // Adds a room to the favorites
141 function addThisFavorite(roomXID, roomName) {
142         // Button path
143         var button = '#favorites .fsearch-results div[data-xid=' + escape(roomXID) + '] a.one-button';
144         
145         // Add a remove button instead of the add one
146         $(button + '.add').replaceWith('<a href="#" class="one-button remove talk-images">' + _e("Remove") + '</a>');
147         
148         // Click event
149         $(button + '.remove').click(function() {
150                 return removeThisFavorite(roomXID, roomName);
151         });
152         
153         // Hide the add button in the (opened?) groupchat
154         $('#' + hex_md5(roomXID) + ' .tools-add').hide();
155         
156         // Add the database entry
157         displayFavorites(roomXID, explodeThis(' (', roomName, 0), getNick(), '0', '');
158         
159         // Publish the favorites
160         favoritePublish();
161         
162         return false;
163 }
164
165 // Removes a room from the favorites
166 function removeThisFavorite(roomXID, roomName) {
167         // Button path
168         var button = '#favorites .fsearch-results div[data-xid=' + escape(roomXID) + '] a.one-button';
169         
170         // Add a remove button instead of the add one
171         $(button + '.remove').replaceWith('<a href="#" class="one-button add talk-images">' + _e("Add") + '</a>');
172         
173         // Click event
174         $(button + '.add').click(function() {
175                 return addThisFavorite(roomXID, roomName);
176         });
177         
178         // Show the add button in the (opened?) groupchat
179         $('#' + hex_md5(roomXID) + ' .tools-add').show();
180         
181         // Remove the favorite
182         removeFavorite(roomXID, true);
183         
184         // Publish the favorites
185         favoritePublish();
186         
187         return false;
188 }
189
190 // Edits a favorite
191 function editFavorite() {
192         // Path to favorites
193         var favorites = '#favorites .';
194         
195         // Reset the favorites
196         resetFavorites();
197         
198         // Show the edit/remove button, hide the others
199         $(favorites + 'fedit-terminate').hide();
200         $(favorites + 'fedit-edit').show();
201         $(favorites + 'fedit-remove').show();
202         
203         // We retrieve the values
204         var xid = $(favorites + 'fedit-head-select').val();
205         var data = XMLFromString(getDB('favorites', xid));
206         
207         // If this is not the default room
208         if(xid != 'none') {
209                 // We apply the values
210                 $(favorites + 'fedit-title').val($(data).find('name').text());
211                 $(favorites + 'fedit-nick').val($(data).find('nick').text());
212                 $(favorites + 'fedit-chan').val(getXIDNick(xid));
213                 $(favorites + 'fedit-server').val(getXIDHost(xid));
214                 $(favorites + 'fedit-password').val($(data).find('password').text());
215                 
216                 if($(data).find('autojoin').text() == '1')
217                         $(favorites + 'fedit-autojoin').attr('checked', true);
218         }
219         
220         else
221                 resetFavorites();
222 }
223
224 // Adds a favorite
225 function addFavorite() {
226         // Path to favorites
227         var favorites = '#favorites .';
228         
229         // We reset the inputs
230         $(favorites + 'fedit-title, ' + favorites + 'fedit-nick, ' + favorites + 'fedit-chan, ' + favorites + 'fedit-server, ' + favorites + 'fedit-password').val('');
231         
232         // Show the add button, hide the others
233         $(favorites + 'fedit-terminate').hide();
234         $(favorites + 'fedit-add').show();
235 }
236
237 // Terminate a favorite editing
238 function terminateThisFavorite(type) {
239         // Path to favorites
240         var favorites = '#favorites ';
241         
242         // We get the values of the current edited groupchat
243         var old_xid = $(favorites + '.fedit-head-select').val();
244         
245         var title = $(favorites + '.fedit-title').val();
246         var nick = $(favorites + '.fedit-nick').val();
247         var room = $(favorites + '.fedit-chan').val();
248         var server = $(favorites + '.fedit-server').val();
249         var xid = room + '@' + server;
250         var password = $(favorites + '.fedit-password').val();
251         var autojoin = '0';
252         
253         if($(favorites + '.fedit-autojoin').filter(':checked').size())
254                 autojoin = '1';
255         
256         // We check the missing values and send this if okay
257         if((type == 'add') || (type == 'edit')) {
258                 if(title && nick && room && server) {
259                         // Remove the edited room
260                         if(type == 'edit')
261                                 removeFavorite(old_xid, true);
262                         
263                         // Display the favorites
264                         displayFavorites(xid, title, nick, autojoin, password);
265                         
266                         // Reset the inputs
267                         resetFavorites();
268                 }
269                 
270                 else {
271                         $(favorites + 'input[required]').each(function() {
272                                 var select = $(this);
273                                 
274                                 if(!select.val())
275                                         $(document).oneTime(10, function() {
276                                                 select.addClass('please-complete').focus();
277                                         });
278                                 else
279                                         select.removeClass('please-complete');  
280                         });
281                 }
282         }
283         
284         // Must remove a favorite?
285         else if(type == 'remove') {
286                 removeFavorite(old_xid, true);
287                 
288                 // Reset the inputs
289                 resetFavorites();
290         }
291         
292         // Publish the new favorites
293         favoritePublish();
294         
295         logThis('Action on this bookmark: ' + room + '@' + server + ' / ' + type, 3);
296         
297         return false;
298 }
299
300 // Removes a favorite
301 function removeFavorite(xid, database) {
302         // We remove the target favorite everywhere needed
303         $('.buddy-conf-groupchat-select option[value=' + xid + ']').remove();
304         $('.fedit-head-select option[value=' + xid + ']').remove();
305         
306         // Must remove it from database?
307         if(database)
308                 removeDB('favorites', xid);
309 }
310
311 // Sends a favorite to the XMPP server
312 function favoritePublish() {
313         var iq = new JSJaCIQ();
314         iq.setType('set');
315         
316         var query = iq.setQuery(NS_PRIVATE);
317         var storage = query.appendChild(iq.buildNode('storage', {'xmlns': NS_BOOKMARKS}));
318         
319         // We generate the XML
320         for(var i = 0; i < sessionStorage.length; i++) {
321                 // Get the pointer values
322                 var current = sessionStorage.key(i);
323                 
324                 // If the pointer is on a stored favorite
325                 if(explodeThis('_', current, 0) == 'favorites') {
326                         var data = XMLFromString(sessionStorage.getItem(current));
327                         var xid = $(data).find('xid').text();
328                         var rName = $(data).find('name').text();
329                         var nick = $(data).find('nick').text();
330                         var password = $(data).find('password').text();
331                         var autojoin = $(data).find('autojoin').text();
332                         
333                         // We create the node for this groupchat
334                         var item = storage.appendChild(iq.buildNode('conference', {'name': rName, 'jid': xid, 'autojoin': autojoin, xmlns: NS_BOOKMARKS}));
335                         item.appendChild(iq.buildNode('nick', {xmlns: NS_BOOKMARKS}, nick));
336                         
337                         if(password)
338                                 item.appendChild(iq.buildNode('password', {xmlns: NS_BOOKMARKS}, password));
339                         
340                         logThis('Bookmark sent: ' + xid, 3);
341                 }
342         }
343         
344         con.send(iq);
345 }
346
347 // Gets a list of the MUC items on a given server
348 function getGCList() {
349         var path = '#favorites .';
350         var gcServer = $('.fsearch-head-server').val();
351         
352         // We reset some things
353         $(path + 'fsearch-oneresult').remove();
354         $(path + 'fsearch-noresults').hide();
355         $(path + 'wait').show();
356         
357         var iq = new JSJaCIQ();
358         iq.setType('get');
359         iq.setTo(gcServer);
360         
361         iq.setQuery(NS_DISCO_ITEMS);
362         
363         con.send(iq, handleGCList);
364 }
365
366 // Handles the MUC items list
367 function handleGCList(iq) {
368         var path = '#favorites .';
369         var from = fullXID(getStanzaFrom(iq));
370         
371         if (!iq || (iq.getType() != 'result')) {
372                 openThisError(3);
373                 
374                 $(path + 'wait').hide();
375                 
376                 logThis('Error while retrieving the rooms: ' + from, 1);
377         }
378         
379         else {
380                 var handleXML = iq.getQuery();
381                 
382                 if($(handleXML).find('item').size()) {
383                         // Initialize the HTML code
384                         var html = '';
385                         
386                         $(handleXML).find('item').each(function() {
387                                 var roomXID = $(this).attr('jid');
388                                 var roomName = $(this).attr('name');
389                                 
390                                 if(roomXID && roomName) {
391                                         // Escaped values
392                                         var escaped_xid = encodeOnclick(roomXID);
393                                         var escaped_name = encodeOnclick(roomName);
394                                         
395                                         // Initialize the room HTML
396                                         html += '<div class="oneresult fsearch-oneresult" data-xid="' + escape(roomXID) + '">' + 
397                                                         '<div class="room-name">' + roomName.htmlEnc() + '</div>' + 
398                                                         '<a href="#" class="one-button join talk-images" onclick="return joinFavorite(\'' + escaped_xid + '\');">' + _e("Join") + '</a>';
399                                         
400                                         // This room is yet a favorite
401                                         if(existDB('favorites', roomXID))
402                                                 html += '<a href="#" class="one-button remove talk-images" onclick="return removeThisFavorite(\'' + escaped_xid + '\', \'' + escaped_name + '\');">' + _e("Remove") + '</a>';
403                                         else
404                                                 html += '<a href="#" class="one-button add talk-images" onclick="return addThisFavorite(\'' + escaped_xid + '\', \'' + escaped_name + '\');">' + _e("Add") + '</a>';
405                                         
406                                         // Close the room HTML
407                                         html += '</div>';
408                                 }
409                         });
410                         
411                         // Append this code to the popup
412                         $(path + 'fsearch-results').append(html);
413                 }
414                 
415                 else
416                         $(path + 'fsearch-noresults').show();
417                 
418                 logThis('Rooms retrieved: ' + from, 3);
419         }
420         
421         $(path + 'wait').hide();
422 }
423
424 // Joins a groupchat from favorites
425 function joinFavorite(room) {
426         quitFavorites();
427         checkChatCreate(room, 'groupchat', '', '', getXIDNick(room));
428         
429         return false;
430 }
431
432 // Displays a given favorite
433 function displayFavorites(xid, name, nick, autojoin, password) {
434         // Generate the HTML code
435         var html = '<option value="' + encodeQuotes(xid) + '">' + name.htmlEnc() + '</option>';
436         
437         // Remove the existing favorite
438         removeFavorite(xid, false);
439         
440         // We complete the select forms
441         $('#buddy-list .gc-join-first-option, #favorites .fedit-head-select-first-option').after(html);
442         
443         // We store the informations
444         var value = '<groupchat><xid>' + xid.htmlEnc() + '</xid><name>' + name.htmlEnc() + '</name><nick>' + nick.htmlEnc() + '</nick><autojoin>' + autojoin.htmlEnc() + '</autojoin><password>' + password.htmlEnc() + '</password></groupchat>';
445         setDB('favorites', xid, value);
446 }
447
448 // Loads the favorites for the popup
449 function loadFavorites() {
450         // Initialize the HTML code
451         var html = '';
452         
453         // Read the database
454         for(var i = 0; i < sessionStorage.length; i++) {
455                 // Get the pointer values
456                 var current = sessionStorage.key(i);
457                 
458                 // If the pointer is on a stored favorite
459                 if(explodeThis('_', current, 0) == 'favorites') {
460                         var data = XMLFromString(sessionStorage.getItem(current));
461                         
462                         // Add the current favorite to the HTML code
463                         html += '<option value="' + encodeQuotes($(data).find('xid').text()) + '">' + $(data).find('name').text().htmlEnc() + '</option>';
464                 }
465         }
466         
467         // Generate specific HTML code
468         var favorites_bubble = '<option value="none" class="gc-join-first-option" selected="">' + _e("Select a favorite") +  '</option>' + html;
469         var favorites_popup = '<option value="none" class="fedit-head-select-first-option" selected="">' + _e("Select a favorite") + '</option>' + html;
470         
471         // Append the HTML code
472         $('#buddy-list .buddy-conf-groupchat-select').html(favorites_bubble);
473         $('#favorites .fedit-head-select').html(favorites_popup);
474 }
475
476 // Plugin launcher
477 function launchFavorites() {
478         var path = '#favorites .';
479         
480         // Keyboard events
481         $(path + 'fsearch-head-server').keyup(function(e) {
482                 if(e.keyCode == 13) {
483                         // No value?
484                         if(!$(this).val())
485                                 $(this).val(HOST_MUC);
486                         
487                         // Get the list
488                         getGCList();
489                 }
490         });
491         
492         $(path + 'fedit-line input').keyup(function(e) {
493                 if(e.keyCode == 13) {
494                         // Edit a favorite
495                         if($(path + 'fedit-edit').is(':visible'))
496                                 terminateThisFavorite('edit');
497                         
498                         // Add a favorite
499                         else
500                                 terminateThisFavorite('add');
501                 }
502         });
503         
504         // Change events
505         $('.fedit-head-select').change(editFavorite);
506         
507         // Click events
508         $(path + 'room-switcher').click(function() {
509                 $(path + 'favorites-content').hide();
510                 resetFavorites();
511         });
512         
513         $(path + 'room-list').click(function() {
514                 $(path + 'favorites-edit').show();
515         });
516         
517         $(path + 'room-search').click(function() {
518                 $(path + 'favorites-search').show();
519                 getGCList();
520         });
521         
522         $(path + 'fedit-add').click(function() {
523                 return terminateThisFavorite('add');
524         });
525         
526         $(path + 'fedit-edit').click(function() {
527                 return terminateThisFavorite('edit');
528         });
529         
530         $(path + 'fedit-remove').click(function() {
531                 return terminateThisFavorite('remove');
532         });
533         
534         $(path + 'bottom .finish').click(function() {
535                 return quitFavorites();
536         });
537 }