2017-04-15 23:16:20 +02:00
$ ( '#save-product-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
2018-04-18 19:03:39 +02:00
var redirectDestination = U ( '/products' ) ;
2018-04-14 11:10:38 +02:00
var returnTo = GetUriParam ( 'returnto' ) ;
2017-04-20 22:01:14 +02:00
if ( returnTo !== undefined )
{
redirectDestination = returnTo + '?createdproduct=' + encodeURIComponent ( $ ( '#name' ) . val ( ) ) ;
}
2017-04-15 23:16:20 +02:00
if ( Grocy . EditMode === 'create' )
{
2018-04-18 19:03:39 +02:00
Grocy . Api . Post ( 'add-object/products' , $ ( '#product-form' ) . serializeJSON ( ) ,
2017-04-15 23:16:20 +02:00
function ( result )
{
2017-04-20 22:01:14 +02:00
window . location . href = redirectDestination ;
2017-04-15 23:16:20 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
else
{
2018-04-18 19:03:39 +02:00
Grocy . Api . Post ( 'edit-object/products/' + Grocy . EditObjectId , $ ( '#product-form' ) . serializeJSON ( ) ,
2017-04-15 23:16:20 +02:00
function ( result )
{
2017-04-20 22:01:14 +02:00
window . location . href = redirectDestination ;
2017-04-15 23:16:20 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
} ) ;
2018-04-16 19:11:32 +02:00
$ ( '#barcode-taginput' ) . tagsManager ( {
'hiddenTagListName' : 'barcode' ,
'tagsContainer' : '#barcode-taginput-container'
} ) ;
2017-04-18 21:15:45 +02:00
2018-04-16 19:11:32 +02:00
if ( Grocy . EditMode === 'edit' )
{
2018-04-18 19:03:39 +02:00
Grocy . Api . Get ( 'get-object/products/' + Grocy . EditObjectId ,
2018-04-16 19:11:32 +02:00
function ( product )
{
if ( product . barcode !== null && product . barcode . length > 0 )
2017-04-18 21:15:45 +02:00
{
2018-04-16 19:11:32 +02:00
product . barcode . split ( ',' ) . forEach ( function ( item )
2017-04-18 21:15:45 +02:00
{
2018-04-16 19:11:32 +02:00
$ ( '#barcode-taginput' ) . tagsManager ( 'pushTag' , item ) ;
} ) ;
2017-04-18 21:15:45 +02:00
}
2018-04-16 19:11:32 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
2017-04-18 21:15:45 +02:00
2018-04-16 19:11:32 +02:00
var prefillName = GetUriParam ( 'prefillname' ) ;
if ( prefillName !== undefined )
{
$ ( '#name' ) . val ( prefillName ) ;
2017-04-15 23:16:20 +02:00
$ ( '#name' ) . focus ( ) ;
2018-04-16 19:11:32 +02:00
}
2017-04-20 22:01:14 +02:00
2018-04-16 19:11:32 +02:00
var prefillBarcode = GetUriParam ( 'prefillbarcode' ) ;
if ( prefillBarcode !== undefined )
{
$ ( '#barcode-taginput' ) . tagsManager ( 'pushTag' , prefillBarcode ) ;
$ ( '#name' ) . focus ( ) ;
}
2017-04-17 16:51:49 +02:00
2017-04-18 19:54:51 +02:00
$ ( '.input-group-qu' ) . on ( 'change' , function ( e )
{
var factor = $ ( '#qu_factor_purchase_to_stock' ) . val ( ) ;
if ( factor > 1 )
{
2018-04-16 19:11:32 +02:00
$ ( '#qu-conversion-info' ) . text ( L ( 'This means 1 #1 purchased will be converted into #2 #3 in stock' , $ ( "#qu_id_purchase option:selected" ) . text ( ) , ( 1 * factor ) . toString ( ) , $ ( "#qu_id_stock option:selected" ) . text ( ) ) ) ;
2018-07-11 19:43:05 +02:00
$ ( '#qu-conversion-info' ) . removeClass ( 'd-none' ) ;
2017-04-18 19:54:51 +02:00
}
else
{
2018-07-11 19:43:05 +02:00
$ ( '#qu-conversion-info' ) . addClass ( 'd-none' ) ;
}
} ) ;
$ ( '#product-form input' ) . keyup ( function ( event )
{
Grocy . FrontendHelpers . ValidateForm ( 'product-form' ) ;
} ) ;
$ ( '#product-form input' ) . keydown ( function ( event )
{
if ( event . keyCode === 13 ) //Enter
{
if ( document . getElementById ( 'product-form' ) . checkValidity ( ) === false ) //There is at least one validation error
{
event . preventDefault ( ) ;
return false ;
}
else
{
$ ( '#save-product-button' ) . click ( ) ;
}
2017-04-18 19:54:51 +02:00
}
} ) ;
2018-04-16 19:11:32 +02:00
$ ( '#name' ) . focus ( ) ;
$ ( '.input-group-qu' ) . trigger ( 'change' ) ;
2018-07-11 19:43:05 +02:00
Grocy . FrontendHelpers . ValidateForm ( 'product-form' ) ;