2018-02-19 08:39:00 +00:00
|
|
|
/**
|
|
|
|
* Notifications service
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
ComunicWeb.components.notifications.service = {
|
|
|
|
|
2018-11-24 17:54:02 +00:00
|
|
|
/**
|
|
|
|
* Last known number of notifications
|
|
|
|
*/
|
|
|
|
last_notifs_number: -1,
|
|
|
|
|
2018-02-19 08:39:00 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2019-01-10 13:59:12 +00:00
|
|
|
var interval = setInterval(function(){
|
2018-02-19 08:39:00 +00:00
|
|
|
|
|
|
|
//Auto-remove interval if the target has been removed
|
2018-11-24 15:01:45 +00:00
|
|
|
if(!target.isConnected){
|
|
|
|
ComunicWeb.common.pageTitle.setNotificationsNumber(0);
|
2019-01-10 13:59:12 +00:00
|
|
|
ComunicWeb.components.notifications.service.last_notifs_number = -1;
|
2018-02-19 08:39:00 +00:00
|
|
|
return clearInterval(interval);
|
2018-11-24 15:01:45 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 08:39:00 +00:00
|
|
|
|
|
|
|
//Get the number of notifications from the API
|
2019-01-10 13:59:12 +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
|
2018-02-28 15:47:32 +00:00
|
|
|
if(response.conversations == 0 && auto_hide)
|
|
|
|
target_conversations.style.display = "none";
|
2018-02-21 15:37:02 +00:00
|
|
|
else
|
2018-02-28 15:47:32 +00:00
|
|
|
target_conversations.style.display = "block";
|
2018-02-21 15:37:02 +00:00
|
|
|
|
|
|
|
}
|
2018-02-19 08:39:00 +00:00
|
|
|
|
2018-11-24 17:54:02 +00:00
|
|
|
//Sum notification number
|
2019-01-10 13:59:12 +00:00
|
|
|
var total_number_notifs = response.notifications + response.conversations;
|
2018-11-24 17:54:02 +00:00
|
|
|
|
2018-11-24 15:01:45 +00:00
|
|
|
//Update page title too
|
2018-11-24 17:54:02 +00:00
|
|
|
ComunicWeb.common.pageTitle.setNotificationsNumber(total_number_notifs);
|
|
|
|
|
|
|
|
//Play song if required
|
2019-01-10 13:59:12 +00:00
|
|
|
if(ComunicWeb.components.notifications.service.last_notifs_number != -1
|
|
|
|
&& total_number_notifs > ComunicWeb.components.notifications.service.last_notifs_number)
|
2018-11-24 17:54:02 +00:00
|
|
|
ComunicWeb.components.notifications.song.play();
|
|
|
|
|
2019-01-10 13:59:12 +00:00
|
|
|
ComunicWeb.components.notifications.service.last_notifs_number = total_number_notifs;
|
2018-02-19 08:39:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|