bludit/bl-kernel/js/bludit-ajax.php

106 lines
2.7 KiB
PHP
Raw Normal View History

2018-05-08 00:15:40 +02:00
class bluditAjax {
2018-06-03 21:51:47 +02:00
// Autosave works only when the content has more than 100 characters
2018-06-24 13:37:45 +02:00
// callBack function need to be showAlert(), this function is for display alerts to the user, defined in alert.php
2018-05-08 00:15:40 +02:00
autosave(uuid, title, content, callBack) {
var ajaxRequest;
if (ajaxRequest) {
ajaxRequest.abort();
}
if (content.length<100) {
return false;
}
ajaxRequest = $.ajax({
type: "POST",
data: {
tokenCSRF: tokenCSRF, // token from env variables
uuid: uuid,
title: title,
content: content
},
url: "<?php echo HTML_PATH_ADMIN_ROOT ?>ajax/save-as-draft"
});
ajaxRequest.done(function (response, textStatus, jqXHR) {
console.log("Bludit AJAX: autosave(): done handler");
callBack("Autosave success");
});
ajaxRequest.fail(function (jqXHR, textStatus, errorThrown) {
console.log("Bludit AJAX: autosave(): fail handler");
callBack("Autosave failure");
});
ajaxRequest.always(function () {
console.log("Bludit AJAX: autosave(): always handler");
});
}
2018-06-24 13:37:45 +02:00
// Alert the user when the user is not logged
userLogged(callBack) {
var ajaxRequest;
if (ajaxRequest) {
ajaxRequest.abort();
}
console.log("[INFO] Bludit AJAX: userLogged(): running");
ajaxRequest = $.ajax({
type: "POST",
data: {
tokenCSRF: tokenCSRF // token from env variables
},
url: "<?php echo HTML_PATH_ADMIN_ROOT ?>ajax/user-logged"
});
ajaxRequest.done(function (response, textStatus, jqXHR) {
console.log("[INFO] Bludit AJAX: userLogged(): done handler");
});
ajaxRequest.fail(function (jqXHR, textStatus, errorThrown) {
// The fail is produced by admin.php when the user is not logged the ajax request is not possible and returns 401
console.log("[INFO] Bludit AJAX: userLogged(): fail handler");
if (jqXHR.status==401) {
callBack("Your are not more logged, Bludit can't save your settings and content.");
}
});
ajaxRequest.always(function () {
console.log("[INFO] Bludit AJAX: userLogged(): always handler");
});
}
2018-05-08 00:15:40 +02:00
generateSlug(text, parentKey, currentKey, callBack) {
var ajaxRequest;
if (ajaxRequest) {
ajaxRequest.abort();
}
ajaxRequest = $.ajax({
type: "POST",
data: {
tokenCSRF: tokenCSRF,
text: text,
parentKey: parentKey,
currentKey: currentKey
},
url: "<?php echo HTML_PATH_ADMIN_ROOT.'ajax/generate-slug' ?>"
});
ajaxRequest.done(function (response, textStatus, jqXHR) {
console.log("Bludit AJAX: generateSlug(): done handler");
callBack.val(response["slug"]);
});
ajaxRequest.fail(function (jqXHR, textStatus, errorThrown) {
console.log("Bludit AJAX: generateSlug(): fail handler");
});
ajaxRequest.always(function () {
console.log("Bludit AJAX: generateSlug(): always handler");
});
}
}