2018-09-22 13:26:58 +02:00
var choresOverviewTable = $ ( '#chores-overview-table' ) . DataTable ( {
2018-07-09 19:27:22 +02:00
'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-07-25 20:03:31 +02:00
} ) ;
2019-01-05 20:06:35 +01:00
$ ( '#chores-overview-table tbody' ) . removeClass ( "d-none" ) ;
2019-03-04 17:43:12 +01:00
choresOverviewTable . columns . adjust ( ) . draw ( ) ;
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-09-22 13:26:58 +02:00
choresOverviewTable . search ( value ) . draw ( ) ;
2018-07-09 19:27:22 +02:00
} ) ;
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
choresOverviewTable . 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-09-22 13:26:58 +02:00
$ ( document ) . on ( 'click' , '.track-chore-button' , function ( e )
2018-05-13 09:38:22 +02:00
{
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-09-22 13:26:58 +02:00
var choreId = $ ( e . currentTarget ) . attr ( 'data-chore-id' ) ;
var choreName = $ ( e . currentTarget ) . attr ( 'data-chore-name' ) ;
2018-05-13 09:38:22 +02:00
var trackedTime = moment ( ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
2019-01-19 14:51:51 +01:00
Grocy . Api . Post ( 'chores/' + choreId + '/execute' , { '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 ( 'chores/' + choreId ,
2018-08-07 20:11:08 +02:00
function ( result )
{
2018-09-22 13:26:58 +02:00
var choreRow = $ ( '#chore-' + choreId + '-row' ) ;
var nextXDaysThreshold = moment ( ) . add ( $ ( "#info-due-chores" ) . data ( "next-x-days" ) , "days" ) ;
2018-08-07 20:11:08 +02:00
var now = moment ( ) ;
var nextExecutionTime = moment ( result . next _estimated _execution _time ) ;
2018-05-13 09:38:22 +02:00
2018-09-22 13:26:58 +02:00
choreRow . removeClass ( "table-warning" ) ;
choreRow . removeClass ( "table-danger" ) ;
2018-08-07 20:11:08 +02:00
if ( nextExecutionTime . isBefore ( now ) )
{
2018-09-22 13:26:58 +02:00
choreRow . addClass ( "table-danger" ) ;
2018-08-07 20:11:08 +02:00
}
else if ( nextExecutionTime . isBefore ( nextXDaysThreshold ) )
{
2018-09-22 13:26:58 +02:00
choreRow . addClass ( "table-warning" ) ;
2018-08-07 20:11:08 +02:00
}
2018-09-22 13:26:58 +02:00
$ ( '#chore-' + choreId + '-last-tracked-time' ) . parent ( ) . effect ( 'highlight' , { } , 500 ) ;
$ ( '#chore-' + choreId + '-last-tracked-time' ) . fadeOut ( 500 , function ( )
2018-08-07 20:11:08 +02:00
{
$ ( this ) . text ( trackedTime ) . fadeIn ( 500 ) ;
} ) ;
2018-09-22 13:26:58 +02:00
$ ( '#chore-' + choreId + '-last-tracked-time-timeago' ) . attr ( 'datetime' , trackedTime ) ;
2018-08-07 20:11:08 +02:00
2018-09-22 13:26:58 +02:00
if ( result . chore . period _type == "dynamic-regular" )
2018-08-07 20:11:08 +02:00
{
2018-09-22 13:26:58 +02:00
$ ( '#chore-' + choreId + '-next-execution-time' ) . parent ( ) . effect ( 'highlight' , { } , 500 ) ;
$ ( '#chore-' + choreId + '-next-execution-time' ) . fadeOut ( 500 , function ( )
2018-08-07 20:11:08 +02:00
{
$ ( this ) . text ( result . next _estimated _execution _time ) . fadeIn ( 500 ) ;
} ) ;
2018-09-22 13:26:58 +02:00
$ ( '#chore-' + choreId + '-next-execution-time-timeago' ) . attr ( 'datetime' , result . next _estimated _execution _time ) ;
2018-08-07 20:11:08 +02:00
}
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2018-09-22 13:26:58 +02:00
toastr . success ( L ( 'Tracked execution of chore #1 on #2' , choreName , 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
2019-03-09 13:11:50 +01:00
$ ( document ) . on ( "click" , ".chore-name-cell" , function ( e )
{
Grocy . Components . ChoreCard . Refresh ( $ ( e . currentTarget ) . attr ( "data-chore-id" ) ) ;
$ ( "#choresoverview-chorecard-modal" ) . modal ( "show" ) ;
} ) ;
2018-08-04 15:44:58 +02:00
function RefreshStatistics ( )
{
2018-09-22 13:26:58 +02:00
var nextXDays = $ ( "#info-due-chores" ) . data ( "next-x-days" ) ;
2019-01-19 00:37:21 -07:00
Grocy . Api . Get ( 'chores' ,
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 _execution _time ) ;
if ( date . isBefore ( now ) )
{
overdueCount ++ ;
}
else if ( date . isBefore ( nextXDaysThreshold ) )
{
dueCount ++ ;
}
} ) ;
2019-01-19 00:37:21 -07:00
2018-09-22 13:26:58 +02:00
$ ( "#info-due-chores" ) . text ( Pluralize ( dueCount , L ( '#1 chore is due to be done within the next #2 days' , dueCount , nextXDays ) , L ( '#1 chores are due to be done within the next #2 days' , dueCount , nextXDays ) ) ) ;
$ ( "#info-overdue-chores" ) . text ( Pluralize ( overdueCount , L ( '#1 chore is overdue to be done' , overdueCount ) , L ( '#1 chores are overdue to be done' , overdueCount ) ) ) ;
2018-08-04 15:44:58 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
RefreshStatistics ( ) ;