From 46bb22b17b5459f100cafabe9d1875b0e134c010 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 24 Nov 2018 16:01:45 +0100 Subject: [PATCH] Display the number of notifications on page title. --- assets/js/common/functionsSchema.js | 16 ++++++ assets/js/common/page.js | 2 +- assets/js/common/pageTitle.js | 53 +++++++++++++++++++ assets/js/components/notifications/service.js | 7 ++- assets/js/pages/groups/pages/create.js | 2 +- assets/js/pages/groups/pages/forbidden.js | 2 +- assets/js/pages/groups/pages/group.js | 2 +- assets/js/pages/groups/pages/members.js | 2 +- assets/js/pages/groups/pages/settings.js | 2 +- assets/js/pages/home/landingPage.js | 2 +- assets/js/pages/logout.js | 3 ++ assets/js/pages/settings/main.js | 2 +- assets/js/pages/userPage/accessForbidden.js | 2 +- assets/js/pages/userPage/main.js | 2 +- system/config/dev.config.php | 1 + 15 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 assets/js/common/pageTitle.js diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index e4790364..1c2ada5a 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -228,6 +228,22 @@ var ComunicWeb = { getAndShowJSONtemplate: function(targetElem, templateURI, additionalData, afterParsingJSONtemplate, cleanContainer){}, }, + /** + * Page title management + */ + pageTitle: { + + /** + * Set a new title to the page + */ + setTitle: function(title){}, + + /** + * Set new number of notifications + */ + setNotificationsNumber: function(number){} + }, + /** * Functions to check data input in forms */ diff --git a/assets/js/common/page.js b/assets/js/common/page.js index d006be26..012eacf6 100644 --- a/assets/js/common/page.js +++ b/assets/js/common/page.js @@ -196,7 +196,7 @@ ComunicWeb.common.page = { } //Change page title - document.title = pageInfos.pageTitle; + ComunicWeb.common.pageTitle.setTitle(pageInfos.pageTitle); //Change page URL, if required if(additionnalData.no_url_update ? !additionnalData.no_url_update : true) diff --git a/assets/js/common/pageTitle.js b/assets/js/common/pageTitle.js new file mode 100644 index 00000000..29d14280 --- /dev/null +++ b/assets/js/common/pageTitle.js @@ -0,0 +1,53 @@ +/** + * Page title management + * + * @author Pierre HUBERT + */ + +ComunicWeb.common.pageTitle = { + + /** + * Current page title + */ + _curr_title: "Comunic", + + /** + * Current number of notifications + */ + _curr_notifications_number: 0, + + /** + * Set a new title to the page + * + * @param {string} title The new title for the page + */ + setTitle: function(title){ + this._curr_title = title; + this.__refresh(); + }, + + /** + * Set new number of notifications + * + * @param {number} number The new number of notifications + */ + setNotificationsNumber: function(number){ + this._curr_notifications_number = number; + this.__refresh(); + }, + + /** + * Refresh document title + */ + __refresh: function(){ + let title = ""; + + if(this._curr_notifications_number > 0) + title += "(" + this._curr_notifications_number + ") "; + + title += this._curr_title; + + document.title = title; + } + +} \ No newline at end of file diff --git a/assets/js/components/notifications/service.js b/assets/js/components/notifications/service.js index 5f4cbce8..224eca8b 100644 --- a/assets/js/components/notifications/service.js +++ b/assets/js/components/notifications/service.js @@ -22,8 +22,11 @@ ComunicWeb.components.notifications.service = { var interval = setInterval(function(){ //Auto-remove interval if the target has been removed - if(!target.isConnected) + if(!target.isConnected){ + ComunicWeb.common.pageTitle.setNotificationsNumber(0); return clearInterval(interval); + } + //Get the number of notifications from the API ComunicWeb.components.notifications.interface.getAllUnread(function(response){ @@ -55,6 +58,8 @@ ComunicWeb.components.notifications.service = { } + //Update page title too + ComunicWeb.common.pageTitle.setNotificationsNumber(response.notifications + response.conversations); }); }, 2000); diff --git a/assets/js/pages/groups/pages/create.js b/assets/js/pages/groups/pages/create.js index e152612b..1c8f99a3 100644 --- a/assets/js/pages/groups/pages/create.js +++ b/assets/js/pages/groups/pages/create.js @@ -14,7 +14,7 @@ ComunicWeb.pages.groups.pages.create = { open: function(target){ //Update page title - document.title = "Create a group"; + ComunicWeb.common.pageTitle.setTitle("Create a group"); //Create page container var pageContainer = createElem2({ diff --git a/assets/js/pages/groups/pages/forbidden.js b/assets/js/pages/groups/pages/forbidden.js index 8f550e62..82b4391d 100644 --- a/assets/js/pages/groups/pages/forbidden.js +++ b/assets/js/pages/groups/pages/forbidden.js @@ -62,7 +62,7 @@ ComunicWeb.pages.groups.pages.forbidden = { display: function(id, result, target){ //Update page title - document.title = result.name; + ComunicWeb.common.pageTitle.setTitle(result.name); //Create a box to contain information about registration var box = createElem2({ diff --git a/assets/js/pages/groups/pages/group.js b/assets/js/pages/groups/pages/group.js index 2a8deea4..430839a2 100644 --- a/assets/js/pages/groups/pages/group.js +++ b/assets/js/pages/groups/pages/group.js @@ -49,7 +49,7 @@ ComunicWeb.pages.groups.pages.group = { display: function(id, info, target){ //Update page title - document.title = info.name; + ComunicWeb.common.pageTitle.setTitle(info.name); //Create page row var pageRow = createElem2({ diff --git a/assets/js/pages/groups/pages/members.js b/assets/js/pages/groups/pages/members.js index be16019e..7a46e6dd 100644 --- a/assets/js/pages/groups/pages/members.js +++ b/assets/js/pages/groups/pages/members.js @@ -61,7 +61,7 @@ ComunicWeb.pages.groups.pages.members = { */ applyGroupInfo: function(id, info, target){ - document.title = info.name + " - Members"; + ComunicWeb.common.pageTitle.setTitle(info.name + " - Members"); //Append the title of the group createElem2({ diff --git a/assets/js/pages/groups/pages/settings.js b/assets/js/pages/groups/pages/settings.js index cc8ea5c6..fbbfb532 100644 --- a/assets/js/pages/groups/pages/settings.js +++ b/assets/js/pages/groups/pages/settings.js @@ -92,7 +92,7 @@ ComunicWeb.pages.groups.pages.settings = { */ display: function(id, settings, target){ - document.title = settings.name + " - Settings"; + ComunicWeb.common.pageTitle.setTitle(settings.name + " - Settings"); //Create form container var formContainer = createElem2({ diff --git a/assets/js/pages/home/landingPage.js b/assets/js/pages/home/landingPage.js index 25d6e66c..25107918 100644 --- a/assets/js/pages/home/landingPage.js +++ b/assets/js/pages/home/landingPage.js @@ -14,7 +14,7 @@ ComunicWeb.pages.home.landingPage = { ComunicWeb.debug.logMessage("Open home landing page."); //Change page title - document.title = "Comunic, a transparent social network"; + ComunicWeb.common.pageTitle.setTitle("Comunic, a transparent social network"); //Prepare additional data var additionalData = { diff --git a/assets/js/pages/logout.js b/assets/js/pages/logout.js index 35b21963..9d1cec30 100644 --- a/assets/js/pages/logout.js +++ b/assets/js/pages/logout.js @@ -20,6 +20,9 @@ ComunicWeb.pages.logout = { //Perform logout ComunicWeb.user.userLogin.logoutUser(); + //Reset notifications number + ComunicWeb.common.pageTitle.setNotificationsNumber(0); + //Clean all caches ComunicWeb.common.system.reset(true, "home"); diff --git a/assets/js/pages/settings/main.js b/assets/js/pages/settings/main.js index 5e1044b9..fce4fbd2 100644 --- a/assets/js/pages/settings/main.js +++ b/assets/js/pages/settings/main.js @@ -52,7 +52,7 @@ ComunicWeb.pages.settings.main = { var section = ComunicWeb.pages.settings.sectionsList[section]; //Update document title - document.title += " - " + section.title; + ComunicWeb.common.pageTitle.setTitle("Settings - " + section.title); //Call handler eval(section.handler + "(args, rightArea);"); diff --git a/assets/js/pages/userPage/accessForbidden.js b/assets/js/pages/userPage/accessForbidden.js index dede3a32..a72f6a48 100644 --- a/assets/js/pages/userPage/accessForbidden.js +++ b/assets/js/pages/userPage/accessForbidden.js @@ -72,7 +72,7 @@ ComunicWeb.pages.userPage.accessForbidden = { showBasicInfos: function(userInfos, target){ //Update page title - document.title = userInfos.firstName + " " + userInfos.lastName; + ComunicWeb.common.pageTitle.setTitle(userInfos.firstName + " " + userInfos.lastName); //Create box root var boxRoot = createElem2({ diff --git a/assets/js/pages/userPage/main.js b/assets/js/pages/userPage/main.js index ae8a2319..6ceebcd0 100644 --- a/assets/js/pages/userPage/main.js +++ b/assets/js/pages/userPage/main.js @@ -121,7 +121,7 @@ ComunicWeb.pages.userPage.main = { displayUserPage: function(infos, params, target){ //Update page title - document.title = infos.firstName + " " + infos.lastName; + ComunicWeb.common.pageTitle.setTitle(infos.firstName + " " + infos.lastName); //Create the section class content var sectionContent = createElem2({ diff --git a/system/config/dev.config.php b/system/config/dev.config.php index 0dab85cd..d957b964 100644 --- a/system/config/dev.config.php +++ b/system/config/dev.config.php @@ -288,6 +288,7 @@ class Dev { "js/common/jsFiles.js", "js/common/debug.js", "js/common/page.js", + "js/common/pageTitle.js", "js/common/notifications.js", "js/common/formChecker.js", "js/common/date.js",