2018-01-14 07:56:17 +00:00
|
|
|
/**
|
|
|
|
* Post editor
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
ComunicWeb.components.posts.edit = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open post editor
|
|
|
|
*
|
|
|
|
* @param {Object} infos Informations about the post to edit
|
|
|
|
* @param {HTMLElement} root Post root element
|
|
|
|
*/
|
|
|
|
open: function(infos, root){
|
|
|
|
|
|
|
|
//Create editor modal
|
|
|
|
var modal = createElem2({
|
|
|
|
type: "div",
|
|
|
|
class: "modal modal-default edit-post-modal"
|
|
|
|
});
|
|
|
|
|
|
|
|
var modalDialog = createElem2({
|
|
|
|
appendTo: modal,
|
|
|
|
type: "div",
|
|
|
|
class: "modal-dialog"
|
|
|
|
});
|
|
|
|
|
|
|
|
var modalContent = createElem2({
|
|
|
|
appendTo: modalDialog,
|
|
|
|
type: "div",
|
|
|
|
class: "modal-content",
|
|
|
|
});
|
|
|
|
|
|
|
|
//Modal header
|
|
|
|
var modalHeader = createElem2({
|
|
|
|
appendTo: modalContent,
|
|
|
|
type: "div",
|
|
|
|
class: "modal-header"
|
|
|
|
});
|
|
|
|
|
|
|
|
var closeModalBtn = createElem2({
|
|
|
|
appendTo: modalHeader,
|
|
|
|
type: "button",
|
|
|
|
class: "close",
|
|
|
|
});
|
|
|
|
closeModalBtn.setAttribute("data-confirm", "false");
|
|
|
|
|
|
|
|
createElem2({
|
|
|
|
appendTo: closeModalBtn,
|
|
|
|
type: "span",
|
|
|
|
innerHTML: "x"
|
|
|
|
});
|
|
|
|
|
|
|
|
var modalTitle = createElem2({
|
|
|
|
appendTo: modalHeader,
|
|
|
|
type: "h4",
|
|
|
|
class: "modal-title",
|
2018-08-05 14:43:23 +00:00
|
|
|
innerHTML: lang("posts_edit_title")
|
2018-01-14 07:56:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//Modal body
|
|
|
|
var modalBody = createElem2({
|
|
|
|
appendTo: modalContent,
|
|
|
|
type: "div",
|
|
|
|
class: "modal-body",
|
|
|
|
});
|
|
|
|
|
|
|
|
//Modal footer
|
|
|
|
var modalFooter = createElem2({
|
|
|
|
appendTo: modalContent,
|
|
|
|
type: "div",
|
|
|
|
class: "modal-footer"
|
|
|
|
});
|
|
|
|
|
|
|
|
var cancelButton = createElem2({
|
|
|
|
appendTo: modalFooter,
|
|
|
|
type: "button",
|
|
|
|
class: "btn btn-default pull-left",
|
2018-08-05 14:43:23 +00:00
|
|
|
innerHTML: lang("posts_edit_cancel")
|
2018-01-14 07:56:17 +00:00
|
|
|
});
|
|
|
|
cancelButton.setAttribute("data-confirm", "false");
|
|
|
|
|
|
|
|
var confirmButton = createElem2({
|
|
|
|
appendTo: modalFooter,
|
|
|
|
type: "button",
|
|
|
|
class: "btn btn-primary",
|
2018-08-05 14:43:23 +00:00
|
|
|
innerHTML: lang("posts_edit_update")
|
2018-01-14 07:56:17 +00:00
|
|
|
});
|
|
|
|
confirmButton.setAttribute("data-confirm", "true");
|
|
|
|
|
|
|
|
//Show the modal
|
|
|
|
$(modal).modal('show');
|
|
|
|
|
|
|
|
//Create the update area
|
|
|
|
var updateDiv = createElem2({
|
|
|
|
appendTo: modalBody,
|
|
|
|
type: "div",
|
2018-03-25 07:43:39 +00:00
|
|
|
class: "editor-container"
|
2018-01-14 07:56:17 +00:00
|
|
|
});
|
|
|
|
|
2018-05-07 16:15:49 +00:00
|
|
|
//iOS fix : use slimscroll if required
|
|
|
|
updateDiv.style.maxHeight = (window.innerHeight - 200) + "px";
|
|
|
|
$(updateDiv).slimscroll({
|
|
|
|
height: "100%"
|
|
|
|
});
|
|
|
|
|
2018-01-14 07:56:17 +00:00
|
|
|
//Create update editor
|
2018-12-27 13:02:01 +00:00
|
|
|
var editorTextarea = createElem2({
|
2018-01-14 07:56:17 +00:00
|
|
|
appendTo: updateDiv,
|
2018-12-27 13:02:01 +00:00
|
|
|
type: "textarea",
|
2018-01-14 07:56:17 +00:00
|
|
|
class: "editor",
|
2018-12-27 13:02:01 +00:00
|
|
|
value: infos.content
|
|
|
|
});
|
|
|
|
|
|
|
|
sceditor.create(editorTextarea, {
|
|
|
|
format: 'bbcode',
|
|
|
|
height: "200px",
|
|
|
|
width: "100%",
|
|
|
|
toolbarExclude: "youtube,image,size,link"
|
2018-01-14 07:56:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//Create function to close modal
|
|
|
|
var closeModal = function(){
|
|
|
|
$(modal).modal('hide');
|
|
|
|
emptyElem(modal);
|
|
|
|
modal.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Handles update request
|
|
|
|
var callback = function(){
|
|
|
|
|
|
|
|
//Check if the update was cancelled
|
|
|
|
if(this.getAttribute("data-confirm") !== "true"){
|
|
|
|
closeModal();
|
|
|
|
return; //Cancel operation
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get the new post content
|
2018-12-27 13:02:01 +00:00
|
|
|
var new_content = sceditor.instance(editorTextarea).getWysiwygEditorValue();
|
2018-01-14 07:56:17 +00:00
|
|
|
|
|
|
|
//Check the new post content
|
|
|
|
if(!ComunicWeb.components.posts.form._check_message(new_content)){
|
2018-08-05 14:43:23 +00:00
|
|
|
ComunicWeb.common.notificationSystem.showNotification(lang("posts_edit_err_invalid_content"), "danger");
|
2018-01-14 07:56:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Lock update button
|
|
|
|
confirmButton.disabled = "true";
|
|
|
|
|
|
|
|
//Perform a request on the API to update message content
|
|
|
|
ComunicWeb.components.posts.interface.update_content(infos.ID, new_content, function(response){
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(response.error){
|
2018-08-05 14:43:23 +00:00
|
|
|
ComunicWeb.common.notificationSystem.showNotification(lang("posts_edit_err_update_content"), "danger");
|
2018-01-14 07:56:17 +00:00
|
|
|
confirmButton.disabled = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Display success
|
2018-08-05 14:43:23 +00:00
|
|
|
ComunicWeb.common.notificationSystem.showNotification(lang("posts_edit_success_update"), "success");
|
2018-01-14 07:56:17 +00:00
|
|
|
|
2018-01-16 05:50:04 +00:00
|
|
|
//Reload post
|
|
|
|
ComunicWeb.components.posts.actions.reload_post(infos.ID, root);
|
|
|
|
|
|
|
|
//Close the modal
|
|
|
|
closeModal();
|
|
|
|
|
2018-01-14 07:56:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
closeModalBtn.onclick = callback;
|
|
|
|
cancelButton.onclick = callback;
|
|
|
|
confirmButton.onclick = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|