2018-02-19 08:39:00 +00:00
|
|
|
/**
|
|
|
|
* Notifications service
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
ComunicWeb.components.notifications.service = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init the service
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} target The target that will receive
|
|
|
|
* the number of unread notifications
|
|
|
|
* @param {Bool} auto_hide Automatically hide the notifications
|
|
|
|
* number if there is not any new notification
|
2018-02-21 15:37:02 +00:00
|
|
|
* @param {HTMLElement} target_conversations Optionnal, defins the target
|
|
|
|
* for the number of conversations
|
2018-02-19 08:39:00 +00:00
|
|
|
*/
|
2018-02-21 15:37:02 +00:00
|
|
|
init: function(target, auto_hide, target_conversations){
|
2018-02-19 08:39:00 +00:00
|
|
|
|
|
|
|
//Initialize interval
|
|
|
|
var interval = setInterval(function(){
|
|
|
|
|
|
|
|
//Auto-remove interval if the target has been removed
|
|
|
|
if(!target.isConnected)
|
|
|
|
return clearInterval(interval);
|
|
|
|
|
|
|
|
//Get the number of notifications from the API
|
2018-02-21 15:27:10 +00:00
|
|
|
ComunicWeb.components.notifications.interface.getAllUnread(function(response){
|
2018-02-19 08:39:00 +00:00
|
|
|
|
|
|
|
//Continue in case of success
|
|
|
|
if(response.error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//Update the target
|
2018-02-21 15:27:10 +00:00
|
|
|
target.innerHTML = response.notifications;
|
2018-02-19 08:39:00 +00:00
|
|
|
|
|
|
|
//If the number of notifications equals 0, hide the target if required
|
2018-02-21 15:27:10 +00:00
|
|
|
if(response.notifications == 0 && auto_hide)
|
2018-02-19 08:39:00 +00:00
|
|
|
target.style.display = "none";
|
|
|
|
else
|
|
|
|
target.style.display = "block";
|
2018-02-21 15:37:02 +00:00
|
|
|
|
|
|
|
//Update the number of conversations if possible too
|
|
|
|
if(target_conversations){
|
|
|
|
|
|
|
|
//Update the target
|
|
|
|
target_conversations.innerHTML = response.conversations;
|
|
|
|
|
|
|
|
//If the number of notifications equals 0, hide the target if required
|
|
|
|
if(response.notifications == 0 && auto_hide)
|
|
|
|
target_conversations.style.display = "none";
|
|
|
|
else
|
|
|
|
target_conversations.style.display = "block";
|
|
|
|
|
|
|
|
}
|
2018-02-19 08:39:00 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|