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

100 lines
2.6 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
},
2018-07-28 18:33:37 +02:00
url: HTML_PATH_ADMIN_ROOT+"ajax/save-as-draft"
2018-05-08 00:15:40 +02:00
});
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();
}
2018-07-02 00:24:53 +02:00
console.log("[INFO] [BLUDIT AJAX] [userLogged()] Checking if the user is logged.");
2018-06-24 13:37:45 +02:00
ajaxRequest = $.ajax({
2018-08-01 22:04:28 +02:00
type: "GET",
2018-07-28 18:33:37 +02:00
url: HTML_PATH_ADMIN_ROOT+"ajax/user-logged"
2018-06-24 13:37:45 +02:00
});
ajaxRequest.done(function (response, textStatus, jqXHR) {
2018-07-02 00:24:53 +02:00
console.log("[INFO] [BLUDIT AJAX] [userLogged()] The user is logged.");
2018-06-24 13:37:45 +02:00
});
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
2018-07-02 00:24:53 +02:00
console.log("[INFO] [BLUDIT AJAX] [userLogged()] The user is NOT logged.");
2018-06-24 13:37:45 +02:00
if (jqXHR.status==401) {
2018-07-17 13:53:50 +02:00
callBack("You are not logged in anymore, so Bludit can't save your settings and content.");
2018-06-24 13:37:45 +02:00
}
});
}
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
},
2018-07-28 18:33:37 +02:00
url: HTML_PATH_ADMIN_ROOT+"ajax/generate-slug"
2018-05-08 00:15:40 +02:00
});
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");
});
}
2018-10-17 22:35:30 +02:00
2018-05-08 00:15:40 +02:00
}