| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  | <template> | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |   <div v-if="yieldDisplay"> | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |     <div class="text-center d-flex align-center"> | 
					
						
							|  |  |  |       <div> | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |         <v-menu v-model="menu" :disabled="!canEditScale" offset-y top nudge-top="6" :close-on-content-click="false"> | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |           <template #activator="{ on, attrs }"> | 
					
						
							|  |  |  |             <v-card class="pa-1 px-2" dark color="secondary darken-1" small v-bind="attrs" v-on="on"> | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |               <v-icon small class="mr-2">{{ $globals.icons.edit }}</v-icon> | 
					
						
							|  |  |  |               <!-- eslint-disable-next-line vue/no-v-html --> | 
					
						
							|  |  |  |               <span v-html="yieldDisplay"></span> | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |             </v-card> | 
					
						
							|  |  |  |           </template> | 
					
						
							|  |  |  |           <v-card min-width="300px"> | 
					
						
							|  |  |  |             <v-card-title class="mb-0"> | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  |               {{ $t("recipe.servings") }} | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |             </v-card-title> | 
					
						
							|  |  |  |             <v-card-text class="mt-n5"> | 
					
						
							|  |  |  |               <div class="mt-4 d-flex align-center"> | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |                 <v-text-field v-model="yieldQuantityEditorValue" type="number" :min="0" hide-spin-buttons @input="recalculateScale(yieldQuantityEditorValue)" /> | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |                 <v-tooltip right color="secondary darken-1"> | 
					
						
							|  |  |  |                   <template #activator="{ on, attrs }"> | 
					
						
							|  |  |  |                     <v-btn v-bind="attrs" icon class="mx-1" small v-on="on" @click="scale = 1"> | 
					
						
							|  |  |  |                       <v-icon> | 
					
						
							|  |  |  |                         {{ $globals.icons.undo }} | 
					
						
							|  |  |  |                       </v-icon> | 
					
						
							|  |  |  |                     </v-btn> | 
					
						
							|  |  |  |                   </template> | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  |                   <span> {{ $t("recipe.reset-servings-count") }} </span> | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |                 </v-tooltip> | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  |             </v-card-text> | 
					
						
							|  |  |  |           </v-card> | 
					
						
							|  |  |  |         </v-menu> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |       <BaseButtonGroup | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |         v-if="canEditScale" | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |         class="pl-2" | 
					
						
							|  |  |  |         :large="false" | 
					
						
							|  |  |  |         :buttons="[ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             icon: $globals.icons.minus, | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  |             text: $tc('recipe.decrease-scale-label'), | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |             event: 'decrement', | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  |             disabled: disableDecrement, | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |           }, | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             icon: $globals.icons.createAlt, | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  |             text: $tc('recipe.increase-scale-label'), | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |             event: 'increment', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         ]" | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |         @decrement="recalculateScale(yieldQuantity - 1)" | 
					
						
							|  |  |  |         @increment="recalculateScale(yieldQuantity + 1)" | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |       /> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  | import { computed, defineComponent, ref, useContext, watch } from "@nuxtjs/composition-api"; | 
					
						
							|  |  |  | import { useScaledAmount } from "~/composables/recipes/use-scaled-amount"; | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default defineComponent({ | 
					
						
							|  |  |  |   props: { | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |     value: { | 
					
						
							|  |  |  |       type: Number, | 
					
						
							|  |  |  |       required: true, | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |     recipeServings: { | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  |       type: Number, | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |       default: 0, | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |     }, | 
					
						
							|  |  |  |     editScale: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: false, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   setup(props, { emit }) { | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |     const { i18n } = useContext(); | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  |     const menu = ref<boolean>(false); | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |     const canEditScale = computed(() => props.editScale && props.recipeServings > 0); | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const scale = computed({ | 
					
						
							|  |  |  |       get: () => props.value, | 
					
						
							|  |  |  |       set: (value) => { | 
					
						
							|  |  |  |         const newScaleNumber = parseFloat(`${value}`); | 
					
						
							|  |  |  |         emit("input", isNaN(newScaleNumber) ? 0 : newScaleNumber); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |     function recalculateScale(newYield: number) { | 
					
						
							|  |  |  |       if (isNaN(newYield) || newYield <= 0) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (props.recipeServings <= 0) { | 
					
						
							|  |  |  |         scale.value = 1; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         scale.value = newYield / props.recipeServings; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |     const recipeYieldAmount = computed(() => { | 
					
						
							|  |  |  |       return useScaledAmount(props.recipeServings, scale.value); | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |     const yieldQuantity = computed(() => recipeYieldAmount.value.scaledAmount); | 
					
						
							|  |  |  |     const yieldDisplay = computed(() => { | 
					
						
							|  |  |  |       return yieldQuantity.value ? i18n.t( | 
					
						
							|  |  |  |         "recipe.serves-amount", { amount: recipeYieldAmount.value.scaledAmountDisplay } | 
					
						
							|  |  |  |       ) as string : ""; | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |     // only update yield quantity when the menu opens, so we don't override the user's input
 | 
					
						
							|  |  |  |     const yieldQuantityEditorValue = ref(recipeYieldAmount.value.scaledAmount); | 
					
						
							|  |  |  |     watch( | 
					
						
							|  |  |  |       () => menu.value, | 
					
						
							|  |  |  |       () => { | 
					
						
							|  |  |  |         if (!menu.value) { | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yieldQuantityEditorValue.value = recipeYieldAmount.value.scaledAmount; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const disableDecrement = computed(() => { | 
					
						
							|  |  |  |       return recipeYieldAmount.value.scaledAmount <= 1; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  |       menu, | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |       canEditScale, | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |       scale, | 
					
						
							| 
									
										
										
										
											2024-11-20 08:46:27 -06:00
										 |  |  |       recalculateScale, | 
					
						
							|  |  |  |       yieldDisplay, | 
					
						
							|  |  |  |       yieldQuantity, | 
					
						
							|  |  |  |       yieldQuantityEditorValue, | 
					
						
							| 
									
										
										
										
											2024-10-01 16:31:04 +02:00
										 |  |  |       disableDecrement, | 
					
						
							| 
									
										
										
										
											2022-06-09 18:01:25 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | </script> |