ComunicWeb/assets/js/components/friends/listModal.js
2018-03-11 09:56:42 +01:00

351 lines
7.4 KiB
JavaScript

/**
* Friends list modal
*
* @author Pierre HUBERT
*/
ComunicWeb.components.friends.listModal = {
/**
* Display the box that contains the list of friends of the user
*
* @param {number} user The ID of the target user
*/
display: function(user){
//Check if the user is requesting its own friends list or not
if(user == userID()){
//Get the list of friends of the current user
ComunicWeb.components.friends.interface.get_current_list(true, function(r){
//Check for error
if(r.error){
ComunicWeb.common.notificationSystem.showNotification("Could not get your list of friends !", "danger");
return;
}
//Show the list
ComunicWeb.components.friends.listModal._show(user, r);
});
}
else
//Try to get the list of friends of the specified user
ComunicWeb.components.friends.interface.get_user_list(user, function(r){
//Check for error
if(r.error){
ComunicWeb.common.notificationSystem.showNotification("Could not get the of friends of the user (it may be private) !", "danger");
return;
}
//Show the list
ComunicWeb.components.friends.listModal._show(user, r);
});
},
/**
* Show the list of friends of a user
*
* @param {number} user The ID of the target user
* @param {object} list The list of users to display
*/
_show: function(user, list){
//Create a modal root
var modal = createElem2({
type: "div",
class: "modal modal-primary input-string-modal"
});
var modalDialog = createElem2({
appendTo: modal,
type: "div",
class: "modal-dialog"
});
var modalContent = createElem2({
appendTo: modalDialog,
type: "div",
class: "modal-content",
});
//Modal header
var modalHeader = createElem2({
appendTo: modalContent,
type: "div",
class: "modal-header"
});
var closeModal = createElem2({
appendTo: modalHeader,
type: "button",
class: "close",
});
createElem2({
appendTo: closeModal,
type: "span",
innerHTML: "x"
});
var modalTitle = createElem2({
appendTo: modalHeader,
type: "h4",
class: "modal-title",
innerHTML: "Friends list"
});
//Modal body
var modalBody = createElem2({
appendTo: modalContent,
type: "div",
class: "modal-body",
});
//Modal footer
var modalFooter = createElem2({
appendTo: modalContent,
type: "div",
class: "modal-footer"
});
var closeButton = createElem2({
appendTo: modalFooter,
type: "button",
class: "btn btn-default",
innerHTML: "Close"
});
//Create the response function
var respond = function(){
//Close modal
$(modal).modal('hide');
emptyElem(modal);
modal.remove();
}
//Make the buttons live
closeButton.onclick = respond;
closeModal.onclick = respond;
//Display the right version of the friends list
if(userID() == user){
//Show the friends list of the user
this._show_personnal(modalBody, list);
}
else {
//Display a read-only list of friends
this._show_read_only(modalBody, list, user);
}
//Show the modal
$(modal).modal('show');
},
/**
* Display a read-only list of friends
*
* @param {HTMLElement} target The target for the list of friends
* @param {array} ids The list of IDs of the target users
* @param {userID} user The ID of the related user
*/
_show_read_only: function(target, ids, user){
//Create the friends list contener
var list = createElem2({
appendTo: target,
type: "div",
class: "friends-list-ro"
});
//Get informations about the users
ComunicWeb.user.userInfos.getMultipleUsersInfos(ids, function(users){
//Check for errors
if(users.error){
ComunicWeb.common.notificationSystem.showNotification("An error occured while retrieving informations about the friends !", "danger");
return;
}
//Parse the list of friends
ids.forEach(id => {
//Display the user
const userContener = createElem2({
appendTo: list,
type: "div",
class: "friend"
});
//Create user link
const userLink = createElem2({
appendTo: userContener,
type: "a"
});
//Add user account image
createElem2({
appendTo: userLink,
type: "img",
src: users["user-" + id].accountImage
});
//Add users name
createElem2({
appendTo: userLink,
type: "div",
class: "friends-name",
innerHTML: userFullName(users["user-" + id])
});
//Make the link button lives
userLink.onclick = function(){
//Open user page
openUserPage(userIDorPath(users["user-" + id]));
//Close all modals
$(".modal").modal("hide");
}
});
});
},
/**
* Show the list of friends of the user
*
* @param {HTMLElement} target The target element for the list
* @param {array} list The list of friends of the user
*/
_show_personnal: function(target, list){
//Get the list of friends ID
var usersID = ComunicWeb.components.friends.utils.getUsersIdFromPersonnalList(list);
//Create list target
var listTarget = createElem2({
appendTo: target,
type: "div",
class: "personnal-friends-list"
});
//Get informations about the users
ComunicWeb.user.userInfos.getMultipleUsersInfos(usersID, function(users){
//Check for errors
if(users.error){
ComunicWeb.common.notificationSystem.showNotification("Could not get informations about friends !", "danger");
return;
}
//Display each friend
list.forEach(friend => {
//Get informations about the friend
const friendID = friend.ID_friend;
//Create friend contener
const friendContener = createElem2({
appendTo: listTarget,
type: "div",
class: "friend"
});
//Create user link
const userLink = createElem2({
appendTo: friendContener,
type: "a"
});
//Add user account image
createElem2({
appendTo: userLink,
type: "img",
src: users["user-" + friendID].accountImage
});
//Add users name
createElem2({
appendTo: userLink,
type: "div",
class: "friends-name",
innerHTML: userFullName(users["user-" + friendID])
});
//Make the link button lives
userLink.onclick = function(){
//Open user page
openUserPage(userIDorPath(users["user-" + friendID]));
//Close all modals
$(".modal").modal("hide");
}
//Check if the friendship has been accepted or not
//Display following state
//Check if the user can post text on user page
//Offer to delete friendship
const deleteLink = createElem2({
appendTo: friendContener,
type: "a",
innerHTML: "<i class='fa fa-trash'></i>"
});
//Make the delete button lives
deleteLink.onclick = function(){
//Prompt user confirmation
if(ComunicWeb.common.messages.confirm("Do you really want to delete this user from your friends list ?", function(confirm){
//Check if the user cancelled the operation
if(!confirm)
return;
//Try to delete the friend from the list
friendContener.style.visibility = "hidden";
ComunicWeb.components.friends.interface.remove_friend(friendID, function(result){
//Make friend contener visible
friendContener.style.visibility = "visible";
//Check for errors
if(result.error){
ComunicWeb.common.notificationSystem.showNotification("Could not delete friend !", "danger");
return;
}
//Delete the element
friendContener.remove();
});
}));
}
});
});
}
};