2018-09-22 13:26:58 +02:00
$ ( '#save-chore-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
2018-11-24 19:40:50 +01:00
var jsonData = $ ( '#chore-form' ) . serializeJSON ( ) ;
Grocy . FrontendHelpers . BeginUiBusy ( "chore-form" ) ;
2018-09-22 13:26:58 +02:00
if ( Grocy . EditMode === 'create' )
{
2019-01-19 14:51:51 +01:00
Grocy . Api . Post ( 'objects/chores' , jsonData ,
2018-09-22 13:26:58 +02:00
function ( result )
{
2019-04-22 22:16:35 +02:00
Grocy . EditObjectId = result . created _object _id ;
Grocy . Components . UserfieldsForm . Save ( function ( )
{
window . location . href = U ( '/chores' ) ;
} ) ;
2018-09-22 13:26:58 +02:00
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "chore-form" ) ;
2018-09-22 13:26:58 +02:00
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
}
) ;
}
else
{
2019-01-19 14:51:51 +01:00
Grocy . Api . Put ( 'objects/chores/' + Grocy . EditObjectId , jsonData ,
2018-09-22 13:26:58 +02:00
function ( result )
{
2019-04-22 22:16:35 +02:00
Grocy . Components . UserfieldsForm . Save ( function ( )
{
window . location . href = U ( '/chores' ) ;
} ) ;
2018-09-22 13:26:58 +02:00
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( "chore-form" ) ;
2018-09-22 13:26:58 +02:00
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
}
) ;
}
} ) ;
$ ( '#chore-form input' ) . keyup ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'chore-form' ) ;
} ) ;
$ ( '#chore-form input' ) . keydown ( function ( event )
{
if ( event . keyCode === 13 ) //Enter
{
2018-09-29 13:41:56 +02:00
event . preventDefault ( ) ;
2018-09-22 13:26:58 +02:00
if ( document . getElementById ( 'chore-form' ) . checkValidity ( ) === false ) //There is at least one validation error
{
return false ;
}
else
{
$ ( '#save-chore-button' ) . click ( ) ;
}
}
} ) ;
2019-04-22 14:01:31 +02:00
var checkboxValues = $ ( "#period_config" ) . val ( ) . split ( "," ) ;
for ( var i = 0 ; i < checkboxValues . length ; i ++ )
{
if ( ! checkboxValues [ i ] . isEmpty ( ) )
{
$ ( "#" + checkboxValues [ i ] ) . prop ( 'checked' , true ) ;
}
}
2019-04-22 22:16:35 +02:00
Grocy . Components . UserfieldsForm . Load ( ) ;
2018-09-22 13:26:58 +02:00
$ ( '#name' ) . focus ( ) ;
Grocy . FrontendHelpers . ValidateForm ( 'chore-form' ) ;
2019-03-04 17:43:12 +01:00
setTimeout ( function ( )
{
$ ( ".input-group-chore-period-type" ) . trigger ( "change" ) ;
} , 100 ) ;
2018-09-22 13:26:58 +02:00
$ ( '.input-group-chore-period-type' ) . on ( 'change' , function ( e )
{
var periodType = $ ( '#period_type' ) . val ( ) ;
var periodDays = $ ( '#period_days' ) . val ( ) ;
2019-04-22 14:01:31 +02:00
$ ( ".period-type-input" ) . addClass ( "d-none" ) ;
$ ( ".period-type-" + periodType ) . removeClass ( "d-none" ) ;
$ ( '#chore-period-type-info' ) . text ( "" ) ;
$ ( "#period_config" ) . val ( "" ) ;
if ( periodType === 'manually' )
{
//
}
else if ( periodType === 'dynamic-regular' )
2018-09-22 13:26:58 +02:00
{
2019-04-22 14:01:31 +02:00
$ ( "label[for='period_days']" ) . text ( L ( "Period days" ) ) ;
$ ( "#period_days" ) . attr ( "min" , "0" ) ;
$ ( "#period_days" ) . attr ( "max" , "9999" ) ;
$ ( "#period_days" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( L ( 'This cannot be negative' ) ) ;
2018-09-22 13:26:58 +02:00
$ ( '#chore-period-type-info' ) . text ( L ( 'This means it is estimated that a new execution of this chore is tracked #1 days after the last was tracked' , periodDays . toString ( ) ) ) ;
}
2019-04-22 14:01:31 +02:00
else if ( periodType === 'daily' )
{
//
}
else if ( periodType === 'weekly' )
{
$ ( "#period_config" ) . val ( $ ( ".period-type-weekly input:checkbox:checked" ) . map ( function ( ) { return this . value ; } ) . get ( ) . join ( "," ) ) ;
}
else if ( periodType === 'monthly' )
2018-09-22 13:26:58 +02:00
{
2019-04-22 14:01:31 +02:00
$ ( "label[for='period_days']" ) . text ( L ( "Day of month" ) ) ;
$ ( "#period_days" ) . attr ( "min" , "1" ) ;
$ ( "#period_days" ) . attr ( "max" , "31" ) ;
$ ( "#period_days" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( L ( 'The amount must be between #1 and #2' , "0" , "31" ) ) ;
2018-09-22 13:26:58 +02:00
}
} ) ;