Menu

[f1bb48]: / js / sessioncontroller.js  Maximize  Restore  History

Download this file

130 lines (114 with data), 4.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* 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 */
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.