/* Jappix - An open social platform These are the Roster Item Exchange JS script for Jappix ------------------------------------------------- License: AGPL Author: Valérian Saliou */ // Bundle var RosterX = (function () { /** * Alias of this * @private */ var self = {}; /** * Opens the rosterx tools * @public * @param {string} data * @return {undefined} */ self.open = function(data) { try { // Popup HTML content var html = '
' + Common._e("Suggested friends") + '
' + '
' + '
' + '' + Common._e("Uncheck all") + '' + '' + Common._e("Check all") + '' + '
' + '
' + '
' + '
' + '' + Common._e("Save") + '' + '' + Common._e("Cancel") + '' + '
'; // Create the popup Popup.create('rosterx', html); // Associate the events self.instance(); // Parse the data self.parse(data); Console.log('Roster Item Exchange popup opened.'); } catch(e) { Console.error('RosterX.open', e); } }; /** * Closes the rosterx tools * @public * @return {boolean} */ self.close = function() { try { // Destroy the popup Popup.destroy('rosterx'); } catch(e) { Console.error('RosterX.close', e); } finally { return false; } }; /** * Parses a rosterx query * @public * @param {string} data * @return {undefined} */ self.parse = function(data) { try { // Main selector var x = $(data).find('x[xmlns="' + NS_ROSTERX + '"]:first'); // Parse data x.find('item').each(function() { // Generate group XML var group = ''; $(this).find('group').each(function() { group += '' + $(this).text().htmlEnc() + ''; }); if(group) group = '' + group + ''; // Display it! self.display($(this).attr('jid'), $(this).attr('name'), group, $(this).attr('action')); }); // Click to check/uncheck $('#rosterx .oneresult').click(function(evt) { // No need to apply when click on input if($(evt.target).is('input[type="checkbox"]')) return; // Input selector var checkbox = $(this).find('input[type="checkbox"]'); // Check or uncheck? if(checkbox.filter(':checked').size()) checkbox.removeAttr('checked'); else checkbox.attr('checked', true); }); } catch(e) { Console.error('RosterX.parse', e); } }; /** * Displays a rosterx item * @public * @param {string} xid * @param {string} nick * @param {string} group * @param {string} action * @return {boolean} */ self.display = function(xid, nick, group, action) { try { // End if no XID if(!xid) return false; // Set up a default action if no one if(!action || (action != 'modify') || (action != 'delete')) action = 'add'; // Override "undefined" for nickname if(!nick) nick = ''; // Display it $('#rosterx .results').append( '
' + '' + '' + nick.htmlEnc() + '' + '' + xid.htmlEnc() + '' + '' + '
' ); return true; } catch(e) { Console.error('RosterX.display', e); } }; /** * Saves the rosterx settings * @public * @return {undefined} */ self.save = function() { try { // Send the requests $('#rosterx .results input[type="checkbox"]').filter(':checked').each(function() { // Read the attributes var nick = $(this).attr('data-name'); var xid = $(this).attr('data-xid'); var action = $(this).attr('data-action'); var group = $(this).attr('data-group'); // Parse groups XML var group_arr = []; if(group) { $(group).find('group').each(function() { group_arr.push($(this).text().revertHtmlEnc()); }); } // Process the asked action var roster_item = $('#roster .' + hex_md5(xid)); switch(action) { // Buddy add case 'add': if(!Common.exists(roster_item)) { Presence.sendSubscribe(xid, 'subscribe'); Roster.send(xid, '', nick, group_arr); } break; // Buddy edit case 'modify': if(Common.exists(roster_item)) Roster.send(xid, '', nick, group_arr); break; // Buddy delete case 'delete': if(Common.exists(roster_item)) Roster.send(xid, 'remove'); break; } }); // Close the popup self.close(); } catch(e) { Console.error('RosterX.save', e); } }; /** * Plugin launcher * @public * @return {undefined} */ self.instance = function() { try { // Click events $('#rosterx .bottom .finish').click(function() { if($(this).is('.save')) return self.save(); if($(this).is('.cancel')) return self.close(); }); $('#rosterx .rosterx-head a').click(function() { if($(this).is('.check')) $('#rosterx .results input[type="checkbox"]').attr('checked', true); else if($(this).is('.uncheck')) $('#rosterx .results input[type="checkbox"]').removeAttr('checked'); return false; }); } catch(e) { Console.error('RosterX.instance', e); } }; /** * Return class scope */ return self; })();