ComunicWeb/assets/js/components/friends/friendsBar.js

182 lines
4.6 KiB
JavaScript
Raw Normal View History

2017-05-31 14:51:52 +00:00
/**
* Friends bar
*
* @author Pierre HUBERT
*/
ComunicWeb.components.friends.bar = {
/**
* Display friends list
*
* @return {Boolean} True for a success
*/
display: function(){
//Log action
ComunicWeb.debug.logMessage("Initialize friends list.");
//Check if friends list already exists or not
var friendsListContainer = byId("friendsList");
//We check if the friend list already exists or not
if(friendsListContainer){
ComunicWeb.debug.logMessage("Notice: friend list already present on the page. Nothing to be done.");
return true;
}
//Create and apply friends list element
var friendsListContainer = createElem("div");
friendsListContainer.id = "friendsList";
//Check if "pageTarget" already exists or not
var pageTarget = byId("pageTarget");
if(pageTarget){
//Insert friends list just before pageTarget
byId("wrapper").insertBefore(friendsListContainer, pageTarget);
}
else{
byId("wrapper").appendChild(friendsListContainer); //Just happend the menubar
}
//Initializate friends list
this.init(friendsListContainer);
//Success
return true;
},
/**
* Initializate a friend list
*
* @param {HTMLElement} friendsListContainer The container of the friend list
* @return {Boolean} True for a success
*/
init: function(friendsListContainer){
//First, create the table container
2017-05-31 17:05:19 +00:00
var listFriendsElemTable = createElem("table", friendsListContainer);
listFriendsElemTable.className = "table table-condensed";
var listFriendsElem = createElem("tbody", listFriendsElemTable);
2017-05-31 14:51:52 +00:00
//Refresh friends list
this.refresh(listFriendsElem);
//Make the friend bar automaticaly refreshed (if the interval wasn't already defined)
if(!this.refreshInterval)
this.refreshInterval = setInterval(function(){
if(byId("friendsList"))
ComunicWeb.components.friends.bar.refresh(listFriendsElem);
}, 1000);
2017-05-31 14:51:52 +00:00
//Success
return true;
},
/**
* Refresh the friendbar
2017-05-31 14:51:52 +00:00
*
* @param {HTMLElement} listFriendsElem The element that contains the list of friens
* @return {Boolean} True for a success
*/
refresh: function(listFriendsElem){
2017-05-31 15:12:51 +00:00
//Refresh it
2017-05-31 17:05:19 +00:00
ComunicWeb.components.friends.list.refresh(function(friendsList){
2017-05-31 14:51:52 +00:00
//Check for error
2017-05-31 17:05:19 +00:00
if(!friendsList){
2017-05-31 15:12:51 +00:00
//Log information
ComunicWeb.debug.logMessage("ERROR : Can't refresh menubar without the latest list !");
//Error
2017-05-31 14:51:52 +00:00
return false;
}
2017-05-31 15:12:51 +00:00
//Get users list to get information about them
usersID = {};
2017-05-31 17:05:19 +00:00
for(i in friendsList){
2017-05-31 15:12:51 +00:00
//Extract user id
2017-05-31 17:05:19 +00:00
var processID = friendsList[i].ID_friend;
2017-05-31 15:12:51 +00:00
usersID["user_"+processID] = processID;
}
//Get users ID informations
2017-05-31 17:05:19 +00:00
ComunicWeb.user.userInfos.getMultipleUsersInfos(usersID, function(usersInfos){
//Clean the area
listFriendsElem.innerHTML = "";
2017-05-31 17:05:19 +00:00
//Show each friend
for(i in friendsList){
//Extract friend ID
var friendID = friendsList[i].ID_friend;
//Create a row
var friendRow = createElem("tr", listFriendsElem);
//Add user avatar
var imageRow = createElem("td", friendRow);
var imageElem = createElem("img", imageRow);
imageElem.src = usersInfos["user-"+friendID].accountImage;
imageElem.className = "account-image";
//Add user name
var nameRow = createElem("td", friendRow);
nameRow.innerHTML = usersInfos["user-"+friendID].firstName + " " + usersInfos["user-"+friendID].lastName;
2017-06-03 12:25:21 +00:00
//Add user login status
2017-06-03 12:33:29 +00:00
var statusRow = createElem("td", friendRow);
statusRow.className = "statusRow";
2017-06-03 12:25:21 +00:00
//Check if user is online or not
var currentTime = ComunicWeb.common.date.time();
var timeDifference = currentTime - friendsList[i].time_last_activity;
if(timeDifference < 30){
//User is logged in
2017-06-03 12:27:29 +00:00
var iconsStats = createElem("i", statusRow);
iconsStats.className = "fa fa-fw fa-circle";
2017-06-03 12:25:21 +00:00
iconsStats.style.color = "green";
}
else {
//User isn't logged in
2017-06-03 12:27:29 +00:00
var logoutTime = createElem("small", statusRow);
logoutTime.innerHTML = ComunicWeb.common.date.diffToStr(timeDifference);
2017-06-03 12:25:21 +00:00
}
2017-05-31 17:05:19 +00:00
}
//Enable slimscroll
$(listFriendsElem.parentNode.parentNode).slimScroll({
height: '100%;'
});
2017-05-31 15:12:51 +00:00
});
2017-05-31 14:51:52 +00:00
});
},
2017-06-03 13:41:04 +00:00
/**
* Toogle show / hide friends bar
*
* @return {Boolean} True for a success
*/
toggleShowHide: function(){
//Get friends list element
var friendListElem = byId("friendsList");
//Log action
ComunicWeb.debug.logMessage("Toggle friends list");
//Check current bar state
if(friendListElem.className == ""){
//Show the bar
friendListElem.className = "visible-bar";
}
else
//Hide the bar
friendListElem.className = "";
//Success
return true;
}
2017-05-31 14:51:52 +00:00
}