grocy/public/viewjs/batteriesoverview.js

83 lines
2.3 KiB
JavaScript

var batteriesOverviewTable = $('#batteries-overview-table').DataTable({
'paginate': false,
'order': [[2, 'desc']],
'columnDefs': [
{ 'orderable': false, 'targets': 0 }
],
'language': JSON.parse(L('datatables_localization')),
'scrollY': false,
'colReorder': true,
'stateSave': true
});
$("#search").on("keyup", function()
{
var value = $(this).val();
if (value === "all")
{
value = "";
}
batteriesOverviewTable.search(value).draw();
});
$(document).on('click', '.track-charge-cycle-button', function(e)
{
var batteryId = $(e.currentTarget).attr('data-battery-id');
var batteryName = $(e.currentTarget).attr('data-battery-name');
var trackedTime = moment().format('YYYY-MM-DD HH:mm:ss');
Grocy.Api.Get('batteries/track-charge-cycle/' + batteryId + '?tracked_time=' + trackedTime,
function(result)
{
$('#battery-' + batteryId + '-last-tracked-time').parent().effect('highlight', {}, 500);
$('#battery-' + batteryId + '-last-tracked-time').fadeOut(500, function () {
$(this).text(trackedTime).fadeIn(500);
});
$('#battery-' + batteryId + '-last-tracked-time-timeago').attr('datetime', trackedTime);
RefreshContextualTimeago();
toastr.success(L('Tracked charge cylce of battery #1 on #2', batteryName, trackedTime));
RefreshStatistics();
},
function(xhr)
{
console.error(xhr);
}
);
});
function RefreshStatistics()
{
var nextXDays = $("#info-due-batteries").data("next-x-days");
Grocy.Api.Get('batteries/get-current',
function(result)
{
var dueCount = 0;
var overdueCount = 0;
var now = moment();
var nextXDaysThreshold = moment().add(nextXDays, "days");
result.forEach(element => {
var date = moment(element.next_estimated_charge_time);
if (date.isBefore(now))
{
overdueCount++;
}
else if (date.isBefore(nextXDaysThreshold))
{
dueCount++;
}
});
$("#info-due-batteries").text(Pluralize(dueCount, L('#1 battery is due to be charged within the next #2 days', dueCount, nextXDays), L('#1 batteries are due to be charged within the next #2 days', dueCount, nextXDays)));
$("#info-overdue-batteries").text(Pluralize(overdueCount, L('#1 battery is overdue to be charged', overdueCount), L('#1 batteries are overdue to be charged', overdueCount)));
},
function(xhr)
{
console.error(xhr);
}
);
}
RefreshStatistics();