Update the number of notifications

This commit is contained in:
Pierre 2018-02-19 09:39:00 +01:00
parent 26bbf1043e
commit f97826752a
5 changed files with 91 additions and 0 deletions

View File

@ -806,6 +806,20 @@ var ComunicWeb = {
//TODO : implement //TODO : implement
},
/**
* Notification refresh service
*/
service: {
//TODO : implement
},
/**
* Notifications interface
*/
interface: {
//TODO : implement
} }
} }

View File

@ -53,6 +53,8 @@ ComunicWeb.components.notifications.dropdown = {
innerHTML: "Notifications" innerHTML: "Notifications"
}); });
//Initialize service
ComunicWeb.components.notifications.service.init(notificationsNumber, true);
}, },
} }

View File

@ -0,0 +1,25 @@
/**
* Notifications interface
*
* @author Pierre HUBERT
*/
ComunicWeb.components.notifications.interface = {
/**
* Get the number of unread notifications
*
* @param {function} callback
*/
getNbUnreads: function(callback){
//Perform API request
var apiURI = "notifications/count_unread";
var params = {};
//Perform the request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback);
}
}

View File

@ -0,0 +1,48 @@
/**
* 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
*/
init: function(target, auto_hide){
//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
ComunicWeb.components.notifications.interface.getNbUnreads(function(response){
//Continue in case of success
if(response.error)
return;
//Update the target
target.innerHTML = response.number;
//If the number of notifications equals 0, hide the target if required
if(response.number == 0 && auto_hide)
target.style.display = "none";
else
target.style.display = "block";
});
}, 2000);
},
}

View File

@ -258,6 +258,8 @@ class Dev {
//Notifications //Notifications
"js/components/notifications/dropdown.js", "js/components/notifications/dropdown.js",
"js/components/notifications/service.js",
"js/components/notifications/interface.js",
//User scripts //User scripts
"js/user/loginTokens.js", "js/user/loginTokens.js",