1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/jappix_ynh.git synced 2024-09-03 19:26:19 +02:00
jappix_ynh/source/app/javascripts/rosterx.js

302 lines
8.3 KiB
JavaScript
Raw Normal View History

2014-03-12 14:52:47 +01:00
/*
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 = {};
2014-04-08 20:14:28 +02:00
/**
2014-03-12 14:52:47 +01:00
* Opens the rosterx tools
* @public
* @param {string} data
* @return {undefined}
*/
self.open = function(data) {
try {
// Popup HTML content
var html =
'<div class="top">' + Common._e("Suggested friends") + '</div>' +
'<div class="content">' +
'<div class="rosterx-head">' +
'<a href="#" class="uncheck">' + Common._e("Uncheck all") + '</a>' +
'<a href="#" class="check">' + Common._e("Check all") + '</a>' +
'</div>' +
'<div class="results"></div>' +
'</div>' +
'<div class="bottom">' +
'<a href="#" class="finish save">' + Common._e("Save") + '</a>' +
'<a href="#" class="finish cancel">' + Common._e("Cancel") + '</a>' +
'</div>';
// 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() {
2014-11-25 20:12:58 +01:00
var this_sel = $(this);
2014-03-12 14:52:47 +01:00
// Generate group XML
var group = '';
2014-11-25 20:12:58 +01:00
this_sel.find('group').each(function() {
group += '<group>' + this_sel.text().htmlEnc() + '</group>';
2014-03-12 14:52:47 +01:00
});
2014-11-25 20:12:58 +01:00
if(group) {
2014-03-12 14:52:47 +01:00
group = '<groups>' + group + '</groups>';
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
// Display it!
2014-11-25 20:12:58 +01:00
self.display(
this_sel.attr('jid'),
this_sel.attr('name'),
group,
this_sel.attr('action')
);
2014-03-12 14:52:47 +01:00
});
// Click to check/uncheck
$('#rosterx .oneresult').click(function(evt) {
// No need to apply when click on input
2014-11-25 20:12:58 +01:00
if($(evt.target).is('input[type="checkbox"]')) {
2014-03-12 14:52:47 +01:00
return;
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
// Input selector
var checkbox = $(this).find('input[type="checkbox"]');
// Check or uncheck?
2014-11-25 20:12:58 +01:00
if(checkbox.filter(':checked').size()) {
2014-03-12 14:52:47 +01:00
checkbox.removeAttr('checked');
2014-11-25 20:12:58 +01:00
} else {
2014-03-12 14:52:47 +01:00
checkbox.attr('checked', true);
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
});
} 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
2014-11-25 20:12:58 +01:00
if(!xid) {
2014-03-12 14:52:47 +01:00
return false;
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
// Set up a default action if no one
2014-11-25 20:12:58 +01:00
if(!action || (action != 'modify') || (action != 'delete')) {
2014-03-12 14:52:47 +01:00
action = 'add';
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
// Override "undefined" for nickname
2014-11-25 20:12:58 +01:00
if(!nick) {
2014-03-12 14:52:47 +01:00
nick = '';
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
// Display it
$('#rosterx .results').append(
'<div class="oneresult">' +
'<input type="checkbox" checked="" data-name="' + Common.encodeQuotes(nick) + '" data-xid="' + Common.encodeQuotes(xid) + '" data-action="' + Common.encodeQuotes(action) + '" data-group="' + Common.encodeQuotes(group) + '" />' +
'<span class="name">' + nick.htmlEnc() + '</span>' +
'<span class="xid">' + xid.htmlEnc() + '</span>' +
'<span class="action ' + action + ' talk-images"></span>' +
'</div>'
);
return true;
} catch(e) {
Console.error('RosterX.display', e);
}
};
/**
* Saves the rosterx settings
* @public
2014-11-25 20:12:58 +01:00
* @return {boolean}
2014-03-12 14:52:47 +01:00
*/
self.save = function() {
try {
// Send the requests
$('#rosterx .results input[type="checkbox"]').filter(':checked').each(function() {
2014-11-25 20:12:58 +01:00
var this_sel = $(this);
2014-03-12 14:52:47 +01:00
// Read the attributes
2014-11-25 20:12:58 +01:00
var nick = this_sel.attr('data-name');
var xid = this_sel.attr('data-xid');
var action = this_sel.attr('data-action');
var group = this_sel.attr('data-group');
2014-03-12 14:52:47 +01:00
// Parse groups XML
var group_arr = [];
if(group) {
$(group).find('group').each(function() {
2014-11-25 20:12:58 +01:00
group_arr.push(this_sel.text().revertHtmlEnc());
2014-03-12 14:52:47 +01:00
});
}
// 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);
2014-11-25 20:12:58 +01:00
} finally {
return false;
2014-03-12 14:52:47 +01:00
}
};
2014-04-08 20:14:28 +02:00
/**
2014-03-12 14:52:47 +01:00
* Plugin launcher
* @public
* @return {undefined}
*/
self.instance = function() {
try {
// Click events
$('#rosterx .bottom .finish').click(function() {
2014-11-25 20:12:58 +01:00
var this_sel = $(this);
if(this_sel.is('.save')) {
2014-03-12 14:52:47 +01:00
return self.save();
2014-11-25 20:12:58 +01:00
}
if(this_sel.is('.cancel')) {
2014-03-12 14:52:47 +01:00
return self.close();
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
});
$('#rosterx .rosterx-head a').click(function() {
2014-11-25 20:12:58 +01:00
var this_sel = $(this);
if(this_sel.is('.check')) {
2014-03-12 14:52:47 +01:00
$('#rosterx .results input[type="checkbox"]').attr('checked', true);
2014-11-25 20:12:58 +01:00
} else if(this_sel.is('.uncheck')) {
2014-03-12 14:52:47 +01:00
$('#rosterx .results input[type="checkbox"]').removeAttr('checked');
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
return false;
});
} catch(e) {
Console.error('RosterX.instance', e);
}
};
/**
* Return class scope
*/
return self;
})();