Can show first message

This commit is contained in:
Pierre 2017-06-25 19:10:26 +02:00
parent fee007d127
commit 7f22b6004c
4 changed files with 144 additions and 11 deletions

View File

@ -63,3 +63,10 @@
-o-transform: translate(0, 0); -o-transform: translate(0, 0);
transform: translate(0, 0); transform: translate(0, 0);
} }
/**
* Conversations messages
*/
#conversationsElem .conversation-msg-image {
height: 50px;
}

View File

@ -27,7 +27,8 @@ function createElem(nodeType, appendTo){
* @param {Object} infos Informations about the HTML node to create * @param {Object} infos Informations about the HTML node to create
* @info {String} type The type of the new node * @info {String} type The type of the new node
* @info {HTMLElement} appendTo HTML Element that will receive the new node * @info {HTMLElement} appendTo HTML Element that will receive the new node
* @info {String} insertBefore Insert before specified HTML element * @info {HTMLElement} insertBefore Insert before specified HTML element
* @info {HTMLElement} insertAsFirstChild Insert the new HTML Element as the first child of the specified element
* @info {String} class The class of the new element * @info {String} class The class of the new element
* @info {String} id The ID of the new element * @info {String} id The ID of the new element
* @info {String} title The title of the new element * @info {String} title The title of the new element
@ -50,6 +51,16 @@ function createElem2(infos){
if(infos.insertBefore) if(infos.insertBefore)
infos.insertBefore.parentNode.insertBefore(newElem, infos.insertBefore); infos.insertBefore.parentNode.insertBefore(newElem, infos.insertBefore);
//Append as the first child of an element
if(infos.insertAsFirstChild){
//Check if the element as already a child or not
if(infos.insertAsFirstChild.firstChild)
infos.insertAsFirstChild.insertBefore(newElem, infos.insertAsFirstChild.firstChild);
//Else we can just append the newly created element
else
infos.insertAsFirstChild.appendChild(newElem);
}
//Specify the class of the element //Specify the class of the element
if(infos.class) if(infos.class)
newElem.className = infos.class; newElem.className = infos.class;
@ -221,7 +232,7 @@ function createFormGroup(infos){
} }
//In case of textarea //In case of textarea
else if(infos.type = "textarea"){ else if(infos.type == "textarea"){
//Fill label value //Fill label value
if(infos.label) if(infos.label)
labelElem.innerHTML = infos.label; labelElem.innerHTML = infos.label;

View File

@ -56,7 +56,7 @@ ComunicWeb.components.conversations.chatWindows = {
infosBox.conversationID = infos.conversationID; infosBox.conversationID = infos.conversationID;
//Change box root class name //Change box root class name
infosBox.rootElem.className += " direct-chat"; infosBox.rootElem.className += " direct-chat direct-chat-primary";
//Adapt close button behaviour //Adapt close button behaviour
infosBox.closeFunction = function(){ infosBox.closeFunction = function(){
@ -80,7 +80,7 @@ ComunicWeb.components.conversations.chatWindows = {
appendTo: infosBox.boxBody, appendTo: infosBox.boxBody,
type: "div", type: "div",
class: "direct-chat-messages", class: "direct-chat-messages",
innerHTML: "<p>Loading, please wait...</p>", innerHTML: "",
}); });
//Add button to get conversation members //Add button to get conversation members
@ -317,7 +317,7 @@ ComunicWeb.components.conversations.chatWindows = {
//Empty messages area //Empty messages area
emptyElem(conversationInfos.box.messagesArea); emptyElem(conversationInfos.box.messagesArea);
conversationInfos.box.messagesArea.innerHTML = "<p>Reloading, please wait...</p>"; conversationInfos.box.messagesArea.innerHTML = "";
//Un-register conversation //Un-register conversation
ComunicWeb.components.conversations.service.unregisterConversation(conversationID); ComunicWeb.components.conversations.service.unregisterConversation(conversationID);
@ -682,6 +682,99 @@ ComunicWeb.components.conversations.chatWindows = {
//Success //Success
return true; return true;
}, },
/**
* Add a message to a conversation window
*
* @param {Integer} conversationID The ID of the conversation to update
* @param {Object} messageInfos Informations about the message to add
* @return {Boolean} True for a success
*/
addMessage: function(conversationID, messageInfos){
console.log(messageInfos);
//First, check if the conversation informations can be found
if(!this.__conversationsCache["conversation-"+conversationID]){
ComunicWeb.debug.logMessage("Conversation Chat Windows : Error ! Couldn't add a message to the conversation because the conversation was not found !");
return false;
}
//Else extract conversation informations
var convInfos = this.__conversationsCache["conversation-"+conversationID];
//Check if it is the current user who sent the message
var userIsPoster = messageInfos.ID_user == userID();
//Create message element
var messageElem = createElem2({
insertAsFirstChild: convInfos.box.messagesArea,
type: "div",
class: "direct-chat-msg " + (userIsPoster ? "right" : "")
});
//Display message header
var messageHeader = createElem2({
appendTo: messageElem,
type: "div",
class: "direct-chat-info clearfix"
});
//Add user name
var usernameElem = createElem2({
appendTo: messageHeader,
type: "span",
class: "direct-chat-name pull-" + (userIsPoster ? "right" : "left"),
innerHTML: "Loading",
});
//Add user account image
var userAccountImage = createElem2({
appendTo: messageElem,
type: "img",
class: "direct-chat-img",
src: ComunicWeb.__config.assetsURL + "img/defaultAvatar.png",
alt: "User account image",
});
//Add message
var messageTargetElem = createElem2({
appendTo: messageElem,
type: "div",
class: "direct-chat-text",
});
//Add text message
createElem2({
appendTo: messageTargetElem,
type: "span",
innerHTML: messageInfos.message,
});
//Check if an image has to be added
if(messageInfos.image_path != null){
createElem2({
appendTo: messageTargetElem,
type: "img",
src: messageInfos.image_path,
class: "conversation-msg-image"
});
}
//Load user informations
if(convInfos.membersInfos["user-" + messageInfos.ID_user]){
//Get informations
var userInfos = convInfos.membersInfos["user-" + messageInfos.ID_user];
//Replace poster name
usernameElem.innerHTML = userInfos.firstName + " " + userInfos.lastName;
userAccountImage.src = userInfos.accountImage;
}
//Success
return true;
},
} }
//Register conversations cache cleaning function //Register conversations cache cleaning function

View File

@ -94,8 +94,7 @@ ComunicWeb.components.conversations.service = {
*/ */
performTask: function(){ performTask: function(){
console.log(this.__serviceCache); //Prepare interface request
//Prepare API request
var newConversations = []; var newConversations = [];
var conversationsToRefresh = {} var conversationsToRefresh = {}
@ -107,13 +106,13 @@ ComunicWeb.components.conversations.service = {
var processConversation = this.__serviceCache[i].conversationID; var processConversation = this.__serviceCache[i].conversationID;
//Check if it is new conversation //Check if it is new conversation
if(this.__serviceCache[i].last_update === 0) if(this.__serviceCache[i].last_message_id === 0)
newConversations.push(processConversation); newConversations.push(processConversation);
//Else perform a simple update of the conversation //Else perform a simple update of the conversation
else { else {
conversationsToRefresh["conversation-"+processConversation] = { conversationsToRefresh["conversation-"+processConversation] = {
last_update: this.__serviceCache[i].last_update, last_message_id: this.__serviceCache[i].last_message_id,
}; };
} }
} }
@ -139,7 +138,7 @@ ComunicWeb.components.conversations.service = {
* @return {Boolean} True for a success * @return {Boolean} True for a success
*/ */
callback: function(result){ callback: function(result){
console.log(result);
//Check for errors //Check for errors
if(result.error){ if(result.error){
ComunicWeb.debug.logMessage("Conversations Service : Couldn't update conversations !"); ComunicWeb.debug.logMessage("Conversations Service : Couldn't update conversations !");
@ -149,6 +148,29 @@ ComunicWeb.components.conversations.service = {
} }
else { else {
//We can continue with the result //We can continue with the result
//Process each conversation
var i;
for(i in result){
//Check if new entries are available
if(result[i].length === 0)
continue; //Nothing to be done
//Extract conversation ID
var conversationID = this.__serviceCache[i].conversationID;
//Extract conversation ID
var messages = result[i];
//We update last message ID with the last message ID
this.__serviceCache[i].last_message_id = messages[messages.length-1].ID;
//We process each message by calling chat windows script to ask it to add messages
var j;
for(j in messages)
ComunicWeb.components.conversations.chatWindows.addMessage(conversationID, messages[j]);
}
} }
//Unlock service //Unlock service
@ -173,7 +195,7 @@ ComunicWeb.components.conversations.service = {
//Register conversation //Register conversation
this.__serviceCache['conversation-' + conversationID] = { this.__serviceCache['conversation-' + conversationID] = {
conversationID: conversationID, conversationID: conversationID,
last_update: 0, last_message_id: 0,
}; };
//Success //Success