2018-09-22 22:01:32 +02:00
var tasksTable = $ ( '#tasks-table' ) . DataTable ( {
'paginate' : false ,
'order' : [ [ 2 , 'desc' ] ] ,
'columnDefs' : [
2018-09-23 09:22:54 +02:00
{ 'orderable' : false , 'targets' : 0 } ,
{ 'visible' : false , 'targets' : 3 }
2018-09-22 22:01:32 +02:00
] ,
'language' : JSON . parse ( L ( 'datatables_localization' ) ) ,
'scrollY' : false ,
'colReorder' : true ,
'stateSave' : true ,
'stateSaveParams' : function ( settings , data )
{
data . search . search = "" ;
2018-09-23 09:22:54 +02:00
} ,
'rowGroup' : {
dataSrc : 3
2018-09-22 22:01:32 +02:00
}
} ) ;
$ ( "#search" ) . on ( "keyup" , function ( )
{
var value = $ ( this ) . val ( ) ;
if ( value === "all" )
{
value = "" ;
}
tasksTable . search ( value ) . draw ( ) ;
} ) ;
$ ( document ) . on ( 'click' , '.do-task-button' , function ( e )
{
var taskId = $ ( e . currentTarget ) . attr ( 'data-task-id' ) ;
2018-09-23 09:22:54 +02:00
var taskName = $ ( e . currentTarget ) . attr ( 'data-task-name' ) ;
2018-09-22 22:01:32 +02:00
var doneTime = moment ( ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ;
2018-09-23 09:22:54 +02:00
Grocy . Api . Get ( 'tasks/mark-task-done/' + taskId + '?tracked_time=' + doneTime ,
2018-09-22 22:01:32 +02:00
function ( )
{
2018-09-23 09:22:54 +02:00
toastr . success ( L ( 'Marked task #1 as completed on #2' , taskName , doneTime ) ) ;
RefreshContextualTimeago ( ) ;
RefreshStatistics ( ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ) ;
2018-09-22 22:01:32 +02:00
2018-09-23 09:22:54 +02:00
$ ( document ) . on ( 'click' , '.delete-task-button' , function ( e )
{
var objectName = $ ( e . currentTarget ) . attr ( 'data-task-name' ) ;
var objectId = $ ( e . currentTarget ) . attr ( 'data-task-id' ) ;
2018-09-22 22:01:32 +02:00
2018-09-23 09:22:54 +02:00
bootbox . confirm ( {
message : L ( 'Are you sure to delete task "#1"?' , objectName ) ,
buttons : {
confirm : {
label : L ( 'Yes' ) ,
className : 'btn-success'
} ,
cancel : {
label : L ( 'No' ) ,
className : 'btn-danger'
}
} ,
callback : function ( result )
{
if ( result === true )
{
Grocy . Api . Get ( 'delete-object/tasks/' + objectId ,
function ( result )
2018-09-22 22:01:32 +02:00
{
2018-09-23 09:22:54 +02:00
$ ( '#task-' + objectId + '-row' ) . fadeOut ( 500 , function ( )
2018-09-22 22:01:32 +02:00
{
2018-09-23 09:22:54 +02:00
$ ( this ) . remove ( ) ;
2018-09-22 22:01:32 +02:00
} ) ;
2018-09-23 09:22:54 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
2018-09-22 22:01:32 +02:00
}
2018-09-23 09:22:54 +02:00
) ;
}
2018-09-22 22:01:32 +02:00
}
2018-09-23 09:22:54 +02:00
} ) ;
2018-09-22 22:01:32 +02:00
} ) ;
function RefreshStatistics ( )
{
var nextXDays = $ ( "#info-due-tasks" ) . data ( "next-x-days" ) ;
Grocy . Api . Get ( 'tasks/get-current' ,
function ( result )
{
var dueCount = 0 ;
var overdueCount = 0 ;
var now = moment ( ) ;
var nextXDaysThreshold = moment ( ) . add ( nextXDays , "days" ) ;
result . forEach ( element => {
2018-09-23 09:22:54 +02:00
var date = moment ( element . due _date ) ;
2018-09-22 22:01:32 +02:00
if ( date . isBefore ( now ) )
{
overdueCount ++ ;
}
else if ( date . isBefore ( nextXDaysThreshold ) )
{
dueCount ++ ;
}
} ) ;
$ ( "#info-due-tasks" ) . text ( Pluralize ( dueCount , L ( '#1 task is due to be done within the next #2 days' , dueCount , nextXDays ) , L ( '#1 tasks are due to be done within the next #2 days' , dueCount , nextXDays ) ) ) ;
$ ( "#info-overdue-tasks" ) . text ( Pluralize ( overdueCount , L ( '#1 task is overdue to be done' , overdueCount ) , L ( '#1 tasks are overdue to be done' , overdueCount ) ) ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
RefreshStatistics ( ) ;