3 Jappix - An open social platform
4 These are the inbox JS script for Jappix
6 -------------------------------------------------
10 Last revision: 08/06/11
14 // Opens the inbox popup
15 function openInbox() {
18 '<div class="top">' + _e("Your inbox") + '</div>' +
20 '<div class="content">' +
21 '<div class="head inbox-head">' +
22 '<div class="head-text inbox-head-text">' + _e("Available actions") + '</div>' +
24 '<div class="head-actions inbox-head-actions">' +
25 '<a href="#" class="a-delete-messages">' + _e("Clean") + '</a>' +
26 '<a href="#" class="a-new-message">' + _e("New") + '</a>' +
27 '<a href="#" class="a-show-messages">' + _e("Received") + '</a>' +
31 '<div class="inbox-results">' +
32 '<p class="inbox-noresults">' + _e("Your inbox is empty.") + '</p>' +
34 '<div class="inbox"></div>' +
37 '<div class="inbox-new">' +
38 '<div class="inbox-new-to inbox-new-block search">' +
39 '<p class="inbox-new-text">' + _e("To") + '</p>' +
41 '<input name="inbox-new-to-input" class="inbox-new-input inbox-new-to-input" type="text" required="" />' +
44 '<div class="inbox-new-topic inbox-new-block">' +
45 '<p class="inbox-new-text">' + _e("Subject") + '</p>' +
47 '<input name="inbox-new-subject-input" class="inbox-new-input inbox-new-subject-input" type="text" required="" />' +
50 '<div class="inbox-new-body inbox-new-block">' +
51 '<p class="inbox-new-text">' + _e("Content") + '</p>' +
53 '<textarea class="inbox-new-textarea" rows="8" cols="60" required=""></textarea>' +
56 '<form class="inbox-new-file inbox-new-block" action="./php/file-share.php" method="post" enctype="multipart/form-data">' +
57 '<p class="inbox-new-text">' + _e("File") + '</p>' +
62 '<div class="inbox-new-send inbox-new-block">' +
63 '<a href="#" class="send one-button talk-images">' + _e("Send message") + '</a>' +
68 '<div class="bottom">' +
69 '<div class="wait wait-medium"></div>' +
71 '<a href="#" class="finish">' + _e("Close") + '</a>' +
75 createPopup('inbox', html);
77 // Associate the events
86 // Closes the inbox popup
87 function closeInbox() {
89 destroyPopup('inbox');
94 // Opens the message compose tool
95 function composeInboxMessage(xid) {
101 $('#inbox .inbox-new-to-input').val(xid);
103 // Focus to the next item
104 $(document).oneTime(10, function() {
105 $('#inbox .inbox-new-subject-input').focus();
112 function storeInbox() {
113 var iq = new JSJaCIQ();
115 var query = iq.setQuery(NS_PRIVATE);
116 var storage = query.appendChild(iq.buildNode('storage', {'xmlns': NS_INBOX}));
118 for(var i = 0; i < sessionStorage.length; i++) {
119 // Get the pointer values
120 var current = sessionStorage.key(i);
122 // If the pointer is on a stored message
123 if(explodeThis('_', current, 0) == 'inbox') {
125 var value = $(XMLFromString(sessionStorage.getItem(current)));
127 // Create the storage node
128 storage.appendChild(iq.buildNode('message', {
129 'id': value.find('id').text().revertHtmlEnc(),
130 'from': value.find('from').text().revertHtmlEnc(),
131 'subject': value.find('subject').text().revertHtmlEnc(),
132 'status': value.find('status').text().revertHtmlEnc(),
133 'date': value.find('date').text().revertHtmlEnc(),
137 value.find('content').text().revertHtmlEnc()
145 // Creates a new normal message
146 function newInboxMessage() {
148 var mPath = '#inbox .';
150 // Reset the previous buddy search
151 resetBuddySearch('#inbox .inbox-new-to');
153 // We switch the divs
154 $(mPath + 'inbox-results, #inbox .a-new-message, #inbox .a-delete-messages').hide();
155 $(mPath + 'inbox-new, #inbox .a-show-messages').show();
157 // We focus on the first input
158 $(document).oneTime(10, function() {
159 $(mPath + 'inbox-new-to-input').focus();
162 // We reset some stuffs
163 cleanNewInboxMessage();
169 function cleanNewInboxMessage() {
171 var mPath = '#inbox .';
173 // We reset the forms
174 $(mPath + 'inbox-new-block:not(form) input, ' + mPath + 'inbox-new textarea').val('').removeClass('please-complete');
175 $(mPath + 'inbox-new-file a').remove();
176 $(mPath + 'inbox-new-file input').show();
178 // We close an eventual opened message
179 $(mPath + 'message-content').remove();
180 $(mPath + 'one-message').removeClass('message-reading');
183 // Sends a normal message
184 function sendInboxMessage(to, subject, body) {
185 // We send the message
186 var mess = new JSJaCMessage();
190 mess.setSubject(subject);
191 mess.setType('normal');
193 // Any file to attach?
194 var attached = '#inbox .inbox-new-file a.file';
199 $(attached).attr('data-attachedtitle') + ' - ' + $(attached).attr('data-attachedhref');
204 con.send(mess, handleErrorReply);
207 // Performs the normal message sender checks
208 function checkInboxMessage() {
209 // We get some informations
210 var mPath = '#inbox ';
211 var to = $(mPath + '.inbox-new-to-input').val();
212 var body = $(mPath + '.inbox-new-textarea').val();
213 var subject = $(mPath + '.inbox-new-subject-input').val();
215 if(to && body && subject) {
217 var xid = new Array(to);
220 if(to.indexOf(',') != -1)
224 var current = xid[i];
227 if(!current || current.match(/^(\s+)$/))
230 // Edit the XID if needed
231 current = current.replace(/ /g, '');
232 current = generateXID(current, 'chat');
234 // We send the message
235 sendInboxMessage(current, subject, body);
237 // We clean the inputs
238 cleanNewInboxMessage();
240 logThis('Inbox message sent: ' + current, 3);
248 $(mPath + 'input[type=text], ' + mPath + 'textarea').each(function() {
251 if(!$(current).val()) {
252 $(document).oneTime(10, function() {
253 $(current).addClass('please-complete').focus();
258 $(current).removeClass('please-complete');
265 // Shows the inbox messages
266 function showInboxMessages() {
268 var mPath = '#inbox .';
270 // We switch the divs
271 $(mPath + 'inbox-new').hide();
272 $(mPath + 'inbox-results').show();
274 // We show a new link in the menu
275 $(mPath + 'a-show-messages').hide();
276 $(mPath + 'a-delete-messages').show();
277 $(mPath + 'a-new-message').show();
279 // We reset some stuffs
280 cleanNewInboxMessage();
285 // Displays a normal message
286 function displayInboxMessage(from, subject, content, status, id, date) {
287 // Generate some paths
288 var inbox = '#inbox .';
289 var one_message = inbox + 'one-message.' + id;
291 // Message yet displayed!
292 if(exists(one_message))
295 // Get the nearest element
296 var stamp = extractStamp(Date.jab2date(date));
297 var nearest = sortElementByStamp(stamp, '#inbox .one-message');
299 // Get the buddy name
300 var name = getBuddyName(from).htmlEnc();
302 // We generate the html code
303 var nContent = '<div class="one-message message-' + status + ' ' + id + ' ' + hex_md5(from) + '" data-stamp="' + stamp + '">' +
304 '<div class="message-head">' +
305 '<div class="avatar-container">' +
306 '<img class="avatar" src="' + './img/others/default-avatar.png' + '" alt="" />' +
309 '<div class="message-jid">' + name + '</div>' +
310 '<div class="message-subject">' + subject.htmlEnc() + '</div>' +
312 '<div class="message-truncated">' + truncate(noLines(content), 90).htmlEnc() + '</div>' +
316 // Display the message
318 $(inbox + 'inbox-results .inbox').append(nContent);
320 $('#inbox .one-message[data-stamp=' + nearest + ']:first').before(nContent);
323 $(one_message + ' .message-head').click(function() {
324 if(!exists(one_message + ' .message-content'))
325 revealInboxMessage(id, from, subject, content, name, date, status);
327 hideInboxMessage(id);
332 // Get the user avatar
333 getAvatar(from, 'cache', 'true', 'forget');
338 // Stores an inbox message
339 function storeInboxMessage(from, subject, content, status, id, date) {
340 // Initialize the XML data
341 var xml = '<message><id>' + id.htmlEnc().htmlEnc() + '</id><date>' + date.htmlEnc().htmlEnc() + '</date><from>' + from.htmlEnc().htmlEnc() + '</from><subject>' + subject.htmlEnc().htmlEnc() + '</subject><status>' + status.htmlEnc().htmlEnc() + '</status><content>' + content.htmlEnc().htmlEnc() + '</content>';
346 // Store this message!
347 setDB('inbox', id, xml);
350 // Removes a given normal message
351 function deleteInboxMessage(id) {
352 // Remove the message from the inbox
353 $('#inbox .one-message.' + id).remove();
355 // Remove the message from the database
356 removeDB('inbox', id);
358 // Check the unread messages
359 checkInboxMessages();
361 // Store the new inbox
367 // Removes all the inbox messages
368 function purgeInbox() {
369 // Remove all the messages from the database
370 for(var i = 0; i < sessionStorage.length; i++) {
371 // Get the pointer values
372 var current = sessionStorage.key(i);
374 // If the pointer is on a stored message
375 if(explodeThis('_', current, 0) == 'inbox')
376 removeDB('inbox', explodeThis('_', current, 1));
379 // Prevent the database lag
380 $(document).oneTime(100, function() {
381 // Store the new inbox
384 // Remove all the messages from the inbox
385 $('#inbox .one-message').remove();
394 // Checks if there are new messages to be notified
395 function checkInboxMessages() {
397 var inbox_link = '#top-content a.inbox-hidable';
398 var no_results = '#inbox .inbox-noresults';
401 var has_messages = false;
403 // Read the number of unread messages
406 // Read the local inbox database
407 for(var i = 0; i < sessionStorage.length; i++) {
409 var current = sessionStorage.key(i);
411 // Check inbox messages
412 if(explodeThis('_', current, 0) == 'inbox') {
413 // Read the current status
414 var status = $(XMLFromString(sessionStorage.getItem(current))).find('status').text();
416 // Found an unread message
417 if(status == 'unread')
427 $(no_results).show();
429 $(no_results).hide();
431 // Reset notifications
432 $(inbox_link + ' .notify').remove();
434 // Any unread message?
437 $(inbox_link).prepend('<div class="notify one-counter" data-counter="' + unread + '">' + unread + '</div>');
445 // Anyway, update the title
451 // Reveal a normal message content
452 function revealInboxMessage(id, from, subject, content, name, date, status) {
454 var all_message = '#inbox .one-message';
455 var one_message = all_message + '.' + id;
456 var one_content = one_message + ' .message-content';
458 // We reset all the other messages
459 $(all_message + ' .message-content').remove();
460 $(all_message).removeClass('message-reading');
464 '<div class="message-content">' +
465 '<div class="message-body">' + filterThisMessage(content, name, true) + '</div>' +
467 '<div class="message-meta">' +
468 '<span class="date">' + parseDate(date) + '</span>' +
470 '<a href="#" class="reply one-button talk-images">' + _e("Reply") + '</a>' +
471 '<a href="#" class="remove one-button talk-images">' + _e("Delete") + '</a>' +
473 '<div class="clear">' +
480 $(one_message).append(html).addClass('message-reading');
483 $(one_content + ' a.reply').click(function() {
484 return replyInboxMessage(id, from, subject, content);
487 $(one_content + ' a.remove').click(function() {
488 return deleteInboxMessage(id);
492 if(status == 'unread') {
493 // Update our database
494 var xml = getDB('inbox', id).replace(/<status>unread<\/status>/i,'<status>read</status>');
495 setDB('inbox', id, xml);
497 // Remove the unread class
498 $(one_message).removeClass('message-unread');
500 // Send it to the server!
504 // Check the unread messages
505 checkInboxMessages();
508 // Hides a normal message content
509 function hideInboxMessage(id) {
511 var inbox = '#inbox .';
512 var one_message = inbox + 'one-message.' + id;
514 // Reset this message
515 $(one_message).removeClass('message-reading');
516 $(one_message + ' .message-content').remove();
519 // Replies to a given normal message
520 function replyInboxMessage(id, from, subject, body) {
521 // We switch to the writing div
525 var inbox = '#inbox .';
528 var body = '\n' + '____________' + '\n\n' + truncate(body, 120);
530 // We apply the generated values to the form
531 $(inbox + 'inbox-new-to-input').val(from);
532 $(inbox + 'inbox-new-subject-input').val(subject);
534 $(document).oneTime(10, function() {
535 $(inbox + 'inbox-new-textarea').val(body).focus().selectRange(1, 0);
541 // Loads the inbox messages
542 function loadInbox() {
543 // Read the local database
544 for(var i = 0; i < sessionStorage.length; i++) {
545 // Get the pointer values
546 var current = sessionStorage.key(i);
548 // If the pointer is on a stored message
549 if(explodeThis('_', current, 0) == 'inbox') {
550 // Get the current value
551 var value = $(XMLFromString(sessionStorage.getItem(current)));
553 // Display the current message
555 value.find('from').text().revertHtmlEnc(),
556 value.find('subject').text().revertHtmlEnc(),
557 value.find('content').text().revertHtmlEnc(),
558 value.find('status').text().revertHtmlEnc(),
559 value.find('id').text().revertHtmlEnc(),
560 value.find('date').text().revertHtmlEnc()
565 // Check new messages
566 checkInboxMessages();
569 // Wait event for file attaching
570 function waitInboxAttach() {
571 $('#inbox .wait').show();
574 // Success event for file attaching
575 function handleInboxAttach(responseXML) {
577 var dData = $(responseXML).find('jappix');
579 // Process the returned data
580 if(dData.find('error').size()) {
583 logThis('Error while attaching the file: ' + dData.find('error').text(), 1);
587 // Get the file values
588 var fName = dData.find('title').text();
589 var fType = dData.find('type').text();
590 var fURL = dData.find('href').text();
592 // Hide the attach link, show the unattach one
593 $('#inbox .inbox-new-file input').hide();
594 $('#inbox .inbox-new-file').append('<a class="file ' + encodeQuotes(fileCategory(explodeThis('/', fType, 1))) + ' talk-images" href="' + encodeQuotes(fURL) + '" target="_blank">' + fName.htmlEnc() + '</a><a href="#" class="remove one-button talk-images">' + _e("Remove") + '</a>');
596 // Set values to the file link
597 $('#inbox .inbox-new-file a.file').attr('data-attachedtitle', fName)
598 .attr('data-attachedhref', fURL);
601 $('#inbox .inbox-new-file a.remove').click(function() {
602 $('#inbox .inbox-new-file a').remove();
603 $('#inbox .inbox-new-file input').show();
608 logThis('File attached.', 3);
611 // Reset the attach bubble
612 $('#inbox .inbox-new-file input[type=file]').val('');
613 $('#inbox .wait').hide();
617 function launchInbox() {
619 var inbox = '#inbox .';
621 // Define the buddy search vars
622 var destination = inbox + 'inbox-new-to';
623 var dHovered = destination + ' ul li.hovered:first';
625 // Send the message when enter pressend
626 $(inbox + 'inbox-new input').keyup(function(e) {
627 if(e.keyCode == 13) {
629 addBuddySearch(destination, $(dHovered).attr('data-xid'));
636 $(inbox + 'inbox-new-to-input').keyup(function(e) {
637 if(e.keyCode != 13) {
639 if((e.keyCode != 40) && (e.keyCode != 38))
640 createBuddySearch(destination);
642 // Navigating with keyboard in the results
643 arrowsBuddySearch(e, destination);
647 // Buddy search lost focus
649 if(!$(destination + ' ul').attr('mouse-hover'))
650 resetBuddySearch(destination);
653 // Buddy search got focus
655 var value = $(this).val();
657 // Add a comma at the end
658 if(value && !value.match(/^(.+)((,)(\s)?)$/))
659 $(this).val(value + ', ');
663 $(inbox + 'a-delete-messages').click(purgeInbox);
664 $(inbox + 'a-new-message').click(newInboxMessage);
665 $(inbox + 'a-show-messages').click(showInboxMessages);
666 $(inbox + 'inbox-new-send a').click(checkInboxMessage);
668 $(inbox + 'bottom .finish').click(function() {
673 var attach_options = {
675 beforeSubmit: waitInboxAttach,
676 success: handleInboxAttach
679 // Upload form submit event
680 $('#inbox .inbox-new-file').submit(function() {
681 if($('#inbox .wait').is(':hidden') && $('#inbox .inbox-new-file input[type=file]').val())
682 $(this).ajaxSubmit(attach_options);
687 // Upload input change event
688 $('#inbox .inbox-new-file input[type=file]').change(function() {
689 if($('#inbox .wait').is(':hidden') && $(this).val())
690 $('#inbox .inbox-new-file').ajaxSubmit(attach_options);