| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <v-autocomplete | 
					
						
							|  |  |  |     v-model="selected" | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |     :items="storeItem" | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |     :value="value" | 
					
						
							|  |  |  |     :label="label" | 
					
						
							|  |  |  |     chips | 
					
						
							|  |  |  |     deletable-chips | 
					
						
							|  |  |  |     item-text="name" | 
					
						
							|  |  |  |     multiple | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |     :prepend-inner-icon="selectorType === Organizer.Tool ? $globals.icons.potSteam : $globals.icons.tags" | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |     return-object | 
					
						
							|  |  |  |     v-bind="inputAttrs" | 
					
						
							|  |  |  |   > | 
					
						
							|  |  |  |     <template #selection="data"> | 
					
						
							|  |  |  |       <v-chip | 
					
						
							|  |  |  |         :key="data.index" | 
					
						
							|  |  |  |         class="ma-1" | 
					
						
							|  |  |  |         :input-value="data.selected" | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |         small | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |         close | 
					
						
							|  |  |  |         label | 
					
						
							|  |  |  |         color="accent" | 
					
						
							|  |  |  |         dark | 
					
						
							|  |  |  |         @click:close="removeByIndex(data.index)" | 
					
						
							|  |  |  |       > | 
					
						
							|  |  |  |         {{ data.item.name || data.item }} | 
					
						
							|  |  |  |       </v-chip> | 
					
						
							|  |  |  |     </template> | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |     <template v-if="showAdd" #append-outer> | 
					
						
							|  |  |  |       <v-btn icon @click="dialog = true"> | 
					
						
							|  |  |  |         <v-icon> | 
					
						
							|  |  |  |           {{ $globals.icons.create }} | 
					
						
							|  |  |  |         </v-icon> | 
					
						
							|  |  |  |       </v-btn> | 
					
						
							|  |  |  |       <RecipeOrganizerDialog v-model="dialog" :item-type="selectorType" @created-item="appendCreated" /> | 
					
						
							|  |  |  |     </template> | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |   </v-autocomplete> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  | import { defineComponent, ref, useContext } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  | import { computed, onMounted } from "vue-demi"; | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  | import RecipeOrganizerDialog from "./RecipeOrganizerDialog.vue"; | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  | import { RecipeCategory, RecipeTag } from "~/types/api-types/user"; | 
					
						
							|  |  |  | import { RecipeTool } from "~/types/api-types/admin"; | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  | import { useTagStore } from "~/composables/store/use-tag-store"; | 
					
						
							|  |  |  | import { useCategoryStore, useToolStore } from "~/composables/store"; | 
					
						
							|  |  |  | import { Organizer, RecipeOrganizer } from "~/types/recipe/organizers"; | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default defineComponent({ | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |   components: { | 
					
						
							|  |  |  |     RecipeOrganizerDialog, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |   props: { | 
					
						
							|  |  |  |     value: { | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |       type: Array as () => (RecipeTag | RecipeCategory | RecipeTool | string)[] | undefined, | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * The type of organizer to use. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     selectorType: { | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |       type: String as () => RecipeOrganizer, | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     inputAttrs: { | 
					
						
							|  |  |  |       type: Object as () => Record<string, any>, | 
					
						
							|  |  |  |       default: () => ({}), | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |     returnObject: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     showAdd: { | 
					
						
							|  |  |  |       type: Boolean, | 
					
						
							|  |  |  |       default: true, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   setup(props, context) { | 
					
						
							|  |  |  |     const selected = computed({ | 
					
						
							|  |  |  |       get: () => props.value, | 
					
						
							|  |  |  |       set: (val) => { | 
					
						
							|  |  |  |         context.emit("input", val); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     onMounted(() => { | 
					
						
							|  |  |  |       if (selected.value === undefined) { | 
					
						
							|  |  |  |         selected.value = []; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const { i18n } = useContext(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const label = computed(() => { | 
					
						
							|  |  |  |       switch (props.selectorType) { | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |         case Organizer.Tag: | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |           return i18n.t("tag.tags"); | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |         case Organizer.Category: | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |           return i18n.t("category.categories"); | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |         case Organizer.Tool: | 
					
						
							|  |  |  |           return i18n.t("tool.tools"); | 
					
						
							| 
									
										
										
										
											2022-04-01 11:05:25 -08:00
										 |  |  |         default: | 
					
						
							| 
									
										
										
										
											2022-08-10 07:12:45 +02:00
										 |  |  |           return i18n.t("general.organizer"); | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |     // ===========================================================================
 | 
					
						
							|  |  |  |     // Store & Items Setup
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const store = (() => { | 
					
						
							|  |  |  |       switch (props.selectorType) { | 
					
						
							|  |  |  |         case Organizer.Tag: | 
					
						
							|  |  |  |           return useTagStore(); | 
					
						
							|  |  |  |         case Organizer.Tool: | 
					
						
							|  |  |  |           return useToolStore(); | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |           return useCategoryStore(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     })(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const items = computed(() => { | 
					
						
							|  |  |  |       if (!props.returnObject) { | 
					
						
							|  |  |  |         return store.items.value.map((item) => item.name); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return store.items.value; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |     function removeByIndex(index: number) { | 
					
						
							|  |  |  |       if (selected.value === undefined) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       const newSelected = selected.value.filter((_, i) => i !== index); | 
					
						
							|  |  |  |       selected.value = [...newSelected]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |     function appendCreated(item: RecipeTag | RecipeCategory | RecipeTool) { | 
					
						
							|  |  |  |       console.log(item); | 
					
						
							|  |  |  |       if (selected.value === undefined) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       selected.value = [...selected.value, item]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const dialog = ref(false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2022-06-03 20:12:32 -08:00
										 |  |  |       Organizer, | 
					
						
							|  |  |  |       appendCreated, | 
					
						
							|  |  |  |       dialog, | 
					
						
							|  |  |  |       storeItem: items, | 
					
						
							| 
									
										
										
										
											2022-04-01 09:50:31 -08:00
										 |  |  |       label, | 
					
						
							|  |  |  |       selected, | 
					
						
							|  |  |  |       removeByIndex, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | </script> |