2019-09-15 16:40:54 +02:00
$ ( '#save-quconversion-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
2022-03-26 10:34:00 +01:00
if ( ! Grocy . FrontendHelpers . ValidateForm ( "quconversion-form" , true ) )
{
return ;
}
2020-12-31 13:45:14 +01:00
if ( $ ( ".combobox-menu-visible" ) . length )
{
return ;
}
2019-09-15 16:40:54 +02:00
var jsonData = $ ( '#quconversion-form' ) . serializeJSON ( ) ;
jsonData . from _qu _id = $ ( "#from_qu_id" ) . val ( ) ;
Grocy . FrontendHelpers . BeginUiBusy ( "quconversion-form" ) ;
if ( Grocy . EditMode === 'create' )
{
Grocy . Api . Post ( 'objects/quantity_unit_conversions' , jsonData ,
function ( result )
{
Grocy . EditObjectId = result . created _object _id ;
Grocy . Components . UserfieldsForm . Save ( function ( )
{
2022-04-06 22:21:21 +02:00
if ( typeof GetUriParam ( "qu-unit" ) !== "undefined" )
2019-09-15 16:40:54 +02:00
{
2022-04-06 22:21:21 +02:00
if ( GetUriParam ( "embedded" ) !== undefined )
2020-11-08 19:00:12 +01:00
{
2022-04-06 22:21:21 +02:00
window . parent . postMessage ( WindowMessageBag ( "Reload" ) , Grocy . BaseUrl ) ;
2020-11-08 19:00:12 +01:00
}
else
{
2022-04-06 22:21:21 +02:00
window . location . href = U ( "/quantityunit/" + GetUriParam ( "qu-unit" ) ) ;
2020-11-08 19:00:12 +01:00
}
2019-09-15 16:40:54 +02:00
}
2022-04-06 22:21:21 +02:00
else
{
window . parent . postMessage ( WindowMessageBag ( "ProductQUConversionChanged" ) , U ( "/product/" + GetUriParam ( "product" ) ) ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , U ( "/product/" + GetUriParam ( "product" ) ) ) ;
}
2019-09-15 16:40:54 +02:00
} ) ;
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( "quconversion-form" ) ;
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
}
) ;
}
else
{
Grocy . Api . Put ( 'objects/quantity_unit_conversions/' + Grocy . EditObjectId , jsonData ,
function ( result )
{
Grocy . Components . UserfieldsForm . Save ( function ( )
{
if ( typeof GetUriParam ( "qu-unit" ) !== "undefined" )
{
2020-11-08 19:00:12 +01:00
if ( GetUriParam ( "embedded" ) !== undefined )
{
window . parent . postMessage ( WindowMessageBag ( "Reload" ) , Grocy . BaseUrl ) ;
}
else
{
window . location . href = U ( "/quantityunit/" + GetUriParam ( "qu-unit" ) ) ;
}
2019-09-15 16:40:54 +02:00
}
else
{
2020-08-17 14:47:33 -05:00
window . parent . postMessage ( WindowMessageBag ( "ProductQUConversionChanged" ) , U ( "/product/" + GetUriParam ( "product" ) ) ) ;
window . parent . postMessage ( WindowMessageBag ( "CloseAllModals" ) , U ( "/product/" + GetUriParam ( "product" ) ) ) ;
2019-09-15 16:40:54 +02:00
}
} ) ;
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( "quconversion-form" ) ;
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
}
) ;
}
} ) ;
$ ( '#quconversion-form input' ) . keyup ( function ( event )
{
$ ( '.input-group-qu' ) . trigger ( 'change' ) ;
Grocy . FrontendHelpers . ValidateForm ( 'quconversion-form' ) ;
} ) ;
$ ( '#quconversion-form input' ) . keydown ( function ( event )
{
2022-03-30 18:00:28 +02:00
if ( event . keyCode === 13 ) // Enter
2019-09-15 16:40:54 +02:00
{
event . preventDefault ( ) ;
2022-03-30 18:00:28 +02:00
if ( ! Grocy . FrontendHelpers . ValidateForm ( 'quconversion-form' ) )
2019-09-15 16:40:54 +02:00
{
return false ;
}
else
{
$ ( '#save-quconversion-button' ) . click ( ) ;
}
}
} ) ;
$ ( '.input-group-qu' ) . on ( 'change' , function ( e )
{
var fromQuId = $ ( "#from_qu_id" ) . val ( ) ;
var toQuId = $ ( "#to_qu_id" ) . val ( ) ;
2022-04-07 07:26:06 +02:00
var factor = Number . parseFloat ( $ ( '#factor' ) . val ( ) ) ;
2019-09-15 16:40:54 +02:00
if ( fromQuId == toQuId )
{
2022-04-07 07:26:06 +02:00
var validationMessage = _ _t ( 'This cannot be equal to %s' , $ ( "#from_qu_id option:selected" ) . text ( ) ) ;
$ ( "#to_qu_id" ) . parent ( ) . find ( ".invalid-feedback" ) . text ( validationMessage ) ;
$ ( "#to_qu_id" ) [ 0 ] . setCustomValidity ( validationMessage ) ;
2019-09-15 16:40:54 +02:00
}
else
{
$ ( "#to_qu_id" ) [ 0 ] . setCustomValidity ( "" ) ;
}
if ( fromQuId && toQuId )
{
2022-04-07 07:26:06 +02:00
$ ( '#qu-conversion-info' ) . text ( _ _t ( 'This means 1 %1$s is the same as %2$s %3$s' , $ ( "#from_qu_id option:selected" ) . text ( ) , ( 1.0 * factor ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) , _ _n ( ( 1.0 * factor ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) , $ ( "#to_qu_id option:selected" ) . text ( ) , $ ( "#to_qu_id option:selected" ) . data ( "plural-form" ) , true ) ) ) ;
2019-09-15 16:40:54 +02:00
$ ( '#qu-conversion-info' ) . removeClass ( 'd-none' ) ;
2022-04-06 22:21:21 +02:00
$ ( '#qu-conversion-inverse-info' ) . removeClass ( 'd-none' ) ;
2022-04-07 07:26:06 +02:00
$ ( '#qu-conversion-inverse-info' ) . text ( _ _t ( 'This means 1 %1$s is the same as %2$s %3$s' , $ ( "#to_qu_id option:selected" ) . text ( ) , ( 1.0 / factor ) . toLocaleString ( { minimumFractionDigits : 0 , maximumFractionDigits : Grocy . UserSettings . stock _decimal _places _amounts } ) , _ _n ( ( 1.0 / factor ) , $ ( "#from_qu_id option:selected" ) . text ( ) , $ ( "#from_qu_id option:selected" ) . data ( "plural-form" ) , true ) ) ) ;
2019-09-15 16:40:54 +02:00
}
else
{
$ ( '#qu-conversion-info' ) . addClass ( 'd-none' ) ;
2020-04-24 10:41:50 -05:00
$ ( '#qu-conversion-inverse-info' ) . addClass ( 'd-none' ) ;
2019-09-15 16:40:54 +02:00
}
Grocy . FrontendHelpers . ValidateForm ( 'quconversion-form' ) ;
} ) ;
Grocy . Components . UserfieldsForm . Load ( ) ;
$ ( '.input-group-qu' ) . trigger ( 'change' ) ;
$ ( '#from_qu_id' ) . focus ( ) ;
Grocy . FrontendHelpers . ValidateForm ( 'quconversion-form' ) ;
2020-11-10 18:11:33 +01:00
if ( GetUriParam ( "qu-unit" ) !== undefined )
{
$ ( "#from_qu_id" ) . attr ( "disabled" , "" ) ;
}