2018-07-09 19:27:22 +02:00
var batteriesOverviewTable = $ ( '#batteries-overview-table' ) . DataTable ( {
'paginate' : false ,
2018-05-13 09:38:22 +02:00
'order' : [ [ 2 , 'desc' ] ] ,
'columnDefs' : [
{ 'orderable' : false , 'targets' : 0 }
] ,
2018-07-10 20:37:13 +02:00
'language' : JSON . parse ( L ( 'datatables_localization' ) ) ,
2018-07-14 08:48:14 +02:00
'scrollY' : false ,
2018-07-14 10:17:12 +02:00
'colReorder' : true ,
2018-09-08 08:56:32 +02:00
'stateSave' : true ,
'stateSaveParams' : function ( settings , data )
{
data . search . search = "" ;
2018-09-25 08:50:28 +02:00
data . columns . forEach ( column =>
{
column . search . search = "" ;
} ) ;
2018-09-08 08:56:32 +02:00
}
2017-11-05 12:41:00 +01:00
} ) ;
2019-01-05 20:06:35 +01:00
$ ( '#batteries-overview-table tbody' ) . removeClass ( "d-none" ) ;
2018-05-13 09:38:22 +02:00
2018-07-09 19:27:22 +02:00
$ ( "#search" ) . on ( "keyup" , function ( )
{
var value = $ ( this ) . val ( ) ;
if ( value === "all" )
{
value = "" ;
}
2019-01-19 00:37:21 -07:00
2018-07-09 19:27:22 +02:00
batteriesOverviewTable . search ( value ) . draw ( ) ;
} ) ;
2018-09-24 19:13:53 +02:00
$ ( "#status-filter" ) . on ( "change" , function ( )
{
var value = $ ( this ) . val ( ) ;
if ( value === "all" )
{
value = "" ;
}
// Transfer CSS classes of selected element to dropdown element (for background)
$ ( this ) . attr ( "class" , $ ( "#" + $ ( this ) . attr ( "id" ) + " option[value='" + value + "']" ) . attr ( "class" ) + " form-control" ) ;
2019-01-19 00:37:21 -07:00
2018-09-24 19:13:53 +02:00
batteriesOverviewTable . column ( 4 ) . search ( value ) . draw ( ) ;
} ) ;
$ ( ".status-filter-button" ) . on ( "click" , function ( )
{
var value = $ ( this ) . data ( "status-filter" ) ;
$ ( "#status-filter" ) . val ( value ) ;
$ ( "#status-filter" ) . trigger ( "change" ) ;
} ) ;
2018-05-13 09:38:22 +02:00
$ ( document ) . on ( 'click' , '.track-charge-cycle-button' , function ( e )
{
2018-09-24 09:30:26 +02:00
e . preventDefault ( ) ;
2018-09-25 16:24:43 +02:00
// Remove the focus from the current button
// to prevent that the tooltip stays until clicked anywhere else
document . activeElement . blur ( ) ;
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . BeginUiBusy ( ) ;
2019-01-19 00:37:21 -07:00
2018-05-13 09:38:22 +02:00
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' ) ;
2019-01-19 14:51:51 +01:00
Grocy . Api . Post ( 'batteries/' + batteryId + '/charge' , { 'tracked_time' : trackedTime } ,
2018-08-07 20:11:08 +02:00
function ( )
2018-05-13 09:38:22 +02:00
{
2019-01-19 00:37:21 -07:00
Grocy . Api . Get ( 'batteries/' + batteryId ,
2018-08-07 20:11:08 +02:00
function ( result )
{
var batteryRow = $ ( '#battery-' + batteryId + '-row' ) ;
var nextXDaysThreshold = moment ( ) . add ( $ ( "#info-due-batteries" ) . data ( "next-x-days" ) , "days" ) ;
var now = moment ( ) ;
var nextExecutionTime = moment ( result . next _estimated _charge _time ) ;
2018-05-13 09:38:22 +02:00
2018-08-07 20:11:08 +02:00
batteryRow . removeClass ( "table-warning" ) ;
batteryRow . removeClass ( "table-danger" ) ;
if ( nextExecutionTime . isBefore ( now ) )
{
batteryRow . addClass ( "table-danger" ) ;
}
else if ( nextExecutionTime . isBefore ( nextXDaysThreshold ) )
{
batteryRow . addClass ( "table-warning" ) ;
}
$ ( '#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 ) ;
if ( result . battery . charge _interval _days != 0 )
{
$ ( '#battery-' + batteryId + '-next-charge-time' ) . parent ( ) . effect ( 'highlight' , { } , 500 ) ;
$ ( '#battery-' + batteryId + '-next-charge-time' ) . fadeOut ( 500 , function ( )
{
$ ( this ) . text ( result . next _estimated _charge _time ) . fadeIn ( 500 ) ;
} ) ;
$ ( '#battery-' + batteryId + '-next-charge-time-timeago' ) . attr ( 'datetime' , result . next _estimated _charge _time ) ;
}
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2018-09-03 22:07:05 +02:00
toastr . success ( L ( 'Tracked charge cycle of battery #1 on #2' , batteryName , trackedTime ) ) ;
2018-08-07 20:11:08 +02:00
RefreshContextualTimeago ( ) ;
RefreshStatistics ( ) ;
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2018-08-07 20:11:08 +02:00
console . error ( xhr ) ;
}
) ;
2018-05-13 09:38:22 +02:00
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2018-05-13 09:38:22 +02:00
console . error ( xhr ) ;
}
) ;
} ) ;
2018-08-04 15:44:58 +02:00
function RefreshStatistics ( )
{
var nextXDays = $ ( "#info-due-batteries" ) . data ( "next-x-days" ) ;
2019-01-19 00:37:21 -07:00
Grocy . Api . Get ( 'batteries' ,
2018-08-04 15:44:58 +02:00
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 ++ ;
}
} ) ;
2019-01-19 00:37:21 -07:00
2018-08-04 15:44:58 +02:00
$ ( "#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 ( ) ;