2018-07-14 18:23:41 +02:00
var recipesTables = $ ( '#recipes-table' ) . DataTable ( {
2020-04-24 11:41:57 -04:00
'order' : [ [ 1 , 'asc' ] ] ,
2018-11-24 19:59:24 +01:00
'columnDefs' : [
2020-04-24 11:41:57 -04:00
{ 'orderable' : false , 'targets' : 0 } ,
2020-12-17 17:33:24 +01:00
{ 'searchable' : false , "targets" : 0 } ,
2022-03-29 20:38:26 +02:00
{ 'visible' : false , 'targets' : 2 } ,
2020-12-17 17:33:24 +01:00
{ "type" : "html-num-fmt" , "targets" : 2 } ,
2022-03-29 20:38:26 +02:00
{ "type" : "html-num-fmt" , "targets" : 3 }
2020-12-07 19:48:33 +01:00
] . concat ( $ . fn . dataTable . defaults . columnDefs ) ,
2020-08-24 18:42:32 +02:00
select : {
style : 'single' ,
selector : 'tr td:not(:first-child)'
} ,
2018-07-15 10:16:36 +02:00
'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
2019-03-05 18:04:16 +01:00
if ( ( typeof GetUriParam ( "tab" ) !== "undefined" && GetUriParam ( "tab" ) === "gallery" ) || window . localStorage . getItem ( "recipes_last_tab_id" ) == "gallery-tab" )
2018-07-15 09:56:10 +02:00
{
2019-03-05 17:59:33 +01:00
$ ( ".nav-tabs a[href='#gallery']" ) . tab ( "show" ) ;
2018-07-15 09:56:10 +02:00
}
2019-03-05 17:59:33 +01:00
var recipe = GetUriParam ( "recipe" ) ;
if ( typeof recipe !== "undefined" )
{
$ ( "#recipes-table tr" ) . removeClass ( "selected" ) ;
var rowId = "#recipe-row-" + recipe ;
$ ( rowId ) . addClass ( "selected" )
2020-04-24 11:41:57 -04:00
var cardId = "#RecipeGalleryCard-" + recipe ;
$ ( cardId ) . addClass ( "border-primary" ) ;
2020-02-09 15:11:38 +01:00
if ( $ ( window ) . width ( ) < 768 )
{
// Scroll to recipe card on mobile
$ ( "#selectedRecipeCard" ) [ 0 ] . scrollIntoView ( ) ;
}
2019-03-05 17:59:33 +01:00
}
2019-09-20 13:37:53 +02:00
if ( GetUriParam ( "search" ) !== undefined )
{
$ ( "#search" ) . val ( GetUriParam ( "search" ) ) ;
2020-08-30 12:18:16 +02:00
setTimeout ( function ( )
2019-09-20 13:37:53 +02:00
{
$ ( "#search" ) . keyup ( ) ;
} , 50 ) ;
}
2021-06-27 20:28:12 +02:00
if ( GetUriParam ( "status" ) !== undefined )
{
$ ( "#status-filter" ) . val ( GetUriParam ( "status" ) ) ;
setTimeout ( function ( )
{
$ ( "#status-filter" ) . trigger ( "change" ) ;
} , 50 ) ;
}
2019-03-05 17:59:33 +01:00
$ ( "a[data-toggle='tab']" ) . on ( "shown.bs.tab" , function ( e )
{
var tabId = $ ( e . target ) . attr ( "id" ) ;
window . localStorage . setItem ( "recipes_last_tab_id" , tabId ) ;
} ) ;
2019-10-15 19:59:14 +02:00
$ ( "#search" ) . on ( "keyup" , Delay ( function ( )
2018-07-14 18:23:41 +02:00
{
var value = $ ( this ) . val ( ) ;
2019-01-19 00:37:21 -07:00
2018-07-14 18:23:41 +02:00
recipesTables . search ( value ) . draw ( ) ;
2019-03-05 19:17:10 +01:00
2023-02-06 20:22:10 +01:00
if ( ! value )
2021-06-27 20:28:12 +02:00
{
RemoveUriParam ( "search" ) ;
}
else
{
UpdateUriParam ( "search" , value ) ;
}
2020-08-29 16:41:27 +02:00
2021-06-27 20:28:12 +02:00
$ ( ".recipe-gallery-item" ) . removeClass ( "d-none" ) ;
2022-04-05 18:21:25 +02:00
$ ( ".recipe-gallery-item .card-title-search:not(:contains_case_insensitive(" + value + "))" ) . parent ( ) . parent ( ) . parent ( ) . addClass ( "d-none" ) ;
2019-10-15 19:59:14 +02:00
} , 200 ) ) ;
2018-07-14 18:23:41 +02:00
2020-11-07 14:53:45 +01:00
$ ( "#clear-filter-button" ) . on ( "click" , function ( )
{
$ ( "#search" ) . val ( "" ) ;
$ ( "#status-filter" ) . val ( "all" ) ;
$ ( "#search" ) . trigger ( "keyup" ) ;
$ ( "#status-filter" ) . trigger ( "change" ) ;
} ) ;
2019-05-03 19:22:58 +02:00
$ ( "#status-filter" ) . on ( "change" , function ( )
{
var value = $ ( this ) . val ( ) ;
if ( value === "all" )
{
value = "" ;
}
2022-03-30 17:39:26 +02:00
recipesTables . column ( recipesTables . colReorder . transpose ( 6 ) ) . search ( value ) . draw ( ) ;
2020-10-04 15:00:34 +02:00
$ ( '.recipe-gallery-item' ) . removeClass ( 'd-none' ) ;
if ( value !== "" )
{
2020-11-15 09:21:54 +01:00
if ( value === 'Xenoughinstock' )
2020-10-04 15:00:34 +02:00
{
$ ( '.recipe-gallery-item' ) . not ( '.recipe-enoughinstock' ) . addClass ( 'd-none' ) ;
}
else if ( value === 'enoughinstockwithshoppinglist' )
{
$ ( '.recipe-gallery-item' ) . not ( '.recipe-enoughinstockwithshoppinglist' ) . addClass ( 'd-none' ) ;
}
if ( value === 'notenoughinstock' )
{
$ ( '.recipe-gallery-item' ) . not ( '.recipe-notenoughinstock' ) . addClass ( 'd-none' ) ;
}
}
2021-06-27 20:28:12 +02:00
2023-02-06 20:22:10 +01:00
if ( ! value )
2021-06-27 20:28:12 +02:00
{
RemoveUriParam ( "status" ) ;
}
else
{
UpdateUriParam ( "status" , value ) ;
}
2019-05-03 19:22:58 +02:00
} ) ;
2020-04-24 11:41:57 -04:00
$ ( ".recipe-delete" ) . on ( 'click' , function ( e )
2018-07-14 18:23:41 +02:00
{
2020-04-24 11:41:57 -04:00
e . preventDefault ( ) ;
2020-08-29 16:41:27 +02:00
2020-10-14 22:58:26 +02:00
var objectName = $ ( e . currentTarget ) . attr ( 'data-recipe-name' ) ;
2018-07-14 18:23:41 +02:00
var objectId = $ ( e . currentTarget ) . attr ( 'data-recipe-id' ) ;
bootbox . confirm ( {
2019-05-01 20:19:18 +02:00
message : _ _t ( 'Are you sure to delete recipe "%s"?' , objectName ) ,
2019-09-24 10:24:47 +02:00
closeButton : false ,
2018-07-14 18:23:41 +02:00
buttons : {
confirm : {
2019-05-01 20:19:18 +02:00
label : _ _t ( 'Yes' ) ,
2018-07-14 18:23:41 +02:00
className : 'btn-success'
} ,
cancel : {
2019-05-01 20:19:18 +02:00
label : _ _t ( 'No' ) ,
2018-07-14 18:23:41 +02:00
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
2021-07-13 19:29:23 +02:00
$ ( ".recipe-copy" ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
var objectId = $ ( e . currentTarget ) . attr ( 'data-recipe-id' ) ;
Grocy . Api . Post ( "recipes/" + objectId . toString ( ) + "/copy" , { } ,
function ( result )
{
window . location . href = U ( '/recipes?recipe=' + result . created _object _id . toString ( ) ) ;
} ,
function ( xhr )
{
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response ) ;
}
) ;
} ) ;
2020-04-24 11:41:57 -04:00
$ ( document ) . on ( 'click' , '.recipe-shopping-list' , function ( e )
2018-07-14 22:49:42 +02:00
{
2020-10-14 22:58:26 +02:00
var objectName = $ ( e . currentTarget ) . attr ( 'data-recipe-name' ) ;
2018-07-14 22:49:42 +02:00
var objectId = $ ( e . currentTarget ) . attr ( 'data-recipe-id' ) ;
bootbox . confirm ( {
2019-05-01 20:19:18 +02:00
message : _ _t ( 'Are you sure to put all missing ingredients for recipe "%s" on the shopping list?' , objectName ) + "<br><br>" + _ _t ( "Uncheck ingredients to not put them on the shopping list" ) + ":" + $ ( "#missing-recipe-pos-list" ) [ 0 ] . outerHTML . replace ( "d-none" , "" ) ,
2019-09-24 10:24:47 +02:00
closeButton : false ,
2018-07-14 22:49:42 +02:00
buttons : {
confirm : {
2019-05-01 20:19:18 +02:00
label : _ _t ( 'Yes' ) ,
2018-07-14 22:49:42 +02:00
className : 'btn-success'
} ,
cancel : {
2019-05-01 20:19:18 +02:00
label : _ _t ( 'No' ) ,
2018-07-14 22:49:42 +02:00
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
2020-04-24 11:41:57 -04:00
$ ( ".recipe-consume" ) . on ( 'click' , function ( e )
2018-08-11 11:48:25 +02:00
{
2020-10-14 22:58:26 +02:00
var objectName = $ ( e . currentTarget ) . attr ( 'data-recipe-name' ) ;
2018-08-11 11:48:25 +02:00
var objectId = $ ( e . currentTarget ) . attr ( 'data-recipe-id' ) ;
bootbox . confirm ( {
2020-12-19 14:03:28 +01:00
message : _ _t ( 'Are you sure to consume all ingredients needed by recipe "%s" (ingredients marked with "only check if any amount is in stock" will be ignored)?' , objectName ) ,
2019-09-24 10:24:47 +02:00
closeButton : false ,
2018-08-11 11:48:25 +02:00
buttons : {
confirm : {
2019-05-01 20:19:18 +02:00
label : _ _t ( 'Yes' ) ,
2018-08-11 11:48:25 +02:00
className : 'btn-success'
} ,
cancel : {
2019-05-01 20:19:18 +02:00
label : _ _t ( 'No' ) ,
2018-08-11 11:48:25 +02:00
className : 'btn-danger'
}
} ,
callback : function ( result )
{
if ( result === true )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . BeginUiBusy ( ) ;
2020-08-30 12:18:16 +02: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 ( ) ;
2019-05-01 20:19:18 +02:00
toastr . success ( _ _t ( 'Removed all ingredients of recipe "%s" from stock' , objectName ) ) ;
2018-08-11 11:48:25 +02:00
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2023-08-15 18:51:40 +02:00
Grocy . FrontendHelpers . ShowGenericError ( "A server error occured while processing your request" , xhr . response ) ;
2018-08-11 11:48:25 +02:00
}
) ;
}
}
} ) ;
} ) ;
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" ) ;
2020-03-06 20:51:09 +01:00
var currentRecipeId = location . search . split ( 'recipe=' ) [ 1 ] ;
2022-02-09 17:48:21 +01:00
if ( BoolVal ( Grocy . UserSettings . recipes _show _list _side _by _side ) )
{
if ( selectedRecipeId . toString ( ) !== currentRecipeId )
{
UpdateUriParam ( "recipe" , selectedRecipeId . toString ( ) ) ;
window . location . reload ( ) ;
}
}
else
2020-03-06 20:51:09 +01:00
{
2022-02-09 17:48:21 +01:00
bootbox . dialog ( {
2024-02-25 10:40:11 +01:00
message : '<iframe class="embed-responsive" src="' + U ( "/recipes?embedded&recipe=" ) + selectedRecipeId + '#fullscreen"></iframe>' ,
2022-02-09 17:48:21 +01:00
size : 'extra-large' ,
backdrop : true ,
closeButton : false ,
buttons : {
cancel : {
label : _ _t ( 'Close' ) ,
className : 'btn-secondary responsive-button' ,
callback : function ( )
{
bootbox . hideAll ( ) ;
}
}
}
} ) ;
2020-03-06 20:51:09 +01:00
}
2018-07-15 09:56:10 +02:00
}
} ) ;
2019-03-05 17:59:33 +01:00
$ ( ".recipe-gallery-item" ) . on ( "click" , function ( e )
{
e . preventDefault ( ) ;
2022-02-09 17:48:21 +01:00
var selectedRecipeId = $ ( this ) . data ( "recipe-id" ) ;
if ( BoolVal ( Grocy . UserSettings . recipes _show _list _side _by _side ) )
{
window . location . href = U ( '/recipes?tab=gallery&recipe=' + selectedRecipeId ) ;
}
else
{
bootbox . dialog ( {
2024-02-25 10:40:11 +01:00
message : '<iframe class="embed-responsive" src="' + U ( "/recipes?embedded&recipe=" ) + selectedRecipeId + '#fullscreen"></iframe>' ,
2022-02-09 17:48:21 +01:00
size : 'extra-large' ,
backdrop : true ,
closeButton : false ,
buttons : {
cancel : {
label : _ _t ( 'Close' ) ,
className : 'btn-secondary responsive-button' ,
callback : function ( )
{
bootbox . hideAll ( ) ;
}
}
}
} ) ;
}
2019-03-05 17:59:33 +01:00
} ) ;
2024-08-24 20:01:01 +02:00
$ ( ".recipe-edit-button" ) . on ( "click" , function ( e )
{
e . stopPropagation ( ) ;
} ) ;
2020-04-24 11:41:57 -04:00
$ ( ".recipe-fullscreen" ) . on ( 'click' , function ( e )
2018-07-15 09:56:10 +02:00
{
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" ) ;
2023-01-05 17:44:07 +01:00
$ ( ".recipe-content-container" ) . toggleClass ( "row" ) ;
$ ( ".recipe-content-container .ingredients" ) . toggleClass ( "tab-pane" ) . toggleClass ( "col-12 col-md-6 col-xl-4" ) ;
$ ( ".recipe-content-container .preparation" ) . toggleClass ( "tab-pane" ) . toggleClass ( "col-12 col-md-6 col-xl-8" ) ;
$ ( ".recipe-headline" ) . toggleClass ( "d-none" ) ;
2019-03-03 13:27:10 +01:00
2020-01-24 22:05:08 +01:00
if ( $ ( "body" ) . hasClass ( "fullscreen-card" ) )
{
window . location . hash = "#fullscreen" ;
}
else
{
window . history . replaceState ( null , null , " " ) ;
}
2018-07-15 09:56:10 +02:00
} ) ;
2019-03-03 13:27:10 +01:00
2020-04-24 11:41:57 -04:00
$ ( ".recipe-print" ) . on ( 'click' , function ( e )
{
2020-08-29 16:41:27 +02:00
e . preventDefault ( ) ;
2020-04-24 11:41:57 -04:00
$ ( "#selectedRecipeCard" ) . removeClass ( "fullscreen" ) ;
$ ( "body" ) . removeClass ( "fullscreen-card" ) ;
$ ( "#selectedRecipeCard .card-header" ) . removeClass ( "fixed-top" ) ;
$ ( "#selectedRecipeCard .card-body" ) . removeClass ( "mt-5" ) ;
window . history . replaceState ( null , null , " " ) ;
window . print ( ) ;
} ) ;
2019-03-03 13:27:10 +01:00
$ ( '#servings-scale' ) . keyup ( function ( event )
{
2020-08-30 12:18:16 +02:00
var data = { } ;
2019-03-03 13:27:10 +01:00
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" ) ;
} ) ;
2021-07-02 18:04:20 +02:00
$ ( document ) . on ( "click" , ".missing-recipe-pos-product-checkbox" , function ( e )
{
e . stopPropagation ( ) ;
$ ( this ) . prop ( "checked" , ! $ ( this ) . prop ( "checked" ) ) ;
$ ( this ) . parent ( ) . parent ( ) . click ( ) ;
} ) ;
2019-03-03 13:27:10 +01:00
if ( window . location . hash === "#fullscreen" )
{
$ ( "#selectedRecipeToggleFullscreenButton" ) . click ( ) ;
}
2021-07-13 19:29:23 +02:00
2022-02-11 17:49:30 +01:00
$ ( document ) . on ( 'click' , '.recipe-grocycode-label-print' , function ( e )
{
e . preventDefault ( ) ;
document . activeElement . blur ( ) ;
var recipeId = $ ( e . currentTarget ) . attr ( 'data-recipe-id' ) ;
Grocy . Api . Get ( 'recipes/' + recipeId + '/printlabel' , function ( labelData )
{
if ( Grocy . Webhooks . labelprinter !== undefined )
{
Grocy . FrontendHelpers . RunWebhook ( Grocy . Webhooks . labelprinter , labelData ) ;
}
} ) ;
} ) ;
2022-04-01 22:43:49 +02:00
$ ( document ) . on ( 'click' , '.ingredient-done-button' , function ( e )
{
e . preventDefault ( ) ;
2023-05-03 08:32:49 +02:00
$ ( ".tooltip" ) . tooltip ( "hide" ) ;
2022-04-01 22:43:49 +02:00
$ ( e . currentTarget ) . parent ( ) . toggleClass ( "text-strike-through" ) ;
} ) ;
2022-12-26 14:25:33 +01:00
$ ( document ) . on ( "click" , ".add-to-mealplan-button" , function ( e )
{
Grocy . Components . DateTimePicker . Init ( true ) ;
Grocy . Components . DateTimePicker . SetValue ( moment ( ) . format ( "YYYY-MM-DD" ) ) ;
Grocy . Components . RecipePicker . Clear ( ) ;
$ ( "#add-to-mealplan-modal" ) . modal ( "show" ) ;
$ ( '#recipe_id' ) . val ( $ ( e . currentTarget ) . attr ( "data-recipe-id" ) ) ;
$ ( '#recipe_id' ) . data ( 'combobox' ) . refresh ( ) ;
$ ( '#recipe_id' ) . trigger ( 'change' ) ;
Grocy . FrontendHelpers . ValidateForm ( "add-to-mealplan-form" ) ;
$ ( "#recipe_servings" ) . focus ( ) ;
} ) ;
$ ( '#save-add-to-mealplan-button' ) . on ( 'click' , function ( e )
{
e . preventDefault ( ) ;
if ( ! Grocy . FrontendHelpers . ValidateForm ( "add-to-mealplan-form" , true ) || $ ( ".combobox-menu-visible" ) . length )
{
return false ;
}
var formData = $ ( '#add-to-mealplan-form' ) . serializeJSON ( ) ;
formData . day = Grocy . Components . DateTimePicker . GetValue ( ) ;
Grocy . Api . Post ( 'objects/meal_plan' , formData ,
function ( result )
{
toastr . success ( _ _t ( "Successfully added the recipe to the meal plan" ) ) ;
$ ( "#add-to-mealplan-modal" ) . modal ( "hide" ) ;
} ,
function ( xhr )
{
Grocy . FrontendHelpers . ShowGenericError ( 'Error while saving, probably this item already exists' , xhr . response )
}
) ;
} ) ;
$ ( '#add-to-mealplan-form input' ) . keydown ( function ( event )
{
if ( event . keyCode === 13 ) // Enter
{
event . preventDefault ( ) ;
if ( ! Grocy . FrontendHelpers . ValidateForm ( 'add-to-mealplan-form' ) )
{
return false ;
}
else
{
$ ( "#save-add-to-mealplan-button" ) . click ( ) ;
}
}
} ) ;