2021-04-03 17:18:01 -08:00
|
|
|
<template>
|
|
|
|
<v-container>
|
2021-05-24 10:12:46 -08:00
|
|
|
<v-row dense>
|
|
|
|
<v-col>
|
|
|
|
<v-text-field
|
|
|
|
v-model="searchString"
|
|
|
|
outlined
|
|
|
|
color="primary accent-3"
|
|
|
|
:placeholder="$t('search.search-placeholder')"
|
|
|
|
append-icon="mdi-magnify"
|
|
|
|
>
|
|
|
|
</v-text-field>
|
|
|
|
</v-col>
|
|
|
|
<v-col cols="12" md="2" sm="12">
|
|
|
|
<v-text-field class="mt-0 pt-0" :label="$t('search.max-results')" v-model="maxResults" type="number" outlined />
|
|
|
|
</v-col>
|
|
|
|
</v-row>
|
2021-04-03 17:18:01 -08:00
|
|
|
|
2021-05-24 10:12:46 -08:00
|
|
|
<v-row dense class="my-0 flex-row align-center justify-space-around">
|
|
|
|
<v-col>
|
|
|
|
<h3 class="pl-2 text-center headline">
|
|
|
|
{{ $t("category.category-filter") }}
|
|
|
|
</h3>
|
|
|
|
<FilterSelector class="mb-1" @update="updateCatParams" />
|
|
|
|
<CategoryTagSelector :solo="true" :dense="false" v-model="includeCategories" :return-object="false" />
|
|
|
|
</v-col>
|
|
|
|
<v-col>
|
|
|
|
<h3 class="pl-2 text-center headline">
|
|
|
|
{{ $t("search.tag-filter") }}
|
|
|
|
</h3>
|
|
|
|
<FilterSelector class="mb-1" @update="updateTagParams" />
|
|
|
|
<CategoryTagSelector
|
|
|
|
:solo="true"
|
|
|
|
:dense="false"
|
|
|
|
v-model="includeTags"
|
|
|
|
:return-object="false"
|
|
|
|
:tag-selector="true"
|
|
|
|
/>
|
|
|
|
</v-col>
|
|
|
|
</v-row>
|
2021-04-03 17:18:01 -08:00
|
|
|
|
2021-05-24 10:12:46 -08:00
|
|
|
<CardSection class="mt-n9" title-icon="mdi-mag" :recipes="showRecipes" :hardLimit="maxResults" @sort="assignFuzzy" />
|
2021-04-03 17:18:01 -08:00
|
|
|
</v-container>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import Fuse from "fuse.js";
|
2021-04-06 22:29:02 -08:00
|
|
|
import CategoryTagSelector from "@/components/FormHelpers/CategoryTagSelector";
|
2021-05-05 14:08:13 -08:00
|
|
|
import CardSection from "@/components/UI/CardSection";
|
2021-04-03 17:18:01 -08:00
|
|
|
import FilterSelector from "./FilterSelector.vue";
|
|
|
|
|
|
|
|
export default {
|
|
|
|
components: {
|
2021-05-05 14:08:13 -08:00
|
|
|
CardSection,
|
2021-04-06 22:29:02 -08:00
|
|
|
CategoryTagSelector,
|
2021-04-03 17:18:01 -08:00
|
|
|
FilterSelector,
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
maxResults: 21,
|
|
|
|
searchResults: [],
|
|
|
|
catFilter: {
|
|
|
|
exclude: false,
|
|
|
|
matchAny: false,
|
|
|
|
},
|
|
|
|
tagFilter: {
|
|
|
|
exclude: false,
|
|
|
|
matchAny: false,
|
|
|
|
},
|
2021-05-05 14:08:13 -08:00
|
|
|
sortedResults: [],
|
2021-04-03 17:18:01 -08:00
|
|
|
includeCategories: [],
|
|
|
|
includeTags: [],
|
|
|
|
options: {
|
|
|
|
shouldSort: true,
|
|
|
|
threshold: 0.6,
|
|
|
|
location: 0,
|
|
|
|
distance: 100,
|
|
|
|
findAllMatches: true,
|
|
|
|
maxPatternLength: 32,
|
|
|
|
minMatchCharLength: 2,
|
|
|
|
keys: ["name", "description"],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2021-04-22 22:13:55 -08:00
|
|
|
mounted() {
|
|
|
|
this.$store.dispatch("requestAllRecipes");
|
|
|
|
},
|
2021-04-03 17:18:01 -08:00
|
|
|
computed: {
|
2021-05-06 21:08:27 -08:00
|
|
|
searchString: {
|
|
|
|
set(q) {
|
|
|
|
this.$router.replace({ query: { ...this.$route.query, q } });
|
|
|
|
},
|
|
|
|
get() {
|
|
|
|
return this.$route.query.q || "";
|
|
|
|
},
|
|
|
|
},
|
2021-04-03 17:18:01 -08:00
|
|
|
allRecipes() {
|
2021-04-27 11:17:00 -08:00
|
|
|
return this.$store.getters.getAllRecipes;
|
2021-04-03 17:18:01 -08:00
|
|
|
},
|
|
|
|
filteredRecipes() {
|
|
|
|
return this.allRecipes.filter(recipe => {
|
2021-05-01 20:46:02 -08:00
|
|
|
const includesTags = this.check(this.includeTags, recipe.tags, this.tagFilter.matchAny, this.tagFilter.exclude);
|
2021-04-03 17:18:01 -08:00
|
|
|
const includesCats = this.check(
|
|
|
|
this.includeCategories,
|
|
|
|
recipe.recipeCategory,
|
|
|
|
this.catFilter.matchAny,
|
|
|
|
this.catFilter.exclude
|
|
|
|
);
|
|
|
|
return [includesTags, includesCats].every(x => x === true);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
fuse() {
|
|
|
|
return new Fuse(this.filteredRecipes, this.options);
|
|
|
|
},
|
|
|
|
fuzzyRecipes() {
|
|
|
|
if (this.searchString.trim() === "") {
|
2021-05-05 14:08:13 -08:00
|
|
|
return this.filteredRecipes;
|
2021-04-03 17:18:01 -08:00
|
|
|
}
|
|
|
|
const result = this.fuse.search(this.searchString.trim());
|
2021-05-05 14:08:13 -08:00
|
|
|
return result.map(x => x.item);
|
2021-04-03 17:18:01 -08:00
|
|
|
},
|
|
|
|
isSearching() {
|
|
|
|
return this.searchString && this.searchString.length > 0;
|
|
|
|
},
|
2021-05-05 14:08:13 -08:00
|
|
|
showRecipes() {
|
|
|
|
if (this.sortedResults.length > 0) {
|
|
|
|
return this.sortedResults;
|
|
|
|
} else {
|
|
|
|
return this.fuzzyRecipes;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2021-04-03 17:18:01 -08:00
|
|
|
methods: {
|
2021-05-05 14:08:13 -08:00
|
|
|
assignFuzzy(val) {
|
|
|
|
this.sortedResults = val;
|
|
|
|
},
|
2021-04-03 17:18:01 -08:00
|
|
|
check(filterBy, recipeList, matchAny, exclude) {
|
|
|
|
let isMatch = true;
|
|
|
|
if (filterBy.length === 0) return isMatch;
|
|
|
|
|
|
|
|
if (recipeList) {
|
|
|
|
if (matchAny) {
|
|
|
|
isMatch = filterBy.some(t => recipeList.includes(t)); // Checks if some items are a match
|
|
|
|
} else {
|
|
|
|
isMatch = filterBy.every(t => recipeList.includes(t)); // Checks if every items is a match
|
|
|
|
}
|
|
|
|
return exclude ? !isMatch : isMatch;
|
|
|
|
} else;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
updateTagParams(params) {
|
|
|
|
this.tagFilter = params;
|
|
|
|
},
|
|
|
|
updateCatParams(params) {
|
|
|
|
this.catFilter = params;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
2021-05-01 20:46:02 -08:00
|
|
|
<style></style>
|