2017-05-26 10:01:54 +00:00
|
|
|
/**
|
|
|
|
* Search form component
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
ComunicWeb.components.searchForm = {
|
|
|
|
/**
|
|
|
|
* Initializate a search form element
|
|
|
|
*
|
2017-05-26 14:11:41 +00:00
|
|
|
* @param {HTMLElement} textInput The text input node
|
|
|
|
* @param {HTMLElement} searchResultBox The target of the results
|
|
|
|
* @return {Boolean} True for a success
|
2017-05-26 10:01:54 +00:00
|
|
|
*/
|
2017-05-26 14:11:41 +00:00
|
|
|
init: function(textInput, searchResultBox){
|
|
|
|
//Log action
|
|
|
|
ComunicWeb.debug.logMessage("Initializate search menu");
|
2017-05-26 10:01:54 +00:00
|
|
|
|
2017-05-26 14:11:41 +00:00
|
|
|
//Create header
|
|
|
|
var searchHeader = createElem("li", searchResultBox);
|
|
|
|
searchHeader.className = "header";
|
|
|
|
searchHeader.innerHTML = "Search results";
|
|
|
|
|
|
|
|
//Box core
|
|
|
|
var searchBoxCore = createElem("li", searchResultBox);
|
|
|
|
var searchBoxContainer = createElem("div", searchBoxCore);
|
|
|
|
searchBoxContainer.className = "searchBoxResultsContainer";
|
|
|
|
|
|
|
|
//Create footer
|
|
|
|
var searchFooter = createElem("li", searchResultBox);
|
|
|
|
searchFooter.className = "footer";
|
|
|
|
|
|
|
|
//Footer link
|
|
|
|
var footerLink = createElem("a", searchFooter);
|
|
|
|
footerLink.innerHTML = "See more results";
|
|
|
|
footerLink.onclick = function(){
|
|
|
|
openPage("search?q=" + this.getAttribute("data-searchValue"));
|
|
|
|
}
|
|
|
|
|
|
|
|
//Make input text lives
|
|
|
|
textInput.onkeyup = function(){
|
|
|
|
ComunicWeb.components.searchForm.ontextchange(textInput, searchResultBox, searchBoxContainer, footerLink);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* What to do on text change
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} textInput The text input node
|
|
|
|
* @param {HTMLElement} searchResultBox The main search box
|
|
|
|
* @param {HTMLElement} searchBoxContainer The target of the results
|
|
|
|
* @param {HTMLElement} footerLink The footer element
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
|
|
|
ontextchange: function(textInput, searchResultBox, searchBoxContainer, footerLink){
|
|
|
|
//We check if the text was removed
|
|
|
|
if(textInput.value == ""){
|
|
|
|
//Text was removed
|
|
|
|
//Hide box
|
|
|
|
searchResultBox.style.display = "none";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//We show the box
|
|
|
|
searchResultBox.style.display = "block";
|
|
|
|
|
|
|
|
//Change "see more result" value
|
|
|
|
footerLink.setAttribute("data-searchValue", textInput.value);
|
2017-05-26 14:15:13 +00:00
|
|
|
|
|
|
|
//Perform a request on the server
|
|
|
|
apiURI = "search/request";
|
|
|
|
params = {
|
|
|
|
query: textInput.value,
|
|
|
|
};
|
2017-05-27 09:57:05 +00:00
|
|
|
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, function(response){
|
|
|
|
|
|
|
|
//Continue only in case of success
|
|
|
|
if(response.error)
|
|
|
|
return false;
|
|
|
|
|
2017-05-26 14:15:13 +00:00
|
|
|
//Remove any remainging element in searchResultBox
|
2017-05-27 09:57:05 +00:00
|
|
|
emptyElem(searchBoxContainer);
|
|
|
|
|
2017-05-26 14:15:13 +00:00
|
|
|
//Create menu list
|
|
|
|
var menuList = createElem("ul", searchBoxContainer);
|
|
|
|
menuList.className = "menu";
|
2017-05-27 09:57:05 +00:00
|
|
|
|
|
|
|
//Process each result
|
|
|
|
for(i in response){
|
|
|
|
|
|
|
|
//Retrieve userID
|
|
|
|
var userID = response[i];
|
|
|
|
|
|
|
|
//Display user informations
|
|
|
|
ComunicWeb.components.searchForm.displayUser(userID, menuList);
|
|
|
|
|
|
|
|
}
|
2017-05-26 14:15:13 +00:00
|
|
|
|
|
|
|
//Enable slimscroll
|
2017-05-27 09:57:05 +00:00
|
|
|
$(menuList).slimScroll({
|
2017-05-26 14:15:13 +00:00
|
|
|
height: '200px',
|
2017-05-27 09:57:05 +00:00
|
|
|
});
|
2017-05-26 14:15:13 +00:00
|
|
|
});
|
|
|
|
|
2017-05-27 09:57:05 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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...";
|
2017-05-26 14:15:13 +00:00
|
|
|
|
2017-05-27 09:57:05 +00:00
|
|
|
//Get informations about user
|
|
|
|
ComunicWeb.user.userInfos.getUserInfos(userID, function(userInfos){
|
|
|
|
|
|
|
|
//Apply informations
|
|
|
|
userImage.src = userInfos.accountImage;
|
|
|
|
usernameElem.innerHTML = userInfos.firstName + " " + userInfos.lastName;
|
|
|
|
|
|
|
|
});
|
2017-05-26 14:11:41 +00:00
|
|
|
},
|
2017-05-26 10:01:54 +00:00
|
|
|
}
|