web-base/js/script.js

280 lines
9.1 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();
this.langEntries = {};
};
2020-04-03 22:10:21 +02:00
this.apiCall = function (func, aParams, callback, onerror) {
aParams = typeof aParams !== 'undefined' ? aParams : {};
callback = typeof callback !== 'undefined' ? callback : function (data) {};
2020-02-09 23:02:19 +01:00
2020-04-03 22:10:21 +02:00
onerror = typeof onerror !== 'undefined' ? onerror : function (msg) {
bootbox.alert("An error occurred: " + msg);
};
const path = '/api' + (func.startsWith('/') ? '' : '/') + func;
$.post(path, aParams, function (data) {
2020-02-09 23:02:19 +01:00
console.log(func + "(): success=" + data.success + " msg=" + data.msg);
2020-04-03 22:10:21 +02:00
if (data.hasOwnProperty('logoutIn') && $("#logoutTimer").length > 0) {
2020-02-09 23:02:19 +01:00
$("#logoutTimer").attr("data-time", data.logoutIn);
}
2020-04-03 22:10:21 +02:00
if (!data.success) {
2020-02-09 23:02:19 +01:00
onerror.call(this, data.msg);
} else {
callback.call(this, data);
}
2020-04-03 22:10:21 +02:00
}, "json").fail(function (jqXHR, textStatus, errorThrown) {
2020-02-09 23:02:19 +01:00
console.log("API-Function Error: " + func + " Status: " + textStatus + " error thrown: " + errorThrown);
2020-02-10 00:52:25 +01:00
onerror.call(this, "An error occurred. API-Function: " + func + " Status: " + textStatus + " - " + errorThrown);
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.addLangEntry = function (key, val) {
this.langEntries[key] = val;
2020-02-09 23:02:19 +01:00
};
2020-04-03 22:10:21 +02:00
this.getLangEntry = function (key) {
if (typeof this.langEntries[key] !== 'undefined' && this.langEntries.hasOwnProperty(key)) {
2020-02-09 23:02:19 +01:00
return this.langEntries[key];
}
return key;
};
2020-04-03 22:10:21 +02:00
this.getUrl = function () {
return this.url;
};
this.getParameters = function () {
return this.aParameters;
};
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) {
2020-02-09 23:02:19 +01:00
if (typeof this.aParameters[param] !== 'undefined' && this.aParameters.hasOwnProperty(param)) {
delete this.aParameters[param];
}
this.updateUrl();
};
2020-04-03 22:10:21 +02:00
this.getParameter = function (param) {
2020-02-09 23:02:19 +01:00
if (typeof this.aParameters[param] !== 'undefined' && this.aParameters.hasOwnProperty(param))
return this.aParameters[param];
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 : '';
this.aParameters[param] = newvalue;
this.updateUrl();
};
2020-04-03 22:10:21 +02:00
this.parseParameters = function () {
2020-02-09 23:02:19 +01:00
this.aParameters = [];
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);
this.aParameters[key] = val;
} else
this.aParameters[param] = '';
}
};
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;
2020-02-09 23:02:19 +01:00
for (var parameter in this.aParameters) {
this.url += (i === 0 ? "?" : "&") + parameter;
if (this.aParameters.hasOwnProperty(parameter) && this.aParameters[parameter].toString().length > 0) {
this.url += "=" + this.aParameters[parameter];
}
i++;
}
};
2020-04-03 22:10:21 +02:00
this.clearParameters = function () {
2020-02-09 23:02:19 +01:00
this.aParameters = [];
this.updateUrl();
};
2020-04-03 22:10:21 +02:00
this.clearUrl = function () {
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.logout = function () {
2020-04-02 21:21:07 +02:00
this.apiCall('user/logout');
2020-02-09 23:02:19 +01:00
};
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];
};
2020-04-03 22:10:21 +02:00
this.showInputDialog = function (title, aInputs, callback, element, onCreated) {
title = typeof title !== "undefined" ? title : "";
aInputs = typeof aInputs !== "undefined" ? aInputs : {};
callback = typeof callback !== "undefined" ? callback : function (aResult, element) {
};
onCreated = typeof onCreated !== "undefined" ? onCreated : function () {
};
2020-02-09 23:02:19 +01:00
var html = '<div class="modal-header"><h4 class="modal-title">' + title + '</h4></div>' +
2020-04-03 22:10:21 +02:00
'<form class="bootbox-form">';
2020-02-09 23:02:19 +01:00
for (var i in aInputs) {
var input = aInputs[i];
2020-04-03 22:10:21 +02:00
if (input.type !== "hidden" && input.type !== "checkbox")
html += '<label for="' + input.name + '">' + input.name + ':</label>';
2020-02-09 23:02:19 +01:00
2020-04-03 22:10:21 +02:00
if (input.type === "select") {
2020-02-09 23:02:19 +01:00
html += '<select id="' + input.id + '" class="bootbox-input bootbox-input-select form-control">';
var aValues = (input.hasOwnProperty("aValues") && typeof input.aValues !== "undefined") ? input.aValues : {};
for (var value in aValues) {
var name = aValues[value];
2020-04-03 22:10:21 +02:00
var selected = (input.value === value) ? " selected" : "";
2020-02-09 23:02:19 +01:00
html += '<option value="' + value + '"' + selected + '>' + name + '</option>';
}
html += '</select>';
input.type = "select";
2020-04-03 22:10:21 +02:00
} else if (input.type === "checkbox") {
2020-02-09 23:02:19 +01:00
html += '<div class="checkbox">' +
2020-04-03 22:10:21 +02:00
'<label><table><tr>' +
'<td style="vertical-align:top;padding-top:3px;">' +
'<input class="bootbox-input bootbox-input-checkbox" id="' + input.id + '" value="1" type="checkbox"' + (input.value ? " checked" : "") + '>' +
'</td>' +
'<td style="padding-left: 5px;">' + input.text + '</td>' +
'</tr></table></label>' +
'</div>';
} else if (input.type === "date") {
2020-02-09 23:02:19 +01:00
html += '<input class="bootbox-input form-control customDatePicker" autocomplete="off" ' +
2020-04-03 22:10:21 +02:00
'type="text" ' +
'name="' + input.name + '" ' +
'id="' + input.id + '" ' +
'value="' + (input.value ? input.value : "") + '"' + (input.readonly ? " readonly" : "") +
(input.maxlength ? ' maxlength="' + input.maxlength + '"' : '') + '>';
} else if (input.type === "time") {
2020-02-09 23:02:19 +01:00
html += '<div class="input-group">' +
2020-04-03 22:10:21 +02:00
'<input class="bootbox-input" autocomplete="off" value="0" pattern="[0-9][0-9]" type="number" name="' + input.name + '" id="' + input.id + 'Hour" style="width:60px;text-align: center">' +
'<span>:</span>' +
'<input class="bootbox-input" autocomplete="off" type="number" name="' + input.name + '" id="' + input.id + 'Minute" value="00" style="width:60px;text-align: center">' +
'</div>';
2020-02-09 23:02:19 +01:00
} else {
html += '<input class="bootbox-input form-control" autocomplete="off" ' +
2020-04-03 22:10:21 +02:00
'type="' + input.type + '" ' +
'name="' + input.name + '" ' +
'id="' + input.id + '" ' +
'value="' + (input.value ? input.value : "") + '"' + (input.readonly ? " readonly" : "") +
(input.maxlength ? ' maxlength="' + input.maxlength + '"' : '') + '>';
2020-02-09 23:02:19 +01:00
}
}
html += '</form>';
2020-04-03 22:10:21 +02:00
var dialog = bootbox.confirm(html, function (result) {
if (result) {
2020-02-09 23:02:19 +01:00
var aResult = [];
for (var i in aInputs) {
var input = aInputs[i];
var value = $("#" + input.id).val();
2020-04-03 22:10:21 +02:00
if (input.type === "select")
2020-02-09 23:02:19 +01:00
value = $("#" + input.id).find(":selected").val();
2020-04-03 22:10:21 +02:00
else if (input.type === "checkbox")
2020-02-09 23:02:19 +01:00
value = $("#" + input.id).prop("checked");
aResult[input.id] = value;
}
callback.call(this, aResult, element);
}
});
2020-04-03 22:10:21 +02:00
dialog.init(function () {
$(".modal-body").on("keypress", "input", function (e) {
if (e.keyCode === 13) {
2020-02-09 23:02:19 +01:00
e.preventDefault();
$(".modal-footer .btn-primary").click();
}
});
onCreated.call(this);
});
};
this.__construct();
};
2020-04-03 22:10:21 +02:00
let jsCore = new Core();
2020-02-09 23:02:19 +01:00
$(document).ready(function() {
});
2020-04-04 01:15:59 +02:00
function createLoadingIcon() {
return '<i class="fas fa-spin fa-spinner"></i>';
}