/*
* phpMyEdit - instant MySQL table editor and code generator
*
* sessioncontroller.js - javascript utilities for phpMyEdit
* _________________________________________________________
*
* Versions 5.7.2 and higher developed by Yves De Billoëz
* All rights reserved.
*
* See README.md file for more information about this software.
* See COPYING file for license information.
*
* Download the latest version from
* https://sourceforge.net/projects/phpmariaedit/
*/
/* $Platon: phpMyEdit/js/sessioncontroller.js,v 5.7.6 2024-03-04 12:57:07 Yves $ */
function pme_getAnyClass(obj) {
if (typeof obj === "undefined") return "undefined";
if (obj === null) return "null";
return obj.constructor.name;
}
// push form to API
async function pushFormToAPI(formData, apiPath, methode = 'POST', callBack = null, callError = null) {
if (pme_getAnyClass(formData) != 'FormData') {
formData = new FormData(formData);
}
pushFormDataToAPI(formData, apiPath, methode, callBack, callError);
}
// push formData to API
async function pushFormDataToAPI(formData, apiPath, methode = 'POST', callBack = null, callError = null) {
let classData = {};
formData.forEach((value, key) => classData[key] = value);
pushJSONToAPI(classData, apiPath, methode, callBack, callError);
}
// push any class to API using JSON
async function pushJSONToAPI(classData, apiPath, methode = 'POST', callBack = null, callError = null) {
let myHeaders = new Headers();
myHeaders.append('Content-Type', 'application/json');
myHeaders.append('Sec-Fetch-Site', 'cross-site');
let requestParam = {
method: methode,
mode: 'cors',
headers: myHeaders
}
if (methode != 'GET') {
let classJSON = JSON.stringify(classData);
//pme_debug_log(classJSON);
requestParam['body'] = classJSON;
} else {
// encode parameters on link
apiPath += '?' + new URLSearchParams(classData);
}
fetch(apiPath, requestParam)
.then(function(response) {
//pme_debug_log('pushJSONToAPI response');
if (!response.ok) pme_debug_log(response);
return response.json();
})
.then(function(responseData) {
//pme_debug_log('pushJSONToAPI responseData');
if (callBack != null) {
callBack(apiPath, responseData);
} else {
console.log('No callback defined for ' + apiPath + ' call');
if (!responseData.ok) pme_debug_log(responseData);
}
})
.catch(function(err) {
if (callError != null) {
callError(apiPath, err);
} else {
console.error('No error calback defined for ' + apiPath + ' call');
//pme_debug_log(err);
}
});
}
function pme_redirecttohttps(loc) {
if (!loc.startsWith('https://')) {
if (loc.startsWith('http://')) {
loc = 'https://' + loc.substring(7);
} else {
loc = 'https://' + loc;
}
}
pme_debug_log('Redirect to ' + loc);
window.location.replace(loc);
}
function pme_clearcookie(name) {
let domain = location.host; // make sure to remove column if domain is using port
document.cookie = name + '=; Max-Age=0; path=/; SameSite=Strict; expires=Thu, 01 Jan 1970 00:00:01 GMT; domain=' + domain.split(':').shift();
}
function pme_debug_log(e) {
//console.log(e);
}
/* return storage, do all the testing and return null or value */
function getPersistentStorageItem(item, defaultValue) {
let value = localStorage.getItem(item);
if ((value !== null) && (value !== "")) return value;
if (defaultValue != null) return defaultValue;
return null;
}
/* set something in storage */
function setPersistentStorageItem(item, value) {
localStorage.setItem(item, value);
}
/* set something to default value if it is missing */
function setPersistentStorageDefault(item, defaultValue) {
if (localStorage.getItem(item) == null) localStorage.setItem(item, defaultValue);
}
/* remove an item from storage */
function clearPersistentStorageItem(item) {
localStorage.removeItem(item);
}
/* eof */