timelaps-front/js/timelaps.js
bglacial e04074843a Fix Add Kart
Set auto refresh Datatable on delete and add
2019-04-18 22:42:50 +02:00

296 lines
11 KiB
JavaScript

function getKartsMenu() {
$.ajax({
type: 'GET',
crossDomain: true,
url: api_url + 'karts/',
dataType: 'json',
success: function (data, status) {
$.each(data, function (index, value) {
$('#link_all').after("" +
"&nbsp;<a id=\"link_" + value.id + "\" href=\"#\" class=\"btn btn-light btn-icon-split btn-sm\">\n" +
" <span class=\"icon text-gray-600\">\n" +
" <i class=\"fas fa-car\"></i>\n" +
"</span>\n" +
" <span class=\"text\">" + value.name + "</span>" +
" </a>");
});
},
error: function (xhr) {
$.notify("APIs unreachable!", "error");
console.log('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
},
});
}
function getPilotsByKart() {
$.ajax({
type: 'GET',
crossDomain: true,
url: api_url + 'karts/',
dataType: 'json',
success: function (data, status) {
$.each(data, function (index, value) {
$('.container-fluid').append("" +
"<div class=\"card shadow mb-4\">\n" +
" <div class=\"card-header py-3\">\n" +
" <h6 class=\"m-0 font-weight-bold text-primary\">\n" +
" Pilot list - "+value.name+" \n" +
" <a href=\"#\" class=\"btn btn-primary btn-circle\" title='Ajouter un piote au karting "+value.name+"'>\n" +
" <i class=\"fas fa-plus\"></i>\n" +
" </a>\n" +
" </h6>\n" +
" </div>\n" +
" <div class=\"card-body\">\n" +
" <div class=\"table-responsive\">\n" +
" <table class=\"table table-bordered\" id=\"dataTable_pilots"+value.id+"\" width=\"100%\" cellspacing=\"0\">\n" +
" <thead>\n" +
" <tr>\n" +
" <th></th>\n" +
" <th>Order</th>\n" +
" <th>Short Name</th>\n" +
" <th>First Name</th>\n" +
" <th>Last Name</th>\n" +
" <th>Ref time (Sec)</th>\n" +
" </tr>\n" +
" </thead>\n" +
" <tbody>\n" +
" </tbody>\n" +
" </table>\n" +
" </div>\n" +
" </div>\n" +
" </div>" +
"");
$('#dataTable_pilots'+value.id).DataTable({
data: value.drivers,
columns: [
{
data: "id",
render: function (data, type, row, meta) {
return '<a href="#" class="btn btn-primary btn-circle"><i class="fas fa-edit"></i></a>&nbsp;<a href="#" class="btn btn-danger btn-circle"><i class="fas fa-trash"></i>';
}
},
{
data: "order"
},
{
data: "first_name"
},
{
data: "first_name"
},
{
data: "last_name"
},
{
data: "ref_time"
}
]
});
});
},
error: function (xhr) {
$.notify("APIs unreachable!", "error");
console.log('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
},
});
}
function getParams() {
$.ajax({
type: 'GET',
crossDomain: true,
url: api_url + 'params/1/',
dataType: 'json',
success: function (data, status) {
$('#id').val(data.id);
$('#autonomy').val(data.autonomy);
$('#stand_minimum_time').val(data.stand_minimum_time);
$('#default_relay').val(data.default_relay);
$('#comment').val(data.comment);
},
error: function (xhr) {
$.notify("APIs unreachable!", "error");
console.log('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
},
});
}
function editParams(data) {
$.ajax({
type: 'PUT',
crossDomain: true,
url: api_url + 'params/1/',
dataType: 'json',
data: data,
success: function (data, status) {
$.notify("Update done!", "success");
getParams();
},
error: function (xhr) {
$.notify("APIs unreachable!", "error");
console.log('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
},
});
}
function getKarts(id) {
$.ajax({
type: 'GET',
crossDomain: true,
url: api_url + 'karts/'+id+'/',
dataType: 'json',
success: function (data, status) {
$('#id').val(data.id);
$('#autonomy').val(data.autonomy);
$('#stand_minimum_time').val(data.stand_minimum_time);
$('#default_relay').val(data.default_relay);
$('#comment').val(data.comment);
},
error: function (xhr) {
$.notify("APIs unreachable!", "error");
console.log('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
},
});
}
function listKarts() {
$.ajax({
type: 'GET',
crossDomain: true,
url: api_url + 'karts/',
dataType: 'json',
success: function (data, status) {
if(tableListKart instanceof $.fn.dataTable.Api){
tableListKart.destroy();
}
tableListKart = $('#dataTable_kart_list').DataTable({
data: data,
columns: [
{
data: "id",
render: function (data, type, row, meta) {
return '<a href="#" onclick="" class="btn btn-primary btn-circle">' +
'<i class="fas fa-edit"></i>' +
'</a>' +
'&nbsp;' +
'<a href="#" onclick="if ( confirm( \'Do you want to delete the kart\' ) ) {deleteKarts('+data+','+meta.row+')}" class="btn btn-danger btn-circle">' +
'<i class="fas fa-trash"></i>' +
'</a>';
}
},
{
data: "name"
},
{
data: "autonomy"
}
]
});
},
error: function (xhr) {
$.notify("APIs unreachable!", "error");
console.log('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
},
});
}
function deleteKarts(id, index) {
$.ajax({
type: 'DELETE',
crossDomain: true,
url: api_url + 'karts/'+id+'/',
dataType: 'json',
success: function (data, status) {
$.notify("Delete done!", "success");
tableListKart.row(index).remove().draw();
},
error: function (xhr) {
$.notify("APIs unreachable!", "error");
console.log('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
},
});
}
function addKarts(data) {
$.ajax({
type: 'POST',
crossDomain: true,
url: api_url + 'karts/',
dataType: 'json',
data: data,
success: function (data, status) {
$.notify("Update done!", "success");
$('#modal_newkart').modal('hide');
$("#saveKartChanges").prop('disabled', false);
listKarts()
},
error: function (xhr) {
$.notify("APIs unreachable!", "error");
console.log('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
$("#saveKartChanges").prop('disabled', false);
},
});
}
function editKarts(data) {
$.ajax({
type: 'PUT',
crossDomain: true,
url: api_url + 'karts/1/',
dataType: 'json',
data: data,
success: function (data, status) {
$.notify("Update done!", "success");
showKarts();
},
error: function (xhr) {
$.notify("APIs unreachable!", "error");
console.log('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
},
});
}
function getDriver(id) {
$.ajax({
type: 'GET',
crossDomain: true,
url: api_url + 'drivers/'+id+'/',
dataType: 'json',
success: function (data, status) {
$('#id').val(data.id);
$('#autonomy').val(data.autonomy);
$('#stand_minimum_time').val(data.stand_minimum_time);
$('#default_relay').val(data.default_relay);
$('#comment').val(data.comment);
},
error: function (xhr) {
$.notify("APIs unreachable!", "error");
console.log('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
},
});
}
function editDriver(data) {
$.ajax({
type: 'PUT',
crossDomain: true,
url: api_url + 'drivers/1/',
dataType: 'json',
data: data,
success: function (data, status) {
$.notify("Update done!", "success");
getPilotsByKart();
},
error: function (xhr) {
$.notify("APIs unreachable!", "error");
console.log('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
},
});
}