3 Jappix - An open social platform
4 These are the favorites JS scripts for Jappix
6 -------------------------------------------------
10 Last revision: 23/06/11
14 // Opens the favorites popup
15 function openFavorites() {
18 '<div class="top">' + _e("Manage favorite rooms") + '</div>' +
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>' +
25 _e("Change favorites") +
28 '<div class="room-switcher room-search">' +
29 '<div class="icon search-icon talk-images"></div>' +
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>' +
40 '<select name="fedit-head-select" class="head-select fedit-head-select"></select>' +
43 '<div class="results fedit-results static-fav-results">' +
44 '<div class="fedit-line">' +
45 '<label>' + _e("Name") + '</label>' +
47 '<input class="fedit-title" type="text" required="" />' +
50 '<div class="fedit-line">' +
51 '<label>' + _e("Nickname") + '</label>' +
53 '<input class="fedit-nick" type="text" value="' + getNick() + '" required="" />' +
56 '<div class="fedit-line">' +
57 '<label>' + _e("Room") + '</label>' +
59 '<input class="fedit-chan" type="text" required="" />' +
62 '<div class="fedit-line">' +
63 '<label>' + _e("Server") + '</label>' +
65 '<input class="fedit-server" type="text" value="' + HOST_MUC + '" required="" />' +
68 '<div class="fedit-line">' +
69 '<label>' + _e("Password") + '</label>' +
71 '<input class="fedit-password" type="password" />' +
74 '<div class="fedit-line">' +
75 '<label>' + _e("Automatic") + '</label>' +
77 '<input type="checkbox" class="fedit-autojoin" />' +
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>' +
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>' +
92 '<input type="text" class="head-input fsearch-head-server" value="' + HOST_MUC + '" />' +
95 '<div class="results fsearch-results static-fav-results">' +
96 '<p class="fsearch-noresults">' + _e("No room found on this server.") + '</p>' +
102 '<div class="bottom">' +
103 '<div class="wait wait-medium"></div>' +
105 '<a href="#" class="finish">' + _e("Close") + '</a>' +
109 createPopup('favorites', html);
111 // Load the favorites
114 // Associate the events
118 // Resets the favorites elements
119 function resetFavorites() {
120 var path = '#favorites ';
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);
132 // Quits the favorites popup
133 function quitFavorites() {
135 destroyPopup('favorites');
140 // Adds a room to the favorites
141 function addThisFavorite(roomXID, roomName) {
143 var button = '#favorites .fsearch-results div[data-xid=' + escape(roomXID) + '] a.one-button';
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>');
149 $(button + '.remove').click(function() {
150 return removeThisFavorite(roomXID, roomName);
153 // Hide the add button in the (opened?) groupchat
154 $('#' + hex_md5(roomXID) + ' .tools-add').hide();
156 // Add the database entry
157 displayFavorites(roomXID, explodeThis(' (', roomName, 0), getNick(), '0', '');
159 // Publish the favorites
165 // Removes a room from the favorites
166 function removeThisFavorite(roomXID, roomName) {
168 var button = '#favorites .fsearch-results div[data-xid=' + escape(roomXID) + '] a.one-button';
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>');
174 $(button + '.add').click(function() {
175 return addThisFavorite(roomXID, roomName);
178 // Show the add button in the (opened?) groupchat
179 $('#' + hex_md5(roomXID) + ' .tools-add').show();
181 // Remove the favorite
182 removeFavorite(roomXID, true);
184 // Publish the favorites
191 function editFavorite() {
193 var favorites = '#favorites .';
195 // Reset the favorites
198 // Show the edit/remove button, hide the others
199 $(favorites + 'fedit-terminate').hide();
200 $(favorites + 'fedit-edit').show();
201 $(favorites + 'fedit-remove').show();
203 // We retrieve the values
204 var xid = $(favorites + 'fedit-head-select').val();
205 var data = XMLFromString(getDB('favorites', xid));
207 // If this is not the default room
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());
216 if($(data).find('autojoin').text() == '1')
217 $(favorites + 'fedit-autojoin').attr('checked', true);
225 function addFavorite() {
227 var favorites = '#favorites .';
229 // We reset the inputs
230 $(favorites + 'fedit-title, ' + favorites + 'fedit-nick, ' + favorites + 'fedit-chan, ' + favorites + 'fedit-server, ' + favorites + 'fedit-password').val('');
232 // Show the add button, hide the others
233 $(favorites + 'fedit-terminate').hide();
234 $(favorites + 'fedit-add').show();
237 // Terminate a favorite editing
238 function terminateThisFavorite(type) {
240 var favorites = '#favorites ';
242 // We get the values of the current edited groupchat
243 var old_xid = $(favorites + '.fedit-head-select').val();
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();
253 if($(favorites + '.fedit-autojoin').filter(':checked').size())
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
261 removeFavorite(old_xid, true);
263 // Display the favorites
264 displayFavorites(xid, title, nick, autojoin, password);
271 $(favorites + 'input[required]').each(function() {
272 var select = $(this);
275 $(document).oneTime(10, function() {
276 select.addClass('please-complete').focus();
279 select.removeClass('please-complete');
284 // Must remove a favorite?
285 else if(type == 'remove') {
286 removeFavorite(old_xid, true);
292 // Publish the new favorites
295 logThis('Action on this bookmark: ' + room + '@' + server + ' / ' + type, 3);
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();
306 // Must remove it from database?
308 removeDB('favorites', xid);
311 // Sends a favorite to the XMPP server
312 function favoritePublish() {
313 var iq = new JSJaCIQ();
316 var query = iq.setQuery(NS_PRIVATE);
317 var storage = query.appendChild(iq.buildNode('storage', {'xmlns': NS_BOOKMARKS}));
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);
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();
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));
338 item.appendChild(iq.buildNode('password', {xmlns: NS_BOOKMARKS}, password));
340 logThis('Bookmark sent: ' + xid, 3);
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();
352 // We reset some things
353 $(path + 'fsearch-oneresult').remove();
354 $(path + 'fsearch-noresults').hide();
355 $(path + 'wait').show();
357 var iq = new JSJaCIQ();
361 iq.setQuery(NS_DISCO_ITEMS);
363 con.send(iq, handleGCList);
366 // Handles the MUC items list
367 function handleGCList(iq) {
368 var path = '#favorites .';
369 var from = fullXID(getStanzaFrom(iq));
371 if (!iq || (iq.getType() != 'result')) {
374 $(path + 'wait').hide();
376 logThis('Error while retrieving the rooms: ' + from, 1);
380 var handleXML = iq.getQuery();
382 if($(handleXML).find('item').size()) {
383 // Initialize the HTML code
386 $(handleXML).find('item').each(function() {
387 var roomXID = $(this).attr('jid');
388 var roomName = $(this).attr('name');
390 if(roomXID && roomName) {
392 var escaped_xid = encodeOnclick(roomXID);
393 var escaped_name = encodeOnclick(roomName);
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>';
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>';
404 html += '<a href="#" class="one-button add talk-images" onclick="return addThisFavorite(\'' + escaped_xid + '\', \'' + escaped_name + '\');">' + _e("Add") + '</a>';
406 // Close the room HTML
411 // Append this code to the popup
412 $(path + 'fsearch-results').append(html);
416 $(path + 'fsearch-noresults').show();
418 logThis('Rooms retrieved: ' + from, 3);
421 $(path + 'wait').hide();
424 // Joins a groupchat from favorites
425 function joinFavorite(room) {
427 checkChatCreate(room, 'groupchat', '', '', getXIDNick(room));
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>';
437 // Remove the existing favorite
438 removeFavorite(xid, false);
440 // We complete the select forms
441 $('#buddy-list .gc-join-first-option, #favorites .fedit-head-select-first-option').after(html);
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);
448 // Loads the favorites for the popup
449 function loadFavorites() {
450 // Initialize the HTML code
454 for(var i = 0; i < sessionStorage.length; i++) {
455 // Get the pointer values
456 var current = sessionStorage.key(i);
458 // If the pointer is on a stored favorite
459 if(explodeThis('_', current, 0) == 'favorites') {
460 var data = XMLFromString(sessionStorage.getItem(current));
462 // Add the current favorite to the HTML code
463 html += '<option value="' + encodeQuotes($(data).find('xid').text()) + '">' + $(data).find('name').text().htmlEnc() + '</option>';
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;
471 // Append the HTML code
472 $('#buddy-list .buddy-conf-groupchat-select').html(favorites_bubble);
473 $('#favorites .fedit-head-select').html(favorites_popup);
477 function launchFavorites() {
478 var path = '#favorites .';
481 $(path + 'fsearch-head-server').keyup(function(e) {
482 if(e.keyCode == 13) {
485 $(this).val(HOST_MUC);
492 $(path + 'fedit-line input').keyup(function(e) {
493 if(e.keyCode == 13) {
495 if($(path + 'fedit-edit').is(':visible'))
496 terminateThisFavorite('edit');
500 terminateThisFavorite('add');
505 $('.fedit-head-select').change(editFavorite);
508 $(path + 'room-switcher').click(function() {
509 $(path + 'favorites-content').hide();
513 $(path + 'room-list').click(function() {
514 $(path + 'favorites-edit').show();
517 $(path + 'room-search').click(function() {
518 $(path + 'favorites-search').show();
522 $(path + 'fedit-add').click(function() {
523 return terminateThisFavorite('add');
526 $(path + 'fedit-edit').click(function() {
527 return terminateThisFavorite('edit');
530 $(path + 'fedit-remove').click(function() {
531 return terminateThisFavorite('remove');
534 $(path + 'bottom .finish').click(function() {
535 return quitFavorites();