/*
Jappix - An open social platform
These are the inbox JS script for Jappix
-------------------------------------------------
License: AGPL
Author: Vanaryon
Last revision: 08/06/11
*/
// Opens the inbox popup
function openInbox() {
// Popup HTML content
var html =
'
';
// Create the popup
createPopup('inbox', html);
// Associate the events
launchInbox();
// Load the messages
loadInbox();
return false;
}
// Closes the inbox popup
function closeInbox() {
// Destroy the popup
destroyPopup('inbox');
return false;
}
// Opens the message compose tool
function composeInboxMessage(xid) {
// Open things
openInbox();
newInboxMessage();
// Apply XID
$('#inbox .inbox-new-to-input').val(xid);
// Focus to the next item
$(document).oneTime(10, function() {
$('#inbox .inbox-new-subject-input').focus();
});
return false;
}
// Stores the inbox
function storeInbox() {
var iq = new JSJaCIQ();
iq.setType('set');
var query = iq.setQuery(NS_PRIVATE);
var storage = query.appendChild(iq.buildNode('storage', {'xmlns': NS_INBOX}));
for(var i = 0; i < sessionStorage.length; i++) {
// Get the pointer values
var current = sessionStorage.key(i);
// If the pointer is on a stored message
if(explodeThis('_', current, 0) == 'inbox') {
// Get the values
var value = $(XMLFromString(sessionStorage.getItem(current)));
// Create the storage node
storage.appendChild(iq.buildNode('message', {
'id': value.find('id').text().revertHtmlEnc(),
'from': value.find('from').text().revertHtmlEnc(),
'subject': value.find('subject').text().revertHtmlEnc(),
'status': value.find('status').text().revertHtmlEnc(),
'date': value.find('date').text().revertHtmlEnc(),
'xmlns': NS_INBOX
},
value.find('content').text().revertHtmlEnc()
));
}
}
con.send(iq);
}
// Creates a new normal message
function newInboxMessage() {
// Init
var mPath = '#inbox .';
// Reset the previous buddy search
resetBuddySearch('#inbox .inbox-new-to');
// We switch the divs
$(mPath + 'inbox-results, #inbox .a-new-message, #inbox .a-delete-messages').hide();
$(mPath + 'inbox-new, #inbox .a-show-messages').show();
// We focus on the first input
$(document).oneTime(10, function() {
$(mPath + 'inbox-new-to-input').focus();
});
// We reset some stuffs
cleanNewInboxMessage();
return false;
}
// Cleans the inbox
function cleanNewInboxMessage() {
// Init
var mPath = '#inbox .';
// We reset the forms
$(mPath + 'inbox-new-block:not(form) input, ' + mPath + 'inbox-new textarea').val('').removeClass('please-complete');
$(mPath + 'inbox-new-file a').remove();
$(mPath + 'inbox-new-file input').show();
// We close an eventual opened message
$(mPath + 'message-content').remove();
$(mPath + 'one-message').removeClass('message-reading');
}
// Sends a normal message
function sendInboxMessage(to, subject, body) {
// We send the message
var mess = new JSJaCMessage();
// Main attributes
mess.setTo(to);
mess.setSubject(subject);
mess.setType('normal');
// Any file to attach?
var attached = '#inbox .inbox-new-file a.file';
if(exists(attached))
body += '\n' +
'\n' +
$(attached).attr('data-attachedtitle') + ' - ' + $(attached).attr('data-attachedhref');
// Set body
mess.setBody(body);
con.send(mess, handleErrorReply);
}
// Performs the normal message sender checks
function checkInboxMessage() {
// We get some informations
var mPath = '#inbox ';
var to = $(mPath + '.inbox-new-to-input').val();
var body = $(mPath + '.inbox-new-textarea').val();
var subject = $(mPath + '.inbox-new-subject-input').val();
if(to && body && subject) {
// New array of XID
var xid = new Array(to);
// More than one XID
if(to.indexOf(',') != -1)
xid = to.split(',');
for(i in xid) {
var current = xid[i];
// No current value?
if(!current || current.match(/^(\s+)$/))
continue;
// Edit the XID if needed
current = current.replace(/ /g, '');
current = generateXID(current, 'chat');
// We send the message
sendInboxMessage(current, subject, body);
// We clean the inputs
cleanNewInboxMessage();
logThis('Inbox message sent: ' + current, 3);
}
// Close the inbox
closeInbox();
}
else {
$(mPath + 'input[type=text], ' + mPath + 'textarea').each(function() {
var current = this;
if(!$(current).val()) {
$(document).oneTime(10, function() {
$(current).addClass('please-complete').focus();
});
}
else
$(current).removeClass('please-complete');
});
}
return false;
}
// Shows the inbox messages
function showInboxMessages() {
// Init
var mPath = '#inbox .';
// We switch the divs
$(mPath + 'inbox-new').hide();
$(mPath + 'inbox-results').show();
// We show a new link in the menu
$(mPath + 'a-show-messages').hide();
$(mPath + 'a-delete-messages').show();
$(mPath + 'a-new-message').show();
// We reset some stuffs
cleanNewInboxMessage();
return false;
}
// Displays a normal message
function displayInboxMessage(from, subject, content, status, id, date) {
// Generate some paths
var inbox = '#inbox .';
var one_message = inbox + 'one-message.' + id;
// Message yet displayed!
if(exists(one_message))
return false;
// Get the nearest element
var stamp = extractStamp(Date.jab2date(date));
var nearest = sortElementByStamp(stamp, '#inbox .one-message');
// Get the buddy name
var name = getBuddyName(from).htmlEnc();
// We generate the html code
var nContent = '
' +
'
' +
'
' +
'' +
'
' +
'
' + name + '
' +
'
' + subject.htmlEnc() + '
' +
'
' + truncate(noLines(content), 90).htmlEnc() + '
' +
'
' +
'
';
// Display the message
if(nearest == 0)
$(inbox + 'inbox-results .inbox').append(nContent);
else
$('#inbox .one-message[data-stamp=' + nearest + ']:first').before(nContent);
// Click events
$(one_message + ' .message-head').click(function() {
if(!exists(one_message + ' .message-content'))
revealInboxMessage(id, from, subject, content, name, date, status);
else
hideInboxMessage(id);
return false;
});
// Get the user avatar
getAvatar(from, 'cache', 'true', 'forget');
return true;
}
// Stores an inbox message
function storeInboxMessage(from, subject, content, status, id, date) {
// Initialize the XML data
var xml = '' + id.htmlEnc().htmlEnc() + '' + date.htmlEnc().htmlEnc() + '' + from.htmlEnc().htmlEnc() + '' + subject.htmlEnc().htmlEnc() + '' + status.htmlEnc().htmlEnc() + '' + content.htmlEnc().htmlEnc() + '';
// End the XML data
xml += '';
// Store this message!
setDB('inbox', id, xml);
}
// Removes a given normal message
function deleteInboxMessage(id) {
// Remove the message from the inbox
$('#inbox .one-message.' + id).remove();
// Remove the message from the database
removeDB('inbox', id);
// Check the unread messages
checkInboxMessages();
// Store the new inbox
storeInbox();
return false;
}
// Removes all the inbox messages
function purgeInbox() {
// Remove all the messages from the database
for(var i = 0; i < sessionStorage.length; i++) {
// Get the pointer values
var current = sessionStorage.key(i);
// If the pointer is on a stored message
if(explodeThis('_', current, 0) == 'inbox')
removeDB('inbox', explodeThis('_', current, 1));
}
// Prevent the database lag
$(document).oneTime(100, function() {
// Store the new inbox
storeInbox();
// Remove all the messages from the inbox
$('#inbox .one-message').remove();
// Reload the inbox
loadInbox();
});
return false;
}
// Checks if there are new messages to be notified
function checkInboxMessages() {
// Selectors
var inbox_link = '#top-content a.inbox-hidable';
var no_results = '#inbox .inbox-noresults';
// Marker
var has_messages = false;
// Read the number of unread messages
var unread = 0;
// Read the local inbox database
for(var i = 0; i < sessionStorage.length; i++) {
// Database pointer
var current = sessionStorage.key(i);
// Check inbox messages
if(explodeThis('_', current, 0) == 'inbox') {
// Read the current status
var status = $(XMLFromString(sessionStorage.getItem(current))).find('status').text();
// Found an unread message
if(status == 'unread')
unread++;
// Update the marker
has_messages = true;
}
}
// No message?
if(!has_messages)
$(no_results).show();
else
$(no_results).hide();
// Reset notifications
$(inbox_link + ' .notify').remove();
// Any unread message?
if(unread) {
// Notify the user
$(inbox_link).prepend('
' + unread + '
');
// Update the title
updateTitle();
return true;
}
// Anyway, update the title
updateTitle();
return false;
}
// Reveal a normal message content
function revealInboxMessage(id, from, subject, content, name, date, status) {
// Message path
var all_message = '#inbox .one-message';
var one_message = all_message + '.' + id;
var one_content = one_message + ' .message-content';
// We reset all the other messages
$(all_message + ' .message-content').remove();
$(all_message).removeClass('message-reading');
// Message content
var html =
'