mirror of
				https://github.com/pierre42100/ComunicWeb
				synced 2025-11-03 19:54:14 +00:00 
			
		
		
		
	Update the number of notifications
This commit is contained in:
		@@ -53,6 +53,8 @@ ComunicWeb.components.notifications.dropdown = {
 | 
			
		||||
			innerHTML: "Notifications"
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		//Initialize service
 | 
			
		||||
		ComunicWeb.components.notifications.service.init(notificationsNumber, true);
 | 
			
		||||
	},
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										25
									
								
								assets/js/components/notifications/interface.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								assets/js/components/notifications/interface.js
									
									
									
									
									
										Normal 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);
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										48
									
								
								assets/js/components/notifications/service.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								assets/js/components/notifications/service.js
									
									
									
									
									
										Normal 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);
 | 
			
		||||
 | 
			
		||||
	},
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user