2018-07-14 18:23:41 +02:00
var recipesTables = $ ( '#recipes-table' ) . DataTable ( {
'paginate' : false ,
2018-08-11 14:38:17 +02:00
'order' : [ [ 0 , 'asc' ] ] ,
2018-11-24 19:59:24 +01:00
'columnDefs' : [
{ 'orderData' : 2 , 'targets' : 1 }
] ,
2018-07-14 18:23:41 +02:00
'language' : JSON . parse ( L ( 'datatables_localization' ) ) ,
'scrollY' : false ,
'colReorder' : true ,
2018-07-14 22:49:42 +02:00
'stateSave' : true ,
2018-09-08 08:56:32 +02:00
'stateSaveParams' : function ( settings , data )
{
data . search . search = "" ;
2018-09-25 08:50:28 +02:00
data . columns . forEach ( column =>
{
column . search . search = "" ;
} ) ;
2018-09-08 08:56:32 +02:00
} ,
2018-07-15 10:16:36 +02:00
'select' : 'single' ,
'initComplete' : function ( )
{
this . api ( ) . row ( { order : 'current' } , 0 ) . select ( ) ;
}
2018-07-14 18:23:41 +02:00
} ) ;
2019-01-05 20:06:35 +01:00
$ ( '#recipes-table tbody' ) . removeClass ( "d-none" ) ;
2019-03-04 17:43:12 +01:00
recipesTables . columns . adjust ( ) . draw ( ) ;
2018-07-14 18:23:41 +02:00
2018-07-15 09:56:10 +02:00
var rowSelect = GetUriParam ( "row" ) ;
if ( typeof rowSelect !== "undefined" )
{
recipesTables . row ( rowSelect ) . select ( ) ;
}
2018-07-14 18:23:41 +02:00
$ ( "#search" ) . on ( "keyup" , function ( )
{
var value = $ ( this ) . val ( ) ;
if ( value === "all" )
{
value = "" ;
}
2019-01-19 00:37:21 -07:00
2018-07-14 18:23:41 +02:00
recipesTables . search ( value ) . draw ( ) ;
} ) ;
2018-08-11 14:38:17 +02:00
$ ( "#selectedRecipeDeleteButton" ) . on ( 'click' , function ( e )
2018-07-14 18:23:41 +02:00
{
var objectName = $ ( e . currentTarget ) . attr ( 'data-recipe-name' ) ;
var objectId = $ ( e . currentTarget ) . attr ( 'data-recipe-id' ) ;
bootbox . confirm ( {
message : L ( 'Are you sure to delete recipe "#1"?' , objectName ) ,
buttons : {
confirm : {
label : L ( 'Yes' ) ,
className : 'btn-success'
} ,
cancel : {
label : L ( 'No' ) ,
className : 'btn-danger'
}
} ,
callback : function ( result )
{
if ( result === true )
{
2019-01-19 14:51:51 +01:00
Grocy . Api . Delete ( 'objects/recipes/' + objectId , { } ,
2018-07-14 18:23:41 +02:00
function ( result )
{
window . location . href = U ( '/recipes' ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
}
}
} ) ;
} ) ;
2018-07-14 22:49:42 +02:00
$ ( document ) . on ( 'click' , '.recipe-order-missing-button' , function ( e )
{
var objectName = $ ( e . currentTarget ) . attr ( 'data-recipe-name' ) ;
var objectId = $ ( e . currentTarget ) . attr ( 'data-recipe-id' ) ;
bootbox . confirm ( {
2019-03-03 17:01:35 +01:00
message : L ( 'Are you sure to put all missing ingredients for recipe "#1" on the shopping list?' , objectName ) + "<br><br>" + L ( "Uncheck ingredients to not put them on the shopping list" ) + ":" + $ ( "#missing-recipe-pos-list" ) [ 0 ] . outerHTML . replace ( "d-none" , "" ) ,
2018-07-14 22:49:42 +02:00
buttons : {
confirm : {
label : L ( 'Yes' ) ,
className : 'btn-success'
} ,
cancel : {
label : L ( 'No' ) ,
className : 'btn-danger'
}
} ,
callback : function ( result )
{
if ( result === true )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . BeginUiBusy ( ) ;
2019-03-03 14:49:46 +01:00
var excludedProductIds = new Array ( ) ;
$ ( ".missing-recipe-pos-product-checkbox:checkbox:not(:checked)" ) . each ( function ( )
{
excludedProductIds . push ( $ ( this ) . data ( "product-id" ) ) ;
} ) ;
Grocy . Api . Post ( 'recipes/' + objectId + '/add-not-fulfilled-products-to-shoppinglist' , { "excludedProductIds" : excludedProductIds } ,
2018-07-14 22:49:42 +02:00
function ( result )
{
window . location . href = U ( '/recipes' ) ;
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2018-07-14 22:49:42 +02:00
console . error ( xhr ) ;
}
) ;
}
}
} ) ;
} ) ;
2018-07-15 09:56:10 +02:00
2018-08-11 11:48:25 +02:00
$ ( "#selectedRecipeConsumeButton" ) . on ( 'click' , function ( e )
{
var objectName = $ ( e . currentTarget ) . attr ( 'data-recipe-name' ) ;
var objectId = $ ( e . currentTarget ) . attr ( 'data-recipe-id' ) ;
bootbox . confirm ( {
message : L ( 'Are you sure to consume all ingredients needed by recipe "#1" (ingredients marked with "check only if a single unit is in stock" will be ignored)?' , objectName ) ,
buttons : {
confirm : {
label : L ( 'Yes' ) ,
className : 'btn-success'
} ,
cancel : {
label : L ( 'No' ) ,
className : 'btn-danger'
}
} ,
callback : function ( result )
{
if ( result === true )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . BeginUiBusy ( ) ;
2019-01-19 14:51:51 +01:00
Grocy . Api . Post ( 'recipes/' + objectId + '/consume' , { } ,
2018-08-11 11:48:25 +02:00
function ( result )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2018-08-11 11:48:25 +02:00
toastr . success ( L ( 'Removed all ingredients of recipe "#1" from stock' , objectName ) ) ;
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2018-08-11 11:48:25 +02:00
console . error ( xhr ) ;
}
) ;
}
}
} ) ;
} ) ;
2018-07-15 09:56:10 +02:00
recipesTables . on ( 'select' , function ( e , dt , type , indexes )
{
if ( type === 'row' )
{
var selectedRecipeId = $ ( recipesTables . row ( indexes [ 0 ] ) . node ( ) ) . data ( "recipe-id" ) ;
window . location . href = U ( '/recipes?recipe=' + selectedRecipeId . toString ( ) + "&row=" + indexes [ 0 ] . toString ( ) ) ;
}
} ) ;
$ ( "#selectedRecipeToggleFullscreenButton" ) . on ( 'click' , function ( e )
{
2019-03-03 13:27:10 +01:00
e . preventDefault ( ) ;
2018-07-15 09:56:10 +02:00
$ ( "#selectedRecipeCard" ) . toggleClass ( "fullscreen" ) ;
2018-10-13 09:18:16 +02:00
$ ( "body" ) . toggleClass ( "fullscreen-card" ) ;
2018-09-30 09:41:22 +02:00
$ ( "#selectedRecipeCard .card-header" ) . toggleClass ( "fixed-top" ) ;
2018-10-03 16:41:21 +02:00
$ ( "#selectedRecipeCard .card-body" ) . toggleClass ( "mt-5" ) ;
2019-03-03 13:27:10 +01:00
window . location . hash = "fullscreen" ;
2018-07-15 09:56:10 +02:00
} ) ;
2019-03-03 13:27:10 +01:00
$ ( '#servings-scale' ) . keyup ( function ( event )
{
var data = { } ;
data . desired _servings = $ ( this ) . val ( ) ;
Grocy . Api . Put ( 'objects/recipes/' + $ ( this ) . data ( "recipe-id" ) , data ,
function ( result )
{
window . location . reload ( ) ;
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ) ;
2019-03-03 14:49:46 +01:00
$ ( document ) . on ( "click" , ".missing-recipe-pos-select-button" , function ( e )
{
e . preventDefault ( ) ;
var checkbox = $ ( this ) . find ( ".form-check-input" ) ;
checkbox . prop ( "checked" , ! checkbox . prop ( "checked" ) ) ;
$ ( this ) . toggleClass ( "list-group-item-primary" ) ;
} ) ;
2019-03-03 13:27:10 +01:00
if ( window . location . hash === "#fullscreen" )
{
$ ( "#selectedRecipeToggleFullscreenButton" ) . click ( ) ;
}