ComunicWeb/assets/js/components/notifications/dropdown.js

131 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-02-18 18:12:17 +00:00
/**
* Notifications menu bar dropdown
*
* @author Pierre HUBERT
*/
ComunicWeb.components.notifications.dropdown = {
/**
* Display notifications dropdown
*
* @param {HTMLElement} target The target of the notification dropdown
*/
display: function(target){
//Create the button
var dropdown = createElem2({
appendTo: target,
type: "li",
2018-02-19 14:58:33 +00:00
class: "dropdown messages-menu"
2018-02-18 18:12:17 +00:00
});
//Add dropdown toggle
var dropdownToggle = createElem2({
appendTo: dropdown,
type: "a",
class: "dropdown-toggle",
href: "#",
innerHTML: '<i class="fa fa-bell-o"></i>'
});
2018-02-18 18:16:48 +00:00
dropdownToggle.setAttribute("data-toggle", "dropdown");
2018-02-18 18:12:17 +00:00
//Add notification number
var notificationsNumber = createElem2({
appendTo: dropdownToggle,
type: "span",
class: "label label-danger",
innerHTML: "0"
});
//Add dropdown menu
2018-02-18 18:16:48 +00:00
var dropdownMenu = createElem2({
appendTo: dropdown,
type: "ul",
class: "dropdown-menu"
});
//Add dropdown header
var dropdownHeader = createElem2({
appendTo: dropdownMenu,
type: "li",
class: "header",
innerHTML: "Notifications"
});
2018-02-18 18:12:17 +00:00
2018-02-19 08:46:51 +00:00
//Add notifications list
2018-02-19 14:58:33 +00:00
var notificationsListContener = createElem2({
2018-02-19 08:46:51 +00:00
appendTo: dropdownMenu,
2018-02-19 14:58:33 +00:00
type: "li"
});
var notificationsList = createElem2({
appendTo: notificationsListContener,
2018-02-19 08:46:51 +00:00
type: "ul",
class: "menu"
});
//Enable slimscroll
$(notificationsList).slimScroll({
height: '100%'
});
2018-02-19 08:39:00 +00:00
//Initialize service
ComunicWeb.components.notifications.service.init(notificationsNumber, true);
2018-02-19 08:46:51 +00:00
//Refresh the notifications list if the user click the dropdown button
dropdownToggle.onclick = function(){
ComunicWeb.components.notifications.dropdown.refresh_list(notificationsList);
}
2018-02-18 18:12:17 +00:00
},
2018-02-19 08:46:51 +00:00
/**
* Refresh the list of notifications
*
* @param {HTMLElement} list The notifications list to refresh
*/
refresh_list: function(list){
2018-02-19 08:46:51 +00:00
//Perform a request on the API
ComunicWeb.components.notifications.interface.get_list_unread(function(result){
//Check for errors
if(result.error){
ComunicWeb.common.notificationSystem.showNotification("An error occured while trying to retrieve notifications list !", "danger");
return;
}
2018-02-19 14:58:33 +00:00
//Get the list of required users informations
var users_id = ComunicWeb.components.notifications.utils.get_users_id(result);
//Get informations about the users
ComunicWeb.user.userInfos.getMultipleUsersInfos(users_id, function(users){
//Check for errors
if(users.error){
ComunicWeb.common.notificationSystem.showNotification("An error occured while trying to retrieve users informations for the notifications !", "danger");
return;
}
//Empty the target list
list.innerHTML = "";
//Process the list of notifications
for (let i = 0; i < result.length; i++) {
const notification = result[i];
//Display the notification
ComunicWeb.components.notifications.ui.display_notification(notification, list, users);
}
}, false);
});
2018-02-19 08:46:51 +00:00
},
2018-02-19 14:58:33 +00:00
2018-02-18 18:12:17 +00:00
}