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

289 lines
7.3 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
2018-01-03 08:17:00 +00:00
var listFriendsElem = createElem2({
appendTo: friendsListContainer,
type: "ul",
class: "menu"
});
2017-05-31 14:51:52 +00:00
//Refresh friends list
this.refresh(listFriendsElem);
2017-06-03 14:39:47 +00:00
//Remove previously existing interval
2017-06-14 09:46:10 +00:00
if(this.refreshInterval){
2017-06-03 14:39:47 +00:00
clearInterval(this.refreshInterval);
2017-06-14 09:46:10 +00:00
}
2017-06-03 14:39:47 +00:00
//Make the friend bar automaticaly refreshed
this.refreshInterval = setInterval(function(){
if(byId("friendsList"))
ComunicWeb.components.friends.bar.refresh(listFriendsElem);
}, 15000);
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
2019-05-16 13:07:58 +00:00
ComunicWeb.user.userInfos.getMultipleUsersInfo(usersID, function(usersInfos){
2017-05-31 17:05:19 +00:00
//Clean the area
listFriendsElem.innerHTML = "";
2017-05-31 17:05:19 +00:00
//Show each friend
for(i in friendsList){
//Show informations about the friend
ComunicWeb.components.friends.bar.showFriendInfos(usersInfos["user-"+friendsList[i].ID_friend], friendsList[i], listFriendsElem);
2017-05-31 17:05:19 +00:00
}
2017-06-07 14:13:49 +00:00
//Check if the friends hasn't any friend
if(friendsList.length == 0){
//Display adapted message
2018-01-03 08:17:00 +00:00
var noFriendMessageRow = createElem("li", listFriendsElem);
var noFriendMessage = createElem("span", noFriendMessageRow);
2017-06-07 14:13:49 +00:00
noFriendMessage.style.color = "#3c8dbc";
noFriendMessage.style.fontSize = "150%";
2018-01-03 08:17:00 +00:00
noFriendMessage.style.textAlign = "justify";
2018-08-04 07:37:36 +00:00
noFriendMessage.innerHTML = lang("friends_bar_no_friends_notice")
2017-06-07 14:13:49 +00:00
}
//Enable slimscroll
2018-01-03 08:17:00 +00:00
$(listFriendsElem.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
/**
* Show a friend informations
*
* @param {Object} userInfos Informations about the user
* @param {Object} friendshipInfos Informations about the friendship
* @param {HTMLElement} listFriendsElem The target for the friends list
* @return {Boolean} True for a success
*/
showFriendInfos: function(userInfos, friendshipInfos, listFriendsElem){
//Extract friend ID
var friendID = friendshipInfos.ID_friend;
//Create a row
2018-01-03 08:17:00 +00:00
var friendRow = createElem("li", listFriendsElem);
//Create link
var friendLink = createElem2({
appendTo: friendRow,
type: "a"
});
//Make the link lives
friendLink.onclick = function(){
2018-07-14 12:18:21 +00:00
openUserPage(userInfos);
2018-04-11 06:57:10 +00:00
//For the responsive mode
ComunicWeb.components.friends.bar.toggleShowHide();
2018-01-03 08:17:00 +00:00
}
2018-01-03 08:17:00 +00:00
//Add user account image
var imageRow = createElem2({
appendTo: friendLink,
type: "div",
class: "pull-left"
});
var imageElem = createElem2({
appendTo: imageRow,
type: "img",
class: "account-image",
src: userInfos.accountImage
});
//Add user name
2018-01-03 08:17:00 +00:00
var nameRow = createElem2({
appendTo: friendLink,
type: "h4",
innerHTML: userInfos.firstName + " " + userInfos.lastName
});
//Add user login status
2018-01-03 08:17:00 +00:00
var statusRow = createElem2({
appendTo: friendLink,
type: "p",
});
//Check if the user was accepted or not
if(friendshipInfos.accepted == "1"){
//Check if user is online or not
var currentTime = ComunicWeb.common.date.time();
var timeDifference = currentTime - friendshipInfos.time_last_activity;
if(timeDifference < 30){
//User is logged in
var iconsStats = createElem("i", statusRow);
iconsStats.className = "fa fa-fw fa-circle";
iconsStats.style.color = "green";
}
else {
//User isn't logged in
var logoutTime = createElem("small", statusRow);
logoutTime.innerHTML = ComunicWeb.common.date.diffToStr(timeDifference);
}
}
else {
//We offer user to accept invitation
var acceptButton = createElem("button", statusRow);
acceptButton.className = "btn btn-xs btn-success";
acceptButton.innerHTML = "<i class='fa fa-check'></i>";
acceptButton.onclick = function(){
ComunicWeb.components.friends.bar.processFriendShipRequest(friendID, true, statusRow)
};
//Insert space
var space = createElem("span", statusRow);
space.innerHTML = "&nbsp";
//But he can also refuse it
var refuseButton = createElem("button", statusRow);
refuseButton.className = "btn btn-xs btn-danger";
refuseButton.innerHTML = "<i class='fa fa-times'></i>";
refuseButton.onclick = function(){
ComunicWeb.components.friends.bar.processFriendShipRequest(friendID, false, statusRow)
};
}
//Sucess
return true;
},
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;
},
/**
* Accept / Deny a friendship request
*
* @param {Integer} friendID The ID of the friend to accept / rejet
* @param {Boolean} accepted True if friendship is accepted, false else
* @param {HTMLElement} statusRow The parent node of friendship area
* @return {Boolean} True for a success
*/
processFriendShipRequest: function(friendID, accepted, statusRow){
//Log action
ComunicWeb.debug.logMessage("Process friendship request "+friendID);
//Change statusRow style
if(accepted)
2018-08-04 07:37:36 +00:00
statusRow.innerHTML = lang("friends_bar_accepted");
else
2018-08-04 07:37:36 +00:00
statusRow.innerHTML = lang("friends_bar_rejected");
//Perform an API request
ComunicWeb.components.friends.list.respondRequest(friendID, accepted);
},
2017-06-03 13:41:04 +00:00
2017-05-31 14:51:52 +00:00
}