2019-04-20 17:04:40 +02:00
$ ( '#save-shoppinglist-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
var jsonData = $ ( '#shoppinglist-form' ) . serializeJSON ( ) ;
Grocy . FrontendHelpers . BeginUiBusy ( "shoppinglist-form" ) ;
2019-09-24 18:27:50 +02:00
if ( GetUriParam ( "updateexistingproduct" ) !== undefined )
{
jsonData . product _amount = jsonData . amount ;
delete jsonData . amount ;
Grocy . Api . Post ( 'stock/shoppinglist/add-product' , jsonData ,
function ( result )
{
if ( GetUriParam ( "embedded" ) !== undefined )
{
Grocy . Api . Get ( 'stock/products/' + jsonData . product _id ,
function ( productDetails )
{
window . parent . postMessage ( WindowMessageBag ( "ShowSuccessMessage" , _ _t ( "Added %1$s of %2$s to the shopping list \"%3$s\"" , jsonData . product _amount + " " + _ _n ( jsonData . product _amount , productDetails . quantity _unit _purchase . name , productDetails . quantity _unit _purchase . name _plural ) , productDetails . product . name , $ ( "#shopping_list_id option:selected" ) . text ( ) ) ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
else
{
window . location . href = U ( '/shoppinglist?list=' + $ ( "#shopping_list_id" ) . val ( ) . toString ( ) ) ;
}
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( "shoppinglist-form" ) ;
console . error ( xhr ) ;
}
) ;
}
2019-04-20 17:04:40 +02:00
if ( Grocy . EditMode === 'create' )
{
Grocy . Api . Post ( 'objects/shopping_list' , jsonData ,
function ( result )
{
2019-09-20 13:37:53 +02:00
if ( GetUriParam ( "embedded" ) !== undefined )
{
Grocy . Api . Get ( 'stock/products/' + jsonData . product _id ,
function ( productDetails )
{
window . parent . postMessage ( WindowMessageBag ( "ShowSuccessMessage" , _ _t ( "Added %1$s of %2$s to the shopping list \"%3$s\"" , jsonData . amount + " " + _ _n ( jsonData . amount , productDetails . quantity _unit _purchase . name , productDetails . quantity _unit _purchase . name _plural ) , productDetails . product . name , $ ( "#shopping_list_id option:selected" ) . text ( ) ) ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
else
{
window . location . href = U ( '/shoppinglist?list=' + $ ( "#shopping_list_id" ) . val ( ) . toString ( ) ) ;
}
2019-04-20 17:04:40 +02:00
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( "shoppinglist-form" ) ;
console . error ( xhr ) ;
}
) ;
}
else
{
Grocy . Api . Put ( 'objects/shopping_list/' + Grocy . EditObjectId , jsonData ,
function ( result )
{
2019-09-20 13:37:53 +02:00
if ( GetUriParam ( "embedded" ) !== undefined )
{
Grocy . Api . Get ( 'stock/products/' + jsonData . product _id ,
function ( productDetails )
{
window . parent . postMessage ( WindowMessageBag ( "ShowSuccessMessage" , _ _t ( "Added %1$s of %2$s to the shopping list \"%3$s\"" , jsonData . amount + " " + _ _n ( jsonData . amount , productDetails . quantity _unit _purchase . name , productDetails . quantity _unit _purchase . name _plural ) , productDetails . product . name , $ ( "#shopping_list_id option:selected" ) . text ( ) ) ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
else
{
window . location . href = U ( '/shoppinglist?list=' + $ ( "#shopping_list_id" ) . val ( ) . toString ( ) ) ;
}
2019-04-20 17:04:40 +02:00
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( "shoppinglist-form" ) ;
console . error ( xhr ) ;
}
) ;
}
} ) ;
Grocy . Components . ProductPicker . GetPicker ( ) . on ( 'change' , function ( e )
{
var productId = $ ( e . target ) . val ( ) ;
if ( productId )
{
Grocy . Components . ProductCard . Refresh ( productId ) ;
Grocy . Api . Get ( 'stock/products/' + productId ,
function ( productDetails )
{
$ ( '#amount_qu_unit' ) . text ( productDetails . quantity _unit _purchase . name ) ;
if ( productDetails . product . allow _partial _units _in _stock == 1 )
{
$ ( "#amount" ) . attr ( "min" , "0.01" ) ;
$ ( "#amount" ) . attr ( "step" , "0.01" ) ;
2019-05-01 20:19:18 +02:00
$ ( "#amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %s' , 0.01 . toLocaleString ( ) ) ) ;
2019-04-20 17:04:40 +02:00
}
else
{
$ ( "#amount" ) . attr ( "min" , "1" ) ;
$ ( "#amount" ) . attr ( "step" , "1" ) ;
2019-05-01 20:19:18 +02:00
$ ( "#amount" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( _ _t ( 'The amount cannot be lower than %s' , '1' ) ) ;
2019-04-20 17:04:40 +02:00
}
$ ( '#amount' ) . focus ( ) ;
Grocy . FrontendHelpers . ValidateForm ( 'shoppinglist-form' ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
} ) ;
Grocy . FrontendHelpers . ValidateForm ( 'shoppinglist-form' ) ;
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
2019-09-20 13:37:53 +02:00
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
2019-04-20 17:04:40 +02:00
if ( Grocy . EditMode === "edit" )
{
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
}
$ ( '#amount' ) . on ( 'focus' , function ( e )
{
if ( Grocy . Components . ProductPicker . GetValue ( ) . length === 0 )
{
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
}
else
{
$ ( this ) . select ( ) ;
}
} ) ;
2019-07-06 17:31:17 +02:00
$ ( '#shoppinglist-form input' ) . keyup ( function ( event )
2019-04-20 17:04:40 +02:00
{
Grocy . FrontendHelpers . ValidateForm ( 'shoppinglist-form' ) ;
} ) ;
2019-07-06 17:31:17 +02:00
$ ( '#shoppinglist-form input' ) . keydown ( function ( event )
2019-04-20 17:04:40 +02:00
{
if ( event . keyCode === 13 ) //Enter
{
event . preventDefault ( ) ;
if ( document . getElementById ( 'shoppinglist-form' ) . checkValidity ( ) === false ) //There is at least one validation error
{
return false ;
}
else
{
$ ( '#save-shoppinglist-button' ) . click ( ) ;
}
}
} ) ;
2019-07-06 17:31:17 +02:00
if ( GetUriParam ( "list" ) !== undefined )
{
$ ( "#shopping_list_id" ) . val ( GetUriParam ( "list" ) ) ;
}