ComunicWeb/assets/js/components/comments/form.js

135 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-01-28 16:03:54 +00: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"){
//Create form contener
var commentForm = createElem2({
appendTo: target,
type: "form",
class: "comment-creation-form"
});
}
else {
//Reset current form
emptyElem(target);
var commentForm = target;
}
2018-01-28 16:03:54 +00: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",
class: "form-control",
2018-01-30 05:49:50 +00:00
placeholder: "New comment...",
name: "content"
2018-01-28 16:03:54 +00:00
});
2018-01-30 05:49:50 +00:00
2018-01-28 16:03:54 +00:00
//Add button group
var buttonsGroup = createElem2({
appendTo: inputGroup,
type: "span",
class: "input-group-btn"
});
//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 05:49:50 +00:00
elemType: "file",
name: "image"
2018-01-28 16:03:54 +00:00
});
var imageButton = createElem2({
appendTo: addImageLabel,
type: "a",
class: "btn btn-flat",
innerHTML: "<i class='fa fa-picture-o'></i>"
})
//Add send button
var sendButton = createElem2({
appendTo: buttonsGroup,
type: "button",
class: "btn btn-default btn-flat",
innerHTML: "Send"
});
//Catch form when submitted
commentForm.onsubmit = function(){
2018-01-30 05:49:50 +00:00
//Check for image
var hasImage = imageFile.files.length > 0;
//Check the comment
if(!hasImage && newCommentText.value < 5){
ComunicWeb.common.notificationSystem.showNotification("Please type a valid comment! (at least 5 characters)", "danger");
return false;
}
//Lock send button
sendButton.disabled = true;
//Try to create the comment
var formData = new FormData(commentForm);
ComunicWeb.components.comments.interface.create(postID, formData, function(result){
//Unlock send button
sendButton.disabled = false;
//Check for errors
if(result.error){
ComunicWeb.common.notificationSystem.showNotification("Couldn't create comment! (check its content)", "danger");
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 10:33:02 +00:00
type: "div",
class: "box-comment"
});
ComunicWeb.components.comments.actions.reload(result.commentID, newCommentTarget);
2018-01-30 05:49:50 +00:00
});
2018-01-28 16:03:54 +00:00
return false;
}
},
}