164 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-01-28 17:03:54 +01:00
/**
* Comments form
*
* @author Pierre HUBERT
*/
ComunicWeb.components.comments.form = {
/**
* Display comments creation form
*
* @param {number} postID The ID of the target post
* @param {HTMLElement} target The target for the form
*/
display: function(postID, target){
//Check if we are creating a new comment of or reseting an existing one
if(target.className != "comment-creation-form"){
2018-03-25 09:43:39 +02:00
//Create form container
var commentForm = createElem2({
appendTo: target,
type: "form",
class: "comment-creation-form"
});
}
else {
//Reset current form
emptyElem(target);
var commentForm = target;
}
2018-01-28 17:03:54 +01:00
//Create input group
var inputGroup = createElem2({
appendTo: commentForm,
type: "div",
class: "input-group input-group-sm"
});
//Add text input
var newCommentText = createElem2({
appendTo: inputGroup,
type: "input",
elemType: "text",
2018-04-22 14:39:34 +02:00
class: "form-control",
2018-08-05 16:29:49 +02:00
placeholder: lang("comments_form_input_placeholder"),
2018-01-30 06:49:50 +01:00
name: "content"
2018-01-28 17:03:54 +01:00
});
2018-05-11 07:51:32 +02:00
newCommentText.maxLength = 255;
2018-01-30 06:49:50 +01:00
2018-01-28 17:03:54 +01:00
//Add button group
var buttonsGroup = createElem2({
appendTo: inputGroup,
type: "span",
class: "input-group-btn"
});
2018-04-20 15:40:09 +02:00
//Add emoji pick button
var addEmojieLabel = createElem2({
appendTo: buttonsGroup,
type: "label",
class: "comment-emoji-select"
});
var imageLabel = createElem2({
appendTo: addEmojieLabel,
type: "a",
class: "btn btn-flat",
innerHTML: "<i class='fa fa-smile-o'></i>"
});
//Add a picker on the label
ComunicWeb.components.emoji.picker.addPicker(newCommentText, addEmojieLabel);
2018-01-28 17:03:54 +01:00
//Add image pick button
var addImageLabel = createElem2({
appendTo: buttonsGroup,
type: "label",
class: "comment-image-select"
});
var imageFile = createElem2({
appendTo: addImageLabel,
type: "input",
2018-01-30 06:49:50 +01:00
elemType: "file",
name: "image"
2018-01-28 17:03:54 +01:00
});
var imageButton = createElem2({
appendTo: addImageLabel,
type: "a",
class: "btn btn-flat",
innerHTML: "<i class='fa fa-picture-o'></i>"
2018-04-20 15:40:09 +02:00
});
2018-01-28 17:03:54 +01:00
//Add send button
var sendButton = createElem2({
appendTo: buttonsGroup,
type: "button",
class: "btn btn-default btn-flat",
2018-08-05 16:29:49 +02:00
innerHTML: lang("comments_form_send")
2018-01-28 17:03:54 +01:00
});
//Catch form when submitted
commentForm.onsubmit = function(){
2018-01-30 06:49:50 +01:00
//Check for image
var hasImage = imageFile.files.length > 0;
//Check the comment
if(!hasImage && newCommentText.value < 5){
2018-08-05 16:29:49 +02:00
ComunicWeb.common.notificationSystem.showNotification(lang("comments_form_err_invalid_comment"), "danger");
2018-01-30 06:49:50 +01:00
return false;
}
//Lock send button
sendButton.disabled = true;
2018-07-03 12:32:44 +02:00
//Prepare the request
var formData = new FormData();
formData.append("content", newCommentText.value);
//Check for image
if(imageFile.files.length > 0)
formData.append("image", imageFile.files[0], imageFile.files[0].name);
//Send the request
2018-01-30 06:49:50 +01:00
ComunicWeb.components.comments.interface.create(postID, formData, function(result){
//Unlock send button
sendButton.disabled = false;
//Check for errors
if(result.error){
2018-08-05 16:29:49 +02:00
ComunicWeb.common.notificationSystem.showNotification(lang("comments_form_err_create_comment"), "danger");
2018-01-30 06:49:50 +01:00
return;
}
//Reset the creation form
ComunicWeb.components.comments.form.display(postID, commentForm);
//Load the new comment before the form element
var newCommentTarget = createElem2({
insertBefore: commentForm,
2018-02-17 11:33:02 +01:00
type: "div",
class: "box-comment"
});
ComunicWeb.components.comments.actions.reload(result.commentID, newCommentTarget);
2018-01-30 06:49:50 +01:00
});
2018-01-28 17:03:54 +01:00
return false;
}
},
}