Conversation messages are sent over the network

This commit is contained in:
Pierre 2017-06-22 11:16:53 +02:00
parent 8e89549fc5
commit 48e7109db4
3 changed files with 120 additions and 2 deletions

View File

@ -564,11 +564,94 @@ ComunicWeb.components.conversations.chatWindows = {
//Log action
ComunicWeb.debug.logMessage("Send a new message in a conversation system.");
console.log(convInfos);
//Extract main fields
var form = convInfos.box.sendMessageForm;
//Check if message is empty
if(!checkString(convInfos.box.sendMessageForm.inputText.value))
if(!checkString(form.inputText.value) && !form.inputImage.files[0]){
ComunicWeb.common.notificationSystem.showNotification("Please type a valid message before trying to send it !", "danger", 2);
return false;
}
//Lock send button
form.sendButton.value = "Sending";
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.value = "Send";
form.sendButton.disabled = false;
return false;
}
//Reset the form
ComunicWeb.components.conversations.chatWindows.resetCreateMessageForm(convInfos);
}
//Check if an image is included with the message or not
if(form.inputImage.files[0]){
//Include the image with the request (export the image as URL)
var reader = new FileReader();
reader.readAsDataURL(form.inputImage.files[0]);
var sendImage = reader.result;
//The function will resume once the image is fully converted
reader.addEventListener("load", function() {
//Send the message throught the interface
ComunicWeb.components.conversations.interface.sendMessage({
conversationID: convInfos.infos.ID,
message: form.inputText.value,
image: reader.result,
callback: onceSent
});
}, false);
}
else {
//Send the message throught the interface
ComunicWeb.components.conversations.interface.sendMessage({
conversationID: convInfos.infos.ID,
message: form.inputText.value,
callback: onceSent
});
}
//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.value = "Send";
form.sendButton.disabled = false;
//Empty textarea
form.inputText.value = "";
form.textarea2.resetHeight();
//Remove image from image input
form.inputImage.value = "";
//Success
return true;

View File

@ -236,6 +236,34 @@ ComunicWeb.components.conversations.interface = {
return true;
},
/**
* Send a new message
*
* @param {Object} infos Informations about the message to send
* @info {Integer} conversationID The ID of the conversation
* @info {String} message The message to send
* @info {String} image Optionnal, base64-encoded image
* @info {function} callback What to do once the image was successfully sent
* @return {Boolean} true for a success
*/
sendMessage: function(infos){
//Perform an API request
var apiURI = "/conversations/sendMessage";
var params = {
message: infos.message,
conversationID: infos.conversationID,
}
//Add an image (if any specified)
if(infos.image)
params.image = infos.image;
//Perform an API request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, infos.callback);
},
/**
* Empty conversations cache
*

View File

@ -55,5 +55,12 @@ textArea2.prototype.getValue = function(){
return this.element.innerText;
};
/**
* Reset textarea height to its minimal height
*/
textArea2.prototype.resetHeight = function(){
this.element.style.height = this.element.style.minHeight;
}
//Save the function in the system
ComunicWeb.components.textarea = textArea2;