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

View File

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

View File

@ -1,33 +1,40 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
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
// ----------------------------------------------------------------------------
// (string) $_POST['title']
$title = isset($_POST['title']) ? $_POST['title'] : false;
// (string) $_POST['content']
$content = isset($_POST['content']) ? $_POST['content'] : false;
// (string) $_POST['uuid']
$uuid = isset($_POST['uuid']) ? $_POST['uuid'] : false;
// ----------------------------------------------------------------------------
// Check 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(
'uuid'=>$autosaveUUID,
'key'=>$autosaveUUID,
'slug'=>$autosaveUUID,
'title'=>$title.' [ Autosave ] ',
'uuid'=>$uuid,
'key'=>$uuid,
'slug'=>$uuid,
'title'=>$title,
'content'=>$content,
'type'=>'draft'
);
// 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 (empty($pageKey)) {
@ -36,8 +43,8 @@ if (empty($pageKey)) {
editPage($page);
}
ajaxResponse(0, 'Autosave successfully.', array(
'uuid'=>$autosaveUUID
ajaxResponse(0, 'Save as draft successfully.', array(
'uuid'=>$uuid
));
?>

View File

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