Autosave and preview function migrated to fetch function

This commit is contained in:
Diego Najar 2019-05-09 19:31:55 +02:00
parent b32bc1e054
commit 38092a051c
4 changed files with 88 additions and 57 deletions

View File

@ -400,8 +400,9 @@ $(document).ready(function() {
var title = $("#jstitle").val(); var title = $("#jstitle").val();
var content = editorGetContent(); var content = editorGetContent();
var ajax = new bluditAjax(); var ajax = new bluditAjax();
ajax.autosave(uuid, title, content, false); bluditAjax.preview(uuid, title, content).then(function(data) {
window.open("<?php echo DOMAIN_PAGES.'autosave-'.$page->uuid().'?preview='.md5('autosave-'.$page->uuid()) ?>", "_blank"); window.open("<?php echo DOMAIN_PAGES.'autosave-'.$uuid.'?preview='.md5('autosave-'.$uuid) ?>", "_blank");
});
}); });
// Button Save // Button Save
@ -444,8 +445,11 @@ $(document).ready(function() {
// Call autosave only when the user change the content // Call autosave only when the user change the content
if (currentContent!=content) { if (currentContent!=content) {
currentContent = content; currentContent = content;
// showAlert is the function to display an alert defined in alert.php bluditAjax.autosave(uuid, title, content).then(function(data) {
ajax.autosave(uuid, title, content, showAlert); if (data.status==0) {
showAlert("Autosave success");
}
});
} }
},1000*60*AUTOSAVE_INTERVAL); },1000*60*AUTOSAVE_INTERVAL);

View File

@ -349,9 +349,9 @@ $(document).ready(function() {
var uuid = $("#jsuuid").val(); var uuid = $("#jsuuid").val();
var title = $("#jstitle").val(); var title = $("#jstitle").val();
var content = editorGetContent(); var content = editorGetContent();
var ajax = new bluditAjax(); bluditAjax.preview(uuid, title, content).then(function(data) {
ajax.autosave(uuid, title, content, false); window.open("<?php echo DOMAIN_PAGES.'autosave-'.$uuid.'?preview='.md5('autosave-'.$uuid) ?>", "_blank");
window.open("<?php echo DOMAIN_PAGES.'autosave-'.$uuid.'?preview='.md5('autosave-'.$uuid) ?>", "_blank"); });
}); });
// Button Save // Button Save
@ -373,13 +373,20 @@ $(document).ready(function() {
// Autosave // Autosave
// Autosave works when the content of the page is bigger than 100 characters // Autosave works when the content of the page is bigger than 100 characters
var currentContent = editorGetContent();
setInterval(function() { setInterval(function() {
var uuid = $("#jsuuid").val(); var uuid = $("#jsuuid").val();
var title = $("#jstitle").val(); var title = $("#jstitle").val();
var content = editorGetContent(); var content = editorGetContent();
var ajax = new bluditAjax(); // Call autosave only when the user change the content
// showAlert is the function to display an alert defined in alert.php if (currentContent!=content) {
ajax.autosave(uuid, title, content, showAlert); currentContent = content;
bluditAjax.autosave(uuid, title, content).then(function(data) {
if (data.status==0) {
showAlert("Autosave success");
}
});
}
},1000*60*AUTOSAVE_INTERVAL); },1000*60*AUTOSAVE_INTERVAL);
}); });

View File

