2017-06-14 14:39:34 +00:00
/ * *
* Conversation chat window functions
*
* @ author Pierre HUBERT
* /
ComunicWeb . components . conversations . chatWindows = {
2017-06-18 07:15:50 +00:00
2017-06-18 09:32:31 +00:00
/ * *
* @ var { Object } _ _conversationsCache Chat windows cache
* /
_ _conversationsCache : { } ,
2017-06-18 07:15:50 +00:00
/ * *
* Open a new conversation window
*
2017-06-18 09:14:26 +00:00
* @ param { Integer } conversationID The ID of the window to open
2017-06-18 07:15:50 +00:00
* @ return { Boolean } True for a success
* /
openConversation : function ( conversationID ) {
//Log action
ComunicWeb . debug . logMessage ( "Opening conversation " + conversationID ) ;
//Create a conversation window
var conversationWindow = this . create ( {
2018-03-25 07:43:39 +00:00
target : byId ( ComunicWeb . components . conversations . manager . _ _conversationsContainerID ) ,
2017-06-18 09:14:26 +00:00
conversationID : conversationID ,
2017-06-18 07:15:50 +00:00
} ) ;
2017-06-18 09:14:26 +00:00
//Load the conversation
this . load ( conversationID , conversationWindow ) ;
//Success
return true ;
} ,
2017-06-21 14:44:10 +00:00
/ * *
* Create a new chat window
*
* @ param { Object } infos Informations required for the new chat window
* @ info { HTMLElement } target The target of the new chat window
* @ info { Integer } conversationID The ID of the target conversation
* @ return { Object } Informations about the new chat window
* /
create : function ( infos ) {
//Log action
ComunicWeb . debug . logMessage ( "Create a new chat window" ) ;
//First, create the generic conversation window
var infosBox = ComunicWeb . components . conversations . windows . create ( infos . target . children [ 0 ] ) ;
//Save conversation ID
infosBox . conversationID = infos . conversationID ;
//Change box root class name
2017-06-25 17:10:26 +00:00
infosBox . rootElem . className += " direct-chat direct-chat-primary" ;
2017-06-21 14:44:10 +00:00
//Adapt close button behaviour
infosBox . closeFunction = function ( ) {
//Remove root element
infosBox . rootElem . remove ( ) ;
//Remove the conversation from opened ones
ComunicWeb . components . conversations . cachingOpened . remove ( infosBox . conversationID ) ;
//Unload conversation
ComunicWeb . components . conversations . chatWindows . unload ( infosBox . conversationID ) ;
}
infosBox . closeButton . onclick = infosBox . closeFunction ;
//Debug
2018-03-25 07:43:39 +00:00
//Create messages container
2017-06-24 17:23:55 +00:00
infosBox . messagesArea = createElem2 ( {
appendTo : infosBox . boxBody ,
type : "div" ,
class : "direct-chat-messages" ,
2017-06-25 17:10:26 +00:00
innerHTML : "" ,
2017-06-24 17:23:55 +00:00
} ) ;
2017-06-21 14:44:10 +00:00
//Add button to get conversation members
infosBox . membersButton = createElem ( "button" ) ;
infosBox . closeButton . parentNode . insertBefore ( infosBox . membersButton , infosBox . closeButton ) ;
infosBox . membersButton . type = "button" ;
infosBox . membersButton . className = "btn btn-box-tool" ;
infosBox . membersButton . setAttribute ( "data-toggle" , "tooltip" ) ;
infosBox . membersButton . setAttribute ( "data-widget" , "chat-pane-toggle" ) ;
infosBox . membersButton . title = "Conversation members" ;
//Add button icon
var buttonIcon = createElem ( "i" , infosBox . membersButton ) ;
buttonIcon . className = "fa fa-users" ;
//Add conversation members pane
var membersPane = createElem ( "div" , infosBox . boxBody ) ;
membersPane . className = "direct-chat-contacts" ;
//Add conversation members list
infosBox . membersList = createElem ( "ul" , membersPane ) ;
infosBox . membersList . className = "contacts-list" ;
//Add send a message form
this . addMessageform ( infosBox ) ;
//Return informations about the chat window
return infosBox ;
} ,
/ * *
* Add a message form to the chat windows
*
* @ param { Object } infosBox Informations about the chat box
* @ return { Boolean } True for a success
* /
addMessageform : function ( infosBox ) {
2018-03-25 07:43:39 +00:00
//Create form container
var conversationFormContainer = createElem2 ( {
2017-06-21 14:44:10 +00:00
appendTo : infosBox . boxFooter ,
2017-06-21 16:29:14 +00:00
type : "form" ,
2017-06-21 14:44:10 +00:00
class : "create-message-form"
} ) ;
2017-06-21 16:29:14 +00:00
//Create input group
2017-06-21 14:44:10 +00:00
var inputGroup = createElem2 ( {
2018-03-25 07:43:39 +00:00
appendTo : conversationFormContainer ,
2017-06-21 14:44:10 +00:00
type : "div" ,
class : "input-group"
} ) ;
//Create text input (for message)
var inputText = createElem2 ( {
appendTo : inputGroup ,
2017-06-21 16:29:14 +00:00
type : "textarea" ,
2017-06-21 14:44:10 +00:00
class : "form-control" ,
placeholder : "New message..." ,
} ) ;
2017-06-26 08:56:12 +00:00
inputText . maxLength = 200 ;
2017-06-21 14:44:10 +00:00
2017-06-21 16:29:14 +00:00
//Enable textarea 2.0 on the message
var textarea2 = new ComunicWeb . components . textarea ( ) ;
textarea2 . init ( {
element : inputText ,
minHeight : "34px" ,
autosize : true ,
} ) ;
//Create image input (for optionnal image)
var inputImage = createElem2 ( {
type : "input" ,
elemType : "file" ,
} ) ;
2017-06-21 14:44:10 +00:00
//Create button group
var buttonGroup = createElem2 ( {
appendTo : inputGroup ,
type : "span" ,
class : "input-group-btn" ,
} ) ;
2018-04-21 06:38:45 +00:00
//Add emojie button
var emojiButton = createElem2 ( {
appendTo : buttonGroup ,
type : "button" ,
elemType : "button" ,
class : "btn btn-flat btn-add-emoji" ,
} ) ;
//Add image icon
createElem2 ( {
type : "i" ,
appendTo : emojiButton ,
class : "fa fa-smile-o"
} ) ;
//Make emojie button lives
ComunicWeb . components . emoji . picker . addPicker ( inputText , emojiButton , function ( ) {
//Make the emojie picker visible
wdtEmojiBundle . popup . style . top = ( abs _height _bottom _screen ( ) - 357 ) + "px" ;
//Make the smile button visible
var currLeft = Number ( wdtEmojiBundle . popup . style . left . replace ( "px" , "" ) ) ;
var potentialLeft = currLeft - 20 ;
if ( potentialLeft > 0 )
wdtEmojiBundle . popup . style . left = potentialLeft + "px" ;
} ) ;
2017-06-21 16:29:14 +00:00
//Add image button
var imageButton = createElem2 ( {
appendTo : buttonGroup ,
type : "button" ,
elemType : "button" ,
class : "btn btn-flat btn-add-image" ,
} ) ;
imageButton . onclick = function ( ) {
//Call file selector
inputImage . click ( ) ;
} ;
//Add image icon
createElem2 ( {
type : "i" ,
appendTo : imageButton ,
class : "fa fa-image"
} ) ;
2017-06-21 14:44:10 +00:00
//Add send button
var sendButton = createElem2 ( {
appendTo : buttonGroup ,
2017-06-23 15:25:29 +00:00
type : "button" ,
2017-06-21 14:44:10 +00:00
class : "btn btn-primary btn-flat" ,
elemType : "submit" ,
2017-06-21 16:29:14 +00:00
} ) ;
2017-06-23 15:25:29 +00:00
//Add send icon
createElem2 ( {
appendTo : sendButton ,
type : "i" ,
class : "fa fa-send-o" ,
} ) ;
2017-06-21 16:29:14 +00:00
//Prevent textarea from adding a new line when pressing enter
$ ( inputText ) . keypress ( function ( event ) {
if ( event . keyCode == 13 ) {
event . preventDefault ( ) ;
sendButton . click ( ) ;
}
2017-06-21 14:44:10 +00:00
} ) ;
2017-06-21 16:29:14 +00:00
//Add required elements to infosBox
infosBox . sendMessageForm = {
2018-03-25 07:43:39 +00:00
formRoot : conversationFormContainer ,
2017-06-21 16:29:14 +00:00
sendButton : sendButton ,
inputText : inputText ,
textarea2 : textarea2 ,
inputImage : inputImage ,
} ;
2017-06-21 14:44:10 +00:00
//Success
return true ;
} ,
2017-06-18 09:14:26 +00:00
/ * *
* Load ( or reload ) a conversation
*
* @ param { Integer } conversationID The ID of the conversation to load
* @ param { Object } conversationWindow Informations about the conversation window
* @ return { Boolean } True for a success
* /
load : function ( conversationID , conversationWindow ) {
2017-06-24 17:23:55 +00:00
//Log action
ComunicWeb . debug . logMessage ( "Loading conversation " + conversationID ) ;
2017-06-18 07:15:50 +00:00
//Change conversation window name (loading state)
this . changeName ( "Loading" , conversationWindow ) ;
//Peform a request to informations about the conversation
ComunicWeb . components . conversations . interface . getInfosOne ( conversationID , function ( informations ) {
//In case of error
if ( informations . error ) {
//Display error notification
ComunicWeb . common . notificationSystem . showNotification ( "Couldn't get informations about the conversation !" , "danger" ) ;
return false ;
}
//Get informations about the members of the conversation
getMultipleUsersInfos ( informations . members , function ( membersInfos ) {
//Quit in case of error
if ( informations . error ) {
//Display error notification
ComunicWeb . common . notificationSystem . showNotification ( "Couldn't get informations about the conversation members !" , "danger" ) ;
return false ;
}
//Create conversation informations root object
var conversationInfos = {
box : conversationWindow ,
membersInfos : membersInfos ,
infos : informations
} ;
2017-06-18 09:32:31 +00:00
//Save conversation informations in the cache
ComunicWeb . components . conversations . chatWindows . _ _conversationsCache [ "conversation-" + conversationID ] = conversationInfos ;
2017-06-18 07:15:50 +00:00
//Change the name of the conversation
ComunicWeb . components . conversations . utils . getName ( informations , function ( conversationName ) {
ComunicWeb . components . conversations . chatWindows . changeName ( conversationName , conversationWindow ) ;
} ) ;
//Update conversation members informations
ComunicWeb . components . conversations . chatWindows . updateMembersList ( conversationInfos ) ;
//Display conversation settings pane
ComunicWeb . components . conversations . chatWindows . showConversationSettings ( conversationInfos ) ;
2017-06-24 17:23:55 +00:00
//Register the conversation in the service
ComunicWeb . components . conversations . service . registerConversation ( conversationID ) ;
2017-06-21 16:29:14 +00:00
//Make send a message button lives
conversationInfos . box . sendMessageForm . formRoot . onsubmit = function ( ) {
//Submit new message
ComunicWeb . components . conversations . chatWindows . submitMessageForm ( conversationInfos ) ;
//Block page reloading
return false ;
} ;
2017-06-18 07:15:50 +00:00
} ) ;
} ) ;
//Success
return true ;
} ,
2017-06-18 09:32:31 +00:00
/ * *
* Unload a chat window
*
* @ param { Integer } conversationID The ID of the conversation to unload
* @ param { Boolean } keepInfos Keep informations about the chat window
* @ return { Boolean } True for a success
* /
unload : function ( conversationID , keepInfos ) {
if ( ! this . _ _conversationsCache [ "conversation-" + conversationID ] ) {
ComunicWeb . debug . logMessage ( "Couldn't unload conversation: " + conversationID + ". It seems not to be loaded..." ) ;
return false ;
}
//Log action
ComunicWeb . debug . logMessage ( "Unloading a conversation: " + conversationID ) ;
2017-06-24 17:23:55 +00:00
//Get informations
var conversationInfos = this . _ _conversationsCache [ "conversation-" + conversationID ] ;
//Empty messages area
emptyElem ( conversationInfos . box . messagesArea ) ;
2017-06-25 17:10:26 +00:00
conversationInfos . box . messagesArea . innerHTML = "" ;
2017-06-24 17:23:55 +00:00
//Un-register conversation
ComunicWeb . components . conversations . service . unregisterConversation ( conversationID ) ;
2017-06-18 09:32:31 +00:00
//Remove informations if required
if ( ! keepInfos ) {
delete this . _ _conversationsCache [ "conversation-" + conversationID ] ;
}
//Success
return true ;
} ,
/ * *
* Unload all chat windows
*
* @ return { Boolean } True for a success
* /
unloadAll : function ( ) {
//Clear conversation object
clearObject ( this . _ _conversationsCache ) ;
//Success
return true ;
} ,
2017-06-16 09:42:28 +00:00
/ * *
* Change the name of the converation at the top of the windows
*
* @ param { String } newName The new name for the conversation window
2017-06-18 09:32:31 +00:00
* @ param { Ojbect } infos Informations about the conversation window
2017-06-16 09:42:28 +00:00
* @ return { Boolean } True for a success
* /
changeName : function ( newName , infos ) {
//Empty name field
emptyElem ( infos . boxTitle ) ;
//Create conversation icon
var conversationIcon = createElem ( "i" , infos . boxTitle ) ;
conversationIcon . className = "fa fa-comments" ;
//Add conversation title
var conversationTitle = createElem ( "span" , infos . boxTitle ) ;
conversationTitle . innerHTML = " " + newName ;
2017-06-17 08:09:37 +00:00
//Success
return true ;
} ,
/ * *
* Update conversation members list
*
* @ param { Object } conversation Informations about the conversation
* @ return { Boolean } True for a success
* /
updateMembersList : function ( conversation ) {
//First, make sure conversation members pane is empty
emptyElem ( conversation . box . membersList ) ;
//Then process each user
var i = 0 ;
for ( i in conversation . infos . members ) {
if ( conversation . membersInfos [ 'user-' + conversation . infos . members [ i ] ] ) {
var memberInfos = conversation . membersInfos [ 'user-' + conversation . infos . members [ i ] ] ;
//Display user informations
var userLi = createElem ( "li" , conversation . box . membersList ) ;
var userLink = createElem ( "a" , userLi ) ;
//Add user account image
var userImage = createElem ( "img" , userLink ) ;
userImage . className = "contacts-list-img" ;
userImage . src = memberInfos . accountImage ;
//Add member informations
var memberInfosList = createElem2 ( {
type : "div" ,
appendTo : userLink ,
class : "contacts-list-info" ,
} ) ;
//Add user name
var memberName = createElem2 ( {
type : "span" ,
appendTo : memberInfosList ,
class : "contacts-list-name" ,
innerHTML : memberInfos . firstName + " " + memberInfos . lastName ,
} ) ;
//Check if members is a moderator or not of the conversation
var memberStatus = conversation . infos . ID _owner == memberInfos . userID ? "Moderator" : "Member" ;
//Add member status
var memberStatus = createElem2 ( {
type : "span" ,
appendTo : memberInfosList ,
class : "contats-list-msg" ,
innerHTML : memberStatus
} ) ;
}
}
//Enable slimscrooll
$ ( conversation . box . membersList ) . slimscroll ( {
height : "100%" ,
color : "#FFFFFF"
} ) ;
2017-06-16 09:42:28 +00:00
//Success
return true ;
2017-06-17 09:20:54 +00:00
} ,
/ * *
* Show conversation settings ( button + pane )
*
2017-06-17 15:24:45 +00:00
* @ param { Object } conversation Informations about the conversation
2017-06-17 09:20:54 +00:00
* @ return { Boolean } True for a success
* /
2017-06-17 15:24:45 +00:00
showConversationSettings : function ( conversation ) {
2017-06-17 09:40:57 +00:00
2017-06-18 09:14:26 +00:00
//First, check conversation settings button and pane don't exists yet
if ( conversation . box . settingsButton ) {
if ( conversation . box . settingsButton . remove ) {
conversation . box . settingsButton . remove ( ) ;
}
}
if ( conversation . box . settingsPane ) {
if ( conversation . box . settingsPane . remove ) {
conversation . box . settingsPane . remove ( ) ;
}
}
2017-06-17 09:20:54 +00:00
//Create and display conversation settings button wheel
2017-06-17 15:24:45 +00:00
conversation . box . settingsButton = createElem2 ( {
2017-06-17 09:20:54 +00:00
type : "button" ,
2017-06-17 15:24:45 +00:00
insertBefore : conversation . box . membersButton ,
2017-06-17 09:20:54 +00:00
class : "btn btn-box-tool" ,
type : "button"
} ) ;
//Add button icon
createElem2 ( {
type : "i" ,
2017-06-17 15:24:45 +00:00
appendTo : conversation . box . settingsButton ,
2017-06-17 09:20:54 +00:00
class : "fa fa-gear" ,
} ) ;
//Create settings pane
var settingsPane = createElem2 ( {
type : "div" ,
2017-06-17 15:24:45 +00:00
appendTo : conversation . box . boxBody ,
2017-06-17 09:20:54 +00:00
class : "conversation-settings-pane" ,
} ) ;
2017-06-18 09:14:26 +00:00
conversation . box . settingsPane = settingsPane ;
2017-06-17 09:20:54 +00:00
//Make the settings button lives
2017-06-17 15:24:45 +00:00
conversation . box . settingsButton . onclick = function ( ) {
2017-06-17 09:40:57 +00:00
//Update settings pane classname
2017-06-17 09:20:54 +00:00
if ( settingsPane . className . includes ( " open" ) )
settingsPane . className = settingsPane . className . replace ( " open" , "" ) ; //Close the pane
else
settingsPane . className += " open" ; //Open the pane
} ;
2017-06-17 09:40:57 +00:00
//Create the conversation form
var settingsForm = ComunicWeb . components . conversations . utils . createConversationForm ( settingsPane ) ;
//Update form informations
2017-06-17 15:24:45 +00:00
settingsForm . createButton . innerHTML = "Update settings" ;
2017-06-17 09:40:57 +00:00
//Update conversation name
2017-06-17 15:24:45 +00:00
if ( conversation . infos . name )
settingsForm . conversationNameInput . value = conversation . infos . name ;
2017-06-18 09:14:26 +00:00
//Check if user is a conversation moderator or not
2017-06-17 15:24:45 +00:00
if ( conversation . infos . ID _owner == userID ( ) ) {
//Update conversation members
ComunicWeb . components . userSelect . pushEntries ( settingsForm . usersElement , conversation . infos . members ) ;
}
else {
//We disable name field
settingsForm . conversationNameInput . disabled = "true" ;
2017-06-17 09:40:57 +00:00
2017-06-17 15:24:45 +00:00
//We hide conversation users (presents in members pane)
settingsForm . usersElement . parentNode . style . display = "none" ;
}
2017-06-17 09:40:57 +00:00
2017-06-18 09:14:26 +00:00
//Update follow conversation checkbox status
2017-06-17 15:24:45 +00:00
if ( conversation . infos . following == "1" ) {
$ ( settingsForm . followConversationInput ) . iCheck ( "check" ) ;
}
else {
$ ( settingsForm . followConversationInput ) . iCheck ( "uncheck" ) ;
}
//Save settings form in global form
conversation . settingsForm = settingsForm ;
2017-06-17 09:40:57 +00:00
2017-06-17 15:24:45 +00:00
//Make update settings button lives
settingsForm . createButton . onclick = function ( ) {
ComunicWeb . components . conversations . chatWindows . submitUpdateForm ( conversation ) ;
} ;
2017-06-17 09:40:57 +00:00
2017-06-17 09:20:54 +00:00
//Success
return true ;
} ,
2017-06-17 08:09:37 +00:00
2017-06-17 15:24:45 +00:00
/ * *
* Process submited update conversation form
*
* @ param { Object } conversation Informations about the conversation
* @ return { Boolean } True for a success
* /
submitUpdateForm : function ( conversation ) {
//Then, get informations about the input
var newValues = {
2017-06-17 15:32:21 +00:00
conversationID : conversation . infos . ID ,
2017-06-17 15:24:45 +00:00
following : conversation . settingsForm . followConversationInput . checked ,
}
//Add other fields if the user is a conversation moderator
if ( conversation . infos . ID _owner == userID ( ) ) {
//Specify conversation name
var nameValue = conversation . settingsForm . conversationNameInput . value
newValues . name = ( nameValue === "" ? false : nameValue ) ;
//Get conversation members
newValues . members = ComunicWeb . components . userSelect . getResults ( conversation . settingsForm . usersElement ) ;
//Check if any users were selected
if ( newValues . members . length === 0 ) {
//Inform user that its input is invalid
ComunicWeb . common . notificationSystem . showNotification ( "Please select at least one user !" , "danger" , 3 ) ;
return false ;
}
}
//Now, freeze the submit button
2017-06-18 09:14:26 +00:00
conversation . settingsForm . createButton . disabled = true ;
2017-06-17 15:24:45 +00:00
//Peform a request through the interface
2017-06-17 15:32:21 +00:00
ComunicWeb . components . conversations . interface . updateSettings ( newValues , function ( result ) {
2017-06-18 09:14:26 +00:00
//Enable again update button
conversation . settingsForm . createButton . disabled = false ;
//Check for errors
if ( result . error )
ComunicWeb . common . notificationSystem . showNotification ( "An error occured while trying to update conversation settings !" , "danger" , 4 ) ;
//Reload the conversation
2017-06-18 09:32:31 +00:00
ComunicWeb . components . conversations . chatWindows . unload ( conversation . infos . ID , true ) ;
ComunicWeb . components . conversations . chatWindows . load ( conversation . infos . ID , conversation . box ) ;
2017-06-17 15:32:21 +00:00
} ) ;
2017-06-17 15:24:45 +00:00
//Success
return true ;
} ,
2017-06-21 16:29:14 +00:00
/ * *
* Submit a new message form
*
* @ param { Object } convInfos Informations about the conversation
* @ return { Boolean } True for a success
* /
submitMessageForm : function ( convInfos ) {
2017-06-21 16:38:42 +00:00
2017-06-21 16:29:14 +00:00
//Log action
ComunicWeb . debug . logMessage ( "Send a new message in a conversation system." ) ;
2017-06-22 09:16:53 +00:00
//Extract main fields
var form = convInfos . box . sendMessageForm ;
2017-06-21 16:29:14 +00:00
//Check if message is empty
2017-06-22 09:16:53 +00:00
if ( ! checkString ( form . inputText . value ) && ! form . inputImage . files [ 0 ] ) {
2017-06-21 16:38:42 +00:00
ComunicWeb . common . notificationSystem . showNotification ( "Please type a valid message before trying to send it !" , "danger" , 2 ) ;
2017-06-22 09:16:53 +00:00
return false ;
}
//Lock send button
form . sendButton . disabled = true ;
//Prepare what to do next
var onceSent = function ( result ) {
//Check for errors
if ( result . error ) {
ComunicWeb . common . notificationSystem . showNotification ( "An error occured while trying to send message! Please try again..." , "danger" , 2 ) ;
//Unlock send button
form . sendButton . disabled = false ;
return false ;
}
//Reset the form
ComunicWeb . components . conversations . chatWindows . resetCreateMessageForm ( convInfos ) ;
}
2018-04-21 09:00:13 +00:00
//Send the message throught the interface
ComunicWeb . components . conversations . interface . sendMessage ( {
conversationID : convInfos . infos . ID ,
message : form . inputText . value ,
image : form . inputImage ,
callback : onceSent
} ) ;
2017-06-22 09:16:53 +00:00
//Success
return true ;
} ,
/ * *
* Reset a create a message form
*
* @ param { Object } infos Informations about the conversation
* @ return { Boolean } True for a success
* /
resetCreateMessageForm : function ( infos ) {
//Extract form informations
var form = infos . box . sendMessageForm ;
//Unlock send button and reset its value
form . sendButton . disabled = false ;
//Empty textarea
form . inputText . value = "" ;
form . textarea2 . resetHeight ( ) ;
//Remove image from image input
form . inputImage . value = "" ;
2017-06-21 16:29:14 +00:00
//Success
return true ;
} ,
2017-06-25 17:10:26 +00:00
/ * *
* 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 ) {
//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 ( ) ;
2017-06-26 09:16:14 +00:00
//Check if this is the first message of the conversation or not
if ( ! convInfos . lastMessage ) {
//Initialize last message object
convInfos . lastMessage = {
userID : 0 ,
messageContainer : false ,
}
}
2017-06-25 17:10:26 +00:00
2017-06-26 09:16:14 +00:00
//Check if message poster is the same as the last message
if ( convInfos . lastMessage . userID == messageInfos . ID _user ) {
//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
var messageContainer = createElem2 ( {
appendTo : convInfos . box . messagesArea ,
type : "div" ,
class : "direct-chat-msg " + ( userIsPoster ? "right" : "" )
} ) ;
2017-06-25 17:10:26 +00:00
2017-06-26 09:16:14 +00:00
//Display message header
var messageHeader = createElem2 ( {
appendTo : messageContainer ,
type : "div" ,
class : "direct-chat-info clearfix"
} ) ;
2017-06-25 17:10:26 +00:00
2017-06-26 09:16:14 +00:00
//Add user name
var usernameElem = createElem2 ( {
appendTo : messageHeader ,
type : "span" ,
class : "direct-chat-name pull-" + ( userIsPoster ? "right" : "left" ) ,
innerHTML : "Loading" ,
} ) ;
//Hide user name if it is the current user
if ( userIsPoster )
usernameElem . style . display = "none" ;
//Add user account image
var userAccountImage = createElem2 ( {
appendTo : messageContainer ,
type : "img" ,
class : "direct-chat-img" ,
src : ComunicWeb . _ _config . assetsURL + "img/defaultAvatar.png" ,
alt : "User account 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 ;
}
//Update conversation informations
convInfos . lastMessage = {
userID : messageInfos . ID _user ,
messageContainer : messageContainer ,
}
}
2017-06-25 17:10:26 +00:00
//Add message
var messageTargetElem = createElem2 ( {
2017-06-26 09:16:14 +00:00
appendTo : messageContainer ,
2017-06-25 17:10:26 +00:00
type : "div" ,
2017-06-26 09:16:14 +00:00
class : "direct-chat-text " + ( ! firstMessageFromUser ? "not-first-message" : "" ) ,
2017-06-25 17:10:26 +00:00
} ) ;
//Add text message
2017-06-26 08:56:12 +00:00
var textMessage = createElem2 ( {
2017-06-25 17:10:26 +00:00
appendTo : messageTargetElem ,
type : "span" ,
2017-07-04 19:13:02 +00:00
innerHTML : removeHtmlTags ( messageInfos . message ) , //Remove HTML tags
2017-06-25 17:10:26 +00:00
} ) ;
//Check if an image has to be added
if ( messageInfos . image _path != null ) {
2017-06-26 13:36:29 +00:00
//Image link
var imageLink = createElem2 ( {
2017-06-25 17:10:26 +00:00
appendTo : messageTargetElem ,
2017-06-26 13:36:29 +00:00
type : "a" ,
href : messageInfos . image _path ,
} ) ;
//Image element
createElem2 ( {
appendTo : imageLink ,
2017-06-25 17:10:26 +00:00
type : "img" ,
src : messageInfos . image _path ,
class : "conversation-msg-image"
} ) ;
2017-06-26 13:36:29 +00:00
//Enable lightbox
imageLink . onclick = function ( ) {
$ ( this ) . ekkoLightbox ( {
alwaysShowClose : true ,
} ) ;
return false ;
}
2017-06-25 17:10:26 +00:00
}
2017-06-26 08:56:12 +00:00
//Parse emojies in text message
ComunicWeb . components . emoji . parser . parse ( {
element : textMessage ,
} ) ;
2018-04-26 04:50:23 +00:00
//Enable slimscroll
2017-06-26 08:49:53 +00:00
$ ( 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
} ) ;
2018-04-26 04:50:23 +00:00
//Initialize top scroll detection if required
this . initTopScrollDetection ( conversationID ) ;
2017-06-25 17:10:26 +00:00
//Success
return true ;
} ,
2018-04-26 04:50:23 +00:00
/ * *
* Init top scroll detection ( if required )
*
* @ param { number } conversationID The ID of the target conversation
* /
initTopScrollDetection : function ( conversationID ) {
//Extract conversation informations
var convInfo = this . _ _conversationsCache [ "conversation-" + conversationID ] ;
//Check if nothing has to be done
if ( convInfo . box . initializedScrollDetection )
return ;
//Mark scroll detection as initialized
convInfo . box . initializedScrollDetection = true ;
var scrollDetectionLocked = false ;
var scrollTopCount = 0 ;
$ ( convInfo . box . messagesArea ) . slimScroll ( ) . bind ( "slimscrolling" , function ( e , pos ) {
if ( scrollDetectionLocked )
return ;
if ( pos != 0 ) {
scrollTopCount = 0 ;
}
scrollTopCount ++ ;
//Check top count
if ( scrollTopCount < 3 )
return ;
//Lock the detection
scrollDetectionLocked = true ;
//Fetch older messages
2018-04-26 15:55:38 +00:00
ComunicWeb . components . conversations . interface . getOlderMessages (
conversationID ,
ComunicWeb . components . conversations . service . getOldestMessageID ( conversationID ) ,
10 ,
function ( result ) {
//Unlock scroll detection
scrollDetectionLocked = false ;
//Check for errors
if ( result . error ) {
notify ( "An error occured while trying to fetch older messages for the conversation !" ) ;
return ;
}
//Check for results
if ( result . length == 0 ) {
//Lock scroll detection in order to avoid useless traffic
scrollDetectionLocked = true ;
return ;
}
//Save the ID of the oldest message
ComunicWeb . components . conversations . service . setOldestMessageID ( result [ 0 ] . ID ) ;
//Process the messages in reverse order
}
) ;
2018-04-26 04:50:23 +00:00
} ) ;
}
2017-06-18 09:32:31 +00:00
}
//Register conversations cache cleaning function
ComunicWeb . common . cacheManager . registerCacheCleaner ( "ComunicWeb.components.conversations.chatWindows.unloadAll" ) ;