2018-07-09 19:27:22 +02:00
var batteriesOverviewTable = $ ( '#batteries-overview-table' ) . DataTable ( {
2020-12-30 13:55:01 +01:00
'order' : [ [ 4 , 'asc' ] ] ,
2018-05-13 09:38:22 +02:00
'columnDefs' : [
2020-01-03 14:18:56 +01:00
{ 'orderable' : false , 'targets' : 0 } ,
2020-12-17 17:33:24 +01:00
{ 'searchable' : false , "targets" : 0 } ,
{ "type" : "html" , "targets" : 3 } ,
{ "type" : "html" , "targets" : 4 }
2020-12-07 19:48:33 +01:00
] . concat ( $ . fn . dataTable . defaults . columnDefs )
2017-11-05 12:41:00 +01:00
} ) ;
2019-01-05 20:06:35 +01:00
$ ( '#batteries-overview-table tbody' ) . removeClass ( "d-none" ) ;
2019-03-04 17:43:12 +01:00
batteriesOverviewTable . columns . adjust ( ) . draw ( ) ;
2018-05-13 09:38:22 +02:00
2019-10-15 19:59:14 +02:00
$ ( "#search" ) . on ( "keyup" , Delay ( function ( )
2018-07-09 19:27:22 +02:00
{
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 ( ) ;
2025-01-11 15:41:04 +01:00
} , 500 ) ) ;
2018-07-09 19:27:22 +02:00
2020-11-07 14:53:45 +01:00
$ ( "#clear-filter-button" ) . on ( "click" , function ( )
{
$ ( "#search" ) . val ( "" ) ;
$ ( "#status-filter" ) . val ( "all" ) ;
2021-11-12 18:26:19 +01:00
batteriesOverviewTable . column ( batteriesOverviewTable . colReorder . transpose ( 5 ) ) . search ( "" ) . draw ( ) ;
2020-11-07 14:53:45 +01:00
batteriesOverviewTable . search ( "" ) . 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
2021-11-12 18:26:19 +01:00
batteriesOverviewTable . column ( batteriesOverviewTable . colReorder . transpose ( 5 ) ) . search ( value ) . draw ( ) ;
2018-09-24 19:13:53 +02:00
} ) ;
2020-04-19 08:51:02 -04:00
$ ( ".status-filter-message" ) . on ( "click" , function ( )
2018-09-24 19:13:53 +02:00
{
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
2023-05-03 08:32:49 +02:00
$ ( ".tooltip" ) . tooltip ( "hide" ) ;
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' ) ;
2022-01-23 17:29:25 +01:00
var nextXDaysThreshold = moment ( ) . add ( $ ( "#info-due-soon-batteries" ) . data ( "next-x-days" ) , "days" ) ;
2018-08-07 20:11:08 +02:00
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" ) ;
}
2020-01-28 19:27:18 +01:00
animateCSS ( "#battery-" + batteryId + "-row td:not(:first)" , "shake" ) ;
2018-08-07 20:11:08 +02:00
2020-01-28 19:27:18 +01:00
$ ( '#battery-' + batteryId + '-last-tracked-time' ) . text ( trackedTime ) ;
$ ( '#battery-' + batteryId + '-last-tracked-time-timeago' ) . attr ( 'datetime' , trackedTime ) ;
2018-08-07 20:11:08 +02:00
if ( result . battery . charge _interval _days != 0 )
{
2020-01-28 19:27:18 +01:00
$ ( '#battery-' + batteryId + '-next-charge-time' ) . text ( result . next _estimated _charge _time ) ;
2018-08-07 20:11:08 +02:00
$ ( '#battery-' + batteryId + '-next-charge-time-timeago' ) . attr ( 'datetime' , result . next _estimated _charge _time ) ;
}
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2019-05-05 14:13:50 +02:00
toastr . success ( _ _t ( 'Tracked charge cycle of battery %1$s on %2$s' , batteryName , trackedTime ) ) ;
2020-01-27 19:00:49 +01:00
RefreshContextualTimeago ( "#battery-" + batteryId + "-row" ) ;
2018-08-07 20:11:08 +02:00
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
2021-07-13 19:29:23 +02:00
$ ( document ) . on ( 'click' , '.battery-grocycode-label-print' , function ( e )
{
e . preventDefault ( ) ;
var batteryId = $ ( e . currentTarget ) . attr ( 'data-battery-id' ) ;
Grocy . Api . Get ( 'batteries/' + batteryId + '/printlabel' , function ( labelData )
{
if ( Grocy . Webhooks . labelprinter !== undefined )
{
Grocy . FrontendHelpers . RunWebhook ( Grocy . Webhooks . labelprinter , labelData ) ;
}
} ) ;
} ) ;
2018-08-04 15:44:58 +02:00
function RefreshStatistics ( )
{
2022-01-23 17:29:25 +01:00
var nextXDays = $ ( "#info-due-soon-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 )
{
2022-01-23 17:29:25 +01:00
var dueTodayCount = 0 ;
var dueSoonCount = 0 ;
2018-08-04 15:44:58 +02:00
var overdueCount = 0 ;
2022-01-23 17:29:25 +01:00
var overdueThreshold = moment ( ) ;
2018-08-04 15:44:58 +02:00
var nextXDaysThreshold = moment ( ) . add ( nextXDays , "days" ) ;
2022-01-23 17:29:25 +01:00
var todayThreshold = moment ( ) . endOf ( "day" ) ;
2020-08-30 12:18:16 +02:00
result . forEach ( element =>
{
2018-08-04 15:44:58 +02:00
var date = moment ( element . next _estimated _charge _time ) ;
2022-01-23 17:29:25 +01:00
if ( date . isBefore ( overdueThreshold ) )
2018-08-04 15:44:58 +02:00
{
overdueCount ++ ;
}
2022-01-23 17:29:25 +01:00
else if ( date . isSameOrBefore ( todayThreshold ) )
{
dueTodayCount ++ ;
dueSoonCount ++ ;
}
else if ( date . isSameOrBefore ( nextXDaysThreshold ) )
2018-08-04 15:44:58 +02:00
{
2022-01-23 17:29:25 +01:00
dueSoonCount ++ ;
2018-08-04 15:44:58 +02:00
}
} ) ;
2019-01-19 00:37:21 -07:00
2022-04-04 20:10:29 +02:00
$ ( "#info-due-today-batteries" ) . html ( '<span class="d-block d-md-none">' + dueTodayCount + ' <i class="fa-solid fa-clock"></i></span><span class="d-none d-md-block">' + _ _n ( dueTodayCount , '%s battery is due to be charged today' , '%s batteries are due to be charged today' ) ) ;
$ ( "#info-due-soon-batteries" ) . html ( '<span class="d-block d-md-none">' + dueSoonCount + ' <i class="fa-solid fa-clock"></i></span><span class="d-none d-md-block">' + _ _n ( dueSoonCount , '%s battery is due to be charged' , '%s batteries are due to be charged' ) + ' ' + _ _n ( nextXDays , 'within the next day' , 'within the next %s days' ) ) ;
$ ( "#info-overdue-batteries" ) . html ( '<span class="d-block d-md-none">' + overdueCount + ' <i class="fa-solid fa-times-circle"></i></span><span class="d-none d-md-block">' + _ _n ( overdueCount , '%s battery is overdue to be charged' , '%s batteries are overdue to be charged' ) ) ;
2018-08-04 15:44:58 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
RefreshStatistics ( ) ;