85 lines
3.0 KiB
JavaScript
85 lines
3.0 KiB
JavaScript
const doAjax = async (actionPath, data, action, hash = undefined, fname = undefined) => {
|
|
fetch(formatURL(actionPath), {
|
|
method: 'POST',
|
|
body: data,
|
|
}).then(response => {
|
|
if (response.status !== 200) {
|
|
msg = "[Fail] Status Code: " + response.status +
|
|
" --> " + response.statusText;
|
|
handleMessage('alert-warning', msg);
|
|
} else {
|
|
response.json().then(data => {
|
|
postAjaxController(data, action, hash, fname);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
const doAjaxUpload = (actionPath, data, fname, action) => {
|
|
let bs64 = btoa(unescape(encodeURIComponent(fname))).split("==")[0];
|
|
const query = '[id="' + bs64 + '"]';
|
|
let progressbar = document.querySelector(query);
|
|
let xhttp = new XMLHttpRequest();
|
|
|
|
xhttp.onreadystatechange = function() {
|
|
if (this.readyState === 4 && this.status === 200) {
|
|
if (this.responseText != null) { // this.responseXML if getting XML data
|
|
postAjaxController(JSON.parse(this.responseText), action);
|
|
} else {
|
|
msg = "[Fail] Status Code: " + response.status +
|
|
"\n[Message] --> " + response.statusText;
|
|
handleMessage('alert-warning', msg);
|
|
}
|
|
}
|
|
};
|
|
|
|
// For upload tracking with GET...
|
|
xhttp.onprogress = function (e) {
|
|
if (e.lengthComputable) {
|
|
percent = (e.loaded / e.total) * 100;
|
|
text = parseFloat(percent).toFixed(2) + '% Complete (' + fname + ')';
|
|
if (e.loaded !== e.total ) {
|
|
updateProgressBar(progressbar, text, percent, "info");
|
|
} else {
|
|
updateProgressBar(progressbar, text, percent, "success");
|
|
}
|
|
}
|
|
}
|
|
|
|
// For upload tracking with POST...
|
|
xhttp.upload.addEventListener("progress", function(e){
|
|
if (e.lengthComputable) {
|
|
percent = parseFloat( Math.floor(
|
|
(
|
|
(e.loaded / e.total) * 100 ).toFixed(2)
|
|
).toFixed(2)
|
|
);
|
|
text = percent + '% Complete (' + fname + ')';
|
|
if (percent <= 95) {
|
|
updateProgressBar(progressbar, text, percent, "info");
|
|
} else {
|
|
updateProgressBar(progressbar, text, percent, "success");
|
|
}
|
|
}
|
|
}, false);
|
|
|
|
xhttp.open("POST", formatURL(actionPath));
|
|
// Force return to be JSON NOTE: Use application/xml to force XML
|
|
xhttp.overrideMimeType('application/json');
|
|
xhttp.send(data);
|
|
}
|
|
|
|
const formatURL = (basePath) => {
|
|
url = window.location.href;
|
|
if ( url.endsWith('/') )
|
|
return url + basePath;
|
|
else
|
|
return url + '/' + basePath;
|
|
}
|
|
|
|
const fetchData = async (url) => {
|
|
let response = null;
|
|
response = await fetch(url);
|
|
return await response.json();
|
|
}
|