ComunicWeb/assets/js/components/posts/form.js

649 lines
17 KiB
JavaScript
Raw Permalink Normal View History

2018-01-04 17:51:49 +00:00
/**
* Posts creation form
*
* @author Pierre HUBERT
*/
ComunicWeb.components.posts.form = {
/**
* Display post creation form
*
* @param {string} kind The kind of page
* @param {int} id The ID of the page
* @param {HTMLElement} target The target of the form
*/
display: function(kind, id, target){
//Log action
ComunicWeb.debug.logMessage("Display post creation form");
//Create form creation box
var boxRoot = createElem2({
appendTo: target,
type: "div",
class: "box box-primary post-form"
});
//Create box body
var boxBody = createElem2({
appendTo: boxRoot,
type: "div",
class: "box-body"
});
2018-04-20 15:03:37 +00:00
//Make sure emojie picker is already initialized
ComunicWeb.components.emoji.picker.init();
//Create post message contener
var newPostMessageContener = createElem2({
appendTo: boxBody,
type: "div"
});
2018-01-04 17:51:49 +00:00
//Create post message textarea
2018-12-27 13:02:01 +00:00
var inputMessageTextarea = createElem2({
2018-04-20 15:03:37 +00:00
appendTo: newPostMessageContener,
2018-12-27 13:02:01 +00:00
type: "textarea",
class: "new-message",
2018-01-06 14:05:55 +00:00
innerHTML: ""
2018-01-04 17:51:49 +00:00
});
2018-12-27 13:02:01 +00:00
var inputMessageToolbarTarget = createElem2({
appendTo: newPostMessageContener,
2018-12-27 18:07:39 +00:00
type: "div",
class: "new-message-content-container"
2018-12-27 13:02:01 +00:00
});
sceditor.create(inputMessageTextarea, {
format: 'bbcode',
width: "100%",
2018-12-27 18:07:39 +00:00
toolbar: 'bold,italic,underline,subscript,superscript,' +
'left,center,right,justify,color,' +
'bulletlist,orderedlist,table,code,quote,source',
emoticonsEnabled: false,
icons: "material",
autoExpand: true,
resizeMaxHeight: -1,
2018-12-27 13:02:01 +00:00
toolbarContainer: inputMessageToolbarTarget
});
2018-01-04 17:51:49 +00:00
2019-01-10 15:28:59 +00:00
//Apply all stylesheets to the editor iframe
ApplySceditorStyle(inputMessageTextarea);
2018-04-20 15:03:37 +00:00
//Enable emojies picker
2019-01-10 13:59:12 +00:00
ComunicWeb.components.emoji.picker.addDetachedPicker(newPostMessageContener, function(emojie){
2018-12-27 13:02:01 +00:00
//Append new emojie to the instance
sceditor.instance(inputMessageTextarea).insertText(emojie);
});
2018-04-20 15:03:37 +00:00
2018-01-04 18:39:22 +00:00
//Add the different post types
2018-03-25 07:43:39 +00:00
var postTypesContainer = createElem2({
2018-01-04 18:39:22 +00:00
appendTo: boxBody,
type: "div",
class: "post-types"
});
//Text
2018-03-25 07:43:39 +00:00
var textType = this._add_post_type(postTypesContainer, "text", lang("_post_type_text"));
2018-01-05 08:11:41 +00:00
textType.checked = true;
2018-01-04 18:39:22 +00:00
//Image
2018-03-25 07:43:39 +00:00
var imageType = this._add_post_type(postTypesContainer, "image", "<i class='fa fa-picture-o'></i> <span class='hidden-xs'>"+lang("_post_type_image")+"</span>");
2018-01-04 18:39:22 +00:00
//Youtube
2018-03-25 07:43:39 +00:00
var youtubeType = this._add_post_type(postTypesContainer, "youtube", "<i class='fa fa-youtube-play'></i> <span class='hidden-xs'>"+lang("_post_type_youtube")+"</span>");
2018-01-04 18:39:22 +00:00
//Link
2018-03-25 07:43:39 +00:00
var linkType = this._add_post_type(postTypesContainer, "link", "<i class='fa fa-link'></i> <span class='hidden-xs'>"+lang("_post_type_link")+"</span>");
2018-01-04 18:39:22 +00:00
//PDF
2018-03-25 07:43:39 +00:00
var pdfType = this._add_post_type(postTypesContainer, "pdf", "<i class='fa fa-file-pdf-o'></i> <span class='hidden-xs'>"+lang("_post_type_pdf")+"</span>");
2018-01-04 18:39:22 +00:00
//Countdown timer
2018-03-25 07:43:39 +00:00
var countdownType = this._add_post_type(postTypesContainer, "countdown", "<i class='fa fa-clock-o'></i> <span class='hidden-xs'>"+lang("_post_type_countdown")+"</span>");
2018-01-04 18:39:22 +00:00
//Survey
2018-03-25 07:43:39 +00:00
var surveyType = this._add_post_type(postTypesContainer, "survey", "<i class='fa fa-pie-chart'></i> <span class='hidden-xs'>"+lang("_post_type_survey")+"</span>");
2018-01-04 17:51:49 +00:00
2018-01-05 08:41:27 +00:00
2018-01-05 16:03:40 +00:00
2018-01-05 10:23:07 +00:00
//Add image upload form
var imgUploadForm = createElem2({
appendTo: boxBody,
type: "div",
class: "post-image"
});
var imgFileInput = createElem2({
appendTo: imgUploadForm,
type: "input",
elemType: "file"
});
2018-01-05 16:03:40 +00:00
//End : image
2018-01-05 10:23:07 +00:00
//Add Youtube input form
var youtubeInputForm = createElem2({
appendTo: boxBody,
type: "div",
class: "post-youtube",
});
var youtubeLinkInput = createFormGroup({
target: youtubeInputForm,
2018-03-11 16:12:08 +00:00
label: lang("_input_youtube_link_label"),
2018-01-05 10:23:07 +00:00
placeholder: "https://youtube.com/watch?v=",
type: "text"
2018-01-05 16:03:40 +00:00
});
//End : Youtube
2018-01-05 16:44:42 +00:00
//Add webpage input form
var linkChooseForm = createElem2({
appendTo: boxBody,
type: "div",
class: "post-weblink"
});
var linkInput = createFormGroup({
target: linkChooseForm,
2018-03-11 16:12:08 +00:00
label: lang("_input_page_url_label"),
2018-01-05 16:44:42 +00:00
placeholder: "https://...",
type: "text"
});
//End : webpage
//Add PDF specific informations
var pdfUploadForm = createElem2({
appendTo: boxBody,
type: "div",
class: "post-pdf"
});
var pdfFileInput = createElem2({
appendTo: pdfUploadForm,
type: "input",
elemType: "file"
});
//End : PDF
2018-01-05 16:03:40 +00:00
//Add countdown timer specific information
var countdownForm = createElem2({
appendTo: boxBody,
type: "div",
class: "post-countdown"
});
//Date end
var dateEndInput = createFormGroup({
target: countdownForm,
2018-03-11 16:12:08 +00:00
label: lang("_input_countdown_enddate"),
placeholder: "dd/mm/yyyy",
type: "text"
});
$(dateEndInput).datepicker({
autoclose: true,
format: "dd/mm/yyyy"
});
//Time end
var container = createElem2({
appendTo: countdownForm,
type: "div",
class: "bootstrap-timepicker"
});
var timeEndInput = createFormGroup({
target: container,
label: lang("_input_countdown_endtime"),
placeholder: "hh:ss",
type: "text"
});
timeEndInput.className += " timepicker";
$(timeEndInput).timepicker({
showInputs: false,
showMeridian: false,
});
//End : countdown timer
//Add survey specific informations
var surveyForm = createElem2({
appendTo: boxBody,
type: "div",
class: "post-survey"
});
//Survey question
var surveyQuestionInput = createFormGroup({
target: surveyForm,
2018-03-11 16:12:08 +00:00
label: lang("_input_survey_question_label"),
placeholder: lang("_input_survey_question_placeholder"),
type: "text"
});
//Survey answers
var surveyAnswerInput = createFormGroup({
target: surveyForm,
2018-03-11 16:12:08 +00:00
label: lang("_input_survey_answers_label"),
type: "select2",
multiple: true,
});
2020-05-17 16:12:02 +00:00
// Specify whether anyone can add suggestions to the survey or not
const allowNewChoicesInput = createFormGroup({
target: surveyForm,
type: "checkbox",
label: tr("Allow everyone to add new answers after the creation of the survey"),
checked: false
});
//Survey message help
var surveyAnswerHelper = createElem2({
appendTo: surveyForm,
type: "p",
2018-03-11 16:12:08 +00:00
innerHTML: lang("_input_survey_answers_hint"),
})
//Enable select2
$(surveyAnswerInput).select2({
tags: true,
tokenSeparators: ['\n'],
dropdownParent: $(surveyQuestionInput)
});
//End: survey specific
2018-01-05 10:23:07 +00:00
//Create post type change handler
var changesHandler = function(){
imgUploadForm.style.display = imageType.checked ? "block" : "none";
youtubeInputForm.style.display = youtubeType.checked ? "block" : "none";
2018-01-05 16:44:42 +00:00
linkChooseForm.style.display = linkType.checked ? "block" : "none";
pdfUploadForm.style.display = pdfType.checked ? "block" : "none";
countdownForm.style.display = countdownType.checked ? "block" : "none";
surveyForm.style.display = surveyType.checked ? "block" : "none";
2018-01-05 10:23:07 +00:00
};
//Apply changesHandler function to all the data types
textType.onclick = changesHandler;
imageType.onclick = changesHandler;
youtubeType.onclick = changesHandler;
linkType.onclick = changesHandler;
pdfType.onclick = changesHandler;
countdownType.onclick = changesHandler;
surveyType.onclick = changesHandler;
2018-03-25 07:43:39 +00:00
//Right container
2018-01-05 08:41:27 +00:00
var rightDiv = createElem2({
appendTo: boxBody,
type: "div",
class: "pull-right"
})
//Add visibility level choice
2018-03-25 07:43:39 +00:00
var visibility_choices_container = createElem2({
2018-01-05 08:41:27 +00:00
appendTo: rightDiv,
type: "div",
2018-03-25 07:43:39 +00:00
class: "post-visiblity-container"
2018-01-05 08:41:27 +00:00
});
//For posts on users page
if(kind == "user"){
//Private post
var privateLevel = ComunicWeb.components.posts.visibilityLevels.private;
var privateInput = this._add_visiblity_choice(visibility_choices_container, "private", privateLevel.name, privateLevel.icon);
//Friends-visible post
var friendsLevel = ComunicWeb.components.posts.visibilityLevels.friends;
var friendsInput = this._add_visiblity_choice(visibility_choices_container, "friends", friendsLevel.name, friendsLevel.icon);
friendsInput.checked = true;
}
2018-01-05 08:41:27 +00:00
//For posts on groups page
if(kind == "group"){
//Members-visible posts
var membersLevel = ComunicWeb.components.posts.visibilityLevels.friends;
var membersInput = this._add_visiblity_choice(visibility_choices_container, "members", membersLevel.name, membersLevel.icon);
membersInput.checked = true;
}
2018-01-05 08:41:27 +00:00
//Worldwide post
var publicLevel = ComunicWeb.components.posts.visibilityLevels.public;
2018-03-25 07:43:39 +00:00
this._add_visiblity_choice(visibility_choices_container, "public", publicLevel.name, publicLevel.icon);
2018-01-05 08:41:27 +00:00
2018-01-04 17:51:49 +00:00
//Add send button
var sendButton = createElem2({
2018-01-05 08:41:27 +00:00
appendTo: rightDiv,
2018-01-04 17:51:49 +00:00
type: "button",
2018-01-05 08:41:27 +00:00
class: "btn btn-primary",
2018-03-11 16:12:08 +00:00
innerHTML: lang("_send")
2018-01-04 17:51:49 +00:00
});
2018-01-06 14:05:55 +00:00
//Make send button lives
sendButton.onclick = function(){
//Generate request
var datas = new FormData();
//Get the message content
2018-12-27 13:02:01 +00:00
var message_content = sceditor.instance(inputMessageTextarea).getWysiwygEditorValue();
2018-01-06 14:05:55 +00:00
datas.append("content", message_content);
//Check if the message includes an image
if(message_content.includes("data:image/")){
2018-03-11 16:12:08 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("_err_drag_image_post"), "danger");
2018-01-06 14:05:55 +00:00
return;
}
//Check the text value
if(textType.checked){
//Check message content
if(!ComunicWeb.components.posts.form._check_message(message_content)){
2018-08-04 07:15:02 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("form_post_err_invalid_message"), "danger");
2018-01-06 14:05:55 +00:00
return;
}
//Specify it is a text
datas.append("kind", "text");
}
//Check for image
else if(imageType.checked){
//Check for image
if(imgFileInput.files.length == 0){
2018-08-04 07:15:02 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("form_post_err_no_image_selected"), "danger");
2018-01-06 14:05:55 +00:00
return;
}
//Append values
datas.append("kind", "image");
datas.append("image", imgFileInput.files[0], imgFileInput.files[0].name);
}
//Check for YouTube video
else if(youtubeType.checked){
//Get the video ID
var videoID = ComunicWeb.components.posts.form._get_youtube_video_id(youtubeLinkInput.value);
//Check its validity
if(!videoID){
2018-08-04 07:15:02 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("form_post_err_invalid_youtube_link"), "danger");
2018-01-06 14:05:55 +00:00
return;
}
//Append values
datas.append("kind", "youtube");
datas.append("youtube_id", videoID);
}
//Check for PDF
else if(pdfType.checked){
//Check for image
if(pdfFileInput.files.length == 0){
2018-08-04 07:15:02 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("form_post_err_no_pdf_selected"), "danger");
2018-01-06 14:05:55 +00:00
return;
}
//Append values
datas.append("kind", "pdf");
2018-01-07 15:29:20 +00:00
datas.append("pdf", pdfFileInput.files[0], pdfFileInput.files[0].name);
2018-01-06 14:05:55 +00:00
}
//Check for weblink
else if(linkType.checked){
//Check the given url
if(!check_url(linkInput.value)){
2018-08-04 07:15:02 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("form_post_err_invalid_url"), "danger");
2018-01-06 14:05:55 +00:00
return;
}
//Append values
datas.append("kind", "weblink");
datas.append("url", linkInput.value);
}
//Check for timer
else if(countdownType.checked){
//Check the given time
if(dateEndInput.value.length < 10){
2018-08-04 07:15:02 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("form_post_err_no_end_date_selected"), "danger");
2018-01-06 14:05:55 +00:00
return;
}
//Convert the date to an array
var end_date_array = dateEndInput.value.split("/");
if(end_date_array.length < 3) {
2018-08-04 07:15:02 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("form_post_err_selected_end_date_invalid"), "danger");
return;
}
//Convert the time to an array (if any)
if(timeEndInput.value.length > 3){
//Get the specified time
var time_end = timeEndInput.value.split(":");
var hours = time_end[0];
var minutes = time_end[1];
}
else {
//Default values
var hours = 0;
var minutes = 0;
}
//Convert the end time to a timestamp
var end_date = new Date();
end_date.setHours(hours, minutes, 0);
end_date.setDate(end_date_array[0]);
2018-01-07 17:44:49 +00:00
end_date.setMonth(end_date_array[1] - 1); //January => 0 / December => 11
end_date.setFullYear(end_date_array[2]);
var time_end = Math.floor(end_date.getTime()/1000);
2018-01-06 14:05:55 +00:00
//Append values
datas.append("kind", "countdown");
datas.append("time-end", time_end);
2018-01-06 14:05:55 +00:00
}
//Check for survey
else if(surveyType.checked){
//Check the given question
if(surveyQuestionInput.value.length < 5){
2018-08-04 07:15:02 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("form_post_err_no_question_for_survey"), "danger");
2018-01-06 14:05:55 +00:00
return;
}
//Get the answers
if(surveyAnswerInput.children.length < 2){
2018-08-04 07:15:02 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("form_post_err_not_enough_survey_options"), "danger");
2018-01-06 14:05:55 +00:00
return;
}
//Process the list of answers
var answerData = $(surveyAnswerInput).select2("data");
var answers = [];
for(i = 0; i < answerData.length; i++){
answers.push(removeHtmlTags(answerData[i].text));
}
//Append values
datas.append("kind", "survey");
datas.append("question", surveyQuestionInput.value);
datas.append("answers", answers.join("<>"));
2020-05-17 16:12:02 +00:00
datas.append("allowNewAnswers", allowNewChoicesInput.checked);
2018-01-06 14:05:55 +00:00
}
//The post type is not supported
else {
2018-08-04 07:15:02 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("form_post_err_no_post_type_selected"), "danger");
2018-01-06 14:05:55 +00:00
return;
}
2018-01-06 14:13:55 +00:00
//Get the visibility level
2018-03-25 07:43:39 +00:00
var visibilityLevel = visibility_choices_container.querySelector("input:checked").value;
2018-01-06 14:13:55 +00:00
datas.append("visibility", visibilityLevel);
//Lock the send button
sendButton.disabled = true;
2018-01-06 14:13:55 +00:00
//Try to perform the request
ComunicWeb.components.posts.interface.send_post(kind, id, datas, function(result){
//Check for errors
if(result.error){
2018-08-04 07:15:02 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("form_post_err_send_new_post"), "danger");
sendButton.disabled = false;
return;
}
//Else
//Display a success notification
2018-08-04 07:15:02 +00:00
ComunicWeb.common.notificationSystem.showNotification(lang("form_post_success_create_post"), "success");
//Refresh current page
ComunicWeb.common.page.refresh_current_page();
});
2018-01-06 14:05:55 +00:00
}
2018-01-05 08:11:41 +00:00
},
/**
* Create and add post type choice
*
* @param {HTMLElement} target The target for the post type
* @param {string} value The value of the new post type
* @param {string} label The label associated with the post type
* @return {HTMLElement} The created input
*/
_add_post_type: function(target, value, label){
2018-03-25 07:43:39 +00:00
var postTypeContainer = createElem2({
2018-01-05 08:11:41 +00:00
appendTo: target,
type: "label",
class: "post-form-choice"
});
var input = createElem2({
2018-03-25 07:43:39 +00:00
appendTo: postTypeContainer,
2018-01-05 08:11:41 +00:00
type: "input",
elemType: "radio",
name: "post_type",
value: value
});
2018-01-04 17:51:49 +00:00
2018-01-05 08:11:41 +00:00
createElem2({
2018-03-25 07:43:39 +00:00
appendTo: postTypeContainer,
2018-01-05 08:11:41 +00:00
type: "span",
innerHTML: label
});
return input;
2018-01-05 08:41:27 +00:00
},
/**
* Create and add visibility level choice
*
* @param {HTMLElement} target The target for the visibility level
* @param {string} value The value of the visibility level
* @param {string} title The title of the visibility level
* @param {string} icon The name of the icon associated with the visibility level
* @return {HTMLElement} The created input
*/
_add_visiblity_choice: function(target, value, title, icon){
//Visibility label
2018-03-25 07:43:39 +00:00
var visibility_container = createElem2({
2018-01-05 08:41:27 +00:00
appendTo: target,
type: "label",
});
//Create input
var visibilityInput = createElem2({
2018-03-25 07:43:39 +00:00
appendTo: visibility_container,
2018-01-05 08:41:27 +00:00
type: "input",
elemType: "radio",
name: "post_visibility",
value: value
});
//Create icon
var visibility_label = createElem2({
2018-03-25 07:43:39 +00:00
appendTo: visibility_container,
2018-01-05 08:41:27 +00:00
type: "span",
innerHTML: "<i class='fa " + icon + "'></i>"
});
return visibilityInput;
},
2018-01-06 14:05:55 +00:00
/**
* Check a given message content
*
* @param {string} message The message to check
* @return {boolean} TRUE if the message is valid / false else
*/
2018-01-14 07:13:59 +00:00
_check_message: function(message){
2018-01-06 14:05:55 +00:00
//Remove break line tags
message = message.replace("<br>", "")
.replace("<br/>", "")
.replace("<br />", "")
.replace("<p>", "")
.replace("</p>", "")
.replace("<b>", "")
.replace("</b>", "");
//Check if the message is too short
if(message.length < 5)
return false;
//The message is valid
return true;
},
/**
* Try to get a youtube video ID from a given URL
*
* @param {string} url The string to get
* @return {boolean|string} False if the URL is invalid / The video
* ID else
*/
_get_youtube_video_id: function(url){
//Check if the youtube domain is included in the URL
if(!url.includes("youtube.com/"))
return false; //The link is considered as invalid
//Check for ID specification
if(!url.includes("v="))
return false;
//Extract video ID
var videoID = url.split("v=")[1];
//Check if there are other parametres after the video ID
if(videoID.includes("&"))
videoID = videoID.split("&")[0];
//Check if the videoID is valid
if(videoID.includes("/"))
return false;
//Return video ID
return videoID;
},
2018-01-04 17:51:49 +00:00
}