2020-11-13 17:30:57 +01:00
Grocy . ShoppingListItemFormInitialLoadDone = false ;
$ ( '#save-shoppinglist-button' ) . on ( 'click' , function ( e )
2019-04-20 17:04:40 +02:00
{
e . preventDefault ( ) ;
2020-12-31 13:45:14 +01:00
if ( $ ( ".combobox-menu-visible" ) . length )
{
return ;
}
2019-04-20 17:04:40 +02:00
var jsonData = $ ( '#shoppinglist-form' ) . serializeJSON ( ) ;
2021-07-02 17:37:06 +02:00
var displayAmount = parseFloat ( jsonData . display _amount ) ;
2020-11-13 17:30:57 +01:00
if ( ! jsonData . product _id )
{
jsonData . amount = jsonData . display _amount ;
}
2020-11-09 22:15:25 +01:00
delete jsonData . display _amount ;
2020-11-13 17:30:57 +01:00
2019-04-20 17:04:40 +02:00
Grocy . FrontendHelpers . BeginUiBusy ( "shoppinglist-form" ) ;
2021-07-04 15:46:19 +02:00
if ( GetUriParam ( "flow" ) === "InplaceAddBarcodeToExistingProduct" )
{
var jsonDataBarcode = { } ;
jsonDataBarcode . barcode = GetUriParam ( "barcode" ) ;
jsonDataBarcode . product _id = jsonData . product _id ;
Grocy . Api . Post ( 'objects/product_barcodes' , jsonDataBarcode ,
function ( result )
{
$ ( "#flow-info-InplaceAddBarcodeToExistingProduct" ) . addClass ( "d-none" ) ;
$ ( '#barcode-lookup-disabled-hint' ) . addClass ( 'd-none' ) ;
$ ( '#barcode-lookup-hint' ) . removeClass ( 'd-none' ) ;
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( "shoppinglist-form" ) ;
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response ) ;
}
) ;
}
2019-09-24 18:27:50 +02:00
if ( GetUriParam ( "updateexistingproduct" ) !== undefined )
{
jsonData . product _amount = jsonData . amount ;
delete jsonData . amount ;
2021-06-27 20:46:21 +02:00
jsonData . list _id = jsonData . shopping _list _id ;
delete jsonData . shopping _list _id ;
2019-09-24 18:27:50 +02:00
Grocy . Api . Post ( 'stock/shoppinglist/add-product' , jsonData ,
function ( result )
{
2020-11-17 19:11:02 +01:00
Grocy . EditObjectId = result . created _object _id ;
Grocy . Components . UserfieldsForm . Save ( ) ;
2019-09-24 18:27:50 +02:00
if ( GetUriParam ( "embedded" ) !== undefined )
{
Grocy . Api . Get ( 'stock/products/' + jsonData . product _id ,
2020-08-30 12:18:16 +02:00
function ( productDetails )
2019-09-24 18:27:50 +02:00
{
2022-02-06 21:09:34 +01:00
window . parent . postMessage ( WindowMessageBag ( "ShowSuccessMessage" , _ _t ( "Added %1$s of %2$s to the shopping list \"%3$s\"" , displayAmount . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) + " " + _ _n ( displayAmount , $ ( "#qu_id option:selected" ) . text ( ) , $ ( "#qu_id option:selected" ) . attr ( "data-qu-name-plural" ) , true ) , productDetails . product . name , $ ( "#shopping_list_id option:selected" ) . text ( ) ) ) , Grocy . BaseUrl ) ;
2020-10-29 11:09:35 -05:00
window . parent . postMessage ( WindowMessageBag ( "ShoppingListChanged" , $ ( "#shopping_list_id" ) . val ( ) . toString ( ) ) , Grocy . BaseUrl ) ;
2019-09-24 18:27:50 +02:00
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
} ,
2020-08-30 12:18:16 +02:00
function ( xhr )
2019-09-24 18:27:50 +02:00
{
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 ) ;
}
) ;
}
2020-12-28 19:59:18 +01:00
else if ( Grocy . EditMode === 'create' )
2019-04-20 17:04:40 +02:00
{
Grocy . Api . Post ( 'objects/shopping_list' , jsonData ,
function ( result )
{
2020-11-17 19:11:02 +01:00
Grocy . EditObjectId = result . created _object _id ;
Grocy . Components . UserfieldsForm . Save ( ) ;
2019-09-20 13:37:53 +02:00
if ( GetUriParam ( "embedded" ) !== undefined )
{
2020-11-13 17:30:57 +01:00
if ( jsonData . product _id )
{
Grocy . Api . Get ( 'stock/products/' + jsonData . product _id ,
function ( productDetails )
{
2022-02-06 21:09:34 +01:00
window . parent . postMessage ( WindowMessageBag ( "ShowSuccessMessage" , _ _t ( "Added %1$s of %2$s to the shopping list \"%3$s\"" , displayAmount . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) + " " + _ _n ( displayAmount , $ ( "#qu_id option:selected" ) . text ( ) , $ ( "#qu_id option:selected" ) . attr ( "data-qu-name-plural" ) , true ) , productDetails . product . name , $ ( "#shopping_list_id option:selected" ) . text ( ) ) ) , Grocy . BaseUrl ) ;
2020-11-13 17:30:57 +01:00
window . parent . postMessage ( WindowMessageBag ( "ShoppingListChanged" , $ ( "#shopping_list_id" ) . val ( ) . toString ( ) ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
else
{
window . parent . postMessage ( WindowMessageBag ( "ShoppingListChanged" , $ ( "#shopping_list_id" ) . val ( ) . toString ( ) ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
}
2019-09-20 13:37:53 +02:00
}
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 )
{
2020-11-17 19:11:02 +01:00
Grocy . Components . UserfieldsForm . Save ( ) ;
2019-09-20 13:37:53 +02:00
if ( GetUriParam ( "embedded" ) !== undefined )
{
2020-11-13 17:30:57 +01:00
if ( jsonData . product _id )
{
Grocy . Api . Get ( 'stock/products/' + jsonData . product _id ,
function ( productDetails )
{
2022-02-06 21:09:34 +01:00
window . parent . postMessage ( WindowMessageBag ( "ShowSuccessMessage" , _ _t ( "Added %1$s of %2$s to the shopping list \"%3$s\"" , displayAmount . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) + " " + _ _n ( displayAmount , $ ( "#qu_id option:selected" ) . text ( ) , $ ( "#qu_id option:selected" ) . attr ( "data-qu-name-plural" ) , true ) , productDetails . product . name , $ ( "#shopping_list_id option:selected" ) . text ( ) ) ) , Grocy . BaseUrl ) ;
2020-11-13 17:30:57 +01:00
window . parent . postMessage ( WindowMessageBag ( "ShoppingListChanged" , $ ( "#shopping_list_id" ) . val ( ) . toString ( ) ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
else
{
window . parent . postMessage ( WindowMessageBag ( "ShoppingListChanged" , $ ( "#shopping_list_id" ) . val ( ) . toString ( ) ) , Grocy . BaseUrl ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , Grocy . BaseUrl ) ;
}
2019-09-20 13:37:53 +02:00
}
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 . Api . Get ( 'stock/products/' + productId ,
2020-08-30 12:18:16 +02:00
function ( productDetails )
2019-04-20 17:04:40 +02:00
{
2020-11-13 17:30:57 +01:00
if ( ! Grocy . ShoppingListItemFormInitialLoadDone )
{
Grocy . Components . ProductAmountPicker . Reload ( productDetails . product . id , productDetails . quantity _unit _stock . id , true ) ;
}
else
{
Grocy . Components . ProductAmountPicker . Reload ( productDetails . product . id , productDetails . quantity _unit _stock . id ) ;
Grocy . Components . ProductAmountPicker . SetQuantityUnit ( productDetails . default _quantity _unit _purchase . id ) ;
}
2020-08-29 16:41:27 +02:00
2021-03-31 22:22:28 +02:00
if ( $ ( "#display_amount" ) . val ( ) . toString ( ) . isEmpty ( ) )
{
$ ( "#display_amount" ) . val ( 1 ) ;
$ ( "#display_amount" ) . trigger ( "change" ) ;
}
2020-11-09 22:15:25 +01:00
$ ( '#display_amount' ) . focus ( ) ;
2019-04-20 17:04:40 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'shoppinglist-form' ) ;
2020-11-13 17:30:57 +01:00
Grocy . ShoppingListItemFormInitialLoadDone = true ;
2019-04-20 17:04:40 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2020-12-29 21:06:31 +01:00
$ ( "#note" ) . trigger ( "input" ) ;
$ ( "#product_id" ) . trigger ( "input" ) ;
2019-04-20 17:04:40 +02:00
} ) ;
Grocy . FrontendHelpers . ValidateForm ( 'shoppinglist-form' ) ;
2021-11-08 21:59:02 +01:00
setTimeout ( function ( )
{
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
} , 250 ) ;
2019-04-20 17:04:40 +02:00
if ( Grocy . EditMode === "edit" )
{
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
}
2020-11-13 17:30:57 +01:00
if ( Grocy . EditMode == "create" )
{
Grocy . ShoppingListItemFormInitialLoadDone = true ;
}
2020-11-09 22:15:25 +01:00
$ ( '#display_amount' ) . on ( 'focus' , function ( e )
2019-04-20 17:04:40 +02:00
{
2020-10-18 14:27:23 +02:00
$ ( this ) . select ( ) ;
2019-04-20 17:04:40 +02:00
} ) ;
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" ) ) ;
}
2020-01-25 18:36:54 +01:00
if ( GetUriParam ( "amount" ) !== undefined )
{
2020-11-15 22:29:47 +01:00
$ ( "#display_amount" ) . val ( parseFloat ( GetUriParam ( "amount" ) ) ) ;
2020-11-15 14:15:09 +01:00
RefreshLocaleNumberInput ( ) ;
2020-11-12 22:47:00 +01:00
$ ( ".input-group-productamountpicker" ) . trigger ( "change" ) ;
2020-01-25 18:36:54 +01:00
Grocy . FrontendHelpers . ValidateForm ( 'shoppinglist-form' ) ;
}
2020-11-17 19:11:02 +01:00
2020-12-28 19:59:18 +01:00
if ( GetUriParam ( "embedded" ) !== undefined )
{
if ( GetUriParam ( "product" ) !== undefined )
{
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
$ ( "#display_amount" ) . focus ( ) ;
}
else
{
Grocy . Components . ProductPicker . GetInputElement ( ) . focus ( ) ;
}
}
2020-12-29 21:06:31 +01:00
var eitherRequiredFields = $ ( "#product_id,#product_id_text_input,#note" ) ;
2021-03-31 22:22:28 +02:00
eitherRequiredFields . prop ( 'required' , "" ) ;
eitherRequiredFields . on ( 'input' , function ( )
2020-12-29 21:06:31 +01:00
{
2021-01-30 12:47:12 +01:00
eitherRequiredFields . not ( this ) . prop ( 'required' , ! $ ( this ) . val ( ) . length ) ;
2020-12-29 21:06:31 +01:00
Grocy . FrontendHelpers . ValidateForm ( 'shoppinglist-form' ) ;
} ) ;
2021-01-30 12:47:12 +01:00
2020-12-29 21:18:51 +01:00
if ( GetUriParam ( "product-name" ) != null )
{
Grocy . Components . ProductPicker . GetPicker ( ) . trigger ( 'change' ) ;
}
2020-11-17 19:11:02 +01:00
Grocy . Components . UserfieldsForm . Load ( ) ;