Splited addMessage and the generation of message HTML element

This commit is contained in:
Pierre 2018-04-27 18:41:57 +02:00
parent 210e8406ad
commit f5818cf142

View File

@ -714,12 +714,48 @@ ComunicWeb.components.conversations.chatWindows = {
convInfos.messages = []; convInfos.messages = [];
} }
//Get message HTML element add append it
var uiMessageInfo = this._get_message_element(convInfos, messageInfo);
convInfos.box.messagesArea.appendChild(uiMessageInfo.rootElem);
//Perform post-processing operations
var num = convInfos.messages.push(uiMessageInfo);
//Check if it is not the first message from the current user
this._makeMessageFollowAnotherMessage(convInfos, num - 1);
//Enable slimscroll
$(convInfos.box.messagesArea).slimscroll({
height: "250px",
});
//Scroll to the bottom of the conversation
var scrollBottom = $(convInfos.box.messagesArea).prop("scrollHeight")+"px";
$(convInfos.box.messagesArea).slimScroll({
scrollTo: scrollBottom
});
//Initialize top scroll detection if required
this.initTopScrollDetection(conversationID);
//Success
return true;
},
/**
* Generate message HTML node based on given information
*
* @param {object} conversationInfo Information about the created conversation
* @param {object} message Information about the target message
* @return {object} Information about the created message element
*/
_get_message_element: function(conversationInfo, message){
//Check if it is the current user who sent the message //Check if it is the current user who sent the message
var userIsPoster = messageInfo.ID_user == userID(); var userIsPoster = message.ID_user == userID();
//Create message element //Create message element
var messageContainer = createElem2({ var messageContainer = createElem2({
appendTo: convInfos.box.messagesArea,
type: "div", type: "div",
class: "direct-chat-msg " + (userIsPoster ? "right" : "") class: "direct-chat-msg " + (userIsPoster ? "right" : "")
}); });
@ -753,17 +789,16 @@ ComunicWeb.components.conversations.chatWindows = {
}); });
//Load user informations //Load user informations
if(convInfos.membersInfos["user-" + messageInfo.ID_user]){ if(conversationInfo.membersInfos["user-" + message.ID_user]){
//Get informations //Get informations
var userInfos = convInfos.membersInfos["user-" + messageInfo.ID_user]; var userInfos = conversationInfo.membersInfos["user-" + message.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;
} }
//Add message //Add message
var messageTargetElem = createElem2({ var messageTargetElem = createElem2({
appendTo: messageContainer, appendTo: messageContainer,
@ -775,24 +810,24 @@ ComunicWeb.components.conversations.chatWindows = {
var textMessage = createElem2({ var textMessage = createElem2({
appendTo: messageTargetElem, appendTo: messageTargetElem,
type: "span", type: "span",
innerHTML: removeHtmlTags(messageInfo.message), //Remove HTML tags innerHTML: removeHtmlTags(message.message), //Remove HTML tags
}); });
//Check if an image has to be added //Check if an image has to be added
if(messageInfo.image_path != null){ if(message.image_path != null){
//Image link //Image link
var imageLink = createElem2({ var imageLink = createElem2({
appendTo: messageTargetElem, appendTo: messageTargetElem,
type:"a", type:"a",
href: messageInfo.image_path, href: message.image_path,
}); });
//Image element //Image element
createElem2({ createElem2({
appendTo: imageLink, appendTo: imageLink,
type: "img", type: "img",
src: messageInfo.image_path, src: message.image_path,
class: "conversation-msg-image" class: "conversation-msg-image"
}); });
@ -810,65 +845,43 @@ ComunicWeb.components.conversations.chatWindows = {
element: textMessage, element: textMessage,
}); });
//Save information about the message //Return information about the message
var uiMessageInfo = { return {
userID: messageInfo.ID_user, userID: message.ID_user,
rootElem: messageContainer, rootElem: messageContainer,
userNameElem: usernameElem, userNameElem: usernameElem,
messageTargetElem: messageTargetElem, messageTargetElem: messageTargetElem,
accountImage: userAccountImage 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
$(convInfos.box.messagesArea).slimscroll({
height: "250px",
});
//Scroll to the bottom of the conversation
var scrollBottom = $(convInfos.box.messagesArea).prop("scrollHeight")+"px";
$(convInfos.box.messagesArea).slimScroll({
scrollTo: scrollBottom
});
//Initialize top scroll detection if required
this.initTopScrollDetection(conversationID);
//Success
return true;
}, },
/** /**
* Generate message HTML node based on given information * Make a conversation message "follow" another conversation message from the
* same user
* *
* @param {object} conversationInfo Information about the created conversation * @param {object} conv Information about the target conversation
* @param {object} info Information about the target message * @param {number} num The number of the conversation message to update
* @return {object} Information about the created message element
*/ */
_get_message_element: function(conversationID, info){ _makeMessageFollowAnotherMessage: function(conv, num){
if(conv.messages[num - 1]){
if(conv.messages[num-1].userID == conv.messages[num].userID){
//Update object class name
conv.messages[num].messageTargetElem.className += " not-first-message";
//Hide user name and account image
conv.messages[num].userNameElem.style.display = "none";
conv.messages[num].accountImage.style.display = "none";
//Update the class of the previous message
conv.messages[num-1].rootElem.className += " not-last-message-from-user";
}
}
}, },