2018-03-11 09:39:09 +00:00
|
|
|
/**
|
|
|
|
* Friend user interface script
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
ComunicWeb.components.friends.ui = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a single friend informations
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} target The target to display the friend
|
|
|
|
* @param {Object} friend Informations about the friendship
|
|
|
|
* @param {Object} user Informations about the user
|
|
|
|
*/
|
|
|
|
show_personnal_friend: function(target, friend, user){
|
|
|
|
|
2018-03-25 07:43:39 +00:00
|
|
|
//Create friend container, if required
|
2018-03-11 09:39:09 +00:00
|
|
|
if(target.className == "friend"){
|
2018-03-25 07:43:39 +00:00
|
|
|
var friendContainer = target;
|
|
|
|
emptyElem(friendContainer);
|
2018-03-11 09:39:09 +00:00
|
|
|
}
|
|
|
|
else
|
2018-03-25 07:43:39 +00:00
|
|
|
var friendContainer = createElem2({
|
2018-03-11 09:39:09 +00:00
|
|
|
appendTo: target,
|
|
|
|
type: "div",
|
|
|
|
class: "friend"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Get informations about the friend
|
|
|
|
const friendID = friend.ID_friend;
|
|
|
|
|
|
|
|
//Create user link
|
|
|
|
const userLink = createElem2({
|
2018-03-25 07:43:39 +00:00
|
|
|
appendTo: friendContainer,
|
2018-03-11 09:39:09 +00:00
|
|
|
type: "a"
|
|
|
|
});
|
|
|
|
|
|
|
|
//Add user account image
|
|
|
|
createElem2({
|
|
|
|
appendTo: userLink,
|
|
|
|
type: "img",
|
|
|
|
src: user.accountImage
|
|
|
|
});
|
|
|
|
|
|
|
|
//Add users name
|
|
|
|
createElem2({
|
|
|
|
appendTo: userLink,
|
|
|
|
type: "div",
|
|
|
|
class: "friends-name",
|
|
|
|
innerHTML: userFullName(user)
|
|
|
|
});
|
|
|
|
|
|
|
|
//Make the link button lives
|
|
|
|
userLink.onclick = function(){
|
|
|
|
|
|
|
|
//Open user page
|
|
|
|
openUserPage(userIDorPath(user));
|
|
|
|
|
|
|
|
//Close all modals
|
|
|
|
$(".modal").modal("hide");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-11 15:22:26 +00:00
|
|
|
//Create actions area
|
|
|
|
var actionsOnFriendArea = createElem2({
|
2018-03-25 07:43:39 +00:00
|
|
|
appendTo: friendContainer,
|
2018-03-11 15:22:26 +00:00
|
|
|
type: "div",
|
|
|
|
class: "friends-actions"
|
|
|
|
});
|
|
|
|
|
2018-03-11 09:39:09 +00:00
|
|
|
//Check if the friendship has been accepted or not
|
|
|
|
if(friend.accepted == 0){
|
|
|
|
|
|
|
|
//Offer the user to accept or reject friendship request
|
|
|
|
//Reject
|
|
|
|
var rejectRequestBtn = createElem2({
|
2018-03-11 15:22:26 +00:00
|
|
|
appendTo: actionsOnFriendArea,
|
2018-03-11 09:39:09 +00:00
|
|
|
type: "input",
|
|
|
|
elemType: "button",
|
|
|
|
class: "btn btn-danger",
|
|
|
|
value: "Refuser"
|
|
|
|
});
|
|
|
|
rejectRequestBtn.setAttribute("data-accept-request", "false");
|
|
|
|
|
2018-03-11 15:22:26 +00:00
|
|
|
add_space(actionsOnFriendArea);
|
2018-03-11 09:39:09 +00:00
|
|
|
|
|
|
|
//Accept
|
|
|
|
var acceptRequestBtn = createElem2({
|
2018-03-11 15:22:26 +00:00
|
|
|
appendTo: actionsOnFriendArea,
|
2018-03-11 09:39:09 +00:00
|
|
|
type: "button",
|
|
|
|
class: "btn btn-success",
|
|
|
|
innerHTML: "Accepter"
|
|
|
|
});
|
|
|
|
acceptRequestBtn.setAttribute("data-accept-request", "true");
|
|
|
|
|
2018-03-11 15:22:26 +00:00
|
|
|
add_space(actionsOnFriendArea);
|
2018-03-11 09:39:09 +00:00
|
|
|
|
|
|
|
//Make the buttons lives
|
|
|
|
var respond = function(){
|
|
|
|
|
|
|
|
//Check whether the request was accepted or not
|
|
|
|
var accept = this.getAttribute("data-accept-request") == "true";
|
|
|
|
|
|
|
|
//Perform the request on the server
|
|
|
|
ComunicWeb.components.friends.list.respondRequest(friendID, accept, function(r){
|
|
|
|
|
|
|
|
//Check for error
|
|
|
|
if(r.error){
|
|
|
|
ComunicWeb.common.notificationSystem.showNotification("Could not respond to request !", "danger");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!accept){
|
2018-03-25 07:43:39 +00:00
|
|
|
friendContainer.remove();
|
2018-03-11 09:39:09 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2018-03-11 15:16:00 +00:00
|
|
|
//Update friendship informations
|
2018-03-25 07:43:39 +00:00
|
|
|
ComunicWeb.components.friends.actions.refresh_single_personnal(friendID, friendContainer);
|
2018-03-11 09:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
acceptRequestBtn.onclick = respond;
|
|
|
|
rejectRequestBtn.onclick = respond;
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
//Display following state
|
2018-03-11 15:16:00 +00:00
|
|
|
var followButton = createElem2({
|
2018-03-11 15:22:26 +00:00
|
|
|
appendTo: actionsOnFriendArea,
|
2018-03-11 15:16:00 +00:00
|
|
|
type: "button",
|
|
|
|
class: "btn btn-primary"
|
|
|
|
});
|
|
|
|
|
|
|
|
if(friend.following == 0){
|
|
|
|
followButton.innerHTML = "Follow";
|
|
|
|
followButton.setAttribute("data-set-following", "true");
|
|
|
|
}
|
|
|
|
else {
|
2018-03-11 15:46:18 +00:00
|
|
|
followButton.innerHTML = "<i class='fa fa-check'></i> Following";
|
2018-03-11 15:16:00 +00:00
|
|
|
followButton.setAttribute("data-set-following", "false");
|
|
|
|
}
|
|
|
|
|
2018-03-11 15:22:26 +00:00
|
|
|
add_space(actionsOnFriendArea);
|
2018-03-11 09:39:09 +00:00
|
|
|
|
2018-03-11 15:16:00 +00:00
|
|
|
followButton.onclick = function(){
|
|
|
|
|
|
|
|
//Check if the request is to follow or not the user
|
|
|
|
var follow = this.getAttribute("data-set-following") == "true";
|
|
|
|
|
|
|
|
//Lock button
|
|
|
|
followButton.disabled = true;
|
|
|
|
|
|
|
|
//Perform callback action
|
|
|
|
ComunicWeb.components.friends.list.setFollowing(friendID, follow, function(r){
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(r.error){
|
|
|
|
ComunicWeb.common.notificationSystem.showNotification("Could not update follow state !", "danger");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Update friendship informations
|
2018-03-25 07:43:39 +00:00
|
|
|
ComunicWeb.components.friends.actions.refresh_single_personnal(friendID, friendContainer);
|
2018-03-11 15:16:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2018-03-11 15:46:18 +00:00
|
|
|
|
|
|
|
//Check if the user can post text on user page
|
|
|
|
var postTextsButton = createElem2({
|
|
|
|
appendTo: actionsOnFriendArea,
|
|
|
|
type: "button",
|
|
|
|
class: "btn btn-primary"
|
|
|
|
});
|
|
|
|
|
|
|
|
if(friend.canPostTexts){
|
|
|
|
postTextsButton.innerHTML = "<i class='fa fa-check'></i> Post Texts";
|
|
|
|
postTextsButton.setAttribute("data-allow-post-texts", "false");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
postTextsButton.innerHTML = "Post Texts";
|
|
|
|
postTextsButton.setAttribute("data-allow-post-texts", "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Make the button lives
|
|
|
|
postTextsButton.onclick = function(){
|
|
|
|
|
|
|
|
//Check out if we have to allow or disallow texts post
|
|
|
|
var allow_post = this.getAttribute("data-allow-post-texts") == "true";
|
|
|
|
|
|
|
|
//Update the status
|
|
|
|
ComunicWeb.components.friends.interface.set_can_post_texts(friendID, allow_post, function(r){
|
|
|
|
|
|
|
|
if(r.error)
|
|
|
|
ComunicWeb.common.notificationSystem.showNotification("Could not update posts creation status !", "danger");
|
|
|
|
|
|
|
|
//Update friendship informations
|
2018-03-25 07:43:39 +00:00
|
|
|
ComunicWeb.components.friends.actions.refresh_single_personnal(friendID, friendContainer);
|
2018-03-11 15:46:18 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
}
|
2018-03-11 09:39:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Offer to delete friendship
|
|
|
|
const deleteLink = createElem2({
|
2018-03-25 07:43:39 +00:00
|
|
|
appendTo: friendContainer,
|
2018-03-11 09:39:09 +00:00
|
|
|
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
|
2018-03-25 07:43:39 +00:00
|
|
|
friendContainer.style.visibility = "hidden";
|
2018-03-11 09:39:09 +00:00
|
|
|
ComunicWeb.components.friends.interface.remove_friend(friendID, function(result){
|
|
|
|
|
2018-03-25 07:43:39 +00:00
|
|
|
//Make friend container visible
|
|
|
|
friendContainer.style.visibility = "visible";
|
2018-03-11 09:39:09 +00:00
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(result.error){
|
|
|
|
ComunicWeb.common.notificationSystem.showNotification("Could not delete friend !", "danger");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Delete the element
|
2018-03-25 07:43:39 +00:00
|
|
|
friendContainer.remove();
|
2018-03-11 09:39:09 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|