ComunicWeb/assets/js/components/conversations/unreadDropdown.js

245 lines
5.8 KiB
JavaScript
Raw Normal View History

/**
* List of unread conversations dropdown
*
* @author Pierre HUERT
*/
ComunicWeb.components.conversations.unreadDropdown = {
/**
* Display unread conversations dropdown
*
* @param {HTMLElement} target The target of the conversations dropdown
* @return {HTMLElement} The HTML element that contains the number of unread conversations
*/
display_dropdown: function(target){
//Create the button
var dropdown = createElem2({
appendTo: target,
type: "li",
class: "dropdown messages-menu new-conversations-dropdown"
});
//Add dropdown toggle
var dropdownToggle = createElem2({
appendTo: dropdown,
type: "a",
class: "dropdown-toggle",
href: "#",
innerHTML: '<i class="fa fa-comments-o"></i>'
});
dropdownToggle.setAttribute("data-toggle", "dropdown");
//Add conversations number
var conversationsNumber = createElem2({
appendTo: dropdownToggle,
type: "span",
class: "label label-danger",
innerHTML: "0"
});
//Add dropdown menu
var dropdownMenu = createElem2({
appendTo: dropdown,
type: "ul",
class: "dropdown-menu"
});
//Add dropdown header
var dropdownHeader = createElem2({
appendTo: dropdownMenu,
type: "li",
class: "header",
2018-08-04 07:58:40 +00:00
innerHTML: lang("conversations_dropdown_header")
});
//Add conversations list
2018-03-25 07:43:39 +00:00
var conversationsListContainer = createElem2({
appendTo: dropdownMenu,
type: "li"
});
var conversationsList = createElem2({
2018-03-25 07:43:39 +00:00
appendTo: conversationsListContainer,
type: "ul",
class: "menu"
});
//Add dropdown bottom
var dropdownBottom = createElem2({
appendTo: dropdownMenu,
type: "li",
class: "footer"
});
//Add a button to offer the user to delete all his conversations
var openConversations = createElem2({
appendTo: dropdownBottom,
type: "a",
href: "#",
innerHTML: " "
});
//Enable slimscroll
$(conversationsList).slimScroll({
height: '100%'
});
//Refresh the unread conversations list if the user click the dropdown button
dropdownToggle.onclick = function(){
ComunicWeb.components.conversations.unreadDropdown.refresh_list_conversations(conversationsList);
}
//Return the number of conversations target
return conversationsNumber;
},
/**
* Refresh the list of conversations
*
* @param {HTMLElement} target The target to display the conversations
*/
2021-03-05 13:37:22 +00:00
refresh_list_conversations: async function(target) {
2021-03-05 13:37:22 +00:00
try {
2021-03-05 13:37:22 +00:00
//Perform a request through the interface
const list = await ConversationsInterface.getUnreadConversations();
//Get the list of users ID
2021-03-05 13:37:22 +00:00
let usersID = new Set();
//Process the list of conversations
2021-03-05 13:37:22 +00:00
for (let entry of list) {
ConversationsUtils.getUsersIDForMessage(entry.message).forEach(id => usersID.add(id))
}
2021-03-05 13:37:22 +00:00
const users = await getUsers([...usersID]);
2021-03-05 13:37:22 +00:00
this._display_list(target, list, users);
}
2021-03-05 13:37:22 +00:00
catch(e) {
console.error(e);
ComunicWeb.common.notificationSystem.showNotification(lang("conversations_dropdown_err_get_list"), "danger");
}
},
/**
* Display the list of conversations
*
* @param {HTMLElement} target The target for the fields
2021-03-05 13:37:22 +00:00
* @param {UnreadConversation[]} conversations The list of conversations
* @param {UsersList} usersInfo Information about related users
*/
2021-03-05 13:37:22 +00:00
_display_list: function(target, conversations, usersInfo){
//Empty the target
target.innerHTML = "";
//Process each conversation
2021-03-05 13:37:22 +00:00
for (let conversation of conversations) {
//Get informations about the user
2021-03-05 13:37:22 +00:00
const user = usersInfo.get(ConversationsUtils.getMainUserForMessage(conversation.message))
//Create list element
var convLi = createElem2({
appendTo: target,
type: "li"
});
//Create the conversation link element
var convLink = createElem2({
appendTo: convLi,
2021-03-05 13:37:22 +00:00
type: "a"
});
2021-03-05 13:37:22 +00:00
convLink.setAttribute("data-conversation-id", conversation.conv.id);
//Add left elements
var leftElems = createElem2({
appendTo: convLink,
type: "div",
class: "pull-left"
});
//Add user account image on the left
var userAccountImage = createElem2({
appendTo: leftElems,
type: "img",
class: "img-circle",
2021-03-05 13:37:22 +00:00
src: conversation.conv.logo ? conversation.conv.logo : user.image
});
2018-02-25 17:24:19 +00:00
//Add item top informations
var liTop = createElem2({
appendTo: convLink,
type: "h4",
2021-03-05 13:37:22 +00:00
innerHTML: user.fullName
2018-02-25 17:24:19 +00:00
});
//Add item top small informations
var liTopSmall = createElem2({
appendTo: liTop,
type: "small"
});
//Add the message post time
2021-03-05 13:37:22 +00:00
createElem2({
2018-02-25 17:24:19 +00:00
appendTo: liTopSmall,
type: "span",
2021-03-05 13:37:22 +00:00
innerHTML: '<i class="fa fa-clock-o"></i> ' + ComunicWeb.common.date.timeDiffToStr(conversation.conv.last_activity) + " ago"
2018-02-25 17:24:19 +00:00
});
//Add conversation name (if available)
2021-03-05 13:37:22 +00:00
if(conversation.conv.name != "" && conversation.conv.name != null){
2018-02-25 17:24:19 +00:00
2021-03-05 13:37:22 +00:00
createElem2({
2018-02-25 17:24:19 +00:00
appendTo: convLink,
type: "p",
2021-03-05 13:37:22 +00:00
innerHTML: conversation.conv.name,
2018-02-25 17:24:19 +00:00
});
}
2021-03-05 13:37:22 +00:00
// Add the message
if (conversation.message.message) {
createElem2({
appendTo: convLink,
type: "p",
class: "message-content",
innerHTML: removeHtmlTags(conversation.message.message)
});
}
// In case of server message
if (conversation.message.server_message) {
createElem2({
appendTo: convLink,
type: "p",
class: "message-content",
innerHTML: ConversationsUtils.getServerMessage(conversation.message, usersInfo)
});
}
2018-02-25 17:28:58 +00:00
//Make the conversation link lives
convLink.onclick = function(){
openConversation(this.getAttribute("data-conversation-id"));
}
}
2018-03-03 12:52:25 +00:00
//Display a specific message if there is not any new conversation
if(conversations.length == 0){
//Display a message in the target
createElem2({
appendTo: target,
type: "p",
class: "no-unread-conversation-msg",
2018-08-04 07:58:40 +00:00
innerHTML: lang("conversations_dropdown_no_unread_notice")
2018-03-03 12:52:25 +00:00
});
}
},
}