web-base/js/script.js

174 lines
4.8 KiB
JavaScript
Raw Normal View History

2020-04-03 22:10:21 +02:00
let Core = function () {
2020-02-09 23:02:19 +01:00
2020-04-03 22:10:21 +02:00
this.__construct = function () {
2020-02-09 23:02:19 +01:00
this.url = document.location.href;
this.parseParameters();
};
2020-07-01 22:13:50 +02:00
this.apiCall = function (func, params, callback) {
params = typeof params !== 'undefined' ? params : {};
2020-04-03 22:10:21 +02:00
callback = typeof callback !== 'undefined' ? callback : function (data) {};
2020-02-09 23:02:19 +01:00
2020-07-15 15:05:54 +02:00
const path = '/api' + (func.startsWith('/') ? '' : '/') + func;
2020-07-01 22:13:50 +02:00
$.post(path, params, function (data) {
2020-02-09 23:02:19 +01:00
console.log(func + "(): success=" + data.success + " msg=" + data.msg);
2020-07-01 22:13:50 +02:00
callback.call(this, data);
2020-04-03 22:10:21 +02:00
}, "json").fail(function (jqXHR, textStatus, errorThrown) {
2020-07-01 22:13:50 +02:00
let msg = func + " Status: " + textStatus + " error thrown: " + errorThrown;
console.log("API-Function Error: " + msg);
callback.call(this, {success: false, msg: "An error occurred. API-Function: " + msg });
2020-02-09 23:02:19 +01:00
});
};
2020-04-03 22:10:21 +02:00
this.getCookie = function (cname) {
2020-02-09 23:02:19 +01:00
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(";");
2020-04-03 22:10:21 +02:00
for (var i = 0; i < ca.length; i++) {
2020-02-09 23:02:19 +01:00
var c = ca[i];
2020-04-03 22:10:21 +02:00
while (c.charAt(0) === ' ') {
2020-02-09 23:02:19 +01:00
c = c.substring(1);
}
2020-04-03 22:10:21 +02:00
if (c.indexOf(name) === 0) {
2020-02-09 23:02:19 +01:00
return c.substring(name.length, c.length);
}
}
return "";
};
2020-04-03 22:10:21 +02:00
this.getUrl = function () {
return this.url;
};
this.getParameters = function () {
2022-08-20 22:17:17 +02:00
return this.parameters;
2020-04-03 22:10:21 +02:00
};
2020-02-09 23:02:19 +01:00
2020-04-03 22:10:21 +02:00
this.setTitle = function (title) {
2020-02-09 23:02:19 +01:00
document.title = title;
};
2020-04-03 22:10:21 +02:00
this.changeURL = function (history) {
if (history) {
2020-02-09 23:02:19 +01:00
window.history.pushState({
"html": document.getElementsByTagName("body")[0].innerHTML,
"pageTitle": document.title
}, "", this.url);
} else {
window.history.replaceState({
"html": document.getElementsByTagName("body")[0].innerHTML,
"pageTitle": document.title
}, "", this.url);
}
};
2020-04-03 22:10:21 +02:00
this.redirect = function () {
2020-02-09 23:02:19 +01:00
window.location = this.url;
};
2020-04-03 22:10:21 +02:00
this.reload = function () {
2020-02-09 23:02:19 +01:00
window.location.reload();
};
2020-04-03 22:10:21 +02:00
this.removeParameter = function (param) {
2022-08-20 22:17:17 +02:00
if (typeof this.parameters[param] !== 'undefined' && this.parameters.hasOwnProperty(param)) {
delete this.parameters[param];
2020-02-09 23:02:19 +01:00
}
this.updateUrl();
};
2020-04-03 22:10:21 +02:00
this.getParameter = function (param) {
2022-08-20 22:17:17 +02:00
if (typeof this.parameters[param] !== 'undefined' && this.parameters.hasOwnProperty(param))
return this.parameters[param];
2020-02-09 23:02:19 +01:00
else
return null;
};
2020-04-03 22:10:21 +02:00
this.setParameter = function (param, newvalue) {
2020-02-09 23:02:19 +01:00
newvalue = typeof newvalue !== 'undefined' ? newvalue : '';
2022-08-20 22:17:17 +02:00
this.parameters[param] = newvalue;
2020-02-09 23:02:19 +01:00
this.updateUrl();
};
2020-04-03 22:10:21 +02:00
this.parseParameters = function () {
2022-08-20 22:17:17 +02:00
this.parameters = [];
2020-04-03 22:10:21 +02:00
if (this.url.indexOf('?') === -1)
2020-02-09 23:02:19 +01:00
return;
var paramString = this.url.substring(this.url.indexOf('?') + 1);
var split = paramString.split('&');
for (var i = 0; i < split.length; i++) {
var param = split[i];
var index = param.indexOf('=');
2020-04-03 22:10:21 +02:00
if (index !== -1) {
2020-02-09 23:02:19 +01:00
var key = param.substr(0, index);
var val = param.substr(index + 1);
2022-08-20 22:17:17 +02:00
this.parameters[key] = val;
2020-02-09 23:02:19 +01:00
} else
2022-08-20 22:17:17 +02:00
this.parameters[param] = '';
2020-02-09 23:02:19 +01:00
}
};
2020-04-03 22:10:21 +02:00
this.updateUrl = function () {
2020-02-09 23:02:19 +01:00
this.clearUrl();
2020-04-03 22:10:21 +02:00
let i = 0;
2022-08-20 22:17:17 +02:00
for (var parameter in this.parameters) {
2020-02-09 23:02:19 +01:00
this.url += (i === 0 ? "?" : "&") + parameter;
2022-08-20 22:17:17 +02:00
if (this.parameters.hasOwnProperty(parameter) && this.parameters[parameter].toString().length > 0) {
this.url += "=" + this.parameters[parameter];
2020-02-09 23:02:19 +01:00
}
i++;
}
};
2020-04-03 22:10:21 +02:00
this.clearParameters = function () {
2022-08-20 22:17:17 +02:00
this.parameters = [];
2020-02-09 23:02:19 +01:00
this.updateUrl();
};
2020-04-03 22:10:21 +02:00
this.clearUrl = function () {
2022-08-20 22:17:17 +02:00
if (this.url.indexOf('#') !== -1)
this.url = this.url.substring(0, this.url.indexOf('#'));
2020-04-03 22:10:21 +02:00
if (this.url.indexOf('?') !== -1)
2020-02-09 23:02:19 +01:00
this.url = this.url.substring(0, this.url.indexOf('?'));
};
2020-04-03 22:10:21 +02:00
this.getJsonDateTime = function (date) {
2020-02-09 23:02:19 +01:00
return date.getFullYear() + "-" +
2020-04-03 22:10:21 +02:00
((date.getMonth() + 1 < 10) ? "0" : "") + (date.getMonth() + 1) + "-" +
(date.getDate() < 10 ? "0" : "") + date.getDate() + " " +
(date.getHours() < 10 ? "0" : "") + date.getHours() + "-" +
(date.getMinutes() < 10 ? "0" : "") + date.getMinutes() + "-" +
(date.getSeconds() < 10 ? "0" : "") + date.getSeconds();
2020-02-09 23:02:19 +01:00
};
2020-04-03 22:10:21 +02:00
this.getJsonDate = function (date) {
2020-02-09 23:02:19 +01:00
return this.getJsonDateTime(date).split(' ')[0];
};
2020-04-03 22:10:21 +02:00
this.getJsonTime = function (date) {
2020-02-09 23:02:19 +01:00
return this.getJsonDateTime(date).split(' ')[1];
};
2022-11-30 16:42:24 +01:00
this.isRecaptchaEnabled = function () {
return (typeof grecaptcha !== 'undefined');
}
2020-02-09 23:02:19 +01:00
this.__construct();
};
2022-11-30 16:42:24 +01:00
let jsCore = new Core();
function L(key) {
let entries = window.languageEntries || {};
let [module, variable] = key.split(".");
if (module && variable && entries.hasOwnProperty(module)) {
let translation = entries[module][variable];
if (translation) {
return translation;
}
}
return "[" + key + "]";
}