diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index 438b6a43..a074a6c3 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -806,6 +806,20 @@ var ComunicWeb = { //TODO : implement + }, + + /** + * Notification refresh service + */ + service: { + //TODO : implement + }, + + /** + * Notifications interface + */ + interface: { + //TODO : implement } } diff --git a/assets/js/components/notifications/dropdown.js b/assets/js/components/notifications/dropdown.js index 45094169..03b3f148 100644 --- a/assets/js/components/notifications/dropdown.js +++ b/assets/js/components/notifications/dropdown.js @@ -53,6 +53,8 @@ ComunicWeb.components.notifications.dropdown = { innerHTML: "Notifications" }); + //Initialize service + ComunicWeb.components.notifications.service.init(notificationsNumber, true); }, } \ No newline at end of file diff --git a/assets/js/components/notifications/interface.js b/assets/js/components/notifications/interface.js new file mode 100644 index 00000000..a4437b68 --- /dev/null +++ b/assets/js/components/notifications/interface.js @@ -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); + + } + +} \ No newline at end of file diff --git a/assets/js/components/notifications/service.js b/assets/js/components/notifications/service.js new file mode 100644 index 00000000..898cd010 --- /dev/null +++ b/assets/js/components/notifications/service.js @@ -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); + + }, + +} \ No newline at end of file diff --git a/system/config/dev.config.php b/system/config/dev.config.php index 682421ee..81cfdc3f 100644 --- a/system/config/dev.config.php +++ b/system/config/dev.config.php @@ -258,6 +258,8 @@ class Dev { //Notifications "js/components/notifications/dropdown.js", + "js/components/notifications/service.js", + "js/components/notifications/interface.js", //User scripts "js/user/loginTokens.js",