Skip to content

Commit

Permalink
Move asset filtering to filters package (#206).
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Jul 18, 2022
1 parent 01f7f69 commit 50591fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
20 changes: 2 additions & 18 deletions cmd/defaults/ejected/cms/media_browser.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,7 @@
let filters = [];
let enabledFilters = [];
let selectedMedia = [];
// Filter assets
$: filteredAssets = assets.filter(asset => {
if (enabledFilters.length == 0) {
// No filters are applied, return all assets
return asset;
}
let allFolders = asset.split('/')
let cut = allFolders.findIndex(i => i === "assets") + 1;
let folders = allFolders.slice(cut, -1);
return enabledFilters.find(enabledFilter => {
if (folders.join('') === enabledFilter.join('')) {
return asset;
}
});
});
$: filteredAssets = [];
const downloadFiles = () => {
selectedMedia.forEach(mediaFile => {
Expand All @@ -49,7 +33,7 @@
</script>

<div class="media-wrapper">
<MediaFilters bind:assets bind:filters bind:enabledFilters />
<MediaFilters bind:assets bind:filters bind:enabledFilters bind:filteredAssets />
<MediaGrid files={filteredAssets} bind:selectedMedia={selectedMedia} bind:changingAsset bind:showMedia />
</div>
{#if selectedMedia.length > 0}
Expand Down
32 changes: 25 additions & 7 deletions cmd/defaults/ejected/cms/media_filters.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<script>
import { isAsset } from './assets_checker.js';
export let assets, filters, enabledFilters;
export let assets, filters, enabledFilters, filteredAssets;
const assetPathToArray = asset => {
// Create an array of path segments.
let allFolders = asset.split('/')
// Get the index right after the assets folder (works with and without baseurl).
let cut = allFolders.findIndex(i => i === "assets") + 1;
// Remove "assets" folder and last (filename) elements.
return allFolders.slice(cut, -1);
}
for (const asset of assets) {
if (isAsset(asset)) {
// Create an array of path segments.
let allFolders = asset.split('/')
// Get the index right after the assets folder (works with and without baseurl).
let cut = allFolders.findIndex(i => i === "assets") + 1;
// Remove first (assets folder) and last (filename) elements.
let folders = allFolders.slice(cut, -1);
let folders = assetPathToArray(asset);
if (folders.length > 0 && !filters.includes(folders)) {
// Get the index of any parent folders that have already been added
let subfolderIndex = filters.findIndex(val => {
Expand Down Expand Up @@ -58,6 +62,20 @@
enabledFilters = [];
}
// Filter assets
$: filteredAssets = assets.filter(asset => {
if (enabledFilters.length == 0) {
// No filters are applied, return all assets
return asset;
}
let folders = assetPathToArray(asset);
return enabledFilters.find(enabledFilter => {
if (folders.join('') === enabledFilter.join('')) {
return asset;
}
});
});
</script>

Expand Down

0 comments on commit 50591fb

Please sign in to comment.