2018-09-22 13:26:58 +02:00
$ ( '#save-chore-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
2020-11-11 17:34:37 +01:00
var jsonData = $ ( '#chore-form' ) . serializeJSON ( ) ;
2019-09-26 17:20:25 +02:00
if ( Grocy . FeatureFlags . GROCY _FEATURE _FLAG _CHORES _ASSIGNMENTS )
{
jsonData . assignment _config = $ ( "#assignment_config" ) . val ( ) . join ( "," ) ;
}
2020-08-29 16:41:27 +02:00
2018-11-24 19:40:50 +01:00
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 ( )
{
2019-09-17 13:13:26 +02:00
Grocy . Api . Post ( 'chores/executions/calculate-next-assignments' , { "chore_id" : Grocy . EditObjectId } ,
2020-08-30 12:18:16 +02:00
function ( result )
2019-09-17 13:13:26 +02:00
{
window . location . href = U ( '/chores' ) ;
} ,
2020-08-30 12:18:16 +02:00
function ( xhr )
2019-09-17 13:13:26 +02:00
{
Grocy . FrontendHelpers . EndUiBusy ( ) ;
console . error ( xhr ) ;
}
) ;
2019-04-22 22:16:35 +02:00
} ) ;
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 ( )
{
2019-09-17 13:13:26 +02:00
Grocy . Api . Post ( 'chores/executions/calculate-next-assignments' , { "chore_id" : Grocy . EditObjectId } ,
2020-08-30 12:18:16 +02:00
function ( result )
2019-09-17 13:13:26 +02:00
{
window . location . href = U ( '/chores' ) ;
} ,
2020-08-30 12:18:16 +02:00
function ( xhr )
2019-09-17 13:13:26 +02:00
{
Grocy . FrontendHelpers . EndUiBusy ( ) ;
console . error ( xhr ) ;
}
) ;
2019-04-22 22:16:35 +02:00
} ) ;
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" ) ;
2019-09-17 13:13:26 +02:00
$ ( ".input-group-chore-assignment-type" ) . trigger ( "change" ) ;
2019-09-18 10:02:52 +02:00
// Click twice to trigger on-click but not change the actual checked state
$ ( "#consume_product_on_execution" ) . click ( ) ;
$ ( "#consume_product_on_execution" ) . click ( ) ;
2019-09-18 10:08:59 +02:00
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
2019-03-04 17:43:12 +01:00
} , 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-10-04 11:14:11 +02:00
var periodInterval = $ ( '#period_interval' ) . val ( ) ;
2018-09-22 13:26:58 +02:00
2019-04-22 14:01:31 +02:00
$ ( ".period-type-input" ) . addClass ( "d-none" ) ;
$ ( ".period-type-" + periodType ) . removeClass ( "d-none" ) ;
2020-11-12 21:35:10 +01:00
$ ( '#chore-period-type-info' ) . attr ( "data-original-title" , "" ) ;
2019-04-22 14:01:31 +02:00
$ ( "#period_config" ) . val ( "" ) ;
if ( periodType === 'manually' )
{
2020-11-12 21:35:10 +01:00
$ ( '#chore-period-type-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore is not scheduled' ) ) ;
2019-04-22 14:01:31 +02:00
}
else if ( periodType === 'dynamic-regular' )
2018-09-22 13:26:58 +02:00
{
2019-05-01 20:19:18 +02:00
$ ( "label[for='period_days']" ) . text ( _ _t ( "Period days" ) ) ;
2019-04-22 14:01:31 +02:00
$ ( "#period_days" ) . attr ( "min" , "0" ) ;
2020-11-17 19:11:02 +01:00
$ ( "#period_days" ) . removeAttr ( "max" ) ;
2020-11-12 21:35:10 +01:00
$ ( '#chore-period-type-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore is scheduled %s days after the last execution' , periodDays . toString ( ) ) ) ;
2018-09-22 13:26:58 +02:00
}
2019-04-22 14:01:31 +02:00
else if ( periodType === 'daily' )
{
2020-11-12 21:35:10 +01:00
$ ( '#chore-period-type-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore is scheduled 1 day after the last execution' ) ) ;
$ ( '#chore-period-interval-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore should only be scheduled every %s days' , periodInterval . toString ( ) ) ) ;
2019-04-22 14:01:31 +02:00
}
else if ( periodType === 'weekly' )
{
2020-11-12 21:35:10 +01:00
$ ( '#chore-period-type-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore is scheduled 1 day after the last execution, but only for the weekdays selected below' ) ) ;
2020-08-30 12:18:16 +02:00
$ ( "#period_config" ) . val ( $ ( ".period-type-weekly input:checkbox:checked" ) . map ( function ( ) { return this . value ; } ) . get ( ) . join ( "," ) ) ;
2020-11-12 21:35:10 +01:00
$ ( '#chore-period-interval-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore should only be scheduled every %s weeks' , periodInterval . toString ( ) ) ) ;
2019-04-22 14:01:31 +02:00
}
else if ( periodType === 'monthly' )
2018-09-22 13:26:58 +02:00
{
2020-11-12 21:35:10 +01:00
$ ( '#chore-period-type-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore is scheduled on the below selected day of each month' ) ) ;
2019-05-01 20:19:18 +02:00
$ ( "label[for='period_days']" ) . text ( _ _t ( "Day of month" ) ) ;
2019-04-22 14:01:31 +02:00
$ ( "#period_days" ) . attr ( "min" , "1" ) ;
$ ( "#period_days" ) . attr ( "max" , "31" ) ;
2020-11-12 21:35:10 +01:00
$ ( '#chore-period-interval-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore should only be scheduled every %s months' , periodInterval . toString ( ) ) ) ;
2018-09-22 13:26:58 +02:00
}
2019-10-04 11:24:51 +02:00
else if ( periodType === 'yearly' )
{
2020-11-12 21:35:10 +01:00
$ ( '#chore-period-type-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore is scheduled 1 year after the last execution' ) ) ;
$ ( '#chore-period-interval-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore should only be scheduled every %s years' , periodInterval . toString ( ) ) ) ;
2019-10-04 11:24:51 +02:00
}
2019-09-17 13:13:26 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'chore-form' ) ;
} ) ;
$ ( '.input-group-chore-assignment-type' ) . on ( 'change' , function ( e )
{
var assignmentType = $ ( '#assignment_type' ) . val ( ) ;
$ ( '#chore-period-assignment-info' ) . text ( "" ) ;
$ ( "#assignment_config" ) . removeAttr ( "required" ) ;
$ ( "#assignment_config" ) . attr ( "disabled" , "" ) ;
if ( assignmentType === 'no-assignment' )
{
2020-11-12 21:35:10 +01:00
$ ( '#chore-assignment-type-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore will not be assigned to anyone' ) ) ;
2019-09-17 13:13:26 +02:00
}
else if ( assignmentType === 'who-least-did-first' )
{
2020-11-12 21:35:10 +01:00
$ ( '#chore-assignment-type-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore will be assigned to the one who executed it least' ) ) ;
2019-09-17 13:13:26 +02:00
$ ( "#assignment_config" ) . attr ( "required" , "" ) ;
$ ( "#assignment_config" ) . removeAttr ( "disabled" ) ;
}
else if ( assignmentType === 'random' )
{
2020-11-12 21:35:10 +01:00
$ ( '#chore-assignment-type-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore will be assigned randomly' ) ) ;
2019-09-17 13:13:26 +02:00
$ ( "#assignment_config" ) . attr ( "required" , "" ) ;
$ ( "#assignment_config" ) . removeAttr ( "disabled" ) ;
}
else if ( assignmentType === 'in-alphabetical-order' )
{
2020-11-12 21:35:10 +01:00
$ ( '#chore-assignment-type-info' ) . attr ( "data-original-title" , _ _t ( 'This means the next execution of this chore will be assigned to the next one in alphabetical order' ) ) ;
2019-09-17 13:13:26 +02:00
$ ( "#assignment_config" ) . attr ( "required" , "" ) ;
$ ( "#assignment_config" ) . removeAttr ( "disabled" ) ;
}
Grocy . FrontendHelpers . ValidateForm ( 'chore-form' ) ;
2018-09-22 13:26:58 +02:00
} ) ;
2019-09-18 10:02:52 +02:00
$ ( "#consume_product_on_execution" ) . on ( "click" , function ( )
{
if ( this . checked )
{
2019-09-19 12:48:02 +02:00
Grocy . Components . ProductPicker . Enable ( ) ;
2019-09-18 10:02:52 +02:00
$ ( "#product_amount" ) . removeAttr ( "disabled" ) ;
}
else
{
2019-09-19 12:48:02 +02:00
Grocy . Components . ProductPicker . Disable ( ) ;
2019-09-18 10:02:52 +02:00
$ ( "#product_amount" ) . attr ( "disabled" , "" ) ;
}
Grocy . FrontendHelpers . ValidateForm ( "chore-form" ) ;
} ) ;
2019-09-18 10:08:59 +02:00
Grocy . Components . ProductPicker . GetPicker ( ) . on ( 'change' , function ( e )
{
var productId = $ ( e . target ) . val ( ) ;
if ( productId )
{
Grocy . Api . Get ( 'stock/products/' + productId ,
function ( productDetails )
{
$ ( '#amount_qu_unit' ) . text ( productDetails . quantity _unit _stock . name ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
} ) ;