2017-06-07 17:24:48 +00:00
|
|
|
/**
|
|
|
|
* Conversations manager
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
ComunicWeb.components.conversations.manager = {
|
2017-06-14 14:39:34 +00:00
|
|
|
|
|
|
|
/**
|
2018-03-25 07:43:39 +00:00
|
|
|
* @var {String} The ID of the conversation container
|
2017-06-14 14:39:34 +00:00
|
|
|
*/
|
2018-03-25 07:43:39 +00:00
|
|
|
__conversationsContainerID: "conversationsElem",
|
2017-06-14 14:39:34 +00:00
|
|
|
|
2017-06-07 17:24:48 +00:00
|
|
|
/**
|
|
|
|
* Display conversations manager
|
|
|
|
*
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
|
|
|
display: function(){
|
|
|
|
|
|
|
|
//Try to get conversation manager
|
2018-03-25 07:43:39 +00:00
|
|
|
var conversationsContainerElem = byId(this.__conversationsContainerID);
|
2017-06-07 17:24:48 +00:00
|
|
|
|
|
|
|
//Check if element exists or not
|
|
|
|
if(conversationsContainerElem){
|
|
|
|
ComunicWeb.debug.logMessage("NOTICE : couldn't initializate conversation manager because a conversation manager is already on the page");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Else inform user and create conversation manager
|
|
|
|
ComunicWeb.debug.logMessage("INFO : initializate conversation manager");
|
|
|
|
|
|
|
|
//Create conversations manager element
|
|
|
|
var conversationsContainerElem = createElem("div");
|
2018-03-25 07:43:39 +00:00
|
|
|
conversationsContainerElem.id = this.__conversationsContainerID;
|
2017-06-07 17:24:48 +00:00
|
|
|
|
|
|
|
//Insert the element at the right place
|
|
|
|
var pageTarget = byId("pageTarget");
|
|
|
|
if(pageTarget){
|
|
|
|
//Insert disucssion element before it
|
|
|
|
byId("wrapper").insertBefore(conversationsContainerElem, pageTarget);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
//Just apply the element
|
|
|
|
byId("wrapper").appendChild(conversationsContainerElem);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Initializate conversation element
|
|
|
|
this.init(conversationsContainerElem);
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializate conversations element
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} conversationsContainerElem The container of the conversation element
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
|
|
|
init: function(conversationsContainerElem){
|
|
|
|
|
|
|
|
//First, add the "open a conversation" new
|
|
|
|
this.addOpenConversationButton(conversationsContainerElem);
|
|
|
|
|
2017-06-24 17:23:55 +00:00
|
|
|
//Intializate conversation service
|
|
|
|
ComunicWeb.components.conversations.service.init();
|
|
|
|
|
2017-06-16 09:42:28 +00:00
|
|
|
//Then, open any already active conversation
|
|
|
|
var openedConversations = ComunicWeb.components.conversations.cachingOpened.getAll();
|
2017-06-16 17:11:41 +00:00
|
|
|
|
2017-06-16 09:42:28 +00:00
|
|
|
//Process opened conversations
|
|
|
|
for(i in openedConversations){
|
|
|
|
if(i < openedConversations.length)
|
2017-06-18 07:15:50 +00:00
|
|
|
ComunicWeb.components.conversations.chatWindows.openConversation(openedConversations[i]);
|
2017-06-16 09:42:28 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 17:24:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the "open conversation" button
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} targetElem The target of the button
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
|
|
|
addOpenConversationButton: function(targetElem){
|
|
|
|
|
|
|
|
//Create the button
|
|
|
|
var addButton = createElem("button", targetElem);
|
|
|
|
addButton.className = "btn btn-primary open-conversation-button";
|
2017-06-24 13:07:23 +00:00
|
|
|
addButton.innerHTML = "Conversations";
|
2017-06-07 17:24:48 +00:00
|
|
|
|
|
|
|
|
2017-06-10 13:18:03 +00:00
|
|
|
//Make button lives
|
2017-06-07 17:24:48 +00:00
|
|
|
addButton.onclick = function(){
|
|
|
|
ComunicWeb.components.conversations.list.display(this);
|
|
|
|
}
|
2017-06-10 07:42:09 +00:00
|
|
|
},
|
2017-06-14 09:46:10 +00:00
|
|
|
|
|
|
|
/**
|
2017-06-16 09:42:28 +00:00
|
|
|
* Add a new conversation to the list of opened conversation accordingly to specified informations
|
2017-06-14 09:46:10 +00:00
|
|
|
*
|
2021-03-06 11:04:01 +00:00
|
|
|
* @param {number} convID Informations about the conversation to open
|
2017-06-14 09:46:10 +00:00
|
|
|
*/
|
2021-03-06 11:04:01 +00:00
|
|
|
addConversation: function(convID){
|
2017-06-14 09:46:10 +00:00
|
|
|
|
2017-06-14 14:39:34 +00:00
|
|
|
//Check if the conversation is already open or not
|
2021-03-06 11:04:01 +00:00
|
|
|
if(ComunicWeb.components.conversations.cachingOpened.isopen(convID)){
|
|
|
|
ComunicWeb.debug.logMessage("The conversation " + convID + " is already opened !");
|
|
|
|
return;
|
2017-06-14 14:39:34 +00:00
|
|
|
}
|
|
|
|
|
2017-06-18 09:14:26 +00:00
|
|
|
//Save conversation ID in session storage
|
2021-03-06 11:04:01 +00:00
|
|
|
ComunicWeb.components.conversations.cachingOpened.add(convID);
|
2017-06-18 09:14:26 +00:00
|
|
|
|
2017-06-16 09:42:28 +00:00
|
|
|
//Open the conversation
|
2021-03-06 11:04:01 +00:00
|
|
|
ComunicWeb.components.conversations.chatWindows.openConversation(convID);
|
2017-06-16 09:42:28 +00:00
|
|
|
},
|
2017-06-19 12:55:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Open a private conversation with only one user
|
|
|
|
*
|
|
|
|
* @param {Integer} otherID The ID of the user with who the conversation will be started
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
|
|
|
openPrivate: function(otherID){
|
|
|
|
|
|
|
|
//Search for such conversation in the database, create it in case of failure
|
|
|
|
//Prepare what to do next
|
|
|
|
var callback = function(result){
|
|
|
|
|
|
|
|
//In case of error
|
|
|
|
if(result.error){
|
|
|
|
//Notify user
|
|
|
|
ComunicWeb.common.notificationSystem.showNotification("Couldn't create a conversation with this user ! Please try again...", "danger", 2);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Open the first conversation
|
2021-03-06 14:06:54 +00:00
|
|
|
ComunicWeb.components.conversations.manager.addConversation(result.conversationsID[0]);
|
2017-06-19 12:55:29 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
//Peform request
|
|
|
|
ComunicWeb.components.conversations.interface.searchPrivate(otherID, true, callback);
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
|
|
|
|
},
|
2017-06-07 17:24:48 +00:00
|
|
|
}
|