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

225 lines
5.0 KiB
JavaScript
Raw Permalink Normal View History

2018-05-13 12:06:32 +00:00
/**
* Conversations list pane
*
* @author Pierre HUBERT
*/
2021-03-05 14:41:31 +00:00
const ConversationsPageListPane = {
2018-05-13 12:06:32 +00:00
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
*/
2020-04-09 09:36:32 +00:00
display: function(target){
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);
2021-03-05 14:41:31 +00:00
ConversationsPageListPane._curr_list = null;
2018-05-16 15:52:42 +00:00
return;
}
//Load the list of conversations
2021-03-05 14:41:31 +00:00
ConversationsPageListPane.refresh_list(boxBody);
2018-05-16 15:52:42 +00:00
}, 5000);
2018-05-20 12:39:55 +00:00
//Force load the list of conversations
2021-03-05 14:41:31 +00:00
ConversationsPageListPane._curr_list = null;
ConversationsPageListPane.refresh_list(boxBody);
2018-05-16 15:52:42 +00:00
},
/**
* Refresh the list of conversations
*
* @param {HTMLElement} target The target for the list
*/
2020-04-09 09:36:32 +00:00
refresh_list: function(target){
//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
2021-03-05 14:41:31 +00:00
ConversationsInterface.getList((result) => {
2018-05-13 12:36:40 +00:00
//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
2021-03-05 14:41:31 +00:00
if(JSON.stringify(ConversationsPageListPane._curr_list) == JSON.stringify(result))
2018-05-16 15:52:42 +00:00
return;
2021-03-05 14:41:31 +00:00
ConversationsPageListPane._curr_list = result;
2018-05-16 15:52:42 +00:00
emptyElem(target); //Remove any previously shown list
2018-05-13 12:36:40 +00:00
//Display the list of conversations
2021-03-05 14:41:31 +00:00
ConversationsPageListPane._display_list(target, result);
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
*/
2020-04-09 09:36:32 +00:00
_display_list: function(target, list){
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"
});
//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
2020-04-09 09:36:32 +00:00
this._display_entry(conversationsContainer, conversation);
2018-05-13 12:36:40 +00:00
}
}
//Enable slimscroll
ComunicWeb.pages.conversations.utils.enableSlimScroll(
conversationsContainer,
ComunicWeb.pages.conversations.utils.getAvailableHeight(),
2020-04-09 09:36:32 +00:00
0);
2018-05-13 12:36:40 +00:00
},
/**
* Display a single conversation entry
*
* @param {HTMLElement} target The target for the conversation
2021-03-05 14:41:31 +00:00
* @param {Conversation} info Information about the conversation to display
2018-05-13 12:36:40 +00:00
*/
2020-04-09 09:36:32 +00:00
_display_entry: function(target, info) {
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
2021-03-05 14:41:31 +00:00
ConversationsPageListPane._curr_list = {};
2018-05-16 15:52:42 +00:00
//Make the choice visible
convLink.className += " selected";
//Open the conversation
2021-03-05 14:41:31 +00:00
openPage("conversations/"+info.id)
});
2018-05-13 12:36:40 +00:00
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",
2021-03-05 14:41:31 +00:00
innerHTML: ComunicWeb.common.date.timeDiffToStr(info.last_activity)
2018-05-13 12:36:40 +00:00
});
//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..."
});
2021-03-05 14:41:31 +00:00
ConversationsUtils.getName(info, function(name){
2018-05-13 12:36:40 +00:00
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",
2021-03-05 14:41:31 +00:00
innerHTML: (info.members.length === 1 ? tr("1 member") : tr("%1% members", {"1": info.members.length}))
2018-05-13 12:36:40 +00:00
});
2018-05-13 12:06:32 +00:00
}
}