diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index 1cc0b42b..5d12dfe7 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -565,6 +565,13 @@ var ComunicWeb = { */ service:{ //TODO : implement + }, + + /** + * Conversations utilities + */ + utils:{ + //TODO : implement } }, diff --git a/assets/js/components/conversations/cachingOpened.js b/assets/js/components/conversations/cachingOpened.js index f5f012d6..7629d694 100644 --- a/assets/js/components/conversations/cachingOpened.js +++ b/assets/js/components/conversations/cachingOpened.js @@ -21,10 +21,10 @@ ComunicWeb.components.conversations.cachingOpened = { //Add new conversation (if required) if(!conversations.includes(conversationID.toString())){ conversations.push(conversationID); - + //Convert into string var conversationsString = conversations.join(";"); - + //Save the new values sessionStorage.setItem(this.__varName, conversationsString); } diff --git a/assets/js/components/conversations/chatWindows.js b/assets/js/components/conversations/chatWindows.js index 1ee9bf1a..673c870a 100644 --- a/assets/js/components/conversations/chatWindows.js +++ b/assets/js/components/conversations/chatWindows.js @@ -39,4 +39,28 @@ ComunicWeb.components.conversations.chatWindows = { 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; + + //Success + return true; + } } \ No newline at end of file diff --git a/assets/js/components/conversations/interface.js b/assets/js/components/conversations/interface.js index 664b8525..02f43162 100644 --- a/assets/js/components/conversations/interface.js +++ b/assets/js/components/conversations/interface.js @@ -9,7 +9,7 @@ ComunicWeb.components.conversations.interface = { /** * @var {Object} __conversationsList Cached list of conversations */ - __conversationsList: false, + __conversationsList: {}, /** * Get and return the list of available conversations @@ -98,4 +98,48 @@ ComunicWeb.components.conversations.interface = { //Success return true; }, -} \ No newline at end of file + + /** + * Get informations about a unique conversation + * + * @param {Integer} conversationID The ID of the conversation + * @param {function} nextStep What to do once the operation is completed + * @param {Boolean} forceRefresh Force informations about the conversation to be fetched (ignore cached informations) + * @return {Boolan} True for a success + */ + getInfosOne: function(conversationID, nextStep, forceRefresh){ + + //First, if the conversation is available in the cache + if(!forceRefresh && this.__conversationsList['conversation-'+conversationID]){ + + //Perform next action now without getting fresh informations on the server + nextStep(this.__conversationsList['conversation-'+conversationID]); + + //Success + return true; + } + + //Else, perform an API request + //TODO : implement ON NEXT DEVELOPMENT SESSION + console.log("Please implement me !!!!!!"); + + //Success + return true; + }, + + /** + * Empty conversations cache + * + * @return {Boolean} True for a success + */ + emptyCache: function(){ + //Empty cache + clearObject(this.__conversationsList); + + //Success + return true; + } +} + +//Register conversations cache cleaning function +ComunicWeb.common.cacheManager.registerCacheCleaner("ComunicWeb.components.conversations.interface.emptyCache"); \ No newline at end of file diff --git a/assets/js/components/conversations/list.js b/assets/js/components/conversations/list.js index 053bf4c1..47dca4df 100644 --- a/assets/js/components/conversations/list.js +++ b/assets/js/components/conversations/list.js @@ -181,8 +181,8 @@ ComunicWeb.components.conversations.list = { //Remove the conversation box infos.listBox.rootElem.remove(); - //Open the conversation (under construction) - ComunicWeb.components.conversations.manager.openConversation({ + //Add & open the conversation + ComunicWeb.components.conversations.manager.addConversation({ conversationID: response.conversationID }); }) @@ -257,8 +257,8 @@ ComunicWeb.components.conversations.list = { //Remove conversations list listBox.rootElem.remove(); - //Open conversation - ComunicWeb.components.conversations.manager.openConversation({ + //Add & open conversation + ComunicWeb.components.conversations.manager.addConversation({ conversationID: conversationInfos.ID }); } @@ -278,44 +278,14 @@ ComunicWeb.components.conversations.list = { //Create the conversation name element var conversationNameElem = createElem("strong", linkElem); - //Check if the conversation has a name or not - if(conversationInfos.name) - conversationNameElem.innerHTML = conversationInfos.name; - else { + //Put conversation name field in a waiting state + conversationNameElem.style.fontSize = "90%"; + conversationNameElem.innerHTML = "Loading..."; - //Put conversation name field in a waiting state - conversationNameElem.style.fontSize = "90%"; - conversationNameElem.innerHTML = "Loading..."; - - //Get informations about the first two members - var firstMembers = []; - - //Retrieve IDs - for(o in conversationInfos.members){ - //Limit number to 2 - if(firstMembers.length < 2){ - - //Check this is a valid entry - if(conversationInfos.members[o]){ - - //Exclude current user ID - if(conversationInfos.members[o] != userID()) - firstMembers.push(conversationInfos.members[o]); - } - } - } - - //Get users informations - ComunicWeb.user.userInfos.getNames(firstMembers, function(usersName){ - - //For conversations with many members (more than 3 - we musn't forget current user) - if(conversationInfos.members.length > 3) - usersName += ", ..."; - - //Apply conversation name - conversationNameElem.innerHTML = usersName; - }); - } + //Get conversation name and apply it + ComunicWeb.components.conversations.utils.getName(conversationInfos, function(conversationName){ + conversationNameElem.innerHTML = conversationName; + }); //Add members number //Create paragraph diff --git a/assets/js/components/conversations/manager.js b/assets/js/components/conversations/manager.js index 1a5b38e7..bfecb07f 100644 --- a/assets/js/components/conversations/manager.js +++ b/assets/js/components/conversations/manager.js @@ -64,6 +64,15 @@ ComunicWeb.components.conversations.manager = { //First, add the "open a conversation" new this.addOpenConversationButton(conversationsContainerElem); + //Then, open any already active conversation + var openedConversations = ComunicWeb.components.conversations.cachingOpened.getAll(); + console.log(openedConversations); + //Process opened conversations + for(i in openedConversations){ + if(i < openedConversations.length) + this.openConversation(openedConversations[i]); + } + }, /** @@ -87,14 +96,13 @@ ComunicWeb.components.conversations.manager = { }, /** - * Open a conversation accordingly to specified informations + * Add a new conversation to the list of opened conversation accordingly to specified informations * * @param {Object} infos Informations about the conversation to open * @info {Integer} conversationID The ID of the conversation to open * @return {Boolean} True or false depending of the success of the operation */ - openConversation: function(infos){ - + addConversation: function(infos){ //We check if a conversation ID was specified or not if(infos.conversationID){ ComunicWeb.debug.logMessage("Open a conversation based on its ID"); @@ -112,18 +120,51 @@ ComunicWeb.components.conversations.manager = { return false; } + //Open the conversation + this.openConversation(conversationID); + + //Success + return true; + }, + + /** + * Open a conversation + * + * @param {Integer} conversationID The ID of the conversation to open + * @return {Boolean} True or false depending of the success of the operation + */ + openConversation: function(conversationID){ + //Log action ComunicWeb.debug.logMessage("Opening conversation " + conversationID); //Save conversation ID in session storage ComunicWeb.components.conversations.cachingOpened.add(conversationID); - //Create a conversation windows - ComunicWeb.components.conversations.chatWindows.create({ + //Create a conversation window + var conversationWindow = ComunicWeb.components.conversations.chatWindows.create({ target: byId(this.__conversationsContenerID), conversationID: conversationID }); + //Change conversation window name (loading state) + ComunicWeb.components.conversations.chatWindows.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"); + } + + //Change the name of the conversation + ComunicWeb.components.conversations.utils.getName(informations, function(conversationName){ + ComunicWeb.components.conversations.chatWindows.changeName(conversationName, conversationWindow); + }); + + }); + //Success return true; } diff --git a/assets/js/components/conversations/utils.js b/assets/js/components/conversations/utils.js new file mode 100644 index 00000000..20c3973c --- /dev/null +++ b/assets/js/components/conversations/utils.js @@ -0,0 +1,58 @@ +/** + * Conversations utilites + * + * @author Pierre HUBERT + */ + +ComunicWeb.components.conversations.utils = { + + /** + * Given conversation informations, returns its name + * + * @param {Object} infos Conversation informations + * @param {Function} afterName What to do once we got conversation name + * @return {Boolean} True for a success + */ + getName: function(infos, afterName){ + + //Check if the conversation has a name or not + if(infos.name) + afterName(infos.name); + else { + + //Get informations about the first two members + var firstMembers = []; + + //Retrieve IDs + for(o in infos.members){ + //Limit number to 2 + if(firstMembers.length < 2){ + + //Check this is a valid entry + if(infos.members[o]){ + + //Exclude current user ID + if(infos.members[o] != userID()) + firstMembers.push(infos.members[o]); + } + } + } + + //Get users informations + ComunicWeb.user.userInfos.getNames(firstMembers, function(usersName){ + + //For conversations with many members (more than 3 - we musn't forget current user) + if(infos.members.length > 3) + usersName += ", ..."; + + //Peform next action now + afterName(usersName); + }); + } + + //Success + return true; + } + + +} \ No newline at end of file diff --git a/corePage/config/dev.config.php b/corePage/config/dev.config.php index de3ea8ce..47e10092 100644 --- a/corePage/config/dev.config.php +++ b/corePage/config/dev.config.php @@ -123,6 +123,7 @@ $config['JSfiles'] = array( "%PATH_ASSETS%js/components/conversations/interface.js", "%PATH_ASSETS%js/components/conversations/service.js", "%PATH_ASSETS%js/components/conversations/cachingOpened.js", + "%PATH_ASSETS%js/components/conversations/utils.js", //User selector "%PATH_ASSETS%js/components/userSelect/userSelect.js",