From 32cf8601f7ee858c452fc18feb213eb181bf2b42 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 27 May 2017 11:57:05 +0200 Subject: [PATCH] Progress on search user component --- assets/js/common/api.js | 2 +- assets/js/common/helpers.js | 19 ++++++ assets/js/components/menuBar/authenticated.js | 4 +- assets/js/components/searchForm/searchForm.js | 60 +++++++++++++++++-- assets/js/user/userInfos.js | 5 +- corePage/config/dev.config.php | 1 + 6 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 assets/js/common/helpers.js diff --git a/assets/js/common/api.js b/assets/js/common/api.js index bd47c97c..3b70c838 100644 --- a/assets/js/common/api.js +++ b/assets/js/common/api.js @@ -57,7 +57,7 @@ ComunicWeb.common.api.makeAPIrequest = function(apiURI, params, requireLoginToke //We check if we got any error if(result.error){ //Log error - ComunicWeb.debug.logMessage("Got an error in a XHR request! " + result.toString()); + ComunicWeb.debug.logMessage("Got an error in a XHR request! \n Request URL: "+requestURL+" \n Response : "+apiXHR.responseText); } //We can do the next step diff --git a/assets/js/common/helpers.js b/assets/js/common/helpers.js new file mode 100644 index 00000000..c31d8262 --- /dev/null +++ b/assets/js/common/helpers.js @@ -0,0 +1,19 @@ +/** + * Helpers + * + * @author Pierre HUBERT + */ + +/** + * Returns the path pointing on an asset + * + * @param {String} assetURI Optionnal, the URI of the asset + * @return {String} The full URL path of the asset + */ +function path_assets(assetURI){ + + if(!assetURI) + assetURI = ""; + + return ComunicWeb.__config.assetsURL+assetURI; +} \ No newline at end of file diff --git a/assets/js/components/menuBar/authenticated.js b/assets/js/components/menuBar/authenticated.js index 3df8bea8..3f8fdf67 100644 --- a/assets/js/components/menuBar/authenticated.js +++ b/assets/js/components/menuBar/authenticated.js @@ -104,7 +104,7 @@ ComunicWeb.components.menuBar.authenticated = { userNameElem.innerHTML = "Loading..."; //Make a request to get informations about the user - ComunicWeb.user.getUserInfos.getUserInfos("current", (function(userInfos){ + ComunicWeb.user.userInfos.getUserInfos("current", (function(userInfos){ //Change user name userNameElem.innerHTML = userInfos.firstName + " "+ userInfos.lastName; @@ -124,7 +124,7 @@ ComunicWeb.components.menuBar.authenticated = { addSearchForm: function(navbarElem){ //Create form element var searchForm = createElem("li", navbarElem); - searchForm.className = "dropdown navbar-form navbar-left notifications-menu"; + searchForm.className = "dropdown navbar-form navbar-left messages-menu"; searchForm.setAttribute("role", "search"); //Create form group diff --git a/assets/js/components/searchForm/searchForm.js b/assets/js/components/searchForm/searchForm.js index 0660d513..91b057ca 100644 --- a/assets/js/components/searchForm/searchForm.js +++ b/assets/js/components/searchForm/searchForm.js @@ -74,19 +74,71 @@ ComunicWeb.components.searchForm = { params = { query: textInput.value, }; - ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, function(){ + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, function(response){ + + //Continue only in case of success + if(response.error) + return false; + //Remove any remainging element in searchResultBox - console.log("result"); + emptyElem(searchBoxContainer); + //Create menu list var menuList = createElem("ul", searchBoxContainer); menuList.className = "menu"; + + //Process each result + for(i in response){ + + //Retrieve userID + var userID = response[i]; + + //Display user informations + ComunicWeb.components.searchForm.displayUser(userID, menuList); + + } //Enable slimscroll - /*$(menuList).slimScroll({ + $(menuList).slimScroll({ height: '200px', - }));*/ + }); }); + + }, + /** + * Display a user on the result list + * + * @param {Integer} userID The ID of the user to display + * @param {HTMLElement} menuList The target list menu + * @return {Boolean} True for a success + */ + displayUser: function(userID, menuList){ + //Create user element + var userListElement = createElem("li", menuList); + var userLinkElement = createElem("a", userListElement); + + //User account image + var userAccountImageContainer = createElem("div", userLinkElement); + userAccountImageContainer.className = "pull-left"; + + var userImage = createElem("img", userAccountImageContainer); + userImage.className = "img-circle"; + userImage.alt = "User image"; + userImage.src = path_assets("img/defaultAvatar.png"); + + //User name + var usernameElem = createElem("h4", userLinkElement); + usernameElem.innerHTML = "Loading..."; + + //Get informations about user + ComunicWeb.user.userInfos.getUserInfos(userID, function(userInfos){ + + //Apply informations + userImage.src = userInfos.accountImage; + usernameElem.innerHTML = userInfos.firstName + " " + userInfos.lastName; + + }); }, } \ No newline at end of file diff --git a/assets/js/user/userInfos.js b/assets/js/user/userInfos.js index 35240027..56367c51 100644 --- a/assets/js/user/userInfos.js +++ b/assets/js/user/userInfos.js @@ -4,7 +4,7 @@ * @author Pierre HUBERT */ -ComunicWeb.user.getUserInfos = { +ComunicWeb.user.userInfos = { /** * @var {String} User infos cache @@ -30,6 +30,7 @@ ComunicWeb.user.getUserInfos = { } else{ ComunicWeb.debug.logMessage("ERROR : getUserInfos not implemented for other user than the current one !"); + return false; } //Specify what to do next @@ -40,7 +41,7 @@ ComunicWeb.user.getUserInfos = { } //Save result - ComunicWeb.user.getUserInfos.usersInfos[""+userID] = result[0]; + ComunicWeb.user.userInfos.usersInfos[""+userID] = result[0]; //Return result afterGetUserInfos(result[0]); diff --git a/corePage/config/dev.config.php b/corePage/config/dev.config.php index 4f2c2cf3..1d46ed26 100644 --- a/corePage/config/dev.config.php +++ b/corePage/config/dev.config.php @@ -82,6 +82,7 @@ $config['JSfiles'] = array( //Create shortcuts for common functions "%PATH_ASSETS%js/common/shorcuts.js", + "%PATH_ASSETS%js/common/helpers.js", //Init script "%PATH_ASSETS%js/init.js",