EinsatzOnline/resources/js/global.js

414 lines
11 KiB
JavaScript

function show_error(api_error){
alert("Fehler "+api_error.error.code+" aufgetreten: "+api_error.error.description);
}
function is_ok(data){
if(data == null){
return true;
}else if(typeof data !== 'object') {
return true;
}else{
if('error' in data){
show_error(data);
return false;
}else{
return true;
}
}
}
let get_event_type = async function(type_id){
const res = await $.ajax({
type: "GET",
url: "/api/events/types/" + type_id,
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
});
if (is_ok(res)) {
return res.name;
}
}
function check_for_permission(callback, permission, entity_id){
let optional_entity = "";
if(entity_id){
optional_entity = "&entity_id="+entity_id;
}
$.ajax({
type: "GET",
url: "/api/info/caller/permissions?permission="+permission+optional_entity,
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
success: function (data) {
if(is_ok(data)) {
callback(data);
}
}
});
}
async function check_for_permission_async(permission, entity_id){
let optional_entity = "";
if(entity_id){
optional_entity = "&entity_id="+entity_id;
}
const res = $.ajax({
type: "GET",
url: "/api/info/caller/permissions?permission="+permission+optional_entity,
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
});
if(is_ok(res)) {
return res;
}
}
let get_member = async function (entity_id){
const res = await $.ajax({
type: "GET",
url: "/api/members/" + entity_id,
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
});
if (is_ok(res)) {
return res;
}
};
let get_members = async function (){
const res = await $.ajax({
type: "GET",
url: "/api/members?limit=10000&offset=0&sort=firstname:asc",
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
});
if (is_ok(res)) {
return res;
}
};
let get_vehicle = async function (entity_id){
const res = await $.ajax({
type: "GET",
url: "/api/resources/vehicles/" + entity_id,
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
});
if (is_ok(res)) {
if(res.vehicle !== null){
return res.vehicle
}
if(res.reduced_vehicle !== null){
return res.reduced_vehicle
}
}
};
let get_vehicles = async function (){
const res = await $.ajax({
type: "GET",
url: "/api/resources/vehicles?entries=10000",
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
});
if (is_ok(res)) {
return res;
}
};
let load_positions_for_instance = async function(instance_id){
const res = await $.ajax({
url: '/api/events/instances/'+instance_id+'/positions',
type: 'GET',
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
}
});
if (is_ok(res)) {
return res;
}
};
let load_positions_for_instance_new = async function (instance_id) {
const res = $.ajax({
url: '/api/events/instances/' + instance_id + '/positions',
type: 'GET',
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
}
});
if (is_ok(res)) {
return res;
} else {
throw res;
}
};
let load_vehicle_positions_for_instance = async function (instance_id) {
const res = await $.ajax({
url: '/api/events/instances/' + instance_id + '/vehicle_positions',
type: 'GET',
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
}
});
if (is_ok(res)) {
return res;
}
};
let remove_instance = function(){
let caller = this;
let instance = $(caller).closest(".instance");
let instance_id = instance.data("instance-id");
$.ajax({
url: '/api/events/instances/'+instance_id,
type: 'DELETE',
contentType: 'application/json',
success: function (data) {
if (is_ok(data)) {
instance.remove();
}
},
timeout: 3000,
error: function () {
alert("Es ist ein Fehler aufgetreten!");
}
});
};
let add_entity_to_position = function(entity, position_instance_id){
$.ajax({
url: '/api/entities/'+entity+'/position_instances/'+position_instance_id,
type: 'PUT',
contentType: 'application/json',
success: function (data) {
if (is_ok(data)) {
return data;
}
},
timeout: 3000,
error: function () {
alert("Es ist ein Fehler aufgetreten!");
}
});
};
let remove_entity_from_position = function(position_instance_id){
$.ajax({
url: '/api/position_instances/'+position_instance_id+'/taken_by',
type: 'DELETE',
contentType: 'application/json',
success: function (data) {
if (is_ok(data)) {
}
},
timeout: 3000,
error: function () {
alert("Es ist ein Fehler aufgetreten!");
}
});
};
let get_organiser = async function(entity_id){
const res = await $.ajax({
type: "GET",
url: "/api/event_organisers/" + entity_id,
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
});
if (is_ok(res)) {
return res;
}
};
let load_event_types_legacy = function(callback){
$.ajax({
url: '/api/events/types',
type: 'GET',
contentType: 'application/json',
success: function (data) {
if (is_ok(data)) {
callback(data);
}
},
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
}
});
};
let load_event_types = async function(){
const res = await $.ajax({
url: '/api/events/types',
type: 'GET',
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
}
});
if(is_ok(res)){
return res;
}
};
let load_groups = function(callback){
$.ajax({
url: '/api/groups',
type: 'GET',
contentType: 'application/json',
success: function (data) {
if (is_ok(data)) {
callback(data);
}
},
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
}
});
};
let load_event_types_async = async function(){
const res = await $.ajax({
url: '/api/events/types',
type: 'GET',
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
}
});
if (is_ok(res)) {
return res;
}
};
let load_groups_async = async function(){
const res = await $.ajax({
url: '/api/groups',
type: 'GET',
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
}
});
if (is_ok(res)) {
return res.groups;
}
};
let check_position_requirements = async function(position_id, member_id){
const res = await $.ajax({
url: '/api/events/units/positions/'+position_id+'/check_requirements?target_to_check='+member_id,
type: 'GET',
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Es ist ein Fehler aufgetreten!");
}
});
if (is_ok(res)) {
return res.requirements_fulfilled;
}
};
let get_caller_permission_context = async function(permission){
const res = await $.ajax({
url: '/api/info/caller/permission_context?permission='+permission,
type: 'GET',
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Es ist ein Fehler aufgetreten!");
}
});
if(is_ok(res)){
return res
}
}
$(document).ajaxStart(function() {
$(".loading_animation").show();
});
$(document).ajaxStop(function () {
$(".loading_animation").hide();
});
let show_saved_animation = function () {
$('.saved_animation').fadeIn(500).fadeOut(500);
$(document).mousemove(function (e) {
$(".saved_animation").css({left: e.pageX + 15, top: e.pageY});
});
$(document).off("mouseover");
};
let combine_start_end_time = function (start, end) {
let date = new Date(start);
let start_date = ('0' + date.getDate()).slice(-2) + '.' + ('0' + (date.getMonth() + 1)).slice(-2) + '.' + date.getFullYear();
let start_time = ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2) + ' - ';
date = new Date(end);
let end_date = ('0' + date.getDate()).slice(-2) + '.' + ('0' + (date.getMonth() + 1)).slice(-2) + '.' + date.getFullYear();
let end_time = ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2);
if (start_date === end_date) {
return start_date + " " + start_time + end_time;
} else {
return start_date + " " + start_time + end_date + " " + end_time;
}
};
let get_related_group = async function(entity_id){
const res = await $.ajax({
type: "GET",
url: "/api/groups/" + entity_id,
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
});
if (is_ok(res)) {
return res.name;
}
};
let load_event_cast_status = async function(event_id){
const res = await $.ajax({
type: "GET",
url: "/api/events/" + event_id+"/cast_status",
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
});
if (is_ok(res)) {
return res;
}
};