ComunicWeb/assets/js/components/comments/ui.js

315 lines
7.0 KiB
JavaScript
Raw Normal View History

2018-01-18 05:53:00 +00:00
/**
* Comments UI
*
* @author Pierre HUBERT
*/
2020-04-01 17:06:57 +00:00
const CommentsUI = {
2018-01-18 05:53:00 +00:00
/**
* Display a list comments
*
* @param {Object} infos Informations about the comments
* @param {int} postID The ID of the post attached to the comments
* @param {HTMLElement} target The target for the comments
*/
display: function(infos, postID, target){
//First, get informations about the users
2018-01-19 06:00:49 +00:00
var usersID = ComunicWeb.components.comments.utils.get_users_id(infos);
2018-01-18 05:53:00 +00:00
2018-01-19 06:00:49 +00:00
//Get informations about the users
2020-04-01 17:06:57 +00:00
ComunicWeb.user.userInfos.getMultipleUsersInfo(usersID, function(result){
2018-01-19 06:00:49 +00:00
//Check for errors
if(result.error){
2018-08-05 14:29:49 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("comments_ui_err_get_users_info"), "danger");
2018-01-19 06:00:49 +00:00
return;
}
//Process the comments
ComunicWeb.components.comments.ui._process_comments(infos, result, postID, target);
}, false);
},
/**
* Process the list of comments
*
* @param {Object} infos Informations about the comments
* @param {Object} userInfos Informations about the users of the comments
* @param {int} postID The ID of the post attached to the comments
* @param {HTMLElement} target The target for the comments
*/
_process_comments: function(infos, usersInfos, postID, target){
2018-03-25 07:43:39 +00:00
//Create comments container
2020-04-01 17:06:57 +00:00
const container = createElem2({
2018-01-19 06:00:49 +00:00
appendTo: target,
type: "div",
class: "box-comments post-comments"
});
2020-04-01 17:06:57 +00:00
container.setAttribute("data-for-post-id", postID);
2018-01-19 06:00:49 +00:00
2018-01-21 19:27:00 +00:00
//Process the list of comments
for(i in infos){
2018-03-25 07:43:39 +00:00
this._show_comment(infos[i], usersInfos['user-' + infos[i].userID], container);
2018-01-21 19:27:00 +00:00
}
2018-01-19 06:00:49 +00:00
2018-01-28 16:03:54 +00:00
//Add comment creation form (if possible)
if(signed_in()){
2018-03-25 07:43:39 +00:00
ComunicWeb.components.comments.form.display(postID, container)
2018-01-28 16:03:54 +00:00
}
2018-01-19 06:00:49 +00:00
},
2018-01-27 17:49:05 +00:00
/**
* Display a single comment
*
* @param {Object} infos Informations about the comment to display
* @param {HTMLElement} target The target for the comment
*/
display_comment: function(infos, target){
//Get informations about the user
ComunicWeb.user.userInfos.getUserInfos(infos.userID, function(result){
//Check for errors
if(result.error){
2018-08-05 14:29:49 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("comments_ui_err_get_user_info"), "danger");
2018-01-27 17:49:05 +00:00
return;
}
//Display the comment
ComunicWeb.components.comments.ui._show_comment(infos, result, target);
});
},
2018-01-19 06:00:49 +00:00
/**
* Show a comment
*
2018-01-21 19:27:00 +00:00
* @param {object} infos Informations about the comment
* @param {object} user Informations about the user
* @param {HTMLElement} target The target for the comment
2018-01-19 06:00:49 +00:00
*/
2018-01-21 19:27:00 +00:00
_show_comment: function(infos, user, target){
2018-03-25 07:43:39 +00:00
//Create comment container (if required)
2018-01-27 17:49:05 +00:00
if(target.className != "box-comment"){
2018-03-25 07:43:39 +00:00
var commentContainer = createElem2({
2018-01-27 17:49:05 +00:00
appendTo: target,
type: "div",
class: "box-comment"
});
}
2018-03-25 07:43:39 +00:00
//Empty comment container
2018-01-27 17:49:05 +00:00
else {
emptyElem(target);
2018-03-25 07:43:39 +00:00
var commentContainer = target;
2018-01-27 17:49:05 +00:00
}
2018-01-21 19:27:00 +00:00
//Add user image
createElem2({
2018-03-25 07:43:39 +00:00
appendTo: commentContainer,
2018-01-21 19:27:00 +00:00
type: "img",
class: "img-circle imgs-sm",
src: user.accountImage
});
//Create comment text
var commentText = createElem2({
2018-03-25 07:43:39 +00:00
appendTo: commentContainer,
2018-01-21 19:27:00 +00:00
type: "div",
class: "comment-text"
});
2018-03-03 13:48:20 +00:00
//Add username
2018-03-25 07:43:39 +00:00
var userNameContainer = createElem2({
2018-01-21 19:27:00 +00:00
appendTo: commentText,
type: "span",
class: "username",
innerHTML: userFullName(user)
});
2018-01-23 05:41:17 +00:00
//Add right elements
2018-03-25 07:43:39 +00:00
var rightContainer = createElem2({
appendTo: userNameContainer,
2018-01-21 19:27:00 +00:00
type: "span",
class: "text-muted pull-right"
});
//Add comment creation date
createElem2({
2018-03-25 07:43:39 +00:00
appendTo: rightContainer,
2018-01-21 19:27:00 +00:00
type: "span",
2018-08-05 14:40:14 +00:00
innerHTML: lang("dates_ago", [ComunicWeb.common.date.timeDiffToStr(infos.time_sent)])
2018-01-21 19:27:00 +00:00
});
2018-01-25 05:58:26 +00:00
//Offer the user the possibility to delete and update the comment if he is allowed to do so
2018-01-23 05:41:17 +00:00
if(userID() == infos.userID){
2018-01-25 05:58:26 +00:00
//Create a button to update the comment
var editCommentLink = createElem2({
2018-03-25 07:43:39 +00:00
appendTo: rightContainer,
2018-01-25 05:58:26 +00:00
type: "a",
class: "edit-comment-link"
});
createElem2({
appendTo: editCommentLink,
2018-01-25 05:58:26 +00:00
type: "i",
class: "fa fa-edit"
});
//Make edit button lives
editCommentLink.onclick = function(){
//Open comment editor
2018-03-25 07:43:39 +00:00
ComunicWeb.components.comments.editor.open(infos, commentContainer);
}
2018-01-25 05:58:26 +00:00
2018-01-23 05:41:17 +00:00
//Create a button to delete the comment
var deleteCommentLink = createElem2({
2018-03-25 07:43:39 +00:00
appendTo: rightContainer,
2018-01-23 05:41:17 +00:00
type: "a",
class: "delete-comment-link"
});
createElem2({
appendTo: deleteCommentLink,
type: "i",
class: "fa fa-trash"
});
2018-01-25 05:58:26 +00:00
//Make delete button lives
deleteCommentLink.onclick = function(){
2018-08-05 14:29:49 +00:00
ComunicWeb.common.messages.confirm(lang("comments_ui_confirm_delete"), function(response){
//Check if user cancelled the operation
if(!response)
return;
//Hide the comment
2018-03-25 07:43:39 +00:00
commentContainer.style.visibility = "hidden";
//Delete the comment
ComunicWeb.components.comments.interface.delete(infos.ID, function(response){
2018-03-25 07:43:39 +00:00
commentContainer.style.visibility = "visible";
//Check for errors
if(response.error){
2018-08-05 14:29:49 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("comments_ui_err_delete_comment"), "danger");
return;
}
2018-01-31 05:50:22 +00:00
//Delete the comment node
2018-03-25 07:43:39 +00:00
emptyElem(commentContainer);
commentContainer.remove();
});
});
}
2018-01-23 05:41:17 +00:00
}
2018-01-21 19:27:00 +00:00
//Add comment content
var commentContent = createElem2({
appendTo: commentText,
type: "div",
2018-03-03 13:52:52 +00:00
class: "comment-content",
2018-01-21 19:27:00 +00:00
innerHTML: infos.content
});
2018-01-18 05:53:00 +00:00
2018-03-03 13:52:52 +00:00
//Parse emojies
2018-05-03 20:05:06 +00:00
ComunicWeb.components.textParser.parse({
2018-03-03 13:52:52 +00:00
element: commentContent
});
2018-01-21 19:27:00 +00:00
//Add comment image (if any)
if(infos.img_url != null){
2018-03-25 07:43:39 +00:00
var commentImageContainer = createElem2({
appendTo: commentText,
type: "div",
2018-03-25 07:43:39 +00:00
class: "comment-img-container"
});
var commentImageLink = createElem2({
2018-03-25 07:43:39 +00:00
appendTo: commentImageContainer,
type: "a",
href: infos.img_url
});
createElem2({
appendTo: commentImageLink,
type: "img",
class: "comment-img",
src: infos.img_url
});
commentImageLink.onclick = function(){
$(this).ekkoLightbox({
alwaysShowClose: true,
});
return false;
}
}
2018-01-27 17:54:12 +00:00
//Load likes
var likesTarget = createElem2({
appendTo: commentText,
type: "div",
});
var userLiking = null;
if(signed_in()){
userLiking = infos.userlike;
}
//Call component
ComunicWeb.components.like.button.display(
"comment",
infos.ID,
infos.likes,
userLiking,
likesTarget
);
2018-01-18 05:53:00 +00:00
},
2020-04-01 17:06:57 +00:00
}
ComunicWeb.components.comments.ui = CommentsUI;
// Register to new comments events
document.addEventListener("new_comment", async (e) => {
const comment = e.detail;
const target = document.querySelector("[data-for-post-id='"+comment.postID+"'].post-comments");
if(target == null)
return;
// Check if there is comment form to avoid or not
const insertBefore = target.querySelector(".comment-creation-form");
const newCommentTarget = createElem2({
insertBefore: insertBefore,
appendTo: insertBefore ? null : target,
type: "div",
class: "box-comment"
});
CommentsUI._show_comment(comment, await userInfo(comment.userID), newCommentTarget)
})