@ -1,33 +1,40 @@
<?php defined('BLUDIT') or die('Bludit CMS.'); <?php defined('BLUDIT') or die('Bludit CMS.');
header('Content-Type: application/json'); header('Content-Type: application/json');
/*
| Create/Edit a page and save as draft
| If the UUID already exists the page is updated
|
| @_POST['title'] string Page title
| @_POST['content'] string Page content
| @_POST['uuid'] string Page uuid
|
| @return array
*/
// $_POST // $_POST
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// (string) $_POST['title']
$title = isset($_POST['title']) ? $_POST['title'] : false; $title = isset($_POST['title']) ? $_POST['title'] : false;
// (string) $_POST['content']
$content = isset($_POST['content']) ? $_POST['content'] : false; $content = isset($_POST['content']) ? $_POST['content'] : false;
// (string) $_POST['uuid']
$uuid = isset($_POST['uuid']) ? $_POST['uuid'] : false; $uuid = isset($_POST['uuid']) ? $_POST['uuid'] : false;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Check UUID // Check UUID
if (empty($uuid)) { if (empty($uuid)) {
ajaxResponse(1, 'Autosave fail. UUID not defined.'); ajaxResponse(1, 'Save as draft fail. UUID not defined.');
} }
$autosaveUUID = 'autosave-'.$uuid;
$page = array( $page = array(
'uuid'=>$autosaveUUID, 'uuid'=>$uuid,
'key'=>$autosaveUUID, 'key'=>$uuid,
'slug'=>$autosaveUUID, 'slug'=>$uuid,
'title'=>$title.' [ Autosave ] ', 'title'=>$title,
'content'=>$content, 'content'=>$content,
'type'=>'draft' 'type'=>'draft'
); );
// Get the page key by the UUID // Get the page key by the UUID
$pageKey = $pages->getByUUID($autosaveUUID); $pageKey = $pages->getByUUID($uuid);
// if pageKey is empty means the autosave page doesn't exist // if pageKey is empty means the autosave page doesn't exist
if (empty($pageKey)) { if (empty($pageKey)) {
@ -36,8 +43,8 @@ if (empty($pageKey)) {
editPage($page); editPage($page);
} }
ajaxResponse(0, 'Autosave successfully.', array( ajaxResponse(0, 'Save as draft successfully.', array(
'uuid'=>$autosaveUUID 'uuid'=>$uuid
)); ));
?> ?>

View File

@ -1,45 +1,58 @@
class bluditAjax { class bluditAjax {
// Autosave works only when the content has more than 100 characters static async preview(uuid, title, content) {
// callBack function need to be showAlert(), this function is for display alerts to the user, defined in alert.php let url = HTML_PATH_ADMIN_ROOT+"ajax/save-as-draft"
autosave(uuid, title, content, callBack) { try {
var ajaxRequest; const response = await fetch(url, {
if (ajaxRequest) { credentials: 'same-origin',
ajaxRequest.abort(); method: "POST",
headers: new Headers({
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}),
body: new URLSearchParams({
'tokenCSRF': tokenCSRF,
'uuid': "autosave-" + uuid,
'title': title,
'content': content
}),
});
const json = await response.json();
return json;
} }
catch (err) {
console.log(err);
return true;
}
}
if ((content.length<100) && callBack) { // Autosave works only when the content has more than 100 characters
static async autosave(uuid, title, content) {
if ((content.length<100)) {
return false; return false;
} }
ajaxRequest = $.ajax({ let url = HTML_PATH_ADMIN_ROOT+"ajax/save-as-draft"
type: "POST", try {
data: { const response = await fetch(url, {
tokenCSRF: tokenCSRF, // token from env variables credentials: 'same-origin',
uuid: uuid, method: "POST",
title: title, headers: new Headers({
content: content 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}, }),
url: HTML_PATH_ADMIN_ROOT+"ajax/save-as-draft" body: new URLSearchParams({
}); 'tokenCSRF': tokenCSRF,
'uuid': "autosave-" + uuid,
ajaxRequest.done(function (response, textStatus, jqXHR) { 'title': title+" [Autosave]",
console.log("Bludit AJAX: autosave(): done handler"); 'content': content
if (callBack) { }),
callBack("Autosave success"); });
} const json = await response.json();
}); return json;
}
ajaxRequest.fail(function (jqXHR, textStatus, errorThrown) { catch (err) {
console.log("Bludit AJAX: autosave(): fail handler"); console.log(err);
if (callBack) { return true;
callBack("Autosave failure"); }
}
});
ajaxRequest.always(function () {
console.log("Bludit AJAX: autosave(): always handler");
});
} }
// Alert the user when the user is not logged // Alert the user when the user is not logged