/*
Fobe JS Utilities 2021
*/
//utility for populating HTML with data from object OR calling a function with the objects current position and replacing the marker with returned data
//[] for calling a function and replacing the marker with the return data (allows for more logic) (turned out to be incredibly useful)
//{} for replacing the marker with the objects data
//TODO: clean up?
function parseHtml(html, limit, object, message, singleObject=false) {
var html_result = '';
var buffer = "";
for (var i = 0; i < limit; i++) {
if (singleObject) {
var objectData = object;
} else {
var objectData = object[i];
}
buffer = html;
//we have reached the end of the data available
if (objectData === undefined) {
break;
}
for (var pos = 0; pos <= html.length; pos++) {
var firstFound = false;
var functionCall = false;
var secondPositionIdentifier = "";
if (html.charAt(pos) == "{") {
firstFound = true;
secondPositionIdentifier = "}";
} else if (html.charAt(pos) == "[") {
firstFound = true;
functionCall = true;
secondPositionIdentifier = "]";
}
if (firstFound) { //first position
for (var len = pos; len; len++) {
if (html.charAt(len) == secondPositionIdentifier) { //second position
var marker = "";
for (var d = pos; d < len+1; d++) { //data between the two positions
marker += html.charAt(d);
}
//where we handle the data O_O
//using replace instead of replaceAll for compatibility
if (!functionCall) {
buffer = buffer.replace(marker, objectData[marker.substring(1, marker.length - 1)]); //replace marker with data from the object
} else {
try {
buffer = buffer.replace(marker, window[marker.substring(1, marker.length - 1)](objectData)); //replace the marker with the data returned from the call
}
catch (error) {
console.log('[functionCall] Something went wrong.')
}
}
break;
}
}
}
}
html_result += buffer;
}
if (html_result == "") {
html_result = message;
}
return html_result;
}
//http get json no cds
function getJSON(url) {
return $.getJSON(url);
}
//http get json cds
function getJSONCDS(url) {
return $.ajax(url, {
xhrFields: {
withCredentials: true
},
crossDomain: true
});
}
//http post json cds
function postJSONCDS(url, jsondata) {
return $.ajax({
type: 'POST',
url: url,
xhrFields: {
withCredentials: true
},
crossDomain: true,
data: jsondata,
dataType: 'json',
});
}
//pretty ghetto but is incredibly useful for multi page helper, ex output: ,1,"ass",2
function parseArrayArgs(args) {
var parsed = ",";
for (const arg of args) {
if (typeof arg == "string") {
parsed += "'"+arg+"',";
} else {
parsed += arg+",";
}
}
return parsed.substring(0, parsed.length - 1); //remove last comma
}
//utility is for making pages easier
function staticPageHelper(api, loadingurl, container, html, page, limit, keyword, message, optionalArgs = "") {
if (loadingurl !== "") {
var loadingHtml = '