Restructured addMessage function

This commit is contained in:
Pierre 2018-04-27 18:26:16 +02:00
parent 370512a6b6
commit 210e8406ad
2 changed files with 100 additions and 69 deletions

View File

@ -92,3 +92,11 @@
#conversationsElem .direct-chat-text.not-first-message::after { #conversationsElem .direct-chat-text.not-first-message::after {
display: none; display: none;
} }
#conversationsElem .direct-chat-msg.not-last-message-from-user {
margin-bottom: 0px;
}
#conversationsElem .direct-chat-msg.not-last-message-from-user .direct-chat-img {
margin-bottom: -5px;
}

View File

@ -695,10 +695,10 @@ ComunicWeb.components.conversations.chatWindows = {
* Add a message to a conversation window * Add a message to a conversation window
* *
* @param {Integer} conversationID The ID of the conversation to update * @param {Integer} conversationID The ID of the conversation to update
* @param {Object} messageInfos Informations about the message to add * @param {Object} messageInfo Information about the message to add
* @return {Boolean} True for a success * @return {Boolean} True for a success
*/ */
addMessage: function(conversationID, messageInfos){ addMessage: function(conversationID, messageInfo){
//First, check if the conversation informations can be found //First, check if the conversation informations can be found
if(!this.__conversationsCache["conversation-"+conversationID]){ if(!this.__conversationsCache["conversation-"+conversationID]){
@ -709,27 +709,13 @@ ComunicWeb.components.conversations.chatWindows = {
//Else extract conversation informations //Else extract conversation informations
var convInfos = this.__conversationsCache["conversation-"+conversationID]; var convInfos = this.__conversationsCache["conversation-"+conversationID];
//Check if it is the current user who sent the message
var userIsPoster = messageInfos.ID_user == userID();
//Check if this is the first message of the conversation or not //Check if this is the first message of the conversation or not
if(!convInfos.lastMessage){ if(!convInfos.messages){
//Initialize last message object convInfos.messages = [];
convInfos.lastMessage = {
userID: 0,
messageContainer: false,
}
} }
//Check if message poster is the same as the last message //Check if it is the current user who sent the message
if(convInfos.lastMessage.userID == messageInfos.ID_user){ var userIsPoster = messageInfo.ID_user == userID();
//Skip message container creation & user avatar rendering...
var messageContainer = convInfos.lastMessage.messageContainer;
var firstMessageFromUser = false;
}
else {
//Initialize user & message informations elements
var firstMessageFromUser = true;
//Create message element //Create message element
var messageContainer = createElem2({ var messageContainer = createElem2({
@ -767,53 +753,46 @@ ComunicWeb.components.conversations.chatWindows = {
}); });
//Load user informations //Load user informations
if(convInfos.membersInfos["user-" + messageInfos.ID_user]){ if(convInfos.membersInfos["user-" + messageInfo.ID_user]){
//Get informations //Get informations
var userInfos = convInfos.membersInfos["user-" + messageInfos.ID_user]; var userInfos = convInfos.membersInfos["user-" + messageInfo.ID_user];
//Replace poster name //Replace poster name
usernameElem.innerHTML = userInfos.firstName + " " + userInfos.lastName; usernameElem.innerHTML = userInfos.firstName + " " + userInfos.lastName;
userAccountImage.src = userInfos.accountImage; userAccountImage.src = userInfos.accountImage;
} }
//Update conversation informations
convInfos.lastMessage = {
userID: messageInfos.ID_user,
messageContainer: messageContainer,
}
}
//Add message //Add message
var messageTargetElem = createElem2({ var messageTargetElem = createElem2({
appendTo: messageContainer, appendTo: messageContainer,
type: "div", type: "div",
class: "direct-chat-text " + (!firstMessageFromUser ? "not-first-message" : ""), class: "direct-chat-text ",
}); });
//Add text message //Add text message
var textMessage = createElem2({ var textMessage = createElem2({
appendTo: messageTargetElem, appendTo: messageTargetElem,
type: "span", type: "span",
innerHTML: removeHtmlTags(messageInfos.message), //Remove HTML tags innerHTML: removeHtmlTags(messageInfo.message), //Remove HTML tags
}); });
//Check if an image has to be added //Check if an image has to be added
if(messageInfos.image_path != null){ if(messageInfo.image_path != null){
//Image link //Image link
var imageLink = createElem2({ var imageLink = createElem2({
appendTo: messageTargetElem, appendTo: messageTargetElem,
type:"a", type:"a",
href: messageInfos.image_path, href: messageInfo.image_path,
}); });
//Image element //Image element
createElem2({ createElem2({
appendTo: imageLink, appendTo: imageLink,
type: "img", type: "img",
src: messageInfos.image_path, src: messageInfo.image_path,
class: "conversation-msg-image" class: "conversation-msg-image"
}); });
@ -831,6 +810,37 @@ ComunicWeb.components.conversations.chatWindows = {
element: textMessage, element: textMessage,
}); });
//Save information about the message
var uiMessageInfo = {
userID: messageInfo.ID_user,
rootElem: messageContainer,
userNameElem: usernameElem,
messageTargetElem: messageTargetElem,
accountImage: userAccountImage
};
//Perform post-processing operations
var num = convInfos.messages.push(uiMessageInfo);
//Check if it is not the first message from the current user
if(convInfos.messages[num - 2]){
if(convInfos.messages[num-2].userID == messageInfo.ID_user){
//Update object class name
uiMessageInfo.messageTargetElem.className += " not-first-message";
//Hide user name and account image
uiMessageInfo.userNameElem.style.display = "none";
uiMessageInfo.accountImage.style.display = "none";
//Update the class of the previous message
convInfos.messages[num-2].rootElem.className += " not-last-message-from-user";
}
}
//Enable slimscroll //Enable slimscroll
$(convInfos.box.messagesArea).slimscroll({ $(convInfos.box.messagesArea).slimscroll({
height: "250px", height: "250px",
@ -847,6 +857,19 @@ ComunicWeb.components.conversations.chatWindows = {
//Success //Success
return true; return true;
},
/**
* Generate message HTML node based on given information
*
* @param {object} conversationInfo Information about the created conversation
* @param {object} info Information about the target message
* @return {object} Information about the created message element
*/
_get_message_element: function(conversationID, info){
}, },
/** /**