site-libsoc/Server/public/js/libraries/serverTools.js

113 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-06-15 01:41:54 +07:00
// Get data from server
export function getData(path,callback) {
let xhr = new XMLHttpRequest();
xhr.overrideMimeType("application/json")
xhr.open('GET', path, true)
2023-06-15 01:41:54 +07:00
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == "200") {
2023-06-15 01:41:54 +07:00
if (callback !== undefined) {
callback(xhr.responseText)
2023-06-15 01:41:54 +07:00
}
}
2023-06-15 01:41:54 +07:00
};
xhr.send()
2023-06-15 01:41:54 +07:00
}
// Parse JSON from given path into a given variable under a given key
export function getJSON(variable,key,path) {
getData(path,function(response) {
// Parse JSON string into object
variable[key] = JSON.parse(response);
});
}
export function sendData(route,data,callback) {
var xhr = new XMLHttpRequest();
xhr.open("POST", route, true)
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.onreadystatechange = function() {
if(xhr.readyState === XMLHttpRequest.DONE) {
var status = xhr.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
if (callback !== undefined) {
callback(xhr.responseText)
}
} else {
2023-07-21 21:38:02 +07:00
callback(false)
console.log("Request gave an error")
2023-06-15 01:41:54 +07:00
// Oh no! There has been an error with the request!
}
}
};
xhr.send(JSON.stringify(data))
}
export function sendText(route,data,callback) {
var xhr = new XMLHttpRequest();
xhr.open("POST", route, true)
xhr.setRequestHeader('Content-Type', 'text/plain')
xhr.onreadystatechange = function() {
if(xhr.readyState === XMLHttpRequest.DONE) {
var status = xhr.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
if (callback !== undefined) {
callback(xhr.responseText)
}
} else {
2023-07-20 04:57:20 +07:00
callback(false)
2023-07-21 21:38:02 +07:00
console.log("Request gave an error")
2023-06-15 01:41:54 +07:00
// Oh no! There has been an error with the request!
}
}
};
xhr.send(data)
}
2023-07-03 03:07:45 +07:00
function onlyUnique(value, index, array) {
return array.indexOf(value) === index;
}
2023-07-03 16:55:46 +07:00
export let locales = {
en: "English",
2023-10-09 19:38:04 +07:00
ru: "Русский",
fr: "français"
2023-07-03 16:55:46 +07:00
}
2023-07-03 03:07:45 +07:00
export function loadLocaleContent(content,componentName,loaded,callback) {
2023-07-03 16:55:46 +07:00
let locale
2023-07-03 03:07:45 +07:00
let langs
2023-07-03 16:55:46 +07:00
let localesAvailable = Object.keys(locales)
let localeUrl = location.href.split("/").filter(x => localesAvailable.includes(x))
if (localeUrl.length>0) {
locale = localeUrl
2023-07-03 03:07:45 +07:00
}
2023-07-03 16:55:46 +07:00
else {
langs = navigator.languages.map(x => x.split("-")[0]).filter(onlyUnique)
for (let lang of langs) {
if (localesAvailable.includes(lang)) {
locale = lang
2023-07-03 03:07:45 +07:00
}
2023-07-03 16:55:46 +07:00
break
}
if (locale==undefined) {
locale = "en"
}
}
getData("/locales/" + locale + "/" + componentName + ".json" ,function(response) {
2023-07-05 01:38:05 +07:00
let parsed = JSON.parse(response)
2023-07-04 01:44:24 +07:00
if (callback!=undefined) {
2023-07-05 01:38:05 +07:00
callback(parsed)
2023-07-04 01:44:24 +07:00
}
2023-07-13 18:32:49 +07:00
content.update((obj) => {
Object.assign(obj,parsed)
return obj
})
loaded.update((val) => {
return val + 1
})
2023-07-03 03:07:45 +07:00
})
2023-07-04 01:27:18 +07:00
return locale
2023-07-03 03:07:45 +07:00
}