2018-07-09 19:27:22 +02:00
var habitsOverviewTable = $ ( '#habits-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 ,
'stateSave' : true
2017-07-25 20:03:31 +02:00
} ) ;
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 = "" ;
}
habitsOverviewTable . search ( value ) . draw ( ) ;
} ) ;
2018-05-13 09:38:22 +02:00
$ ( document ) . on ( 'click' , '.track-habit-button' , function ( e )
{
var habitId = $ ( e . currentTarget ) . attr ( 'data-habit-id' ) ;
var habitName = $ ( e . currentTarget ) . attr ( 'data-habit-name' ) ;
var trackedTime = moment ( ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
Grocy . Api . Get ( 'habits/track-habit-execution/' + habitId + '?tracked_time=' + trackedTime ,
function ( result )
{
$ ( '#habit-' + habitId + '-last-tracked-time' ) . parent ( ) . effect ( 'highlight' , { } , 500 ) ;
$ ( '#habit-' + habitId + '-last-tracked-time' ) . fadeOut ( 500 , function ( ) {
$ ( this ) . text ( trackedTime ) . fadeIn ( 500 ) ;
} ) ;
$ ( '#habit-' + habitId + '-last-tracked-time-timeago' ) . attr ( 'datetime' , trackedTime ) ;
RefreshContextualTimeago ( ) ;
toastr . success ( L ( 'Tracked execution of habit #1 on #2' , habitName , trackedTime ) ) ;
2018-08-04 15:44:58 +02:00
RefreshStatistics ( ) ;
2018-05-13 09:38:22 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ) ;
2018-08-04 15:44:58 +02:00
function RefreshStatistics ( )
{
var nextXDays = $ ( "#info-due-habits" ) . data ( "next-x-days" ) ;
Grocy . Api . Get ( 'habits/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 _execution _time ) ;
if ( date . isBefore ( now ) )
{
overdueCount ++ ;
}
else if ( date . isBefore ( nextXDaysThreshold ) )
{
dueCount ++ ;
}
} ) ;
$ ( "#info-due-habits" ) . text ( Pluralize ( dueCount , L ( '#1 habit is due to be done within the next #2 days' , dueCount , nextXDays ) , L ( '#1 habits are due to be done within the next #2 days' , dueCount , nextXDays ) ) ) ;
$ ( "#info-overdue-habits" ) . text ( Pluralize ( overdueCount , L ( '#1 habit is overdue to be done' , overdueCount ) , L ( '#1 habits are overdue to be done' , overdueCount ) ) ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
RefreshStatistics ( ) ;