| 
									
										
										
										
											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-07-31 15:07:19 -08:00
										 |  |  |     <h2 class="mb-4">{{ $t("recipe.ingredients") }}</h2> | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |     <div> | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |       <div v-for="(ingredient, index) in value" :key="generateKey('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)"> | 
					
						
							|  |  |  |           <v-checkbox hide-details :value="checked[index]" class="pt-0 my-auto py-auto" color="secondary"> </v-checkbox> | 
					
						
							|  |  |  |           <v-list-item-content> | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |             <VueMarkdown class="ma-0 pa-0 text-subtitle-1 dense-markdown" :source="parseIngredientText(ingredient)"> | 
					
						
							|  |  |  |             </VueMarkdown> | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |           </v-list-item-content> | 
					
						
							|  |  |  |         </v-list-item> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script> | 
					
						
							|  |  |  | import VueMarkdown from "@adapttive/vue-markdown"; | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  | import { useFraction } from "@/composables/use-fraction"; | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  | import { utils } from "@/utils"; | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |   components: { | 
					
						
							|  |  |  |     VueMarkdown, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   props: { | 
					
						
							|  |  |  |     value: { | 
					
						
							|  |  |  |       type: Array, | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  |     const { frac } = useFraction(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function parseIngredientText(ingredient) { | 
					
						
							|  |  |  |       if (props.disableAmount) { | 
					
						
							|  |  |  |         return ingredient.note; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |       const { quantity, food, unit, note } = ingredient; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       let return_qty = ""; | 
					
						
							|  |  |  |       if (unit?.fraction) { | 
					
						
							|  |  |  |         const fraction = frac(quantity * props.scale, 10, true); | 
					
						
							|  |  |  |         if (fraction[0] !== undefined && fraction[0] > 0) { | 
					
						
							|  |  |  |           return_qty += fraction[0]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (fraction[1] > 0) { | 
					
						
							|  |  |  |           return_qty += ` <sup>${fraction[1]}</sup>⁄<sub>${fraction[2]}</sub>`; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2021-08-31 14:39:02 -08:00
										 |  |  |         return_qty = quantity * props.scale; | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return `${return_qty} ${unit?.name || " "}  ${food?.name || " "} ${note}`; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { parseIngredientText }; | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |   }, | 
					
						
							|  |  |  |   data() { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       drag: false, | 
					
						
							|  |  |  |       checked: [], | 
					
						
							|  |  |  |       showTitleEditor: [], | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   watch: { | 
					
						
							|  |  |  |     value: { | 
					
						
							|  |  |  |       handler() { | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |         this.showTitleEditor = this.value.map((x) => this.validateTitle(x.title)); | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |       }, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |   mounted() { | 
					
						
							|  |  |  |     this.checked = this.value.map(() => false); | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |     this.showTitleEditor = this.value.map((x) => this.validateTitle(x.title)); | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |   methods: { | 
					
						
							|  |  |  |     generateKey(item, index) { | 
					
						
							|  |  |  |       return utils.generateUniqueKey(item, index); | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |     toggleChecked(index) { | 
					
						
							|  |  |  |       this.$set(this.checked, index, !this.checked[index]); | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-31 15:07:19 -08:00
										 |  |  |     validateTitle(title) { | 
					
						
							|  |  |  |       return !(title === null || title === ""); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     toggleShowTitle(index) { | 
					
						
							|  |  |  |       const newVal = !this.showTitleEditor[index]; | 
					
						
							|  |  |  |       if (!newVal) { | 
					
						
							|  |  |  |         this.value[index].title = ""; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       this.$set(this.showTitleEditor, index, newVal); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | </script> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <style> | 
					
						
							|  |  |  | .dense-markdown p { | 
					
						
							|  |  |  |   margin: auto !important; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | </style> |