| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  | <template> | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |   <div v-if="value && value.length > 0"> | 
					
						
							| 
									
										
										
										
											2021-11-04 18:15:23 -08:00
										 |  |  |     <div class="d-flex justify-start"> | 
					
						
							| 
									
										
										
										
											2021-11-22 20:10:48 -09:00
										 |  |  |       <h2 class="mb-2 mt-1">{{ $t("recipe.ingredients") }}</h2> | 
					
						
							| 
									
										
										
										
											2021-11-20 14:30:38 -09:00
										 |  |  |       <AppButtonCopy btn-class="ml-auto" :copy-text="ingredientCopyText" /> | 
					
						
							| 
									
										
										
										
											2021-11-04 18:15:23 -08:00
										 |  |  |     </div> | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |     <div> | 
					
						
							| 
									
										
										
										
											2021-09-04 20:24:32 -08:00
										 |  |  |       <div v-for="(ingredient, index) in value" :key="'ingredient' + index"> | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |         <h3 v-if="showTitleEditor[index]" class="mt-2">{{ ingredient.title }}</h3> | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |         <v-divider v-if="showTitleEditor[index]"></v-divider> | 
					
						
							|  |  |  |         <v-list-item dense @click="toggleChecked(index)"> | 
					
						
							| 
									
										
										
										
											2022-03-22 20:41:54 -08:00
										 |  |  |           <v-checkbox hide-details :value="checked[index]" class="pt-0 my-auto py-auto" color="secondary" /> | 
					
						
							| 
									
										
										
										
											2022-05-22 11:16:23 -08:00
										 |  |  |           <v-list-item-content :key="ingredient.quantity"> | 
					
						
							| 
									
										
										
										
											2023-08-21 17:32:09 +02:00
										 |  |  |             <RecipeIngredientListItem :ingredient="ingredient" :disable-amount="disableAmount" :scale="scale" /> | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |           </v-list-item-content> | 
					
						
							|  |  |  |         </v-list-item> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | <script lang="ts"> | 
					
						
							|  |  |  | import { computed, defineComponent, reactive, toRefs } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2023-08-21 17:32:09 +02:00
										 |  |  | import RecipeIngredientListItem from "./RecipeIngredientListItem.vue"; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { parseIngredientText } from "~/composables/recipes"; | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  | import { RecipeIngredient } from "~/lib/api/types/recipe"; | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-04 18:15:23 -08:00
										 |  |  | export default defineComponent({ | 
					
						
							| 
									
										
										
										
											2023-08-21 17:32:09 +02:00
										 |  |  |   components: { RecipeIngredientListItem }, | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |   props: { | 
					
						
							|  |  |  |     value: { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |       type: Array as () => RecipeIngredient[], | 
					
						
							| 
									
										
										
										
											2021-08-07 16:49:55 -08:00
										 |  |  |       default: () => [], | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |     disableAmount: { | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |       type: Boolean, | 
					
						
							| 
									
										
										
										
											2021-08-23 12:24:38 -08:00
										 |  |  |       default: false, | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |     scale: { | 
					
						
							|  |  |  |       type: Number, | 
					
						
							|  |  |  |       default: 1, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   setup(props) { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     function validateTitle(title?: string) { | 
					
						
							| 
									
										
										
										
											2022-02-13 12:23:42 -09:00
										 |  |  |       return !(title === undefined || title === "" || title === null); | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const state = reactive({ | 
					
						
							|  |  |  |       checked: props.value.map(() => false), | 
					
						
							|  |  |  |       showTitleEditor: computed(() => props.value.map((x) => validateTitle(x.title))), | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-04 18:15:23 -08:00
										 |  |  |     const ingredientCopyText = computed(() => { | 
					
						
							| 
									
										
										
										
											2024-01-22 14:21:04 -06:00
										 |  |  |       const components: string[] = []; | 
					
						
							|  |  |  |       props.value.forEach((ingredient) => { | 
					
						
							|  |  |  |         if (ingredient.title) { | 
					
						
							|  |  |  |           if (components.length) { | 
					
						
							|  |  |  |             components.push(""); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           components.push(`[${ingredient.title}]`); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         components.push(parseIngredientText(ingredient, props.disableAmount, props.scale, false)); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return components.join("\n"); | 
					
						
							| 
									
										
										
										
											2021-11-04 18:15:23 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |     function toggleChecked(index: number) { | 
					
						
							|  |  |  |       // TODO Find a better way to do this - $set is not available, and
 | 
					
						
							|  |  |  |       // direct array modifications are not propagated for some reason
 | 
					
						
							|  |  |  |       state.checked.splice(index, 1, !state.checked[index]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  |       ...toRefs(state), | 
					
						
							|  |  |  |       ingredientCopyText, | 
					
						
							|  |  |  |       toggleChecked, | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-11-04 18:15:23 -08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <style> | 
					
						
							|  |  |  | .dense-markdown p { | 
					
						
							|  |  |  |   margin: auto !important; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | </style> |