diff --git a/assets/js/components/conversations/chatWindows.js b/assets/js/components/conversations/chatWindows.js index bf6b9219..a9de9f1d 100644 --- a/assets/js/components/conversations/chatWindows.js +++ b/assets/js/components/conversations/chatWindows.js @@ -75,7 +75,13 @@ ComunicWeb.components.conversations.chatWindows = { //Debug - infosBox.boxBody.innerHTML = "
Hello world

"; + //Create messages contener + infosBox.messagesArea = createElem2({ + appendTo: infosBox.boxBody, + type: "div", + class: "direct-chat-messages", + innerHTML: "

Loading, please wait...

", + }); //Add button to get conversation members infosBox.membersButton = createElem("button"); @@ -221,6 +227,10 @@ ComunicWeb.components.conversations.chatWindows = { * @return {Boolean} True for a success */ load: function(conversationID, conversationWindow){ + + //Log action + ComunicWeb.debug.logMessage("Loading conversation " + conversationID); + //Change conversation window name (loading state) this.changeName("Loading", conversationWindow); @@ -265,6 +275,9 @@ ComunicWeb.components.conversations.chatWindows = { //Display conversation settings pane ComunicWeb.components.conversations.chatWindows.showConversationSettings(conversationInfos); + //Register the conversation in the service + ComunicWeb.components.conversations.service.registerConversation(conversationID); + //Make send a message button lives conversationInfos.box.sendMessageForm.formRoot.onsubmit = function(){ @@ -299,6 +312,16 @@ ComunicWeb.components.conversations.chatWindows = { //Log action ComunicWeb.debug.logMessage("Unloading a conversation: " + conversationID); + //Get informations + var conversationInfos = this.__conversationsCache["conversation-"+conversationID]; + + //Empty messages area + emptyElem(conversationInfos.box.messagesArea); + conversationInfos.box.messagesArea.innerHTML = "

Reloading, please wait...

"; + + //Un-register conversation + ComunicWeb.components.conversations.service.unregisterConversation(conversationID); + //Remove informations if required if(!keepInfos){ delete this.__conversationsCache["conversation-"+conversationID]; diff --git a/assets/js/components/conversations/manager.js b/assets/js/components/conversations/manager.js index 17202a1e..ae092719 100644 --- a/assets/js/components/conversations/manager.js +++ b/assets/js/components/conversations/manager.js @@ -64,6 +64,9 @@ ComunicWeb.components.conversations.manager = { //First, add the "open a conversation" new this.addOpenConversationButton(conversationsContainerElem); + //Intializate conversation service + ComunicWeb.components.conversations.service.init(); + //Then, open any already active conversation var openedConversations = ComunicWeb.components.conversations.cachingOpened.getAll(); @@ -73,9 +76,6 @@ ComunicWeb.components.conversations.manager = { ComunicWeb.components.conversations.chatWindows.openConversation(openedConversations[i]); } - //Intializate conversation service - ComunicWeb.components.conversations.service.init(); - }, /** diff --git a/assets/js/components/conversations/service.js b/assets/js/components/conversations/service.js index 2a483ed0..c1b55f0b 100644 --- a/assets/js/components/conversations/service.js +++ b/assets/js/components/conversations/service.js @@ -9,9 +9,14 @@ ComunicWeb.components.conversations.service = { /** - * @var {Integer} intervalID The ID of the current service interval + * @var {Integer} __intervalID The ID of the current service interval */ __intervalID: false, + + /** + * @var {Object} __serviceCache The service cache + */ + __serviceCache: false, /** * Initializate conversation service @@ -20,6 +25,9 @@ ComunicWeb.components.conversations.service = { */ init: function(){ + //Make sure the cache is empty + this.emptyCache(); + //Check if an interval already exists or not if(this.__intervalID) clearInterval(this.__intervalID); //Remove old interval @@ -27,7 +35,7 @@ ComunicWeb.components.conversations.service = { //Initializate interval this.__intervalID = setInterval(function(){ ComunicWeb.components.conversations.service.call(); - }, 1500); + }, 2000); ComunicWeb.common.cacheManager.registerInterval(this.__intervalID); //Success @@ -40,6 +48,111 @@ ComunicWeb.components.conversations.service = { * @return {Boolean} True for a success */ call: function(){ - console.log("Conversation service called !"); - } -} \ No newline at end of file + + //Check if the conversation element still exists or not + if(!byId("conversationsElem")){ + ComunicWeb.debug.logMessage("Conversation Service : Couldn't locate conversation element, unregistering service !"); + clearInterval(this.__intervalID); + return false; + } + + //Perform service task + this.performTask(); + }, + + /** + * Perform service task + * + * @return {Boolean} True for a success + */ + performTask: function(){ + console.log(this.__serviceCache); + //Prepare API request + var newConversations = []; + var conversationsToRefresh = {} + + //Process each conversation + var i; + for(i in this.__serviceCache){ + + //Extract conversation ID + var processConversation = this.__serviceCache[i].conversationID; + + //Check if it is new conversation + if(this.__serviceCache[i].last_update === 0) + newConversations.push(processConversation); + + //Else perform a simple update of the conversation + else { + conversationsToRefresh["conversation-"+processConversation] = { + last_update: this.__serviceCache[i].last_update, + }; + } + } + + //Perform a request on the interface + + //Success + return true; + }, + + /** + * Register a new conversation + * + * @param {Integer} conversationID The ID of the conversation to register + * @return {Boolean} True for a success + */ + registerConversation: function(conversationID){ + + //Create conversation object + if(!this.__serviceCache) + this.__serviceCache = {}; //Create service cache object + + //Register conversation + this.__serviceCache['conversation-' + conversationID] = { + conversationID: conversationID, + last_update: 0, + }; + + //Success + return true; + }, + + /** + * Unregister a conversation + * + * @param {Integer} conversationID The ID of the conversation to remove + * @return {Boolean} True for a success + */ + unregisterConversation: function(conversationID){ + + //Log action + ComunicWeb.debug.logMessage("Unregistering conversation " + conversationID + " from service."); + + if(this.__serviceCache){ + if(this.__serviceCache['conversation-'+conversationID]){ + delete this.__serviceCache['conversation-'+conversationID]; //Remove conversation + } + } + + //Success + return true; + }, + + /** + * Empty service cache (unregister all conversations) + * + * @return {Boolean} True for a success + */ + emptyCache: function(){ + if(this.__serviceCache){ + clearObject(this.__serviceCache); + } + + //Success + return true; + }, +} + +//Register service cache +ComunicWeb.common.cacheManager.registerCacheCleaner("ComunicWeb.components.conversations.service.emptyCache"); \ No newline at end of file