Compare commits
2 Commits
7ab3482850
...
a936b736de
| Author | SHA1 | Date | |
|---|---|---|---|
|
a936b736de
|
|||
|
bc170abcdf
|
@@ -6,13 +6,21 @@
|
|||||||
categories = [],
|
categories = [],
|
||||||
selected = null,
|
selected = null,
|
||||||
onChange = () => {},
|
onChange = () => {},
|
||||||
lang = 'de'
|
lang = 'de',
|
||||||
|
useAndLogic = true
|
||||||
} = $props();
|
} = $props();
|
||||||
|
|
||||||
const isEnglish = $derived(lang === 'en');
|
const isEnglish = $derived(lang === 'en');
|
||||||
const label = $derived(isEnglish ? 'Category' : 'Kategorie');
|
const label = $derived(isEnglish ? 'Category' : 'Kategorie');
|
||||||
const selectLabel = $derived(isEnglish ? 'Select category...' : 'Kategorie auswählen...');
|
const selectLabel = $derived(isEnglish ? 'Select category...' : 'Kategorie auswählen...');
|
||||||
|
|
||||||
|
// Convert selected to array for OR mode, keep as single value for AND mode
|
||||||
|
const selectedArray = $derived(
|
||||||
|
useAndLogic
|
||||||
|
? (selected ? [selected] : [])
|
||||||
|
: (Array.isArray(selected) ? selected : (selected ? [selected] : []))
|
||||||
|
);
|
||||||
|
|
||||||
let inputValue = $state('');
|
let inputValue = $state('');
|
||||||
let dropdownOpen = $state(false);
|
let dropdownOpen = $state(false);
|
||||||
|
|
||||||
@@ -37,9 +45,22 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleCategorySelect(category) {
|
function handleCategorySelect(category) {
|
||||||
|
if (useAndLogic) {
|
||||||
|
// AND mode: single select
|
||||||
onChange(category);
|
onChange(category);
|
||||||
inputValue = '';
|
inputValue = '';
|
||||||
dropdownOpen = false;
|
dropdownOpen = false;
|
||||||
|
} else {
|
||||||
|
// OR mode: multi-select toggle
|
||||||
|
const currentSelected = Array.isArray(selected) ? selected : (selected ? [selected] : []);
|
||||||
|
if (currentSelected.includes(category)) {
|
||||||
|
const newSelected = currentSelected.filter(c => c !== category);
|
||||||
|
onChange(newSelected.length > 0 ? newSelected : null);
|
||||||
|
} else {
|
||||||
|
onChange([...currentSelected, category]);
|
||||||
|
}
|
||||||
|
inputValue = '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleKeyDown(event) {
|
function handleKeyDown(event) {
|
||||||
@@ -49,8 +70,7 @@
|
|||||||
const matchedCat = categories.find(c => c.toLowerCase() === value.toLowerCase())
|
const matchedCat = categories.find(c => c.toLowerCase() === value.toLowerCase())
|
||||||
|| filteredCategories[0];
|
|| filteredCategories[0];
|
||||||
if (matchedCat) {
|
if (matchedCat) {
|
||||||
onChange(matchedCat);
|
handleCategorySelect(matchedCat);
|
||||||
inputValue = '';
|
|
||||||
}
|
}
|
||||||
} else if (event.key === 'Escape') {
|
} else if (event.key === 'Escape') {
|
||||||
dropdownOpen = false;
|
dropdownOpen = false;
|
||||||
@@ -58,8 +78,14 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleRemove() {
|
function handleRemove(category) {
|
||||||
|
if (useAndLogic) {
|
||||||
onChange(null);
|
onChange(null);
|
||||||
|
} else {
|
||||||
|
const currentSelected = Array.isArray(selected) ? selected : (selected ? [selected] : []);
|
||||||
|
const newSelected = currentSelected.filter(c => c !== category);
|
||||||
|
onChange(newSelected.length > 0 ? newSelected : null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -188,7 +214,7 @@
|
|||||||
{#each filteredCategories as category}
|
{#each filteredCategories as category}
|
||||||
<TagChip
|
<TagChip
|
||||||
tag={category}
|
tag={category}
|
||||||
selected={false}
|
selected={selectedArray.includes(category)}
|
||||||
removable={false}
|
removable={false}
|
||||||
onToggle={() => handleCategorySelect(category)}
|
onToggle={() => handleCategorySelect(category)}
|
||||||
/>
|
/>
|
||||||
@@ -197,15 +223,17 @@
|
|||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Selected category display below -->
|
<!-- Selected categories display below -->
|
||||||
{#if selected}
|
{#if selectedArray.length > 0}
|
||||||
<div class="selected-category">
|
<div class="selected-category">
|
||||||
|
{#each selectedArray as category}
|
||||||
<TagChip
|
<TagChip
|
||||||
tag={selected}
|
tag={category}
|
||||||
selected={true}
|
selected={true}
|
||||||
removable={true}
|
removable={true}
|
||||||
onToggle={handleRemove}
|
onToggle={() => handleRemove(category)}
|
||||||
/>
|
/>
|
||||||
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -14,20 +14,20 @@
|
|||||||
|
|
||||||
let checked = $state(enabled);
|
let checked = $state(enabled);
|
||||||
|
|
||||||
|
// Watch for changes to checked and call onToggle
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
checked = enabled;
|
// Track checked as dependency
|
||||||
|
const currentChecked = checked;
|
||||||
|
// Call onToggle whenever checked changes
|
||||||
|
onToggle(currentChecked);
|
||||||
});
|
});
|
||||||
|
|
||||||
function handleChange() {
|
|
||||||
onToggle(checked);
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
.filter-section {
|
.filter-section {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 0.5rem;
|
gap: 0.3rem;
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
}
|
}
|
||||||
@@ -69,6 +69,5 @@
|
|||||||
<Toggle
|
<Toggle
|
||||||
bind:checked={checked}
|
bind:checked={checked}
|
||||||
label=""
|
label=""
|
||||||
onchange={handleChange}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
import IconFilter from './IconFilter.svelte';
|
import IconFilter from './IconFilter.svelte';
|
||||||
import SeasonFilter from './SeasonFilter.svelte';
|
import SeasonFilter from './SeasonFilter.svelte';
|
||||||
import FavoritesFilter from './FavoritesFilter.svelte';
|
import FavoritesFilter from './FavoritesFilter.svelte';
|
||||||
|
import LogicModeToggle from './LogicModeToggle.svelte';
|
||||||
|
|
||||||
let {
|
let {
|
||||||
availableCategories = [],
|
availableCategories = [],
|
||||||
@@ -15,13 +16,16 @@
|
|||||||
selectedIcon = null,
|
selectedIcon = null,
|
||||||
selectedSeasons = [],
|
selectedSeasons = [],
|
||||||
selectedFavoritesOnly = false,
|
selectedFavoritesOnly = false,
|
||||||
|
useAndLogic = true,
|
||||||
lang = 'de',
|
lang = 'de',
|
||||||
isLoggedIn = false,
|
isLoggedIn = false,
|
||||||
|
hideFavoritesFilter = false,
|
||||||
onCategoryChange = () => {},
|
onCategoryChange = () => {},
|
||||||
onTagToggle = () => {},
|
onTagToggle = () => {},
|
||||||
onIconChange = () => {},
|
onIconChange = () => {},
|
||||||
onSeasonChange = () => {},
|
onSeasonChange = () => {},
|
||||||
onFavoritesToggle = () => {}
|
onFavoritesToggle = () => {},
|
||||||
|
onLogicModeToggle = () => {}
|
||||||
} = $props();
|
} = $props();
|
||||||
|
|
||||||
const isEnglish = $derived(lang === 'en');
|
const isEnglish = $derived(lang === 'en');
|
||||||
@@ -84,11 +88,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.filter-panel.with-favorites {
|
.filter-panel.with-favorites {
|
||||||
grid-template-columns: 120px 120px 1fr 160px 90px;
|
grid-template-columns: 110px 120px 120px 1fr 160px 90px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.filter-panel.without-favorites {
|
.filter-panel.without-favorites {
|
||||||
grid-template-columns: 120px 120px 1fr 160px;
|
grid-template-columns: 110px 120px 120px 1fr 160px;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 968px) {
|
@media (max-width: 968px) {
|
||||||
@@ -133,12 +137,19 @@
|
|||||||
<span class="arrow" class:open={filtersOpen}>▼</span>
|
<span class="arrow" class:open={filtersOpen}>▼</span>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<div class="filter-panel" class:open={filtersOpen} class:with-favorites={isLoggedIn} class:without-favorites={!isLoggedIn}>
|
<div class="filter-panel" class:open={filtersOpen} class:with-favorites={isLoggedIn && !hideFavoritesFilter} class:without-favorites={!isLoggedIn || hideFavoritesFilter}>
|
||||||
|
<LogicModeToggle
|
||||||
|
{useAndLogic}
|
||||||
|
onToggle={onLogicModeToggle}
|
||||||
|
{lang}
|
||||||
|
/>
|
||||||
|
|
||||||
<CategoryFilter
|
<CategoryFilter
|
||||||
categories={availableCategories}
|
categories={availableCategories}
|
||||||
selected={selectedCategory}
|
selected={selectedCategory}
|
||||||
onChange={onCategoryChange}
|
onChange={onCategoryChange}
|
||||||
{lang}
|
{lang}
|
||||||
|
{useAndLogic}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<IconFilter
|
<IconFilter
|
||||||
@@ -146,6 +157,7 @@
|
|||||||
selected={selectedIcon}
|
selected={selectedIcon}
|
||||||
onChange={onIconChange}
|
onChange={onIconChange}
|
||||||
{lang}
|
{lang}
|
||||||
|
{useAndLogic}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<TagFilter
|
<TagFilter
|
||||||
@@ -162,7 +174,7 @@
|
|||||||
{months}
|
{months}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{#if isLoggedIn}
|
{#if isLoggedIn && !hideFavoritesFilter}
|
||||||
<FavoritesFilter
|
<FavoritesFilter
|
||||||
enabled={selectedFavoritesOnly}
|
enabled={selectedFavoritesOnly}
|
||||||
onToggle={onFavoritesToggle}
|
onToggle={onFavoritesToggle}
|
||||||
|
|||||||
@@ -6,13 +6,21 @@
|
|||||||
availableIcons = [],
|
availableIcons = [],
|
||||||
selected = null,
|
selected = null,
|
||||||
onChange = () => {},
|
onChange = () => {},
|
||||||
lang = 'de'
|
lang = 'de',
|
||||||
|
useAndLogic = true
|
||||||
} = $props();
|
} = $props();
|
||||||
|
|
||||||
const isEnglish = $derived(lang === 'en');
|
const isEnglish = $derived(lang === 'en');
|
||||||
const label = 'Icon';
|
const label = 'Icon';
|
||||||
const selectLabel = $derived(isEnglish ? 'Select icon...' : 'Icon auswählen...');
|
const selectLabel = $derived(isEnglish ? 'Select icon...' : 'Icon auswählen...');
|
||||||
|
|
||||||
|
// Convert selected to array for OR mode, keep as single value for AND mode
|
||||||
|
const selectedArray = $derived(
|
||||||
|
useAndLogic
|
||||||
|
? (selected ? [selected] : [])
|
||||||
|
: (Array.isArray(selected) ? selected : (selected ? [selected] : []))
|
||||||
|
);
|
||||||
|
|
||||||
let inputValue = $state('');
|
let inputValue = $state('');
|
||||||
let dropdownOpen = $state(false);
|
let dropdownOpen = $state(false);
|
||||||
|
|
||||||
@@ -37,9 +45,22 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleIconSelect(icon) {
|
function handleIconSelect(icon) {
|
||||||
|
if (useAndLogic) {
|
||||||
|
// AND mode: single select
|
||||||
onChange(icon);
|
onChange(icon);
|
||||||
inputValue = '';
|
inputValue = '';
|
||||||
dropdownOpen = false;
|
dropdownOpen = false;
|
||||||
|
} else {
|
||||||
|
// OR mode: multi-select toggle
|
||||||
|
const currentSelected = Array.isArray(selected) ? selected : (selected ? [selected] : []);
|
||||||
|
if (currentSelected.includes(icon)) {
|
||||||
|
const newSelected = currentSelected.filter(i => i !== icon);
|
||||||
|
onChange(newSelected.length > 0 ? newSelected : null);
|
||||||
|
} else {
|
||||||
|
onChange([...currentSelected, icon]);
|
||||||
|
}
|
||||||
|
inputValue = '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleKeyDown(event) {
|
function handleKeyDown(event) {
|
||||||
@@ -49,8 +70,14 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleRemove() {
|
function handleRemove(icon) {
|
||||||
|
if (useAndLogic) {
|
||||||
onChange(null);
|
onChange(null);
|
||||||
|
} else {
|
||||||
|
const currentSelected = Array.isArray(selected) ? selected : (selected ? [selected] : []);
|
||||||
|
const newSelected = currentSelected.filter(i => i !== icon);
|
||||||
|
onChange(newSelected.length > 0 ? newSelected : null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -180,7 +207,7 @@
|
|||||||
{#each filteredIcons as icon}
|
{#each filteredIcons as icon}
|
||||||
<TagChip
|
<TagChip
|
||||||
tag={icon}
|
tag={icon}
|
||||||
selected={false}
|
selected={selectedArray.includes(icon)}
|
||||||
removable={false}
|
removable={false}
|
||||||
onToggle={() => handleIconSelect(icon)}
|
onToggle={() => handleIconSelect(icon)}
|
||||||
/>
|
/>
|
||||||
@@ -189,15 +216,17 @@
|
|||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Selected icon display below -->
|
<!-- Selected icons display below -->
|
||||||
{#if selected}
|
{#if selectedArray.length > 0}
|
||||||
<div class="selected-icon">
|
<div class="selected-icon">
|
||||||
|
{#each selectedArray as icon}
|
||||||
<TagChip
|
<TagChip
|
||||||
tag={selected}
|
tag={icon}
|
||||||
selected={true}
|
selected={true}
|
||||||
removable={true}
|
removable={true}
|
||||||
onToggle={handleRemove}
|
onToggle={() => handleRemove(icon)}
|
||||||
/>
|
/>
|
||||||
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
routePrefix = '/rezepte',
|
routePrefix = '/rezepte',
|
||||||
lang = 'de',
|
lang = 'de',
|
||||||
recipes = [],
|
recipes = [],
|
||||||
|
isLoggedIn = false,
|
||||||
onSearchResults = (ids, categories) => {},
|
onSearchResults = (ids, categories) => {},
|
||||||
recipesSlot
|
recipesSlot
|
||||||
}: {
|
}: {
|
||||||
@@ -18,6 +19,7 @@
|
|||||||
routePrefix?: string,
|
routePrefix?: string,
|
||||||
lang?: string,
|
lang?: string,
|
||||||
recipes?: any[],
|
recipes?: any[],
|
||||||
|
isLoggedIn?: boolean,
|
||||||
onSearchResults?: (ids: any[], categories: any[]) => void,
|
onSearchResults?: (ids: any[], categories: any[]) => void,
|
||||||
recipesSlot?: Snippet
|
recipesSlot?: Snippet
|
||||||
} = $props();
|
} = $props();
|
||||||
@@ -89,7 +91,7 @@
|
|||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
<section>
|
<section>
|
||||||
<Search icon={active_icon} {lang} {recipes} {onSearchResults}></Search>
|
<Search icon={active_icon} {lang} {recipes} {isLoggedIn} {onSearchResults}></Search>
|
||||||
</section>
|
</section>
|
||||||
<section>
|
<section>
|
||||||
{@render recipesSlot?.()}
|
{@render recipesSlot?.()}
|
||||||
|
|||||||
145
src/lib/components/LogicModeToggle.svelte
Normal file
145
src/lib/components/LogicModeToggle.svelte
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
<script>
|
||||||
|
import "$lib/css/nordtheme.css";
|
||||||
|
|
||||||
|
let {
|
||||||
|
useAndLogic = true,
|
||||||
|
onToggle = () => {},
|
||||||
|
lang = 'de'
|
||||||
|
} = $props();
|
||||||
|
|
||||||
|
const isEnglish = $derived(lang === 'en');
|
||||||
|
const label = $derived(isEnglish ? 'Filter Mode' : 'Filter-Modus');
|
||||||
|
const andLabel = $derived(isEnglish ? 'AND' : 'UND');
|
||||||
|
const orLabel = $derived(isEnglish ? 'OR' : 'ODER');
|
||||||
|
|
||||||
|
let checked = $state(useAndLogic);
|
||||||
|
|
||||||
|
// Watch for changes to checked and call onToggle
|
||||||
|
$effect(() => {
|
||||||
|
const currentChecked = checked;
|
||||||
|
onToggle(currentChecked);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.filter-section {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.3rem;
|
||||||
|
max-width: 100%;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 968px) {
|
||||||
|
.filter-section {
|
||||||
|
max-width: 500px;
|
||||||
|
gap: 0.3rem;
|
||||||
|
align-items: flex-start;
|
||||||
|
margin: 0 auto;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.filter-label {
|
||||||
|
font-size: 0.9rem;
|
||||||
|
color: var(--nord2);
|
||||||
|
font-weight: 600;
|
||||||
|
margin-bottom: 0.25rem;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
.filter-label {
|
||||||
|
color: var(--nord6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 968px) {
|
||||||
|
.filter-label {
|
||||||
|
font-size: 0.85rem;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.toggle-container {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
color: var(--nord4);
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
.toggle-container {
|
||||||
|
color: var(--nord6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.toggle-switch {
|
||||||
|
position: relative;
|
||||||
|
width: 44px;
|
||||||
|
height: 24px;
|
||||||
|
background: var(--nord10);
|
||||||
|
border-radius: 24px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.toggle-switch.or-mode {
|
||||||
|
background: var(--nord13);
|
||||||
|
}
|
||||||
|
|
||||||
|
.toggle-knob {
|
||||||
|
position: absolute;
|
||||||
|
width: 20px;
|
||||||
|
height: 20px;
|
||||||
|
border-radius: 50%;
|
||||||
|
background: white;
|
||||||
|
top: 2px;
|
||||||
|
left: 2px;
|
||||||
|
transition: transform 0.3s ease;
|
||||||
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.toggle-switch.or-mode .toggle-knob {
|
||||||
|
transform: translateX(20px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.mode-label {
|
||||||
|
min-width: 40px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mode-label.active {
|
||||||
|
color: var(--nord10);
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
.mode-label.active {
|
||||||
|
color: var(--nord8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.toggle-switch.or-mode + .mode-label.or {
|
||||||
|
color: var(--nord13);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<div class="filter-section">
|
||||||
|
<div class="filter-label">{label}</div>
|
||||||
|
<div class="toggle-container">
|
||||||
|
<span class="mode-label" class:active={checked}>{andLabel}</span>
|
||||||
|
<div
|
||||||
|
class="toggle-switch"
|
||||||
|
class:or-mode={!checked}
|
||||||
|
onclick={() => checked = !checked}
|
||||||
|
role="button"
|
||||||
|
tabindex="0"
|
||||||
|
onkeydown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); checked = !checked; } }}
|
||||||
|
>
|
||||||
|
<div class="toggle-knob"></div>
|
||||||
|
</div>
|
||||||
|
<span class="mode-label or" class:active={!checked}>{orLabel}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
import { browser } from '$app/environment';
|
import { browser } from '$app/environment';
|
||||||
import "$lib/css/nordtheme.css";
|
import "$lib/css/nordtheme.css";
|
||||||
import FilterPanel from './FilterPanel.svelte';
|
import FilterPanel from './FilterPanel.svelte';
|
||||||
|
import { getCategories } from '$lib/js/categories';
|
||||||
|
|
||||||
// Filter props for different contexts
|
// Filter props for different contexts
|
||||||
let {
|
let {
|
||||||
@@ -13,11 +14,13 @@
|
|||||||
favoritesOnly = false,
|
favoritesOnly = false,
|
||||||
lang = 'de',
|
lang = 'de',
|
||||||
recipes = [],
|
recipes = [],
|
||||||
categories = [],
|
|
||||||
onSearchResults = (matchedIds, matchedCategories) => {},
|
onSearchResults = (matchedIds, matchedCategories) => {},
|
||||||
isLoggedIn = false
|
isLoggedIn = false
|
||||||
} = $props();
|
} = $props();
|
||||||
|
|
||||||
|
// Generate categories internally based on language
|
||||||
|
const categories = $derived(getCategories(lang));
|
||||||
|
|
||||||
const isEnglish = $derived(lang === 'en');
|
const isEnglish = $derived(lang === 'en');
|
||||||
const searchResultsUrl = $derived(isEnglish ? '/recipes/search' : '/rezepte/search');
|
const searchResultsUrl = $derived(isEnglish ? '/recipes/search' : '/rezepte/search');
|
||||||
const labels = $derived({
|
const labels = $derived({
|
||||||
@@ -39,6 +42,7 @@
|
|||||||
let selectedIcon = $state(null);
|
let selectedIcon = $state(null);
|
||||||
let selectedSeasons = $state([]);
|
let selectedSeasons = $state([]);
|
||||||
let selectedFavoritesOnly = $state(false);
|
let selectedFavoritesOnly = $state(false);
|
||||||
|
let useAndLogic = $state(true);
|
||||||
|
|
||||||
// Initialize from props (for backwards compatibility)
|
// Initialize from props (for backwards compatibility)
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
@@ -52,7 +56,9 @@
|
|||||||
// Apply non-text filters (category, tags, icon, season)
|
// Apply non-text filters (category, tags, icon, season)
|
||||||
function applyNonTextFilters(recipeList) {
|
function applyNonTextFilters(recipeList) {
|
||||||
return recipeList.filter(recipe => {
|
return recipeList.filter(recipe => {
|
||||||
// Category filter
|
if (useAndLogic) {
|
||||||
|
// AND mode: recipe must satisfy ALL active filters
|
||||||
|
// Category filter (single value in AND mode)
|
||||||
if (selectedCategory && recipe.category !== selectedCategory) {
|
if (selectedCategory && recipe.category !== selectedCategory) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -65,7 +71,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Icon filter
|
// Icon filter (single value in AND mode)
|
||||||
if (selectedIcon && recipe.icon !== selectedIcon) {
|
if (selectedIcon && recipe.icon !== selectedIcon) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -84,6 +90,27 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
} else {
|
||||||
|
// OR mode: recipe must satisfy AT LEAST ONE active filter
|
||||||
|
const categoryArray = Array.isArray(selectedCategory) ? selectedCategory : (selectedCategory ? [selectedCategory] : []);
|
||||||
|
const iconArray = Array.isArray(selectedIcon) ? selectedIcon : (selectedIcon ? [selectedIcon] : []);
|
||||||
|
|
||||||
|
const hasActiveFilters = categoryArray.length > 0 || selectedTags.length > 0 || iconArray.length > 0 || selectedSeasons.length > 0 || selectedFavoritesOnly;
|
||||||
|
|
||||||
|
// If no filters active, return all
|
||||||
|
if (!hasActiveFilters) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if recipe matches any filter
|
||||||
|
const matchesCategory = categoryArray.length > 0 ? categoryArray.includes(recipe.category) : false;
|
||||||
|
const matchesTags = selectedTags.length > 0 ? selectedTags.some(tag => (recipe.tags || []).includes(tag)) : false;
|
||||||
|
const matchesIcon = iconArray.length > 0 ? iconArray.includes(recipe.icon) : false;
|
||||||
|
const matchesSeasons = selectedSeasons.length > 0 ? selectedSeasons.some(s => (recipe.season || []).includes(s)) : false;
|
||||||
|
const matchesFavorites = selectedFavoritesOnly ? recipe.isFavorite : false;
|
||||||
|
|
||||||
|
return matchesCategory || matchesTags || matchesIcon || matchesSeasons || matchesFavorites;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,10 +185,9 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter change handlers
|
// Filter change handlers - the effect will automatically trigger search
|
||||||
function handleCategoryChange(newCategory) {
|
function handleCategoryChange(newCategory) {
|
||||||
selectedCategory = newCategory;
|
selectedCategory = newCategory;
|
||||||
performSearch(searchQuery);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleTagToggle(tag) {
|
function handleTagToggle(tag) {
|
||||||
@@ -170,22 +196,32 @@
|
|||||||
} else {
|
} else {
|
||||||
selectedTags = [...selectedTags, tag];
|
selectedTags = [...selectedTags, tag];
|
||||||
}
|
}
|
||||||
performSearch(searchQuery);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleIconChange(newIcon) {
|
function handleIconChange(newIcon) {
|
||||||
selectedIcon = newIcon;
|
selectedIcon = newIcon;
|
||||||
performSearch(searchQuery);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleSeasonChange(newSeasons) {
|
function handleSeasonChange(newSeasons) {
|
||||||
selectedSeasons = newSeasons;
|
selectedSeasons = newSeasons;
|
||||||
performSearch(searchQuery);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleFavoritesToggle(enabled) {
|
function handleFavoritesToggle(enabled) {
|
||||||
selectedFavoritesOnly = enabled;
|
selectedFavoritesOnly = enabled;
|
||||||
performSearch(searchQuery);
|
}
|
||||||
|
|
||||||
|
function handleLogicModeToggle(useAnd) {
|
||||||
|
useAndLogic = useAnd;
|
||||||
|
|
||||||
|
// When switching to AND mode, convert arrays to single values
|
||||||
|
if (useAnd) {
|
||||||
|
if (Array.isArray(selectedCategory)) {
|
||||||
|
selectedCategory = selectedCategory.length > 0 ? selectedCategory[0] : null;
|
||||||
|
}
|
||||||
|
if (Array.isArray(selectedIcon)) {
|
||||||
|
selectedIcon = selectedIcon.length > 0 ? selectedIcon[0] : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleSubmit(event) {
|
function handleSubmit(event) {
|
||||||
@@ -202,11 +238,17 @@
|
|||||||
performSearch('');
|
performSearch('');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debounced search effect - only triggers search 100ms after user stops typing
|
// Debounced search effect - triggers search when query or filters change
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (browser && recipes.length > 0) {
|
if (browser && recipes.length > 0) {
|
||||||
// Read searchQuery to track it as a dependency
|
// Read all dependencies to track them
|
||||||
const query = searchQuery;
|
const query = searchQuery;
|
||||||
|
const cat = selectedCategory;
|
||||||
|
const tags = selectedTags;
|
||||||
|
const icn = selectedIcon;
|
||||||
|
const seasons = selectedSeasons;
|
||||||
|
const favsOnly = selectedFavoritesOnly;
|
||||||
|
const andLogic = useAndLogic;
|
||||||
|
|
||||||
// Set debounce timer
|
// Set debounce timer
|
||||||
const timer = setTimeout(() => {
|
const timer = setTimeout(() => {
|
||||||
@@ -355,12 +397,15 @@ scale: 0.8 0.8;
|
|||||||
{selectedIcon}
|
{selectedIcon}
|
||||||
{selectedSeasons}
|
{selectedSeasons}
|
||||||
{selectedFavoritesOnly}
|
{selectedFavoritesOnly}
|
||||||
|
{useAndLogic}
|
||||||
{lang}
|
{lang}
|
||||||
{isLoggedIn}
|
{isLoggedIn}
|
||||||
|
hideFavoritesFilter={favoritesOnly}
|
||||||
onCategoryChange={handleCategoryChange}
|
onCategoryChange={handleCategoryChange}
|
||||||
onTagToggle={handleTagToggle}
|
onTagToggle={handleTagToggle}
|
||||||
onIconChange={handleIconChange}
|
onIconChange={handleIconChange}
|
||||||
onSeasonChange={handleSeasonChange}
|
onSeasonChange={handleSeasonChange}
|
||||||
onFavoritesToggle={handleFavoritesToggle}
|
onFavoritesToggle={handleFavoritesToggle}
|
||||||
|
onLogicModeToggle={handleLogicModeToggle}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
routePrefix = '/rezepte',
|
routePrefix = '/rezepte',
|
||||||
lang = 'de',
|
lang = 'de',
|
||||||
recipes = [],
|
recipes = [],
|
||||||
|
isLoggedIn = false,
|
||||||
onSearchResults = (ids, categories) => {},
|
onSearchResults = (ids, categories) => {},
|
||||||
recipesSlot
|
recipesSlot
|
||||||
}: {
|
}: {
|
||||||
@@ -18,6 +19,7 @@
|
|||||||
routePrefix?: string,
|
routePrefix?: string,
|
||||||
lang?: string,
|
lang?: string,
|
||||||
recipes?: any[],
|
recipes?: any[],
|
||||||
|
isLoggedIn?: boolean,
|
||||||
onSearchResults?: (ids: any[], categories: any[]) => void,
|
onSearchResults?: (ids: any[], categories: any[]) => void,
|
||||||
recipesSlot?: Snippet
|
recipesSlot?: Snippet
|
||||||
} = $props();
|
} = $props();
|
||||||
@@ -58,7 +60,7 @@ a.month:hover,
|
|||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
<section>
|
<section>
|
||||||
<Search season={active_index + 1} {lang} {recipes} {onSearchResults}></Search>
|
<Search season={active_index + 1} {lang} {recipes} {isLoggedIn} {onSearchResults}></Search>
|
||||||
</section>
|
</section>
|
||||||
<section>
|
<section>
|
||||||
{@render recipesSlot?.()}
|
{@render recipesSlot?.()}
|
||||||
|
|||||||
@@ -30,10 +30,10 @@
|
|||||||
.toggle-wrapper input[type="checkbox"] {
|
.toggle-wrapper input[type="checkbox"] {
|
||||||
appearance: none;
|
appearance: none;
|
||||||
-webkit-appearance: none;
|
-webkit-appearance: none;
|
||||||
width: 51px;
|
width: 44px;
|
||||||
height: 31px;
|
height: 24px;
|
||||||
background: var(--nord2);
|
background: var(--nord2);
|
||||||
border-radius: 31px;
|
border-radius: 24px;
|
||||||
position: relative;
|
position: relative;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
transition: background 0.3s ease;
|
transition: background 0.3s ease;
|
||||||
@@ -55,8 +55,8 @@
|
|||||||
.toggle-wrapper input[type="checkbox"]::before {
|
.toggle-wrapper input[type="checkbox"]::before {
|
||||||
content: '';
|
content: '';
|
||||||
position: absolute;
|
position: absolute;
|
||||||
width: 27px;
|
width: 20px;
|
||||||
height: 27px;
|
height: 20px;
|
||||||
border-radius: 50%;
|
border-radius: 50%;
|
||||||
top: 2px;
|
top: 2px;
|
||||||
left: 2px;
|
left: 2px;
|
||||||
|
|||||||
9
src/lib/js/categories.ts
Normal file
9
src/lib/js/categories.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
/**
|
||||||
|
* Get the list of recipe categories based on language
|
||||||
|
*/
|
||||||
|
export function getCategories(lang: string): string[] {
|
||||||
|
const isEnglish = lang === 'en';
|
||||||
|
return isEnglish
|
||||||
|
? ["Main course", "Noodle", "Bread", "Dessert", "Soup", "Side dish", "Salad", "Cake", "Breakfast", "Sauce", "Ingredient", "Drink", "Spread", "Biscuits", "Snack"]
|
||||||
|
: ["Hauptspeise", "Nudel", "Brot", "Dessert", "Suppe", "Beilage", "Salat", "Kuchen", "Frühstück", "Sauce", "Zutat", "Getränk", "Aufstrich", "Guetzli", "Snack"];
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
import Card from '$lib/components/Card.svelte';
|
import Card from '$lib/components/Card.svelte';
|
||||||
import Search from '$lib/components/Search.svelte';
|
import Search from '$lib/components/Search.svelte';
|
||||||
import LazyCategory from '$lib/components/LazyCategory.svelte';
|
import LazyCategory from '$lib/components/LazyCategory.svelte';
|
||||||
|
import { getCategories } from '$lib/js/categories';
|
||||||
let { data } = $props<{ data: PageData }>();
|
let { data } = $props<{ data: PageData }>();
|
||||||
let current_month = new Date().getMonth() + 1;
|
let current_month = new Date().getMonth() + 1;
|
||||||
|
|
||||||
@@ -21,9 +22,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const isEnglish = $derived(data.lang === 'en');
|
const isEnglish = $derived(data.lang === 'en');
|
||||||
const categories = $derived(isEnglish
|
const categories = $derived(getCategories(data.lang));
|
||||||
? ["Main course", "Noodle", "Bread", "Dessert", "Soup", "Side dish", "Salad", "Cake", "Breakfast", "Sauce", "Ingredient", "Drink", "Spread", "Biscuits", "Snack"]
|
|
||||||
: ["Hauptspeise", "Nudel", "Brot", "Dessert", "Suppe", "Beilage", "Salat", "Kuchen", "Frühstück", "Sauce", "Zutat", "Getränk", "Aufstrich", "Guetzli", "Snack"]);
|
|
||||||
|
|
||||||
// Pre-compute category-to-recipes Map for O(1) lookups
|
// Pre-compute category-to-recipes Map for O(1) lookups
|
||||||
const recipesByCategory = $derived.by(() => {
|
const recipesByCategory = $derived.by(() => {
|
||||||
@@ -101,7 +100,7 @@ h1{
|
|||||||
<h1>{labels.title}</h1>
|
<h1>{labels.title}</h1>
|
||||||
<p class=subheading>{labels.subheading}</p>
|
<p class=subheading>{labels.subheading}</p>
|
||||||
|
|
||||||
<Search lang={data.lang} recipes={data.all_brief} categories={categories} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search>
|
<Search lang={data.lang} recipes={data.all_brief} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search>
|
||||||
|
|
||||||
{#if seasonRecipes.length > 0}
|
{#if seasonRecipes.length > 0}
|
||||||
<LazyCategory title={labels.inSeason} eager={true}>
|
<LazyCategory title={labels.inSeason} eager={true}>
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
<h1>{label} <q>{data.category}</q>:</h1>
|
<h1>{label} <q>{data.category}</q>:</h1>
|
||||||
<Search category={data.category} lang={data.lang} recipes={data.recipes} onSearchResults={handleSearchResults}></Search>
|
<Search category={data.category} lang={data.lang} recipes={data.recipes} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search>
|
||||||
<section>
|
<section>
|
||||||
<Recipes>
|
<Recipes>
|
||||||
{#each rand_array(filteredRecipes) as recipe}
|
{#each rand_array(filteredRecipes) as recipe}
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ h1{
|
|||||||
{/if}
|
{/if}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<Search favoritesOnly={true} lang={data.lang} recipes={data.favorites} onSearchResults={handleSearchResults}></Search>
|
<Search favoritesOnly={true} lang={data.lang} recipes={data.favorites} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search>
|
||||||
|
|
||||||
{#if data.error}
|
{#if data.error}
|
||||||
<p class="empty-state">{labels.errorLoading} {data.error}</p>
|
<p class="empty-state">{labels.errorLoading} {data.error}</p>
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
return data.season.filter(r => matchedRecipeIds.has(r._id));
|
return data.season.filter(r => matchedRecipeIds.has(r._id));
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
<IconLayout icons={data.icons} active_icon={data.icon} routePrefix="/{data.recipeLang}" lang={data.lang} recipes={data.season} onSearchResults={handleSearchResults}>
|
<IconLayout icons={data.icons} active_icon={data.icon} routePrefix="/{data.recipeLang}" lang={data.lang} recipes={data.season} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}>
|
||||||
{#snippet recipesSlot()}
|
{#snippet recipesSlot()}
|
||||||
<Recipes>
|
<Recipes>
|
||||||
{#each rand_array(filteredRecipes) as recipe}
|
{#each rand_array(filteredRecipes) as recipe}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@
|
|||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SeasonLayout active_index={current_month-1} {months} routePrefix="/{data.recipeLang}" lang={data.lang} recipes={data.season} onSearchResults={handleSearchResults}>
|
<SeasonLayout active_index={current_month-1} {months} routePrefix="/{data.recipeLang}" lang={data.lang} recipes={data.season} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}>
|
||||||
{#snippet recipesSlot()}
|
{#snippet recipesSlot()}
|
||||||
<Recipes>
|
<Recipes>
|
||||||
{#each rand_array(filteredRecipes) as recipe}
|
{#each rand_array(filteredRecipes) as recipe}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
<h1>{label} <q>{data.tag}</q>:</h1>
|
<h1>{label} <q>{data.tag}</q>:</h1>
|
||||||
<Search tag={data.tag} lang={data.lang} recipes={data.recipes} onSearchResults={handleSearchResults}></Search>
|
<Search tag={data.tag} lang={data.lang} recipes={data.recipes} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search>
|
||||||
<section>
|
<section>
|
||||||
<Recipes>
|
<Recipes>
|
||||||
{#each rand_array(filteredRecipes) as recipe}
|
{#each rand_array(filteredRecipes) as recipe}
|
||||||
|
|||||||
Reference in New Issue
Block a user