ComunicWeb/assets/js/components/conversations/chatWindows.js

394 lines
12 KiB
JavaScript
Raw Normal View History

/**
* Conversation chat window functions
*
* @author Pierre HUBERT
*/
ComunicWeb.components.conversations.chatWindows = {
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({
target: byId(ComunicWeb.components.conversations.manager.__conversationsContenerID),
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;
},
/**
* 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-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
};
//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);
});
});
//Success
return true;
},
/**
* 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");
2017-06-18 07:15:50 +00:00
//First, create the generic conversation window
var infosBox = ComunicWeb.components.conversations.windows.create(infos.target.children[0]);
2017-06-17 08:09:37 +00:00
//Save conversation ID
infosBox.conversationID = infos.conversationID;
2017-06-17 08:09:37 +00:00
//Change box root class name
infosBox.rootElem.className += " direct-chat";
//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);
}
infosBox.closeButton.onclick = infosBox.closeFunction;
2017-06-17 08:09:37 +00:00
//Debug
infosBox.boxBody.innerHTML = "<div class='direct-chat-messages'>Hello world</p>";
//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";
//Return informations about the chat window
return infosBox;
},
/**
* Change the name of the converation at the top of the windows
*
* @param {String} newName The new name for the conversation window
* @param {Ojbect} infos INformations about the conversation window
* @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"
});
//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;
//Export conversation ID (to avoid error after)
var conversationID = conversation.infos.ID;
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
ComunicWeb.components.conversations.chatWindows.load(conversationID, conversation.box);
2017-06-17 15:32:21 +00:00
console.log("Function callback !!!!!!!!!!!!!!", result);
});
2017-06-17 15:24:45 +00:00
//Success
return true;
},
}