ComunicWeb/assets/js/pages/conversations/listPane.js

256 lines
6.1 KiB
JavaScript
Raw Normal View History

2018-05-13 12:06:32 +00:00
/**
* Conversations list pane
*
* @author Pierre HUBERT
*/
ComunicWeb.pages.conversations.listPane = {
2018-05-16 15:52:42 +00:00
/**
* Save current list
*/
_curr_list: {},
2018-05-13 12:06:32 +00:00
/**
* Display the list of conversation
*
* @param {HTMLElement} target The target of the page
* @param {Object} args Additional arguments
2018-05-13 12:06:32 +00:00
*/
display: function(target, args){
2018-05-13 12:06:32 +00:00
//Create the box
var listBox = createElem2({
appendTo: target,
type: "div",
class: "box box-solid conversations-list-box"
2018-05-13 12:06:32 +00:00
});
//Box header
var boxHeader = createElem2({
appendTo: listBox,
type: "div",
class: "box-header with-border"
});
//Box title
createElem2({
appendTo: boxHeader,
type: "h3",
class: "box-title",
innerHTML: "Conversations"
});
//Box body
var boxBody = createElem2({
appendTo: listBox,
type: "div",
class: "box-body no-padding"
});
2018-05-16 15:52:42 +00:00
var interval = setInterval(function(){
//Check if box body is still connected
if(!boxBody.isConnected){
clearInterval(interval);
ComunicWeb.pages.conversations.listPane._curr_list = null;
return;
}
//Load the list of conversations
ComunicWeb.pages.conversations.listPane.refresh_list(boxBody, args);
}, 5000);
2018-05-13 12:36:40 +00:00
//Load the list of conversations
2018-05-16 15:52:42 +00:00
ComunicWeb.pages.conversations.listPane.refresh_list(boxBody, args);
},
/**
* Refresh the list of conversations
*
* @param {HTMLElement} target The target for the list
* @param {Object} args Additionnal arguments
*/
refresh_list: function(target, args){
//Loading message, if required
if(target.childElementCount == 0){
var loadingMsg = createElem2({
appendTo: target,
type: "div",
class: "conv-list-loading-msg",
innerHTML: "Loading, please wait..."
});
}
else
//Create empty division
var loadingMsg = document.createElement("div");
//Perform a request over the interface
2018-05-13 12:36:40 +00:00
ComunicWeb.components.conversations.interface.getList(function(result){
//Check for errors
if(result.error){
loadingMsg.innerHTML = "An error occured !";
return;
}
//Remove loading message
loadingMsg.remove();
2018-05-16 15:52:42 +00:00
//Check if it is required to apply new list
if(JSON.stringify(ComunicWeb.pages.conversations.listPane._curr_list) == JSON.stringify(result))
return;
ComunicWeb.pages.conversations.listPane._curr_list = result;
emptyElem(target); //Remove any previously shown list
2018-05-13 12:36:40 +00:00
//Display the list of conversations
ComunicWeb.pages.conversations.listPane._display_list(target, result, args);
2018-05-13 12:36:40 +00:00
});
2018-05-13 12:36:40 +00:00
},
/**
* Display the list of conversations
*
* @param {HTMLElement} target The target for the list
* @param {Object} list The list of conversations
* @param {Object} args Additional arguments
2018-05-13 12:36:40 +00:00
*/
_display_list: function(target, list, args){
2018-05-13 12:36:40 +00:00
//Create the conversations container
var conversationsContainer = createElem2({
appendTo: target,
type: "ul",
class: "nav nav-pills nav-stacked"
});
2018-05-16 15:52:42 +00:00
//Make sure there isn't any selected conversation currently
args.selected_conversation = null;
2018-05-13 12:36:40 +00:00
//Process the list of conversations
2018-05-14 14:58:23 +00:00
for (var num in list) {
2018-05-13 12:36:40 +00:00
if (list.hasOwnProperty(num)) {
2018-05-14 14:58:23 +00:00
var conversation = list[num];
2018-05-13 12:36:40 +00:00
//Display conversation element
2018-05-16 15:52:42 +00:00
args = this._display_entry(conversationsContainer, conversation, args);
2018-05-13 12:36:40 +00:00
}
}
//Enable slimscroll
ComunicWeb.pages.conversations.utils.enableSlimScroll(
conversationsContainer,
ComunicWeb.pages.conversations.utils.getAvailableHeight(),
0);
2018-05-13 12:36:40 +00:00
2018-05-16 15:52:42 +00:00
//Scroll to selected conversation, if possible
if(args.selected_conversation != null){
var newScrollPos = args.selected_conversation.offsetTop - 30;
if(newScrollPos < 0)
newScrollPos = 0;
//Enable slimscroll again
ComunicWeb.pages.conversations.utils.enableSlimScroll(
conversationsContainer,
ComunicWeb.pages.conversations.utils.getAvailableHeight() + 55,
newScrollPos);
2018-05-16 15:52:42 +00:00
}
2018-05-13 12:36:40 +00:00
},
/**
* Display a single conversation entry
*
* @param {HTMLElement} target The target for the conversation
* @param {Object} info Information about the conversation to display
* @param {Object} args Additional arguments
2018-05-16 15:52:42 +00:00
* @return Additional arguments (may be modified)
2018-05-13 12:36:40 +00:00
*/
_display_entry: function(target, info, args) {
2018-05-13 12:36:40 +00:00
//Create conversation container element
var convContainer = createElem2({
appendTo: target,
type: "li"
});
//Create conversation link element
var convLink = createElem2({
appendTo: convContainer,
type: "a"
});
convLink.addEventListener("click", function(e){
2018-05-16 15:52:42 +00:00
//Force conversation list refresh
ComunicWeb.pages.conversations.listPane._curr_list = {};
//Make the choice visible
convLink.className += " selected";
//Open the conversation
args.opener(info.ID);
});
2018-05-13 12:36:40 +00:00
2018-05-16 04:47:41 +00:00
//Check if it is the current conversation
2018-05-16 15:52:42 +00:00
if(args.getCurrentID() == info.ID){
convLink.className += " selected";
//Save selected conversation link
args.selected_conversation = convContainer;
}
2018-05-16 04:47:41 +00:00
2018-05-13 12:36:40 +00:00
//Add conversation last activity on the rigth
var lastActivityContainer = createElem2({
appendTo: convLink,
type: "small",
class: "pull-right last-activity",
innerHTML: "<i class='fa fa-clock-o'></i> "
});
//Add last activity
createElem2({
appendTo: lastActivityContainer,
type: "span",
innerHTML: ComunicWeb.common.date.timeDiffToStr(info.last_active)
});
//Add conversation name
var conversationName = createElem2({
appendTo: convLink,
type: info.saw_last_message ? "span" : "strong",
2018-05-13 12:36:40 +00:00
innerHTML: "Loading..."
});
ComunicWeb.components.conversations.utils.getName(info, function(name){
conversationName.innerHTML = name;
});
//Append the number of members of the conversation
var membersNumberContainer = createElem2({
appendTo: convLink,
type: "p",
class: "number-members-conversation"
});
var membersNumberContainerSmall = createElem2({
appendTo: membersNumberContainer,
type: "small",
innerHTML: "<i class='fa fa-users'></i> "
});
createElem2({
appendTo: membersNumberContainerSmall,
type: "span",
innerHTML: (info.members.length === 1 ? "1 member" : info.members.length + " members")
});
2018-05-16 15:52:42 +00:00
//Return the list of arguments
return args;
2018-05-13 12:06:32 +00:00
}
}