2017-07-25 20:03:31 +02:00
$ ( '#save-habit-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
if ( Grocy . EditMode === 'create' )
{
2018-04-18 19:03:39 +02:00
Grocy . Api . Post ( 'add-object/habits' , $ ( '#habit-form' ) . serializeJSON ( ) ,
2017-07-25 20:03:31 +02:00
function ( result )
{
2018-04-18 19:03:39 +02:00
window . location . href = U ( '/habits' ) ;
2017-07-25 20:03:31 +02:00
} ,
function ( xhr )
{
2018-09-08 09:26:12 +02:00
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
2017-07-25 20:03:31 +02:00
}
) ;
}
else
{
2018-04-18 19:03:39 +02:00
Grocy . Api . Post ( 'edit-object/habits/' + Grocy . EditObjectId , $ ( '#habit-form' ) . serializeJSON ( ) ,
2017-07-25 20:03:31 +02:00
function ( result )
{
2018-04-18 19:03:39 +02:00
window . location . href = U ( '/habits' ) ;
2017-07-25 20:03:31 +02:00
} ,
function ( xhr )
{
2018-09-08 09:26:12 +02:00
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
2017-07-25 20:03:31 +02:00
}
) ;
}
} ) ;
2018-07-11 19:43:05 +02:00
$ ( '#habit-form input' ) . keyup ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'habit-form' ) ;
} ) ;
$ ( '#habit-form input' ) . keydown ( function ( event )
{
if ( event . keyCode === 13 ) //Enter
{
if ( document . getElementById ( 'habit-form' ) . checkValidity ( ) === false ) //There is at least one validation error
{
event . preventDefault ( ) ;
return false ;
}
else
{
$ ( '#save-habit-button' ) . click ( ) ;
}
}
} ) ;
2018-04-16 19:11:32 +02:00
$ ( '#name' ) . focus ( ) ;
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'habit-form' ) ;
2017-07-25 20:03:31 +02:00
$ ( '.input-group-habit-period-type' ) . on ( 'change' , function ( e )
{
var periodType = $ ( '#period_type' ) . val ( ) ;
var periodDays = $ ( '#period_days' ) . val ( ) ;
if ( periodType === 'dynamic-regular' )
{
2018-04-21 19:18:00 +02:00
$ ( '#habit-period-type-info' ) . text ( L ( 'This means it is estimated that a new execution of this habit is tracked #1 days after the last was tracked' , periodDays . toString ( ) ) ) ;
2018-07-11 19:43:05 +02:00
$ ( '#habit-period-type-info' ) . removeClass ( 'd-none' ) ;
2017-07-25 20:03:31 +02:00
}
else
{
2018-07-11 19:43:05 +02:00
$ ( '#habit-period-type-info' ) . addClass ( 'd-none' ) ;
2017-07-25 20:03:31 +02:00
}
} ) ;