2018-09-22 13:26:58 +02:00
var choresOverviewTable = $ ( '#chores-overview-table' ) . DataTable ( {
2018-05-13 09:38:22 +02:00
'order' : [ [ 2 , 'desc' ] ] ,
'columnDefs' : [
2020-01-03 14:18:56 +01:00
{ 'orderable' : false , 'targets' : 0 } ,
{ 'searchable' : false , "targets" : 0 }
2020-12-07 19:48:33 +01:00
] . concat ( $ . fn . dataTable . defaults . columnDefs )
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
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-09-22 13:26:58 +02:00
choresOverviewTable . search ( value ) . draw ( ) ;
2019-10-15 19:59:14 +02:00
} , 200 ) ) ;
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
2019-09-17 13:13:26 +02:00
choresOverviewTable . column ( 5 ) . search ( value ) . draw ( ) ;
2018-09-24 19:13:53 +02:00
} ) ;
2019-09-17 16:50:29 +02:00
$ ( "#user-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" ) ;
choresOverviewTable . column ( 6 ) . search ( value ) . draw ( ) ;
if ( ! value . isEmpty ( ) )
{
UpdateUriParam ( "user" , $ ( "#user-filter option:selected" ) . data ( "user-id" ) ) ;
}
} ) ;
2020-11-07 14:53:45 +01:00
$ ( "#clear-filter-button" ) . on ( "click" , function ( )
{
$ ( "#search" ) . val ( "" ) ;
$ ( "#status-filter" ) . val ( "all" ) ;
$ ( "#user-filter" ) . val ( "all" ) ;
choresOverviewTable . column ( 5 ) . search ( "" ) . draw ( ) ;
choresOverviewTable . column ( 6 ) . search ( "" ) . draw ( ) ;
choresOverviewTable . search ( "" ) . draw ( ) ;
} ) ;
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" ) ;
} ) ;
2020-04-19 08:51:02 -04:00
$ ( ".user-filter-message" ) . on ( "click" , function ( )
2019-09-17 16:50:29 +02:00
{
2019-09-17 16:58:42 +02:00
var value = $ ( this ) . data ( "user-filter" ) ;
2019-09-17 16:50:29 +02:00
$ ( "#user-filter" ) . val ( value ) ;
$ ( "#user-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
2019-05-04 16:13:05 +02:00
Grocy . Api . Get ( 'objects/chores/' + choreId ,
function ( chore )
2018-05-13 09:38:22 +02:00
{
2019-05-04 16:13:05 +02:00
var trackedTime = moment ( ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
if ( chore . track _date _only == 1 )
{
trackedTime = moment ( ) . format ( 'YYYY-MM-DD' ) ;
}
Grocy . Api . Post ( 'chores/' + choreId + '/execute' , { 'tracked_time' : trackedTime } ,
function ( )
2018-08-07 20:11:08 +02:00
{
2019-05-04 16:13:05 +02:00
Grocy . Api . Get ( 'chores/' + choreId ,
function ( result )
2018-08-07 20:11:08 +02:00
{
2019-05-04 16:13:05 +02:00
var choreRow = $ ( '#chore-' + choreId + '-row' ) ;
var nextXDaysThreshold = moment ( ) . add ( $ ( "#info-due-chores" ) . data ( "next-x-days" ) , "days" ) ;
var now = moment ( ) ;
var nextExecutionTime = moment ( result . next _estimated _execution _time ) ;
choreRow . removeClass ( "table-warning" ) ;
choreRow . removeClass ( "table-danger" ) ;
2019-09-17 17:17:33 +02:00
$ ( '#chore-' + choreId + '-due-filter-column' ) . html ( "" ) ;
2019-05-04 16:13:05 +02:00
if ( nextExecutionTime . isBefore ( now ) )
{
choreRow . addClass ( "table-danger" ) ;
2019-09-17 17:17:33 +02:00
$ ( '#chore-' + choreId + '-due-filter-column' ) . html ( "overdue" ) ;
2019-05-04 16:13:05 +02:00
}
else if ( nextExecutionTime . isBefore ( nextXDaysThreshold ) )
{
choreRow . addClass ( "table-warning" ) ;
2019-09-17 17:17:33 +02:00
$ ( '#chore-' + choreId + '-due-filter-column' ) . html ( "duesoon" ) ;
2019-05-04 16:13:05 +02:00
}
2020-01-28 19:27:18 +01:00
animateCSS ( "#chore-" + choreId + "-row td:not(:first)" , "shake" ) ;
$ ( '#chore-' + choreId + '-last-tracked-time' ) . text ( trackedTime ) ;
2019-05-04 16:13:05 +02:00
$ ( '#chore-' + choreId + '-last-tracked-time-timeago' ) . attr ( 'datetime' , trackedTime ) ;
if ( result . chore . period _type == "dynamic-regular" )
{
2020-01-28 19:27:18 +01:00
$ ( '#chore-' + choreId + '-next-execution-time' ) . text ( result . next _estimated _execution _time ) ;
2019-05-04 16:13:05 +02:00
$ ( '#chore-' + choreId + '-next-execution-time-timeago' ) . attr ( 'datetime' , result . next _estimated _execution _time ) ;
}
2019-09-17 13:13:26 +02:00
if ( result . chore . next _execution _assigned _to _user _id != null )
{
2020-01-28 19:27:18 +01:00
$ ( '#chore-' + choreId + '-next-execution-assigned-user' ) . text ( result . next _execution _assigned _user . display _name ) ;
2019-09-17 13:13:26 +02:00
}
2019-05-04 16:13:05 +02:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2019-05-05 14:13:50 +02:00
toastr . success ( _ _t ( 'Tracked execution of chore %1$s on %2$s' , choreName , trackedTime ) ) ;
2019-05-04 16:13:05 +02:00
RefreshStatistics ( ) ;
2019-09-14 13:05:36 +02:00
// Delay due to delayed/animated set of new timestamps above
setTimeout ( function ( )
{
2020-01-27 19:00:49 +01:00
RefreshContextualTimeago ( "#chore-" + choreId + "-row" ) ;
2019-09-17 17:17:33 +02:00
// Refresh the DataTable to re-apply filters
choresOverviewTable . rows ( ) . invalidate ( ) . draw ( false ) ;
$ ( ".input-group-filter" ) . trigger ( "change" ) ;
2019-09-14 13:05:36 +02:00
} , 550 ) ;
2019-05-04 16:13:05 +02:00
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( ) ;
console . error ( xhr ) ;
}
) ;
2018-08-07 20:11:08 +02:00
} ,
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 )
{
2019-05-04 16:13:05 +02:00
Grocy . FrontendHelpers . EndUiBusy ( "choretracking-form" ) ;
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 ;
2019-09-17 13:13:26 +02:00
var assignedToMeCount = 0 ;
2018-08-04 15:44:58 +02:00
var now = moment ( ) ;
var nextXDaysThreshold = moment ( ) . add ( nextXDays , "days" ) ;
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 _execution _time ) ;
if ( date . isBefore ( now ) )
{
overdueCount ++ ;
}
else if ( date . isBefore ( nextXDaysThreshold ) )
{
dueCount ++ ;
}
2019-09-17 13:13:26 +02:00
if ( parseInt ( element . next _execution _assigned _to _user _id ) == Grocy . UserId )
{
assignedToMeCount ++ ;
}
2018-08-04 15:44:58 +02:00
} ) ;
2019-01-19 00:37:21 -07:00
2020-11-07 14:53:45 +01:00
$ ( "#info-due-chores" ) . html ( '<span class="d-block d-md-none">' + dueCount + ' <i class="fas fa-clock"></i></span><span class="d-none d-md-block">' + _ _n ( dueCount , '%s chore is due to be done' , '%s chores are due to be done' ) + ' ' + _ _n ( nextXDays , 'within the next day' , 'within the next %s days' ) ) ;
$ ( "#info-overdue-chores" ) . html ( '<span class="d-block d-md-none">' + overdueCount + ' <i class="fas fa-times-circle"></i></span><span class="d-none d-md-block">' + _ _n ( overdueCount , '%s chore is overdue to be done' , '%s chores are overdue to be done' ) ) ;
$ ( "#info-assigned-to-me-chores" ) . html ( '<span class="d-block d-md-none">' + assignedToMeCount + ' <i class="fas fa-exclamation-circle"></i></span><span class="d-none d-md-block">' + _ _n ( assignedToMeCount , '%s chore is assigned to me' , '%s chores are assigned to me' ) ) ;
2018-08-04 15:44:58 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2019-09-17 16:50:29 +02:00
if ( GetUriParam ( "user" ) !== undefined )
{
$ ( "#user-filter" ) . val ( "xx" + GetUriParam ( "user" ) + "xx" ) ;
$ ( "#user-filter" ) . trigger ( "change" ) ;
}
2018-08-04 15:44:58 +02:00
RefreshStatistics ( ) ;